<?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.CustomerAnalysisMapper">
    <select id="getListPage"
            parameterType="cn.iocoder.yudao.module.delivery.entity.CustomerAnalysisReq"
            resultType="cn.iocoder.yudao.module.delivery.entity.CustomerAnalysisResp">
        with dataTJ AS(
        SELECT number,name,c.id,(
        SELECT nickname from system_user where id=c.customer_service
        ) as salesman,c.customer_service as salesmanid,
        (SELECT label from system_dict_data
        where dict_type='customer_source' and value=c.source) as sourcename,
        c.source,(u_d.dept_id) as deptid,(u_d.deptName) as deptname,
        (select round(sum(vz),2) from view_order_businesstime where customer_id = c.id and business_time BETWEEN
        '${sdate}' AND '${edate}') AS allsumvolume,
        (select round(sum(vz),2) from view_order_businesstime where customer_id = c.id and business_time BETWEEN
        '${sduibidate}' AND '${eduibidate}') as allsumvolumeTb,
        <!--海运-->
        (select round(sum(charge_volume),2) from view_order_businesstime where customer_id = c.id and
        business_time BETWEEN '${sdate}' AND '${edate}' and transport_id=1) AS sumvolume1,
        (select round(sum(charge_volume),2) from view_order_businesstime where customer_id = c.id and
        business_time BETWEEN '${sduibidate}' AND '${eduibidate}' and transport_id=1) as sumvolumeTb1,
        <!--空运-->
        (select round(sum(charge_weight)/100,2) from view_order_businesstime where customer_id = c.id and
        business_time BETWEEN '${sdate}' AND '${edate}' and transport_id=3) AS sumweightV3,
        (select round(sum(charge_weight)/100,2) from view_order_businesstime where customer_id = c.id and
        business_time BETWEEN '${sduibidate}' AND '${eduibidate}' and transport_id=3) as sumweightTbV3,
        '4' as monthAvg,
        '5' as monthAvgTb,
        '6' as seaMonthAvg,
        '7' as seaMonthAvgTb,
        '8' as airMothAvg,
        '9' as airMothAvgTb,
        <!--重货 计算占比,不计算同步-->
        (select round(sum(vz),2) from view_order_businesstime where customer_id = c.id and
        business_time BETWEEN '${sdate}' AND '${edate}' and order_type=2) as weightSumV,
        <!--泡货 计算占比,不计算同步-->
        (select round(sum(vz),2) from view_order_businesstime where customer_id = c.id and
        business_time BETWEEN '${sdate}' AND '${edate}' and order_type=3) as phSumV,
        <!--控货 计算占比,不计算同步-->
        (select round(sum(vz),2) from view_order_businesstime where customer_id = c.id and
        business_time BETWEEN '${sdate}' AND '${edate}' and is_cargo_control=1) as khSumV,
        (CASE WHEN c.is_new = 1 THEN '是' ELSE '否' END) as isFirst,
        (if(c.is_new = 1,c.update_time,'')) as firstDate,
        <!--提货率 已提货箱数除总入库总箱数-->
        (select sum(charge_quantity) from view_order_businesstime where customer_id = c.id and
        business_time BETWEEN '${sdate}' AND '${edate}') as chargequantity,
        (select sum(pick_num) from view_order_businesstime where customer_id = c.id and
        business_time BETWEEN '${sdate}' AND '${edate}') as pickquantity,
        (CASE WHEN c.is_new = 1 THEN '新客户' ELSE '旧客户' END ) as cusYjType,
        (SELECT name_zh from ecw_country where deleted=0 and id=c.country) as country,
        c.type, c.create_time as createtime
        from ecw_customer c left join (
        SELECT d.NAME as deptName,u.dept_id,u.id as userid FROM
        system_user u LEFT JOIN system_dept d ON u.dept_id = d.id
        ) AS u_d ON c.customer_service = u_d.userid
        <include refid="WherePage"/>
        )
        select * from dataTJ
        <include refid="WherePage2"/>


        <if test="orderfield!=null and orderfield!=''
         and ordertype!=null and ordertype!=''">
            order by ${orderfield} ${ordertype}
        </if>

        limit #{start},#{size}
    </select>

    <sql id="WherePage2">
        <where>
            <if test="sdate5!=null and sdate5!=''">
                <!--首次成交时间-->
                <if test="edate5!=null and edate5!=''">
                    and firstDate BETWEEN #{sdate5} AND #{edate5}
                </if>
            </if>
            <if test="searchDataType1!=null and searchDataType1!=''">
                <if test="searchData1!=null and searchData1!=''">
                    <!--总值查询-->
                    <choose>
                        <when test="searchDataType1 =='1'.toString()">
                            <!--大于等于的写法-->
                            and allsumvolume <![CDATA[ >= ]]> #{searchData1}
                        </when>
                        <when test="searchDataType1 =='2'.toString()">
                            and allsumvolume= #{searchData1}
                        </when>
                        <when test="searchDataType1 =='3'.toString()">
                            <!--小于等于的写法-->
                            and allsumvolume <![CDATA[ <= ]]> #{searchData1}
                        </when>
                    </choose>
                </if>
            </if>

            <if test="searchDataType2!=null and searchDataType2!=''">
                <if test="searchData2!=null and searchData2!=''">
                    <!--海运拼柜-->
                    <choose>
                        <when test="searchDataType2 =='1'.toString()">
                            <!--大于等于的写法-->
                            and sumvolume1 <![CDATA[ >= ]]> #{searchData2}
                        </when>
                        <when test="searchDataType2 =='2'.toString()">
                            and sumvolume1 = #{searchData2}
                        </when>
                        <when test="searchDataType2 =='3'.toString()">
                            <!--小于等于的写法-->
                            and sumvolume1 <![CDATA[ <= ]]> #{searchData2}
                        </when>
                    </choose>
                </if>
            </if>
            <if test="searchDataType3!=null and searchDataType3!=''">
                <if test="searchData3!=null and searchData3!=''">
                    <!--专线空运-->
                    <choose>
                        <when test="searchDataType3 =='1'.toString()">
                            <!--大于等于的写法-->
                            and sumweightV3 <![CDATA[ >= ]]> #{searchData3}
                        </when>
                        <when test="searchDataType3 =='2'.toString()">
                            and sumweightV3 = #{searchData3}
                        </when>
                        <when test="searchDataType3 =='3'.toString()">
                            <!--小于等于的写法-->
                            and sumweightV3 <![CDATA[ <= ]]> #{searchData3}
                        </when>
                    </choose>
                </if>
            </if>
        </where>
    </sql>

    <sql id="WherePage">
        <where>
            and c.deleted = 0 and c.STATUS=3
            <if test="name!=null and name!=''">
                <choose>
                    <when test="searchtype =='in'.toString()">
                        and (name like concat('%',#{name},'%') or
                        number like concat('%',#{name},'%'))
                    </when>
                    <when test="searchtype =='notin'.toString()">
                        and (name not like concat('%',#{name},'%') or
                        number not like concat('%',#{name},'%'))
                    </when>
                    <when test="searchtype =='eq'.toString()">
                        and (name= #{name} or number=#{name})
                    </when>
                    <when test="searchtype =='noeq'.toString()">
                        and (name != #{name} or number != #{name})
                    </when>
                    <otherwise>
                        and (name= #{name} or number=#{name})
                    </otherwise>
                </choose>
            </if>
            <if test="first!=null and first!=''">
                <!--是否首次成交-->
                and c.is_new= #{first}
            </if>
            <if test="cusYjType!=null and cusYjType!=''">
                <!--业绩类型-->
                and c.is_new= #{cusYjType}
            </if>

            <!--客户来源-->
            <if test="sourcestr!=null and sourcestr!=''">
                and source= #{sourcestr}
            </if>

            <if test="sourcestrs!=null and sourcestrs!=''">
                and source in ${sourcestrs}
            </if>

            <!--客户角色-->
            <if test="customerrolestr!=null and customerrolestr!=''">
                and type =#{customerrolestr}
            </if>
            <if test="customerrolestrs!=null and customerrolestrs!=''">
                and type in ${customerrolestrs}
            </if>

            <!-- 添加国家多选功能-->
            <if test="countrystr!=null and countrystr!=''">
                and c.country= #{countrystr}
            </if>
            <if test="countrystrs!=null and countrystrs!=''">
                and c.country in ${countrystrs}
            </if>

            <if test="salesmanidstr!=null and salesmanidstr!=''">
                <!--有具体的客户经理,就不算掉入公海池客户-->
                and c.is_in_open_sea=0
                and c.customer_service= #{salesmanidstr}
            </if>

            <if test="salesmanidstrs!=null and salesmanidstrs!=''">
                <!--有具体的客户经理,就不算掉入公海池客户-->
                and c.is_in_open_sea=0
                and c.customer_service in ${salesmanidstrs}
            </if>

            <if test="deptidstr!=null and deptidstr!=''">
                and u_d.dept_id= #{deptidstr}
            </if>
            <if test="deptidstrs!=null and deptidstrs!=''">
                and u_d.dept_id in ${deptidstrs}
            </if>
            <if test="sdate4!=null and sdate4!=''">
                <!--客户创建时间-->
                <if test="edate4!=null and edate4!=''">
                    and c.create_time BETWEEN #{sdate4} AND #{edate4}
                </if>
            </if>
        </where>
    </sql>

    <select id="GetCount" resultType="java.lang.Long">
        <!--
         SELECT count(*)
         from ecw_customer c left join (
         SELECT d.NAME as deptName,u.dept_id,u.id as userid FROM
         system_user u LEFT JOIN system_dept d ON u.dept_id = d.id
         ) AS u_d ON c.customer_service = u_d.userid
         <include refid="WherePage"/>-->
        WITH dataTJ as(
        SELECT c.id,
        (if(c.is_new = 1,c.update_time,'')) as firstDate,
        (select round(sum(vz),2) from view_order_businesstime where customer_id = c.id and business_time BETWEEN
        '${sdate}' AND '${edate}') AS allsumvolume,
        (select round(sum(charge_volume),2) from view_order_businesstime where customer_id = c.id and
        business_time BETWEEN '${sdate}' AND '${edate}' and transport_id=1) AS sumvolume1,
        (select round(sum(charge_weight),2) from view_order_businesstime where customer_id = c.id and
        business_time BETWEEN '${sdate}' AND '${edate}' and transport_id=3) AS sumweightV3
        from ecw_customer c left join (
        SELECT d.NAME as deptName,u.dept_id,u.id as userid FROM
        system_user u LEFT JOIN system_dept d ON u.dept_id = d.id
        ) AS u_d ON c.customer_service = u_d.userid
        <include refid="WherePage"/>
        )
        select count(*) from dataTJ
        <include refid="WherePage2"/>

    </select>

    <select id="getList"
            parameterType="cn.iocoder.yudao.module.delivery.entity.CustomerAnalysisReq"
            resultType="cn.iocoder.yudao.module.delivery.entity.CustomerAnalysisResp">
        with dataTJ AS(
        SELECT number,name,c.id,(
        SELECT nickname from system_user where id=c.customer_service
        ) as salesman,c.customer_service as salesmanid,
        (SELECT label from system_dict_data
        where dict_type='customer_source' and value=c.source) as sourcename,
        c.source,(u_d.dept_id) as deptid,(u_d.deptName) as deptname,
        (select round(sum(vz),2) from view_order_businesstime where customer_id = c.id and business_time BETWEEN
        '${sdate}' AND '${edate}') AS allsumvolume,
        (select round(sum(vz),2) from view_order_businesstime where customer_id = c.id and business_time BETWEEN
        '${sduibidate}' AND '${eduibidate}') as allsumvolumeTb,
        <!--海运-->
        (select round(sum(charge_volume),2) from view_order_businesstime where customer_id = c.id and
        business_time BETWEEN '${sdate}' AND '${edate}' and transport_id=1) AS sumvolume1,
        (select round(sum(charge_volume),2) from view_order_businesstime where customer_id = c.id and
        business_time BETWEEN '${sduibidate}' AND '${eduibidate}' and transport_id=1) as sumvolumeTb1,
        <!--空运-->
        (select round(sum(charge_weight)/100,2) from view_order_businesstime where customer_id = c.id and
        business_time BETWEEN '${sdate}' AND '${edate}' and transport_id=3) AS sumweightV3,
        (select round(sum(charge_weight)/100,2) from view_order_businesstime where customer_id = c.id and
        business_time BETWEEN '${sduibidate}' AND '${eduibidate}' and transport_id=3) as sumweightTbV3,
        '4' as monthAvg,
        '5' as monthAvgTb,
        '6' as seaMonthAvg,
        '7' as seaMonthAvgTb,
        '8' as airMothAvg,
        '9' as airMothAvgTb,
        <!--重货 计算占比,不计算同步-->
        (select round(sum(vz),2) from view_order_businesstime where customer_id = c.id and
        business_time BETWEEN '${sdate}' AND '${edate}' and order_type=2) as weightSumV,
        <!--泡货 计算占比,不计算同步-->
        (select round(sum(vz),2) from view_order_businesstime where customer_id = c.id and
        business_time BETWEEN '${sdate}' AND '${edate}' and order_type=3) as phSumV,
        <!--控货 计算占比,不计算同步-->
        (select round(sum(vz),2) from view_order_businesstime where customer_id = c.id and
        business_time BETWEEN '${sdate}' AND '${edate}' and is_cargo_control=1) as khSumV,
        (CASE WHEN c.is_new = 1 THEN '是' ELSE '否' END) as isFirst,
        (if(c.is_new = 1,c.update_time,'')) as firstDate,
        <!--提货率 已提货箱数除总入库总箱数-->
        (select sum(charge_quantity) from view_order_businesstime where customer_id = c.id and
        business_time BETWEEN '${sdate}' AND '${edate}') as chargequantity,
        (select sum(pick_num) from view_order_businesstime where customer_id = c.id and
        business_time BETWEEN '${sdate}' AND '${edate}') as pickquantity,
        (CASE WHEN c.is_new = 1 THEN '新客户' ELSE '旧客户' END ) as cusYjType,
        (SELECT name_zh from ecw_country where deleted=0 and id=c.country) as country,
        c.type, c.create_time as createtime
        from ecw_customer c left join (
        SELECT d.NAME as deptName,u.dept_id,u.id as userid FROM
        system_user u LEFT JOIN system_dept d ON u.dept_id = d.id
        ) AS u_d ON c.customer_service = u_d.userid
        <include refid="WherePage"/>
        )
        select * from dataTJ
        <include refid="WherePage2"/>
        order by allsumvolume desc,number desc

    </select>

    <select id="getListPage_New"
            parameterType="cn.iocoder.yudao.module.delivery.entity.CustomerAnalysisReq"
            resultType="cn.iocoder.yudao.module.delivery.entity.CustomerAnalysisResp">
        select a.*,
        sum(voTb.vz) as allsumvolumeTb,
        sum(if(voTb.transport_id = 1, voTb.charge_volume, 0)) as sumvolumeTb1,
        sum(if(voTb.transport_id = 3, voTb.charge_weight, 0)) as sumweightTb3
        from (
        SELECT vc.number,
        vc.name,
        vc.id,
        vc.salesman,
        vc.salesmanid,
        vc.is_in_open_sea,
        vc.sourcename,
        vc.source,
        vc.deptid,
        vc.deptname,
        vc.isFirst,
        vc.firstDate,
        vc.cusYjType,
        vc.country,
        vc.countryID,
        vc.type,
        vc.createtime,
        sum(vo.vz) as allsumvolume,
        sum(if(vo.transport_id = 1, vo.charge_volume, 0)) as sumvolume1,
        sum(if(vo.transport_id = 3, vo.charge_weight, 0)) as sumweight3,
        sum(if(vo.order_type = 2, vo.vz, 0)) as weightSumV,
        sum(if(vo.is_cargo_control = 1, vo.vz, 0)) as khSumV,
        sum(if(vo.order_type = 3, vo.vz, 0)) as phSumV,
        sum(vo.charge_quantity) as charge_quantity,
        sum(vo.pick_num) as pick_num
        from view_customer_businesstime vc
        left join view_order_businesstime vo on vc.id = vo.customer_id
        where vo.business_time BETWEEN '${sdate}' and '${edate}'
        GROUP BY vc.id) as a
        left join view_order_businesstime voTb on a.id = voTb.customer_id
        and voTb.business_time BETWEEN '${sduibidate}' and '${eduibidate}'
        <include refid="WherePage_new"/>
        GROUP BY a.id
        order by a.allsumvolume desc
    </select>

    <sql id="WherePage_new">
        <where>
            <if test="isnew!=null and isnew!=''">
            </if>
            <if test="source!=null and source!=''">
                and a.source= #{source}
            </if>
            <if test="customerrole!=null and customerrole!=''">
                and a.type= #{customerrole}
            </if>
            <if test="country!=null and country!=''">
                and a.countryID= #{country}
            </if>
            <if test="salesmanid!=null and salesmanid!=''">
                <!--有具体的客户经理,就不算掉入公海池客户-->
                and a.is_in_open_sea=0
                and a.salesmanid= #{salesmanid}
            </if>
            <if test="deptid!=null and deptid!=''">
                and a.deptid= #{deptid}
            </if>
            <if test="searchDataType4!=null and searchDataType4!=''">
                <if test="sdate4!=null and sdate4!=''
                 and edate4!=null and edate4!=''">
                    <choose>
                        <when test="searchDataType4 =='1'">
                            and a.firstDate BETWEEN #{sdate4} AND #{edate4}
                        </when>
                        <when test="searchDataType4 =='0'">
                            and a.createtime BETWEEN #{sdate4} AND #{edate4}
                        </when>
                    </choose>
                </if>

            </if>

        </where>
    </sql>

</mapper>