<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.njzscloud.supervisory.bs.mapper.ShouJuStatisticsMapper">

    <select id="getCarTrends" resultType="com.njzscloud.supervisory.bs.pojo.result.CarTrends">
        SELECT a.car_number, a.weight, a.order_status, a.station_id, b.name station_name
        FROM (SELECT car_number, weight, order_status, station_id
              FROM (SELECT a.car_number,
                           a.weight,
                           IF(a.status = 1, '已完成', IF(a.status = 4, '已取消', IF(a.car_status = 0, '已预约', IF(a.car_status = 1 AND a.admin_status = 0, '待看料', '待出场')))) order_status,
                           a.station_id
                    FROM ba_order a
                             INNER JOIN ba_goods b ON b.id = a.goods_id AND b.id NOT IN (287, 286, 279, 278, 293, 273, 271, 270, 262, 2)
                    WHERE a.station_id = 1
                      AND a.status IN (0, 1, 2, 3)
                      AND a.type != 4
                      AND b.name NOT LIKE '%青石%'
                      AND b.name NOT LIKE '%矿石%'
                    ORDER BY a.create_time DESC
                    LIMIT 5) t1
              UNION ALL
              SELECT car_number, weight, order_status, station_id
              FROM (SELECT a.car_number,
                           a.weight,
                           IF(a.status = 1, '已完成', IF(a.status = 4, '已取消', IF(a.car_status = 0, '已预约', IF(a.car_status = 1 AND a.admin_status = 0, '待看料', '待出场')))) order_status,
                           a.station_id
                    FROM ba_order a
                             INNER JOIN ba_goods b ON b.id = a.goods_id AND b.id NOT IN (287, 286, 279, 278, 293, 273, 271, 270, 262, 2)
                    WHERE a.station_id = 2
                      AND a.type != 4
                      AND b.name NOT LIKE '%青石%'
                      AND b.name NOT LIKE '%矿石%'
                      AND a.status IN (0, 1, 2, 3)
                    ORDER BY a.create_time DESC
                    LIMIT 5) t2) a
                 INNER JOIN ba_station b ON b.id = a.station_id
    </select>
    <select id="getUserSummary" resultType="com.njzscloud.supervisory.bs.pojo.result.UserSummary">
        SELECT MAX(IF(group_id = 1, c, 0)) san_hu,
               MAX(IF(group_id = 2, c, 0)) qing_yun,
               MAX(IF(group_id = 7, c, 0)) wu_ye
        FROM (SELECT group_id, FLOOR(COUNT(*) / 2) c
              FROM ba_user
              WHERE group_id IN (1, 2, 7)
              GROUP BY group_id) t
    </select>
    <select id="getGarbageDisposeSummary"
            resultType="com.njzscloud.supervisory.bs.pojo.result.GarbageDisposeSummary">
        SELECT garbage_name, weight, 1 depot_type, '入库' depot_name
        FROM (SELECT garbage_name, weight
              FROM (SELECT b.name garbage_name, IFNULL(SUM(a.weight), 0) weight
                    FROM ba_order a
                             INNER JOIN ba_goods b ON b.id = a.goods_id AND b.depot_type = 1 AND b.id NOT IN (287, 286, 279, 278, 293, 273, 271, 270, 262, 2)
                             INNER JOIN ba_goods_category c ON c.id = b.category_id
                    WHERE b.name NOT LIKE '%青石%'
                      AND b.name NOT LIKE '%拆除垃圾(派车清运零星客户专用)%'
                      AND b.name NOT LIKE '%拆除垃圾（活动优惠）%'
                      AND b.name NOT LIKE '%矿石%'
                      AND a.type != 4
                    GROUP BY b.name) t
              ORDER BY weight DESC
              LIMIT 3) t1
        UNION ALL
        SELECT garbage_name, weight, 2 depot_type, '出库' depot_name
        FROM (SELECT garbage_name, weight
              FROM (SELECT b.name garbage_name, IFNULL(SUM(a.weight), 0) weight
                    FROM ba_order a
                             INNER JOIN ba_goods b ON b.id = a.goods_id AND b.depot_type = 2 AND b.id NOT IN (287, 286, 279, 278, 293, 273, 271, 270, 262, 2)
                             INNER JOIN ba_goods_category c ON c.id = b.category_id
                    WHERE b.name NOT LIKE '%青石%'
                      AND b.name NOT LIKE '%矿石%'
                      AND b.name NOT LIKE '%一期项目%'
                      AND a.type != 4
                    GROUP BY b.name) t
              ORDER BY weight DESC
              LIMIT 3) t2
    </select>

    <select id="getCurrentMonthGarbageDisposeSummary"
            resultType="com.njzscloud.supervisory.bs.pojo.result.GarbageDisposeSummary">
        SELECT goods_name garbage_name, weight
        FROM (SELECT a.goods_name, IFNULL(SUM(a.weight), 0) weight
              FROM ba_order a
                       INNER JOIN ba_goods b ON b.id = a.goods_id AND b.depot_type = 2 AND b.id NOT IN (287, 286, 279, 278, 293, 273, 271, 270, 262, 2)
              WHERE a.out_time >= UNIX_TIMESTAMP(DATE_FORMAT(CURDATE(), '%Y-%m-01 00:00:00'))
                AND a.type != 4
                AND b.name NOT LIKE '%青石%'
                AND b.name NOT LIKE '%矿石%'
                AND a.out_time <![CDATA[ < ]]> UNIX_TIMESTAMP(DATE_ADD(DATE_FORMAT(CURDATE(), '%Y-%m-01'), INTERVAL 1 MONTH))
              GROUP BY a.goods_name) t
        ORDER BY weight DESC
        LIMIT 6
    </select>
    <select id="getOrderSummary" resultType="com.njzscloud.supervisory.bs.pojo.result.OrderSummary">
        SELECT *
        FROM (SELECT t1.id user_id, finish, c quantity, t2.group_id, t2.company, '清运公司' group_name
              FROM (SELECT b.id,
                           IF(a.status = 1, 1, 0) AS finish,
                           COUNT(*)               AS c
                    FROM ba_order a
                             INNER JOIN ba_user b ON b.id = a.vip_user AND b.group_id = 2
                             INNER JOIN ba_goods c ON c.id = a.goods_id AND c.id NOT IN (287, 286, 279, 278, 293, 273, 271, 270, 262, 2)
                    WHERE a.status IN (0, 1, 2, 3)
                      AND a.type != 4
                      AND c.name NOT LIKE '%青石%'
                      AND c.name NOT LIKE '%矿石%'
                    GROUP BY b.id, IF(a.status = 1, 1, 0)) t1
                       INNER JOIN ba_user t2 ON t2.id = t1.id
              ORDER BY c DESC
              LIMIT 47) tt1
        UNION ALL
        SELECT *
        FROM (SELECT t1.id user_id, finish, c quantity, t2.group_id, t2.company, '物业' group_name
              FROM (SELECT b.id,
                           IF(a.status = 1, 1, 0) AS finish,
                           COUNT(*)               AS c
                    FROM ba_order a
                             INNER JOIN ba_user b ON b.id IN (230, 343, 435)
                             INNER JOIN ba_goods c ON c.id = a.goods_id AND c.id NOT IN (287, 286, 279, 278, 293, 273, 271, 270, 262, 2)
                    WHERE a.status IN (0, 1, 2, 3)
                      AND a.type != 4
                      AND c.name NOT LIKE '%青石%'
                      AND c.name NOT LIKE '%矿石%'
                    GROUP BY b.id, IF(a.status = 1, 1, 0)) t1
                       INNER JOIN ba_user t2 ON t2.id = t1.id
              ORDER BY c DESC) tt2
    </select>
    <select id="obtainOrder" resultType="com.njzscloud.supervisory.bs.pojo.result.OrderInfo">
        SELECT a.car_number,
               a.goods_name,
               IF(a.status = 1, '已完成', IF(a.status = 4, '已取消', IF(a.car_status = 0, '已预约', IF(a.car_status = 1 AND a.admin_status = 0, '待看料', '待出场')))) order_status,
               a.weight,
               IF(a.in_time = 0, NULL, FROM_UNIXTIME(a.in_time))                                                                                                       in_time,
               IF(a.out_time = 0, NULL, FROM_UNIXTIME(a.out_time))                                                                                                     out_time
        FROM ba_order a
                 INNER JOIN ba_goods b ON b.id = a.goods_id AND b.id NOT IN (287, 286, 279, 278, 293, 273, 271, 270, 262, 2)
        WHERE a.vip_user = #{userId}
          AND a.type != 4
          AND b.name NOT LIKE '%青石%'
          AND b.name NOT LIKE '%矿石%'
        ORDER BY a.create_time DESC
        LIMIT 10
    </select>
    <select id="disposeWeight" resultType="java.lang.Double">
        SELECT IFNULL(SUM(a.weight), 0) weight
        FROM ba_order a
                 INNER JOIN ba_goods b ON b.id = a.goods_id AND b.depot_type = 1 AND b.id NOT IN (287, 286, 279, 278, 293, 273, 271, 270, 262, 2)
        WHERE a.out_time >= #{startTime}
          AND a.out_time <![CDATA[ <= ]]> #{endTime}
          AND b.name NOT LIKE '%青石%'
          AND b.name NOT LIKE '%矿石%'
          AND a.type != 4
    </select>
    <select id="getTodayOrderSummary" resultType="com.njzscloud.supervisory.bs.pojo.result.TodayOrderSummary">
        SELECT MAX(IF(order_status = '已完成', order_count, 0))                                                                                                                                                                                                             completed,
               MAX(IF(order_status = '已取消', order_count, 0))                                                                                                                                                                                                             cancelled,
               MAX(IF(order_status = '已预约', order_count, 0))                                                                                                                                                                                                             waiting,
               MAX(IF(order_status = '已完成', order_count, 0)) + MAX(IF(order_status = '已取消', order_count, 0)) + MAX(IF(order_status = '已预约', order_count, 0)) + MAX(IF(order_status = '待看料', order_count, 0)) + MAX(IF(order_status = '待出场', order_count, 0)) total
        FROM (SELECT order_status, COUNT(*) order_count
              FROM (SELECT IF(a.status = 1, '已完成', IF(a.status = 4, '已取消', IF(a.car_status = 0, '已预约', IF(a.car_status = 1 AND a.admin_status = 0, '待看料', '待出场')))) order_status
                    FROM ba_order a
                             INNER JOIN ba_goods b ON b.id = a.goods_id AND b.id NOT IN (287, 286, 279, 278, 293, 273, 271, 270, 262, 2)
                    WHERE a.create_time >= UNIX_TIMESTAMP(CURDATE())
                      AND a.type != 4
                      AND b.name NOT LIKE '%青石%'
                      AND b.name NOT LIKE '%矿石%'
                      AND a.create_time <![CDATA[ < ]]> UNIX_TIMESTAMP(DATE_ADD(CURDATE(), INTERVAL 24 HOUR))) t
              GROUP BY order_status) tt
    </select>
    <select id="getOrderAmountSummary"
            resultType="com.njzscloud.supervisory.bs.pojo.result.OrderAmountSummary">
        SELECT a.price amount, FROM_UNIXTIME(a.out_time) out_time, a.who_pay cd
        FROM ba_order a
                 INNER JOIN ba_goods b ON b.id = a.goods_id AND b.id NOT IN (287, 286, 279, 278, 293, 273, 271, 270, 262, 2)
        WHERE a.status = 1
          AND a.out_time >= #{startTime}
          AND a.out_time <![CDATA[ <= ]]> #{endTime}
          AND a.type != 4
          AND b.name NOT LIKE '%青石%'
          AND b.name NOT LIKE '%矿石%'
    </select>

    <select id="getGoodsNames" resultType="java.lang.String">
        SELECT name
        FROM ba_goods
        <if test="ew.customSqlSegment != null and ew.customSqlSegment != ''">
            ${ew.customSqlSegment}
        </if>
        LIMIT #{count}
    </select>
</mapper>
