<?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.SalesAnalysisMapper">
    <select id="getListPage"
            parameterType="cn.iocoder.yudao.module.delivery.entity.SalesAnalysisReq"
            resultType="cn.iocoder.yudao.module.delivery.entity.SalesAnalysisResp">
        select c.order_id as orderId, order_no as orderNo, tidan_no as tidanNo,
        c.status,salesman_id as salesmanid,
        (SELECT nickname from system_user where id=c.salesman_id) as salesmanidName,
        c.transport_id as transportId,c.drawee,
        if(c.transport_id=1,'海运拼柜','专线空运') as transportIdName,
        order_type as ordertype,use_weight_unit as useweightunit,
        use_volume_unit as usevolumeunit,
        c.marks,w.start_warehouse_name as startwarehousename,
        w.dst_warehouse_name as dstwarehousename,w.muDiGuo,w.muDiShi,
        c.channel_id as channelid,channel.name_zh as channelidName,
        c.create_time as createtime,sum_quantity as sumquantity,
        sum_volume as sumvolume,sum_weight as sumweight,
        if(is_cargo_control=0,'否','是') as iscargocontrol,
        ( if( c.transport_id = 1 , c.load_time, c.rucang_time) ) AS businesstime,
        cf.number as fhNumber,sh.number as shNumber,
        cf.id as cfCustomerId,sh.id as shCustomerId,
        cf.resource_type as cfresourceType,sh.resource_type as shResource_type,
        cf.is_new as cfYeJiType,sh.is_new as shYeJitype,
        (SELECT sum(ecw_order_pickup.pick_num) FROM ecw_order_pickup
        WHERE ( ecw_order_pickup.order_id = c.order_no)) AS picknum,
        (select in_time from ecw_order_warehouse_in where
        order_id=c.order_id ORDER BY in_time asc LIMIT 1) as firstInTime
        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"/>
        ORDER BY c.create_time asc limit #{start}, #{size}
    </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>


    <select id="GetCount" resultType="java.lang.Long">
        SELECT count(1) 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"/>
    </select>

    <select id="getList"
            parameterType="cn.iocoder.yudao.module.delivery.entity.SalesAnalysisReq"
            resultType="cn.iocoder.yudao.module.delivery.entity.SalesAnalysisExcelResp">
        select order_no as orderNo, tidan_no as tidanNo,
        c.status,salesman_id as salesmanid,transport_id,marks,
        w.start_warehouse_name as startwarehousename,
        w.dst_warehouse_name as dstwarehousename,w.muDiGuo,w.muDiShi,
        c.channel_id as channelid,channel.name_zh as channelidName,
        c.create_time as createtime,sum_quantity as sumquantity,
        sum_volume as sumvolume,sum_weight as sumweight,
        is_cargo_control as iscargocontrol
        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_sdest.title_zh as muDiShi,
        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
        <include refid="WherePage"/>

    </select>

</mapper>