<?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="cn.iocoder.yudao.module.delivery.mapper.StatisticalSummaryMapper">
    <select id="GetStatisticalSummary"
            resultType="cn.iocoder.yudao.module.delivery.entity.orderdata.StatisticalSummary">
        select c.order_id,
        sum(sum_quantity) as sumBox,
        sum(if(c.transport_id = 1, sum_volume, 0)) as sumM3,
        sum(if(c.transport_id = 3, sum_weight, 0)) as sumKG,
        ROUND(sum(IF(c.transport_id = 1,
        c.sum_volume / (SELECT ecw_vz.vz FROM ecw_vz WHERE ecw_vz.fuhao = 'M3' LIMIT 1),
        round(c.sum_weight / (SELECT ecw_vz.vz FROM ecw_vz WHERE ecw_vz.fuhao = 'KG' LIMIT 1), 2))),
        2) as sumV,
        sum(if(cf.is_new = 1, sum_quantity, 0)) as sumBoxNew,
        sum(if(cf.is_new = 1, if(c.transport_id = 1, sum_volume, 0), 0)) as sumM3New,
        sum(if(cf.is_new = 1, if(c.transport_id = 3, sum_weight, 0), 0)) as sumKGNew,
        round(sum(if(cf.is_new = 1, IF(c.transport_id = 1, c.sum_volume /
        (SELECT ecw_vz.vz FROM ecw_vz WHERE ecw_vz.fuhao = 'M3' LIMIT 1),
        round(c.sum_weight /
        (SELECT ecw_vz.vz FROM ecw_vz WHERE ecw_vz.fuhao = 'KG' LIMIT 1),
        2)), 0)), 2) as sumVNew,
        sum(if(cf.is_new = 0, sum_quantity, 0)) as sumBoxOld,
        sum(if(cf.is_new = 0, if(c.transport_id = 1, sum_volume, 0), 0)) as sumM3Old,
        sum(if(cf.is_new = 0, if(c.transport_id = 3, sum_weight, 0), 0)) as sumKGOld,
        round(sum(if(cf.is_new = 0, IF(c.transport_id = 1, c.sum_volume /
        (SELECT ecw_vz.vz FROM ecw_vz WHERE ecw_vz.fuhao = 'M3' LIMIT 1),
        round(c.sum_weight /
        (SELECT ecw_vz.vz FROM ecw_vz WHERE ecw_vz.fuhao = 'KG' LIMIT 1),
        2)), 0)), 2) as sumVOld
        from ecw_order c
        left join (SELECT ewl.id AS line_id,
        ew_start.id AS start_warehouse_id,
        ew_dest.id AS dst_warehouse_id,
        ew_start.title_zh AS start_title_zh,
        ew_dest.title_zh AS dst_warehouse_name,
        r_dest.title_zh as muDiGuo,
        r_dest.id as muDiGuoID,
        r_sdest.title_zh as muDiShi,
        r_sdest.id as muDiShiID,
        ew_start.title_zh AS start_warehouse_name
        FROM ecw_warehouse_line ewl
        LEFT JOIN ecw_warehouse ew_start ON ewl.start_warehouse_id = ew_start.id
        LEFT JOIN ecw_warehouse ew_dest ON ewl.dest_warehouse_id = ew_dest.id
        left join ecw_region r_dest on ew_dest.guojia = r_dest.id
        LEFT JOIN ecw_region r_sdest on ew_dest.shi = r_sdest.id
        ) w ON w.line_id = c.line_id
        left join ecw_channel channel on channel.channel_id = c.channel_id and channel.deleted = 0
        left join ecw_order_consignor nor on nor.order_id = c.order_id and nor.deleted = 0
        left join ecw_customer cf on cf.id = nor.customer_id and cf.deleted = 0
        left join ecw_order_consignee nee on nee.order_id = c.order_id and nee.deleted = 0
        left join ecw_customer sh on sh.id = nee.customer_id and sh.deleted = 0
        <include refid="WherePage"></include>

    </select>

    <sql id="WherePage">
        <where>
            and c.deleted = 0
            <if test="orderNo!=null and orderNo!=''">
                <!--订单编号-->
                <if test="SearchType1!=null and SearchType1!=''">
                    <choose>
                        <when test="SearchType1 =='in'.toString()">
                            and c.order_no like concat('%',#{orderNo},'%')
                        </when>
                        <when test="SearchType1 =='notin'.toString()">
                            and c.order_no not like concat('%',#{orderNo},'%')
                        </when>
                        <when test="SearchType1 =='eq'.toString()">
                            and c.order_no= #{orderNo}
                        </when>
                        <when test="SearchType1 =='noeq'.toString()">
                            and c.order_no!= #{orderNo}
                        </when>
                        <otherwise>
                            and c.order_no= #{orderNo}
                        </otherwise>
                    </choose>
                </if>
            </if>
            <if test="tidanNo!=null and tidanNo!=''">
                <!--提单编号-->
                <if test="SearchType2!=null and SearchType2!=''">
                    <choose>
                        <when test="SearchType2 =='in'.toString()">
                            and c.tidan_no like concat('%',#{tidanNo},'%')
                        </when>
                        <when test="SearchType2 =='notin'.toString()">
                            and c.tidan_no not like concat('%',#{tidanNo},'%')
                        </when>
                        <when test="SearchType2 =='eq'.toString()">
                            and c.tidan_no= #{tidanNo}
                        </when>
                        <when test="SearchType2 =='noeq'.toString()">
                            and c.tidan_no!= #{tidanNo}
                        </when>
                        <otherwise>
                            and c.tidan_no= #{tidanNo}
                        </otherwise>
                    </choose>
                </if>
            </if>
            <if test="fhNumber!=null and fhNumber!=''">
                <!--发货人编号-->
                <if test="SearchType3!=null and SearchType3!=''">
                    <choose>
                        <when test="SearchType3 =='in'.toString()">
                            and cf.number like concat('%',#{fhNumber},'%')
                        </when>
                        <when test="SearchType3 =='notin'.toString()">
                            and cf.number not like concat('%',#{fhNumber},'%')
                        </when>
                        <when test="SearchType3 =='eq'.toString()">
                            and cf.number=#{fhNumber}
                        </when>
                        <when test="SearchType3 =='noeq'.toString()">
                            and cf.number!= #{fhNumber}
                        </when>
                        <otherwise>
                            and cf.number= #{fhNumber}
                        </otherwise>
                    </choose>
                </if>
            </if>
            <if test="shNumber!=null and shNumber!=''">
                <!--收货人编号-->
                <if test="SearchType4!=null and SearchType4!=''">
                    <choose>
                        <when test="SearchType4 =='in'.toString()">
                            and sh.number like concat('%',#{shNumber},'%')
                        </when>
                        <when test="SearchType4 =='notin'.toString()">
                            and sh.number not like concat('%',#{shNumber},'%')
                        </when>
                        <when test="SearchType4 =='eq'.toString()">
                            and sh.number= #{shNumber}
                        </when>
                        <when test="SearchType4 =='noeq'.toString()">
                            and sh.number!= #{shNumber}
                        </when>
                        <otherwise>
                            and sh.number= #{shNumber}
                        </otherwise>
                    </choose>
                </if>
            </if>
            <if test="marks!=null and marks!=''">
                <!--唛头-->
                <if test="SearchType5!=null and SearchType5!=''">
                    <choose>
                        <when test="SearchType5 =='in'.toString()">
                            and c.marks like concat('%',#{marks},'%')
                        </when>
                        <when test="SearchType5 =='notin'.toString()">
                            and c.marks not like concat('%',#{marks},'%')
                        </when>
                        <when test="SearchType5 =='eq'.toString()">
                            and c.marks=#{marks}
                        </when>
                        <when test="SearchType5 =='noeq'.toString()">
                            and c.marks!= #{marks}
                        </when>
                        <otherwise>
                            and c.marks= #{marks}
                        </otherwise>
                    </choose>
                </if>
            </if>
            <if test="salesmanId!=null and salesmanId!=''">
                <!--客户经理-->
                and c.salesman_id= #{salesmanId}
            </if>
            <if test="status!=null and status!=''">
                <!--订单状态-->
                and c.status= #{status}
            </if>
            <if test="transportId!=null and transportId!=''">
                <!--运输方式查询条件-->
                and c.transport_id= #{transportId}
            </if>
            <if test="startWarehouseId!=null and startWarehouseId!=''">
                <!--始发仓-->
                and w.start_warehouse_id= #{startWarehouseId}
            </if>
            <if test="muDiGuo!=null and muDiGuo!=''">
                <!--目的国-->
                and w.muDiGuoID= #{muDiGuo}
            </if>
            <if test="muDiShi!=null and muDiShi!=''">
                <!--目的城市-->
                and w.muDiShiID= #{muDiShi}
            </if>
            <if test="dstwarehouseid!=null and dstwarehouseid!=''">
                <!--目的仓-->
                and w.dst_warehouse_id= #{dstwarehouseid}
            </if>
            <if test="isCargoControl!=null">
                <!--是否控货 注意整型值的判断-->
                and c.is_cargo_control= #{isCargoControl}
            </if>
            <if test="zpType!=null and zpType!=''">
                <!--重泡货类型-->
                and c.order_type= #{zpType}
            </if>
            <if test="drawee!=null and drawee!=''">
                <!--付款方-->
                and c.drawee= #{drawee}
            </if>
            <if test="shippingChannelId!=null and shippingChannelId!=''">
                <!--渠道-->
                and c.channel_id= #{shippingChannelId}
            </if>
            <if test="ResourceType!=null and ResourceType!=''">
                <!--资源类型-->
                and c.channel_id= #{ResourceType}
            </if>
            <if test="YeJiType!=null and YeJiType!=''">
                <!--业绩类型-->
                and c.channel_id= #{YeJiType}
            </if>
            <if test="dateSearch1!=null and dateSearch1!=''
              and sdate1!=null and sdate1!='' and edate2!=null and edate2!=''">
                <!--订单创建时间-->
                and c.create_time BETWEEN #{sdate1} AND #{edate2}
            </if>
            <if test="dataSearch3!=null and dataSearch3!=''">
                <if test="dataSearch1!=null and dataSearch1!=''">
                    <if test="dataSearch2=null and dataSearch2!=''">
                        <!--数据筛选-->
                        <choose>
                            <when test="dataSearch1 =='1'.toString()">
                                <!--入仓箱数-->
                            </when>
                            <when test="dataSearch1 =='2'.toString()">
                                <!--入仓体积-->
                            </when>
                            <when test="dataSearch1 =='3'.toString()">
                                <!--入仓重量-->
                            </when>
                            <when test="dataSearch1 =='3'.toString()">
                                <!--重/泡标准-->
                            </when>
                            <when test="dataSearch1 =='3'.toString()">
                                <!--提货率-->
                            </when>
                        </choose>
                    </if>
                </if>
            </if>
        </where>
    </sql>


</mapper>