<?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.SupervisionStatisticsMapper">
    <select id="getDispatchSummary" resultType="com.njzscloud.supervisory.bs.pojo.result.DispatchSummary">
        SELECT MAX(IF(order_status = 'DaiPaiDan', c, 0))    dai_pai_che,
               MAX(IF(order_status = 'DaiJieDan', c, 0))    wei_jie_dan,
               MAX(IF(order_status = 'QingYunZhong', c, 0)) yun_shu_zhong,
               MAX(IF(order_status = 'YiWanCheng', c, 0))   yi_wan_cheng
        FROM (SELECT order_status, COUNT(*) c
              FROM order_info
              WHERE order_status IN ('DaiPaiDan', 'DaiJieDan', 'QingYunZhong', 'YiWanCheng')
              GROUP BY order_status) t
    </select>

    <select id="getYuYueSummary" resultType="com.njzscloud.supervisory.bs.pojo.result.YuYueSummary">
        SELECT zuo_tian,
               jin_tian,
               IF(zuo_tian = 0, IF(jin_tian = 0, 0, 100),
                  ROUND((jin_tian - zuo_tian / zuo_tian) * 100, 2)) zeng_zhang_lv
        FROM (SELECT MAX(IF(t = 1, c, 0)) zuo_tian,
                     MAX(IF(t = 2, c, 0)) jin_tian
              FROM (SELECT COUNT(*) c, 1 t
                    FROM order_info a
                    WHERE a.create_time >= UNIX_TIMESTAMP(CURDATE())
                      AND a.create_time <![CDATA[ < ]]>  UNIX_TIMESTAMP(DATE_ADD(CURDATE(), INTERVAL -24 HOUR))
                    UNION ALL
                    SELECT COUNT(*) c, 2 t
                    FROM order_info a
                    WHERE a.create_time >= UNIX_TIMESTAMP(CURDATE())
                      AND a.create_time <![CDATA[ < ]]>  UNIX_TIMESTAMP(DATE_ADD(CURDATE(), INTERVAL 24 HOUR))) t) tt
    </select>

    <select id="getDisposeTotalWeight" resultType="java.lang.Integer">
        SELECT SUM(settle_weight) total_weight
        FROM order_info a
                 INNER JOIN order_car_in_out b ON b.id = a.car_in_out_id
        WHERE order_status = 'YiWanCheng'
    </select>

    <select id="getCarSummary" resultType="com.njzscloud.supervisory.bs.pojo.result.CarSummary">
        SELECT MAX(IF(t = 1, c, 0)) yun_shu_zhong,
               MAX(IF(t = 2, c, 0)) car_count
        FROM (SELECT COUNT(*) c, 1 t
              FROM order_info
              WHERE order_status IN ('QingYunZhong', 'YiJinChang', 'YiChuChang')
              UNION ALL
              SELECT COUNT(*) c, 2 t
              FROM biz_truck) t
    </select>

    <select id="getTodayCarCount" resultType="java.lang.Integer">
        SELECT COUNT(*) car_count
        FROM order_info a
        WHERE a.create_time >= UNIX_TIMESTAMP(CURDATE())
          AND a.create_time <![CDATA[ < ]]> UNIX_TIMESTAMP(DATE_ADD(CURDATE(), INTERVAL 24 HOUR))
          AND a.order_status = 'YiWanCheng'
    </select>

    <select id="getRegionSummary" resultType="com.njzscloud.supervisory.bs.pojo.result.RegionSummary">
        SELECT b.area,
               IFNULL(SUM(c.settle_weight), 0) total_weight
        FROM order_info a
                 INNER JOIN order_cargo_place b ON b.id = a.cargo_place_id
                 INNER JOIN order_car_in_out c ON c.id = a.car_in_out_id

        WHERE a.order_status = 'YiWanCheng'
        GROUP BY b.area
    </select>

    <select id="getCompanySummary" resultType="com.njzscloud.supervisory.bs.pojo.result.CompanySummary">
        SELECT t1.total_weight, t2.company_name company
        FROM (SELECT IFNULL(SUM(b.settle_weight), 0) total_weight, a.trans_company_id
              FROM order_info a
                       INNER JOIN order_car_in_out b ON b.id = a.car_in_out_id
              WHERE order_status = 'YiWanCheng'
              GROUP BY a.trans_company_id) t1
                 INNER JOIN biz_company t2 ON t2.id = t1.trans_company_id
        ORDER BY t1.total_weight DESC
        LIMIT 5
    </select>

    <select id="getStationInfo" resultType="com.njzscloud.supervisory.bs.pojo.result.StationInfo">
        SELECT company_name `name`,
               lat          latitude,
               lng          longitude
        FROM biz_company
        WHERE station = 1
    </select>

    <select id="getOrders" resultType="com.njzscloud.supervisory.bs.pojo.result.Order">
        SELECT a.id                                    order_id,
               a.sn                                    order_no,
               d.nickname                              realname,
               d.phone                                 mobile,
               i.category_name                         goods_category_name,
               a.create_time,
               CASE order_status
                   WHEN 'YiYuYue' THEN '已预约'
                   WHEN 'DaiPaiDan' THEN '待派单'
                   WHEN 'DaiJieDan' THEN '待接单'
                   WHEN 'YiJieDan' THEN '已接单'
                   WHEN 'QingYunZhong' THEN '清运中'
                   WHEN 'YiJinChang' THEN '已进场'
                   WHEN 'YiChuChang' THEN '已出场'
                   WHEN 'YiWanCheng' THEN '已完成'
                   ELSE ''
                   END                                 order_status,
               g.license_plate                         car_number,
               f.nickname                              driver_name,
               e.goods_name                            goods_name,
               c.company_name                          company,
               h.address,
               h.lng                                   lon,
               h.lat,
               IF(a.order_status = 'YiWanCheng', 1, 0) finish,
               g.gps
        FROM order_info a
                 LEFT JOIN (SELECT a.id, a.company_name, b.nickname
                            FROM biz_company a
                                     INNER JOIN sys_user b ON b.id = a.user_id) c ON c.id = a.trans_company_id
                 LEFT JOIN sys_user d ON d.id = a.user_id
                 LEFT JOIN sys_user f ON f.id = a.driver_id
                 LEFT JOIN order_goods e ON e.id = a.goods_id
                 LEFT JOIN biz_truck g ON g.id = a.truck_id
                 INNER JOIN order_cargo_place h ON h.id = a.cargo_place_id
                 INNER JOIN goods_category i ON i.id = e.goods_category_id
        WHERE a.order_status IN ('YiYuYue', 'DaiPaiDan', 'DaiJieDan', 'QingYunZhong', 'YiJinChang', 'YiChuChang', 'YiWanCheng')
        ORDER BY a.create_time DESC
        LIMIT 50
    </select>

    <select id="getProjects" resultType="com.njzscloud.supervisory.bs.pojo.result.Project">
        SELECT g.project_name,
               g.area,
               g.town     street,
               g.address,
               c.nickname realname,
               c.phone    mobile
        FROM biz_project g
                 LEFT JOIN sys_user c ON c.id = g.user_id
        ORDER BY g.create_time DESC
        LIMIT 10
    </select>
</mapper>
