<?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.fs.crm.mapper.CrmCustomerMapper">

    <resultMap type="CrmCustomer" id="CrmCustomerResult">
        <result property="customerId"    column="customer_id"    />
        <result property="customerCode"    column="customer_code"    />
        <result property="customerName"    column="customer_name"    />
        <result property="mobile"    column="mobile"    />
        <result property="sex"    column="sex"    />
        <result property="weixin"    column="weixin"    />
        <result property="remark"    column="remark"    />
        <result property="userId"    column="user_id"    />
        <result property="createUserId"    column="create_user_id"    />
        <result property="receiveUserId"    column="receive_user_id"    />
        <result property="customerUserId"    column="customer_user_id"    />
        <result property="address"    column="address"    />
        <result property="cityIds"    column="city_ids"    />
        <result property="location"    column="location"    />
        <result property="detailAddress"    column="detail_address"    />
        <result property="lng"    column="lng"    />
        <result property="lat"    column="lat"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="status"    column="status"    />
        <result property="isReceive"    column="is_receive"    />
        <result property="deptId"    column="dept_id"    />
        <result property="isDel"    column="is_del"    />
        <result property="customerType"    column="customer_type"    />
        <result property="receiveTime"    column="receive_time"    />
        <result property="poolTime"    column="pool_time"    />
        <result property="companyId"    column="company_id"    />
        <result property="isLine"    column="is_line"    />
        <result property="source"    column="source"    />
        <result property="tags"    column="tags"    />
        <result property="extJson"    column="ext_json"    />
        <result property="visitStatus"    column="visit_status"    />
        <result property="registerDate"    column="register_date"    />
        <result property="registerLinkUrl"    column="register_link_url"    />
        <result property="registerDesc"    column="register_desc"    />
        <result property="registerSubmitTime"    column="register_submit_time"    />
        <result property="isPool"    column="is_pool"    />
        <result property="registerType"    column="register_type"    />
        <result property="payMoney"    column="pay_money"    />
        <result property="buyCount"    column="buy_count"    />
        <result property="sourceCode"    column="source_code"    />
        <result property="pushTime"    column="push_time"    />
        <result property="pushCode"    column="push_code"    />
        <result property="visitTime"    column="visit_time"    />
        <result property="trafficSource"   column="traffic_source"    />
        <result property="importType"    column="import_type"    />
        <result property="volume"    column="volume"    />
        <result property="thirdAccount"    column="third_account"    />
        <result property="clueId"    column="clue_id"    />
        <result property="customerLevel"    column="customer_level"    />
        <result property="isTop"    column="is_top"    />
    </resultMap>

    <sql id="selectCrmCustomerVo">
        select customer_id, customer_code, customer_name, mobile, sex, weixin, remark, user_id, create_user_id, receive_user_id, customer_user_id, address,city_ids, location, detail_address, lng, lat, create_time, update_time, status, is_receive, dept_id, is_del, customer_type, receive_time, pool_time, company_id, is_line, source, tags,ext_json,visit_status,register_date,register_link_url,register_desc,register_submit_time,is_pool,register_type,pay_money,buy_count,source_code,push_time,push_code,visit_time,traffic_source,import_type,third_account,clue_id,customer_level,is_top from crm_customer
    </sql>

    <select id="selectCrmCustomerList" parameterType="CrmCustomer" resultMap="CrmCustomerResult">
        <include refid="selectCrmCustomerVo"/>
        <where>
            <if test="customerCode != null  and customerCode != ''"> and customer_code = #{customerCode}</if>
            <if test="customerName != null  and customerName != ''"> and customer_name like concat('%', #{customerName}, '%')</if>
            <if test="mobile != null  and mobile != ''"> and mobile = #{mobile}</if>
            <if test="sex != null "> and sex = #{sex}</if>
            <if test="weixin != null  and weixin != ''"> and weixin = #{weixin}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="createUserId != null "> and create_user_id = #{createUserId}</if>
            <if test="receiveUserId != null "> and receive_user_id = #{receiveUserId}</if>
            <if test="customerUserId != null "> and customer_user_id = #{customerUserId}</if>
            <if test="address != null  and address != ''"> and address = #{address}</if>
            <if test="location != null  and location != ''"> and location = #{location}</if>
            <if test="detailAddress != null  and detailAddress != ''"> and detail_address = #{detailAddress}</if>
            <if test="lng != null  and lng != ''"> and lng = #{lng}</if>
            <if test="lat != null  and lat != ''"> and lat = #{lat}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="isReceive != null "> and is_receive = #{isReceive}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="isDel != null "> and is_del = #{isDel}</if>
            <if test="customerType != null "> and customer_type = #{customerType}</if>
            <if test="receiveTime != null "> and receive_time = #{receiveTime}</if>
            <if test="poolTime != null "> and pool_time = #{poolTime}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="isLine != null "> and is_line = #{isLine}</if>
            <if test="source != null "> and source = #{source}</if>
            <if test="tags != null  and tags != ''"> and tags = #{tags}</if>
            <if test="isPool != null "> and is_pool = #{isPool}</if>
            <if test="trafficSource != null "> and traffic_source = #{trafficSource}</if>
            <if test="importType != null "> and import_type = #{importType}</if>
            <if test="thirdAccount != null "> and third_account = #{thirdAccount}</if>
            <if test="clueId != null "> and clue_id = #{clueId}</if>
            <if test="customerLevel != null "> and customer_level = #{customerLevel}</if>
            <if test="isTop != null "> and is_top = #{isTop}</if>
        </where>
        order by customer_id desc
    </select>

    <select id="selectCrmCustomerById" parameterType="Long" resultMap="CrmCustomerResult">
        <include refid="selectCrmCustomerVo"/>
        where customer_id = #{customerId}
    </select>

    <select id="selectCrmCustomerByMobile" parameterType="CrmCustomer" resultMap="CrmCustomerResult">
        <include refid="selectCrmCustomerVo"/>
        where is_line = 1 and mobile = #{mobile} and customer_id != #{customerId} and company_id = #{companyId}
    </select>

    <insert id="insertCrmCustomer" parameterType="CrmCustomer" useGeneratedKeys="true" keyProperty="customerId">
        insert into crm_customer
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="customerCode != null">customer_code,</if>
            <if test="customerName != null">customer_name,</if>
            <if test="mobile != null">mobile,</if>
            <if test="sex != null">sex,</if>
            <if test="weixin != null">weixin,</if>
            <if test="remark != null">remark,</if>
            <if test="userId != null">user_id,</if>
            <if test="createUserId != null">create_user_id,</if>
            <if test="receiveUserId != null">receive_user_id,</if>
            <if test="customerUserId != null">customer_user_id,</if>
            <if test="address != null">address,</if>
            <if test="cityIds != null">city_ids,</if>
            <if test="location != null">location,</if>
            <if test="detailAddress != null">detail_address,</if>
            <if test="lng != null">lng,</if>
            <if test="lat != null">lat,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="status != null">status,</if>
            <if test="isReceive != null">is_receive,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="isDel != null">is_del,</if>
            <if test="customerType != null">customer_type,</if>
            <if test="receiveTime != null">receive_time,</if>
            <if test="poolTime != null">pool_time,</if>
            <if test="companyId != null">company_id,</if>
            <if test="isLine != null">is_line,</if>
            <if test="source != null">source,</if>
            <if test="tags != null">tags,</if>
            <if test="extJson != null">ext_json,</if>
            <if test="visitStatus != null">visit_status,</if>
            <if test="registerDate != null">register_date,</if>
            <if test="registerLinkUrl != null">register_link_url,</if>
            <if test="registerDesc != null">register_desc,</if>
            <if test="registerSubmitTime != null">register_submit_time,</if>
            <if test="isPool != null">is_pool,</if>
            <if test="registerType != null">register_type,</if>
            <if test="payMoney != null">pay_money,</if>
            <if test="buyCount != null">buy_count,</if>
            <if test="sourceCode != null">source_code,</if>
            <if test="pushTime != null">push_time,</if>
            <if test="pushCode != null">push_code,</if>
            <if test="visitTime != null">visit_time,</if>
            <if test="trafficSource != null">traffic_source,</if>
            <if test="importType != null">import_type,</if>
            <if test="thirdAccount != null">third_account,</if>
            <if test="clueId != null">clue_id,</if>
            <if test="customerLevel != null">customer_level,</if>
            <if test="isTop != null">is_top,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="customerCode != null">#{customerCode},</if>
            <if test="customerName != null">#{customerName},</if>
            <if test="mobile != null">#{mobile},</if>
            <if test="sex != null">#{sex},</if>
            <if test="weixin != null">#{weixin},</if>
            <if test="remark != null">#{remark},</if>
            <if test="userId != null">#{userId},</if>
            <if test="createUserId != null">#{createUserId},</if>
            <if test="receiveUserId != null">#{receiveUserId},</if>
            <if test="customerUserId != null">#{customerUserId},</if>
            <if test="address != null">#{address},</if>
            <if test="cityIds != null">#{cityIds},</if>
            <if test="location != null">#{location},</if>
            <if test="detailAddress != null">#{detailAddress},</if>
            <if test="lng != null">#{lng},</if>
            <if test="lat != null">#{lat},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="status != null">#{status},</if>
            <if test="isReceive != null">#{isReceive},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="customerType != null">#{customerType},</if>
            <if test="receiveTime != null">#{receiveTime},</if>
            <if test="poolTime != null">#{poolTime},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="isLine != null">#{isLine},</if>
            <if test="source != null">#{source},</if>
            <if test="tags != null">#{tags},</if>
            <if test="extJson != null">#{extJson},</if>
            <if test="visitStatus != null">#{visitStatus},</if>
            <if test="registerDate != null">#{registerDate},</if>
            <if test="registerLinkUrl != null">#{registerLinkUrl},</if>
            <if test="registerDesc != null">#{registerDesc},</if>
            <if test="registerSubmitTime != null">#{registerSubmitTime},</if>
            <if test="isPool != null">#{isPool},</if>
            <if test="registerType != null">#{registerType},</if>
            <if test="payMoney != null">#{payMoney},</if>
            <if test="buyCount != null">#{buyCount},</if>
            <if test="sourceCode != null">#{sourceCode},</if>
            <if test="pushTime != null">#{pushTime},</if>
            <if test="pushCode != null">#{pushCode},</if>
            <if test="visitTime != null">#{visitTime},</if>
            <if test="trafficSource != null">#{trafficSource},</if>
            <if test="importType != null">#{importType},</if>
            <if test="thirdAccount != null">#{thirdAccount},</if>
            <if test="clueId != null">#{clueId},</if>
            <if test="customerLevel != null">#{customerLevel},</if>
            <if test="isTop != null">#{isTop},</if>
         </trim>
    </insert>

    <update id="updateCrmCustomer" parameterType="CrmCustomer">
        update crm_customer
        <trim prefix="SET" suffixOverrides=",">
            <if test="customerCode != null">customer_code = #{customerCode},</if>
            <if test="customerName != null">customer_name = #{customerName},</if>
            <if test="mobile != null">mobile = #{mobile},</if>
            <if test="sex != null">sex = #{sex},</if>
            <if test="weixin != null">weixin = #{weixin},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="createUserId != null">create_user_id = #{createUserId},</if>
            <if test="receiveUserId != null">receive_user_id = #{receiveUserId},</if>
            <if test="customerUserId != null">customer_user_id = #{customerUserId},</if>
            <if test="address != null">address = #{address},</if>
            <if test="cityIds != null">city_ids = #{cityIds},</if>
            <if test="location != null">location = #{location},</if>
            <if test="detailAddress != null">detail_address = #{detailAddress},</if>
            <if test="lng != null">lng = #{lng},</if>
            <if test="lat != null">lat = #{lat},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="isReceive != null">is_receive = #{isReceive},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="customerType != null">customer_type = #{customerType},</if>
            <if test="receiveTime != null">receive_time = #{receiveTime},</if>
            <if test="poolTime != null">pool_time = #{poolTime},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="isLine != null">is_line = #{isLine},</if>
            <if test="source != null">source = #{source},</if>
            <if test="tags != null">tags = #{tags},</if>
            <if test="extJson != null">ext_json = #{extJson},</if>
            <if test="visitStatus != null">visit_status = #{visitStatus},</if>
            <if test="registerDate != null">register_date = #{registerDate},</if>
            <if test="registerLinkUrl != null">register_link_url = #{registerLinkUrl},</if>
            <if test="registerDesc != null">register_desc = #{registerDesc},</if>
            <if test="registerSubmitTime != null">register_submit_time = #{registerSubmitTime},</if>
            <if test="isPool != null">is_pool = #{isPool},</if>
            <if test="registerType != null">register_type = #{registerType},</if>
            <if test="payMoney != null">pay_money = #{payMoney},</if>
            <if test="buyCount != null">buy_count = #{buyCount},</if>
            <if test="sourceCode != null">source_code = #{sourceCode},</if>
            <if test="pushTime != null">push_time = #{pushTime},</if>
            <if test="pushCode != null">push_code = #{pushCode},</if>
            <if test="visitTime != null">visit_time = #{visitTime},</if>
            <if test="trafficSource != null">traffic_source = #{trafficSource},</if>
            <if test="importType != null">import_type = #{importType},</if>
            <if test="thirdAccount != null">third_account = #{thirdAccount},</if>
            <if test="clueId != null">clue_id = #{clueId},</if>
            <if test="customerLevel != null">customer_level = #{customerLevel},</if>
            <if test="isTop != null">is_top = #{isTop},</if>
        </trim>
        where customer_id = #{customerId}
    </update>

    <delete id="deleteCrmCustomerById" parameterType="Long">
        delete from crm_customer where customer_id = #{customerId}
    </delete>

    <delete id="deleteCrmCustomerByIds" parameterType="String">
        delete from crm_customer where customer_id in
        <foreach item="customerId" collection="array" open="(" separator="," close=")">
            #{customerId}
        </foreach>
    </delete>

    <select id="selectCrmCustomerCounts" resultType="com.alibaba.fastjson.JSONObject">
        SELECT  count( IF ( t.is_pool =0, 1, NULL )) AS receiveCount,  t.type,  count( IF ( t.is_pool = 1, 1, NULL )) AS poolCount
        FROM
        (
        SELECT DATE_FORMAT( cu.create_time, #{sqlDateFormat} ) AS type,  cu.is_pool
        FROM
        crm_customer_user AS cu
        WHERE
        (DATE_FORMAT(cu.create_time,#{sqlDateFormat}) between #{beginTime} and #{finalTime})
        AND cu.company_user_id IN
        <foreach item="item" collection="userIds" open="(" separator="," close=")">
            #{item}
        </foreach>
        ) t
        GROUP BY t.type
    </select>
    <select id="selectCrmMyCustomerListQuery" resultType="com.fs.crm.vo.CrmMyCustomerListQueryVO">
        select cu.*,c.create_time as customer_create_time,c.visit_status,c.remark,c.register_desc,c.register_submit_time,
               c.customer_code,c.customer_name,c.mobile,c.sex,c.weixin,c.address,c.is_receive,c.customer_type,c.source,
               c.tags,c.receive_time,c.customer_level
        from crm_customer_user cu
            inner join crm_customer c on c.customer_user_id=cu.customer_user_id
        where cu.is_pool=0  and c.is_del = 0
        <if test="maps.companyId != null">
            and cu.company_id =#{maps.companyId}
        </if>
        <if test="maps.customerLevel != null">
            and c.customer_level =#{maps.customerLevel}
        </if>
        <if test="maps.companyUserId != null">
            and cu.company_user_id =#{maps.companyUserId}
        </if>
        <if test="maps.address != null and  maps.address != ''">
            and c.address like CONCAT('%',#{maps.address},'%')
        </if>
        <if test="maps.customerCode != null and  maps.customerCode != ''">
            and c.customer_code like CONCAT('%',#{maps.customerCode},'%')
        </if>
        <if test="maps.customerName != null and  maps.customerName != ''">
            and c.customer_name like CONCAT('%',#{maps.customerName},'%')
        </if>
        <if test="maps.mobile != null and  maps.mobile != ''">
            and c.mobile like CONCAT('%',#{maps.mobile},'%')
        </if>
        <if test="maps.status != null and  maps.status != ''">
            and c.visit_status IN
            <foreach collection="maps.status.split(',')" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="maps.isHisOrder != null and  maps.isHisOrder == 1">
            and (select ifnull(count(1),0) from crm_customer_his_order h where h.customer_id=c.customer_id )  &gt; 0
        </if>
        <if test="maps.isHisOrder != null and  maps.isHisOrder == 0">
            and (select ifnull(count(1),0) from crm_customer_his_order h where h.customer_id=c.customer_id )  = 0
        </if>
        <if test="maps.customerType != null">
            and c.customer_type IN
            <foreach collection="maps.customerType.split(',')" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="maps.source != null and  maps.source != ''">
            and c.source IN
            <foreach collection="maps.source.split(',')" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="maps.tags != null and  maps.tags != ''">
            <foreach collection="maps.tags.split(',')" item="tag" open="(" separator="OR" close=")">
                find_in_set(#{tag},c.tags)
            </foreach>
        </if>
        <if test="maps.customerCreateTime != null">
            AND date_format(c.create_time,'%y%m%d') &gt;= date_format(#{maps.customerCreateTime[0]},'%y%m%d')
            AND date_format(c.create_time,'%y%m%d') &lt;= date_format(#{maps.customerCreateTime[1]},'%y%m%d')
        </if>
        <if test="maps.beginTime != null and maps.beginTime != ''">
            and date_format(c.receive_time,'%y%m%d') &gt;= date_format(#{maps.beginTime},'%y%m%d')
        </if>
        <if test="maps.endTime != null and maps.endTime != ''">
            and date_format(c.receive_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d')
        </if>
        order by cu.customer_user_id desc
    </select>
    <select id="selectCrmMyAssistListQuery" resultType="com.fs.crm.vo.CrmMyCustomerListQueryVO">
        select cu.*,c.create_time as customer_create_time,c.visit_status,c.remark,c.register_desc,c.register_submit_time,
        c.customer_code,c.customer_name,c.mobile,c.sex,c.weixin,c.address,c.is_receive,c.customer_type,c.source,
        c.tags,c.receive_time, c.customer_level
        from crm_customer_assist ca
        LEFT JOIN  crm_customer c on c.customer_id=ca.customer_id
        LEFT JOIN crm_customer_user cu on c.customer_user_id=cu.customer_user_id
        where c.is_pool=0  and c.is_del = 0
        <if test="maps.companyId != null">
            and ca.company_id =#{maps.companyId}
        </if>
        <if test="maps.companyUserId != null">
            and ca.company_user_id =#{maps.companyUserId}
        </if>
        <if test="maps.customerLevel != null">
            and c.customer_level =#{maps.customerLevel}
        </if>
        <if test="maps.address != null and  maps.address != ''">
            and c.address like CONCAT('%',#{maps.address},'%')
        </if>
        <if test="maps.customerCode != null and  maps.customerCode != ''">
            and c.customer_code like CONCAT('%',#{maps.customerCode},'%')
        </if>
        <if test="maps.customerName != null and  maps.customerName != ''">
            and c.customer_name like CONCAT('%',#{maps.customerName},'%')
        </if>
        <if test="maps.mobile != null and  maps.mobile != ''">
            and c.mobile like CONCAT('%',#{maps.mobile},'%')
        </if>
        <if test="maps.status != null and  maps.status != ''">
            and c.visit_status IN
            <foreach collection="maps.status.split(',')" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="maps.isHisOrder != null and  maps.isHisOrder == 1">
            and (select ifnull(count(1),0) from crm_customer_his_order h where h.customer_id=c.customer_id )  &gt; 0
        </if>
        <if test="maps.isHisOrder != null and  maps.isHisOrder == 0">
            and (select ifnull(count(1),0) from crm_customer_his_order h where h.customer_id=c.customer_id )  = 0
        </if>
        <if test="maps.customerType != null">
            and c.customer_type IN
            <foreach collection="maps.customerType.split(',')" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="maps.source != null and  maps.source != ''">
            and c.source IN
            <foreach collection="maps.source.split(',')" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="maps.tags != null and  maps.tags != ''">
            <foreach collection="maps.tags.split(',')" item="tag" open="(" separator="OR" close=")">
                find_in_set(#{tag},c.tags)
            </foreach>
        </if>
        <if test="maps.customerCreateTime != null">
            AND date_format(c.create_time,'%y%m%d') &gt;= date_format(#{maps.customerCreateTime[0]},'%y%m%d')
            AND date_format(c.create_time,'%y%m%d') &lt;= date_format(#{maps.customerCreateTime[1]},'%y%m%d')
        </if>
        <if test="maps.beginTime != null and maps.beginTime != ''">
            and date_format(c.receive_time,'%y%m%d') &gt;= date_format(#{maps.beginTime},'%y%m%d')
        </if>
        <if test="maps.endTime != null and maps.endTime != ''">
            and date_format(c.receive_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d')
        </if>
        order by cu.customer_user_id desc
    </select>
    <select id="selectCustomerIdByCompanyUserId" resultType="java.lang.Long">
        SELECT customer_id FROM crm_customer_user where company_user_id = #{companyUserId}
    </select>
</mapper>
