<?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.CustomerAnalysisMapper2">
    <select id="getListPage"
            parameterType="cn.iocoder.yudao.module.delivery.entity.CustomerAnalysisReq"
            resultType="cn.iocoder.yudao.module.delivery.entity.CustomerAnalysisResp">
        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,
        1 AS allsumvolume,
        2 as allsumvolumeTb,
        3 AS sumvolume1,
        4 as sumvolumeTb1,
        5 AS sumweight3,
        6 as sumweightTb3,
        7 as monthAvg,
        8 as monthAvgTb,
        9 as seaMonthAvg,
        10 as seaMonthAvgTb,
        11 as airMothAvg,
        12 as airMothAvgTb,
        13 as weightSumV,
        14 as phSumV,
        15 as khSumV,
        (CASE WHEN c.is_new = 1 THEN '是' ELSE '否' END) as isFirst,
        (if(c.is_new = 1,c.update_time,''))as firstDate,
        16 as chargequantity,
        17 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.country as countryID, 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>

    <sql id="WherePage">
        <where>
            and c.deleted = 0
            <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="isnew!=null and isnew!=''">
                and is_new= #{isnew}
            </if>
            <if test="source!=null and source!=''">
                and source= #{source}
            </if>
            <if test="customerrole!=null and customerrole!=''">
                and type= #{customerrole}
            </if>
            <if test="country!=null and country!=''">
                and c.country= #{country}
            </if>
            <if test="salesmanid!=null and salesmanid!=''">
                <!--有具体的客户经理,就不算掉入公海池客户-->
                and c.is_in_open_sea=0
                and c.customer_service= #{salesmanid}
            </if>
            <if test="deptid!=null and deptid!=''">
                and u_d.dept_id= #{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 firstDate BETWEEN #{sdate4} AND #{edate4}
                        </when>
                        <when test="searchDataType4 =='0'">
                            and c.create_time BETWEEN #{sdate4} AND #{edate4}
                        </when>
                    </choose>
                </if>
            </if>
        </where>
    </sql>
    <select id="GetCount" resultType="java.lang.Long">
        SELECT count(1)
        from ecw_customer
        <include refid="WherePage"/>
    </select>
</mapper>