<?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.ruoyi.logistics.mapper.LargeScreenReportMapper">



   <!-- 按照订单类型查询订单量-->
    <select id="queryOrderTypeReport"  parameterType="java.util.Map" resultType="java.util.HashMap"  >

        SELECT dict_label orderTypeName ,dict_value orderType,ifnull(num,0) num  FROM (

        SELECT dict_label, dict_value FROM sys_dict_data WHERE dict_type = 'logistics_type') a
        left join (
        SELECT
            order_type,
            count(1) num
        FROM
            biz_waybill_order d
        WHERE   order_status != 6

        <if test="orderType != null and orderType != ''">
            and d.order_type = #{orderType}
        </if>
        <if test="startTime != null and startTime != ''">
            and DATE_FORMAT(d.create_time, '%Y-%m-%d') >= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            and DATE_FORMAT(d.create_time, '%Y-%m-%d')<![CDATA[<=]]>  #{endTime}
        </if>
        <if test="deptId != null and deptId != ''">
            and d.dept_id = #{deptId}
        </if>
        <!-- 数据范围过滤 -->
        ${params.dataScope}

        GROUP BY
            order_type) b ON a.dict_value = b.order_type
        </select>




    <!-- 每日订单量-->
    <select id="queryDayOrdersReport"  parameterType="java.util.Map" resultType="java.util.HashMap"  >
        SELECT
        date_list.day_time AS time,
        IFNULL(t.order_num, 0) AS order_num,
        IFNULL(t.delivery_num, 0) AS delivery_num,
        IFNULL(t.proper_num, 0) AS proper_num
        FROM
        (
        SELECT
        DATE_FORMAT(
        DATE_ADD(#{startTime}, INTERVAL (a + 10*b + 100*c) DAY),
        '%Y-%m-%d'
        ) AS day_time
        FROM
        (SELECT 0 a UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) ta,
        (SELECT 0 b UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) tb,
        (SELECT 0 c UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) tc
        HAVING
        day_time<![CDATA[<=]]>#{endTime}
        ) date_list
        LEFT JOIN (
        SELECT
        DATE_FORMAT(create_time, '%Y-%m-%d') AS time,
        SUM(order_status != 6) AS order_num,
        SUM(order_status != 6 AND order_status != 5) AS delivery_num,
        SUM(order_status = 5) AS proper_num
        FROM biz_waybill_order d
        WHERE DATE_FORMAT(d.create_time, '%Y-%m-%d') >=#{startTime} AND DATE_FORMAT(d.create_time, '%Y-%m-%d') <![CDATA[<=]]> #{endTime}
        <if test="orderType != null and orderType != ''">
            and d.order_type = #{orderType}
        </if>
        <if test="deptId != null and deptId != ''">
            and d.dept_id = #{deptId}
        </if>
        <!-- 数据范围过滤 -->
        ${params.dataScope}

        GROUP BY DATE_FORMAT(d.create_time, '%Y-%m-%d')
        ) t ON date_list.day_time = t.time
        ORDER BY date_list.day_time;

    </select>




   <!-- 按照省份查询订单量和金额（整合版）-->
    <select id="queryProvinceOrdersReport"  parameterType="java.util.Map" resultType="java.util.HashMap"  >
        SELECT
            province_name,
            merger_name,
            IFNULL(outflow_num, 0) AS outflowNum,
            IFNULL(inflow_num, 0) AS inflowNum,
            IFNULL(outflow_amount, 0) AS outflowAmount,
            IFNULL(inflow_amount, 0) AS inflowAmount
        FROM (
            SELECT
                a.NAME AS province_name,
                a.merger_name,
                -- 流出订单数（该省作为发货地）
                IFNULL(outflow_order.num, 0) AS outflow_num,
                -- 流入订单数（该省作为收货地）
                IFNULL(inflow_order.num, 0) AS inflow_num,
                -- 流出金额（该省作为发货地）
                IFNULL(outflow_cost.amt, 0) AS outflow_amount,
                -- 流入金额（该省作为收货地）
                IFNULL(inflow_cost.amt, 0) AS inflow_amount
            FROM
                (SELECT NAME, merger_name FROM sys_city WHERE `level` = 0) a
            LEFT JOIN (
                -- 流出订单统计
                SELECT
                    sender_province,
                    COUNT(1) AS num
                FROM biz_waybill_order d
                WHERE d.order_status != 6
                <if test="orderType != null and orderType != ''">
                    AND d.order_type = #{orderType}
                </if>
                <if test="startTime != null and startTime != ''">
                    AND DATE_FORMAT(d.create_time, '%Y-%m-%d') >= #{startTime}
                </if>
                <if test="endTime != null and endTime != ''">
                    AND DATE_FORMAT(d.create_time, '%Y-%m-%d') <![CDATA[<=]]> #{endTime}
                </if>
                <if test="deptId != null and deptId != ''">
                    AND d.dept_id = #{deptId}
                </if>
                ${params.dataScope}
                GROUP BY sender_province
            ) outflow_order ON a.NAME = outflow_order.sender_province
            LEFT JOIN (
                -- 流入订单统计
                SELECT
                    receiver_province,
                    COUNT(1) AS num
                FROM biz_waybill_order d
                WHERE d.order_status != 6
                <if test="orderType != null and orderType != ''">
                    AND d.order_type = #{orderType}
                </if>
                <if test="startTime != null and startTime != ''">
                    AND DATE_FORMAT(d.create_time, '%Y-%m-%d') >= #{startTime}
                </if>
                <if test="endTime != null and endTime != ''">
                    AND DATE_FORMAT(d.create_time, '%Y-%m-%d') <![CDATA[<=]]> #{endTime}
                </if>
                <if test="deptId != null and deptId != ''">
                    AND d.dept_id = #{deptId}
                </if>
                ${params.dataScope}
                GROUP BY receiver_province
            ) inflow_order ON a.NAME = inflow_order.receiver_province
            LEFT JOIN (
                -- 流出金额统计
                SELECT
                    d.sender_province,
                    IFNULL(SUM(IFNULL(c.adjust_amount, c.rate_amount)), 0) AS amt
                FROM biz_waybill_order d
                LEFT JOIN biz_waybill_cost_details c ON d.waybill_id = c.waybill_id
                WHERE d.order_status != 6
                <if test="orderType != null and orderType != ''">
                    AND d.order_type = #{orderType}
                </if>
                <if test="startTime != null and startTime != ''">
                    AND DATE_FORMAT(d.create_time, '%Y-%m-%d') >= #{startTime}
                </if>
                <if test="endTime != null and endTime != ''">
                    AND DATE_FORMAT(d.create_time, '%Y-%m-%d') <![CDATA[<=]]> #{endTime}
                </if>
                <if test="deptId != null and deptId != ''">
                    AND d.dept_id = #{deptId}
                </if>
                ${params.dataScope}
                GROUP BY d.sender_province
            ) outflow_cost ON a.NAME = outflow_cost.sender_province
            LEFT JOIN (
                -- 流入金额统计
                SELECT
                    d.receiver_province,
                    IFNULL(SUM(IFNULL(c.adjust_amount, c.rate_amount)), 0) AS amt
                FROM biz_waybill_order d
                LEFT JOIN biz_waybill_cost_details c ON d.waybill_id = c.waybill_id
                WHERE d.order_status != 6
                <if test="orderType != null and orderType != ''">
                    AND d.order_type = #{orderType}
                </if>
                <if test="startTime != null and startTime != ''">
                    AND DATE_FORMAT(d.create_time, '%Y-%m-%d') >= #{startTime}
                </if>
                <if test="endTime != null and endTime != ''">
                    AND DATE_FORMAT(d.create_time, '%Y-%m-%d') <![CDATA[<=]]> #{endTime}
                </if>
                <if test="deptId != null and deptId != ''">
                    AND d.dept_id = #{deptId}
                </if>
                ${params.dataScope}
                GROUP BY d.receiver_province
            ) inflow_cost ON a.NAME = inflow_cost.receiver_province
        ) t
            <if test="rankType != null and rankType != '' and rankType == 1">
                ORDER BY outflow_amount DESC
            </if>
            <if test="rankType != null and rankType != '' and rankType == 2">
                ORDER BY outflow_num DESC
            </if>
    </select>

    <!-- 供应商列表-->
    <select id="queryDeptList"  parameterType="java.util.Map" resultType="java.util.HashMap"  >
        select d.dept_id deptId,d.dept_name deptName
        from  sys_dept d
        where  d.status=0 and d.del_flag=0
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>


    <!-- 省份列表-->
    <select id="queryProvinceList"  parameterType="java.util.Map" resultType="java.util.HashMap"  >
        SELECT
        a.name AS provinceName,
        a.lng,
        a.lat,
        IFNULL(SUM(CASE WHEN a.name = d.sender_province THEN 1 ELSE 0 END), 0) AS sendCount,
        IFNULL(SUM(CASE WHEN a.name = d.receiver_province THEN 1 ELSE 0 END), 0) AS recvCount
        FROM sys_city a
        LEFT JOIN biz_waybill_order d
        ON (a.name = d.sender_province OR a.name = d.receiver_province)

        <if test="orderType != null and orderType != ''">
            and d.order_type = #{orderType}
        </if>
        <if test="startTime != null and startTime != ''">
            and DATE_FORMAT(d.create_time, '%Y-%m-%d') >= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            and DATE_FORMAT(d.create_time, '%Y-%m-%d')<![CDATA[<=]]>  #{endTime}
        </if>
        <if test="deptId != null and deptId != ''">
            and d.dept_id = #{deptId}
        </if>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        WHERE a.level = 0
        GROUP BY a.name, a.lng, a.lat
        ORDER BY sendCount DESC, recvCount DESC;




    </select>





    <!-- 物流轨迹省份查询流出-->
    <select id="queryLogisticsTrajectoryOutflow"  parameterType="java.util.Map" resultType="java.util.HashMap"  >
        SELECT
            d.receiver_province provinceName,
            b.lng,
            b.lat,
            count(1) num,
            IFNULL(SUM(IFNULL(c.adjust_amount, c.rate_amount)), 0.00) amt
        FROM
            biz_waybill_order d
                LEFT JOIN sys_city b ON  d.receiver_province = b.NAME
                LEFT JOIN biz_waybill_cost_details c ON d.waybill_id = c.waybill_id
        WHERE
            b.LEVEL = 0
       and    d.order_status != 6
        <if test="orderType != null and orderType != ''">
            and   d.order_type = #{orderType}
        </if>
        <if test="startTime != null and startTime != ''">
            and DATE_FORMAT( d.create_time, '%Y-%m-%d') >= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            and DATE_FORMAT( d.create_time, '%Y-%m-%d')<![CDATA[<=]]>  #{endTime}
        </if>
        <if test="deptId != null and deptId != ''">
            and   d.dept_id = #{deptId}
        </if>
        <if test="searchProvince != null and searchProvince != ''">
            and  d.sender_province = #{searchProvince}
        </if>
        ${params.dataScope}
        group by d.receiver_province
    </select>
    <!-- 物流轨迹省份查询流入-->
    <select id="queryLogisticsTrajectoryinflow"  parameterType="java.util.Map" resultType="java.util.HashMap"  >
        SELECT
            d.sender_province provinceName,
            b.lng,
            b.lat,
            count(1) num,
            IFNULL(SUM(IFNULL(c.adjust_amount, c.rate_amount)), 0.00) amt
        FROM
            biz_waybill_order    d
                LEFT JOIN sys_city b ON d.sender_province = b.NAME
                LEFT JOIN biz_waybill_cost_details c ON d.waybill_id = c.waybill_id
        WHERE
            b.LEVEL = 0
        and  d.order_status != 6
        <if test="orderType != null and orderType != ''">
            and  d.order_type = #{orderType}
        </if>
        <if test="startTime != null and startTime != ''">
            and DATE_FORMAT(d.create_time, '%Y-%m-%d') >= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            and DATE_FORMAT(d.create_time, '%Y-%m-%d')<![CDATA[<=]]>  #{endTime}
        </if>
        <if test="deptId != null and deptId != ''">
            and  d.dept_id = #{deptId}
        </if>
        <if test="searchProvince != null and searchProvince != ''">
            and  d.receiver_province = #{searchProvince}
        </if>
        ${params.dataScope}
        group by d.sender_province

    </select>
    <!-- 按照快递快运区分统计-->
    <select id="queryTransportTypeReport"  parameterType="java.util.Map" resultType="java.util.HashMap"  >
        SELECT
            c.dict_label typeName,
            ifnull( d.num, 0 ) num
        FROM
            ( SELECT DISTINCT dict_label FROM sys_dict_data WHERE dict_type = 'rjsd_logistics_product' ) c
                LEFT JOIN (
                SELECT
                    dict_label typeName,
                    count( 1 ) num
                FROM
                    biz_waybill_order d
                        LEFT JOIN ( SELECT dict_label, dict_value FROM sys_dict_data WHERE dict_type = 'rjsd_logistics_product' ) b ON d.product_code = b.dict_value
                WHERE
                    d.order_status != 6
        <if test="orderType != null and orderType != ''">
            and  d.order_type = #{orderType}
        </if>
        <if test="startTime != null and startTime != ''">
            and DATE_FORMAT(d.create_time, '%Y-%m-%d') >= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            and DATE_FORMAT(d.create_time, '%Y-%m-%d')<![CDATA[<=]]>  #{endTime}
        </if>
        <if test="deptId != null and deptId != ''">
            and  d.dept_id = #{deptId}
        </if>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
                GROUP BY
                    dict_label
            ) d ON c.dict_label = d.typeName
    </select>

    <!-- 物流费用统计-->
    <select id="queryFreeOrdersReport"  parameterType="java.util.Map" resultType="java.util.HashMap"  >
        SELECT
        w.dict_label typeName,
        ifnull( q.rate_amount, 0.00 ) rateAmount
        FROM
        ( SELECT dict_label, dict_value FROM sys_dict_data WHERE dict_type = 'logistics_type' ) w
        LEFT JOIN (
        SELECT
        sum(
        ifnull( a.adjust_amount, a.rate_amount )) rate_amount,
        order_type
        FROM
        biz_waybill_cost_details a
        LEFT JOIN biz_waybill_order d ON a.external_waybill_no = d.external_waybill_no
        WHERE
        d.order_status != 6
        <if test="orderType != null and orderType != ''">
            and  d.order_type = #{orderType}
        </if>
        <if test="startTime != null and startTime != ''">
            and DATE_FORMAT(d.create_time, '%Y-%m-%d') >= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            and DATE_FORMAT(d.create_time, '%Y-%m-%d')<![CDATA[<=]]>  #{endTime}
        </if>
        <if test="deptId != null and deptId != ''">
            and  d.dept_id = #{deptId}
        </if>

        <!-- 数据范围过滤 -->
        ${params.dataScope}
        GROUP BY
        order_type
        ) q ON w.dict_value = q.order_type
 </select>


    <!--供应商按照金额排名-->
    <select id="queryOrdersReportByFree"  parameterType="java.util.Map" resultType="java.util.HashMap"  >
        SELECT
            t.dept_name deptName,
            t.rate_amount rateAmount,
            (@rank := @rank + 1) AS rankNum
        FROM (
                 SELECT
                     dept_name,
                     IFNULL(rate_amount, 0) AS rate_amount
                 FROM
                     sys_dept w
                         LEFT JOIN (
                         SELECT
                             SUM(IFNULL(a.adjust_amount, a.rate_amount)) AS rate_amount,
                             a.dept_id
                         FROM
                             biz_waybill_cost_details a
                                 LEFT JOIN biz_waybill_order b
                                           ON a.external_waybill_no = b.external_waybill_no
                         WHERE
                             b.order_status != 6
        <if test="orderType != null and orderType != ''">
            and  b.order_type = #{orderType}
        </if>
        <if test="startTime != null and startTime != ''">
            and DATE_FORMAT(b.create_time, '%Y-%m-%d') >= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            and DATE_FORMAT(b.create_time, '%Y-%m-%d')<![CDATA[<=]]>  #{endTime}
        </if>
        <if test="deptId != null and deptId != ''">
            and  a.dept_id = #{deptId}
        </if>
                         GROUP BY
                             a.dept_id
                     ) q ON w.dept_id = q.dept_id
                 where w.del_flag='0'
                   and w.status='0'
                 ORDER BY
                     rate_amount DESC
             ) t,
             (SELECT @rank := 0) r
            LIMIT 10;


    </select>


    <!--供应商按照订单量排名-->
    <select id="queryOrdersReportByNum"  parameterType="java.util.Map" resultType="java.util.HashMap"  >
        SELECT
            t.dept_name deptName,
            t.num,
            (@rank := @rank + 1) AS rankNum
        FROM (
                 SELECT
                     dept_name,
                     IFNULL(num, 0) AS num
                 FROM
                     sys_dept w
                         LEFT JOIN (
                         SELECT
                             COUNT(external_waybill_no) AS num,
                             dept_id
                         FROM
                             biz_waybill_order
                         WHERE
                             order_status != 6
        <if test="orderType != null and orderType != ''">
            and  order_type = #{orderType}
        </if>
        <if test="startTime != null and startTime != ''">
            and DATE_FORMAT(create_time, '%Y-%m-%d') >= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            and DATE_FORMAT(create_time, '%Y-%m-%d')<![CDATA[<=]]>  #{endTime}
        </if>
        <if test="deptId != null and deptId != ''">
            and  dept_id = #{deptId}
        </if>
                         GROUP BY
                             dept_id
                     ) q ON w.dept_id = q.dept_id

                 where w.del_flag='0'
                   and w.status='0'
                 ORDER BY
                     num DESC
             ) t,
             (SELECT @rank := 0) r
            LIMIT 10;

    </select>



    <!--供应商月度增长率-->
    <select id="queryDeptMonthlyGrowthRate"  parameterType="java.util.Map" resultType="java.util.HashMap"  >
        SELECT
            t1.month,
            t1.rate_amount,
            IFNULL(t2.rate_amount, 0) AS last_month_amount,
            CASE
                WHEN IFNULL(t2.rate_amount, 0) = 0 and IFNULL(t1.rate_amount, 0) = 0 then  0
                WHEN IFNULL(t2.rate_amount, 0) = 0 and IFNULL(t1.rate_amount, 0) !=0 then  100
                ELSE ROUND((t1.rate_amount - t2.rate_amount) / t2.rate_amount * 100 , 2)
                END AS growth_rate
        FROM (
                 SELECT
                     months.month,
                     IFNULL(data.rate_amount, 0) AS rate_amount
                 FROM (
                          SELECT DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL (a) MONTH), '%Y-%m') AS month
                          FROM (
                              SELECT 0 a UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5
                              ) t
                      ) months
                          LEFT JOIN (
                     SELECT
                         SUM(IFNULL(a.adjust_amount, a.rate_amount)) rate_amount,
                         DATE_FORMAT(d.create_time, '%Y-%m') AS month
                     FROM biz_waybill_cost_details a
                         LEFT JOIN biz_waybill_order d ON a.external_waybill_no = d.external_waybill_no
                     WHERE d.order_status != 6


        <if test="orderType != null and orderType != ''">
            and  d.order_type = #{orderType}
        </if>
        <if test="startDate != null and startDate != ''">
            and DATE_FORMAT(d.create_time, '%Y-%m-%d') >= #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            and DATE_FORMAT(d.create_time, '%Y-%m-%d')<![CDATA[<=]]>  #{endDate}
        </if>
        <if test="deptId != null and deptId != ''">
            AND d.dept_id = #{deptId}
        </if>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
                     GROUP BY DATE_FORMAT(d.create_time, '%Y-%m')
                 ) data ON months.month = data.month
             ) t1
                 LEFT JOIN (
            SELECT
                months.month,
                IFNULL(data.rate_amount, 0) AS rate_amount
            FROM (
                     SELECT DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL (a) MONTH), '%Y-%m') AS month
                     FROM (
                         SELECT 0 a UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5
                         ) t
                 ) months
                     LEFT JOIN (
                SELECT
                    SUM(IFNULL(a.adjust_amount, a.rate_amount)) rate_amount,
                    DATE_FORMAT(d.create_time, '%Y-%m') AS month
                FROM biz_waybill_cost_details a
                    LEFT JOIN biz_waybill_order d ON a.external_waybill_no = d.external_waybill_no
                WHERE d.order_status != 6
        <if test="orderType != null and orderType != ''">
            and  d.order_type = #{orderType}
        </if>
        <if test="startDate != null and startDate != ''">
            and DATE_FORMAT(d.create_time, '%Y-%m-%d') >= #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            and DATE_FORMAT(d.create_time, '%Y-%m-%d')<![CDATA[<=]]>  #{endDate}
        </if>
        <if test="deptId != null and deptId != ''">
            AND d.dept_id = #{deptId}
        </if>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
                GROUP BY DATE_FORMAT(d.create_time, '%Y-%m')
            ) data ON months.month = data.month
        ) t2 ON t1.month = DATE_FORMAT(STR_TO_DATE(CONCAT(t2.month, '-01'), '%Y-%m-%d') + INTERVAL 1 MONTH, '%Y-%m')
        ORDER BY t1.month;

    </select>
</mapper>