EntmicroCustomerFollowMapper.xml 14.3 KB
<?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.com.glsx.entmicro.mapper.EntmicroCustomerFollowMapper">
    <resultMap id="BaseResultMap" type="cn.com.glsx.entmicro.entity.EntmicroCustomerFollow">
        <!--
          WARNING - @mbg.generated
        -->
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="customer_id" jdbcType="INTEGER" property="customerId"/>
        <result column="staff_id" jdbcType="INTEGER" property="staffId"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
        <result column="description" jdbcType="VARCHAR" property="description"/>
        <result column="remark_mobiles" jdbcType="VARCHAR" property="remarkMobiles"/>
        <result column="add_way" jdbcType="TINYINT" property="addWay"/>
        <result column="oper_userid" jdbcType="VARCHAR" property="operUserid"/>
        <result column="created_by" jdbcType="INTEGER" property="createdBy"/>
        <result column="created_time" jdbcType="TIMESTAMP" property="createdTime"/>
        <result column="updated_by" jdbcType="INTEGER" property="updatedBy"/>
        <result column="updated_time" jdbcType="TIMESTAMP" property="updatedTime"/>
        <result column="del_flag" jdbcType="TINYINT" property="delFlag"/>
        <result column="externalUserId" jdbcType="VARCHAR" property="externalUserId"/>
        <result column="user_id" jdbcType="VARCHAR" property="userid"/>
    </resultMap>

    <resultMap id="CustomerRelationDataPushModelMap"
               type="cn.com.glsx.entmicro.model.customer.CustomerRelationDataPushModel">
        <result column="merchantId" jdbcType="INTEGER" property="merchantId"/>
        <result column="addWay" jdbcType="INTEGER" property="addWay"/>
        <result column="customerPhone" jdbcType="VARCHAR" property="customerPhone"/>
        <result column="userId" jdbcType="VARCHAR" property="userId"/>
        <result column="addWechatStatus" jdbcType="INTEGER" property="addWechatStatus"/>
        <result column="joinGroupStatus" jdbcType="INTEGER" property="joinGroupStatus"/>
    </resultMap>

    <sql id="Base_Column_List">
        <!--
          WARNING - @mbg.generated
        -->
        id, customer_id, staff_id, remark, description, remark_mobiles, add_way,oper_userid, created_by,
        created_time, updated_by, updated_time, del_flag
    </sql>

    <update id="deleteRelation">
        update entmicro_customer_follow scf2
        set scf2.del_flag = -1
        where scf2.id=#{id}
    </update>

    <update id="deleteCustomer">
        update entmicro_customer_follow
        set del_flag = -1
        where `customer_id` = #{id}
    </update>
    <update id="updatelist">
        <foreach collection="list" item="item" index="index" open="" close="" separator=";">
            update entmicro_customer_follow
            <set>
                updated_time=NOW(),
                remark=#{item.remark},
                description=#{item.description},
                <if test="item.remarkMobiles != null and item.remarkMobiles != ''">
                    remark_mobiles=#{item.remarkMobiles}
                </if>
            </set>
            where
            customer_id = #{item.customerId}
            and
            staff_id = #{item.staffId}
        </foreach>
    </update>
    <select id="findFollowByExternalUserId" resultType="cn.com.glsx.entmicro.entity.EntmicroCustomerFollow">
        select scf.id,
        scf.customer_id as customerId,
        es.user_id as userid
        from entmicro_customer_follow scf
        left join scrm_customer sc on scf.customer_id = sc.id
        left join entmicro_staff es on scf.staff_id = es.id and es.del_flag = 0
        where sc.user_id = #{externalUserId}
        and scf.del_flag = 0
        GROUP BY scf.id
    </select>

    <select id="findAllFollowByExternalUserId" resultType="cn.com.glsx.entmicro.entity.EntmicroCustomerFollow">
        select scf.id,
        scf.customer_id as customerId,
        scf.staff_id as staffId,
        es.user_id as userid
        from entmicro_customer_follow scf
        left join scrm_customer sc on scf.customer_id = sc.id
        left join entmicro_staff es on scf.staff_id = es.id
        where sc.user_id = #{externalUserId}
        group by scf.id
    </select>

    <select id="findRelationByStaffId" resultMap="BaseResultMap">
        select customer_id,
        staff_id
        from entmicro_customer_follow
        where del_flag = 0
        and customer_id = #{customerId}
        and staff_id in
        <foreach collection="staffId" open="(" close=")" item="staffId" separator=",">
            #{staffId}
        </foreach>
    </select>

    <select id="findRelationById" resultType="cn.com.glsx.entmicro.model.response.CustomerFollowStaffResponse">
        SELECT t1.id as id,
        t2.customer_name as customerName,
        t3.user_id as staffUserId,
        t2.user_id as customerUserId,
        t3.staff_name as staffName,
        t4.relevance_merchant AS relevanceMerchant
        FROM entmicro_customer_follow t1
        LEFT JOIN `scrm_customer` t2 ON t1.customer_id = t2.id
        LEFT JOIN `entmicro_staff` t3 ON t1.staff_id = t3.id
        LEFT JOIN `entmicro_staff_depart_relation` t4 ON t3.id = t4.staff_id
        WHERE t1.id = #{id}
        GROUP BY t1.id
    </select>

    <select id="findRelationByUserId" resultType="cn.com.glsx.entmicro.model.response.CustomerFollowStaffResponse">
        SELECT t1.id as id,
        t2.customer_name as customerName,
        t2.user_id as customerUserId,
        t3.user_id as staffUserId,
        t3.staff_name as staffName,
        t1.created_time as createdTime
        FROM entmicro_customer_follow t1
        LEFT JOIN `scrm_customer` t2 ON t1.customer_id = t2.id
        LEFT JOIN `entmicro_staff` t3 ON t1.staff_id = t3.id
        WHERE t2.del_flag = 0 and t1.del_flag = 0 and t3.del_flag = 0
        and (t2.user_id = #{id} or t2.one_id = #{id})
        order by t1.created_time desc
    </select>

    <update id="updatePhone">
        update entmicro_customer_follow
        set remark_mobiles = #{remarkMobiles},
        updated_time = NOW()
        where id = #{id}
    </update>

    <select id="findByRelationId" resultType="cn.com.glsx.entmicro.model.response.CustomerInfo">
        SELECT t2.one_id as oneId,
        t2.customer_name as customerName
        FROM `entmicro_customer_follow` t1
        LEFT JOIN `scrm_customer` t2 ON t1.customer_id = t2.id
        WHERE t1.id = #{id}
    </select>

    <select id="findByStaffId" resultMap="BaseResultMap">
        SELECT customer_id,
        staff_id
        FROM `entmicro_customer_follow`
        WHERE staff_id = #{staffId}
        AND del_flag = 0
    </select>


    <update id="updateBatchInfo">
        <foreach collection="customer" separator=";" item="item">
            update entmicro_customer_follow
            set remark_mobiles = #{item.remarkPhone},
            updated_time = NOW()
            where customer_id = #{item.id}
            and del_flag = 0
            and (remark_mobiles is null
            OR remark_mobiles = "")
        </foreach>
    </update>

    <select id="findBatchRelationInfo" resultMap="BaseResultMap">
        select scf.id,
        scf.customer_id,
        scf.staff_id,
        scf.remark_mobiles,
        es.user_id,
        sc.user_id as externalUserId
        from entmicro_customer_follow scf
        left join scrm_customer sc on scf.customer_id = sc.id
        left join entmicro_staff es on scf.staff_id = es.id
        where sc.user_id in
        <foreach collection="userIdList" open="(" close=")" item="item" separator=",">
            #{item}
        </foreach>
        and scf.del_flag = 0
        group by scf.id
    </select>

    <select id="mapByState" resultMap="BaseResultMap">
        SELECT state,
        id,
        created_time
        FROM `entmicro_customer_follow`
        WHERE state IN
        <foreach collection="stateList" item="state" open="(" separator="," close=")">
            #{state}
        </foreach>
        <if test="date != null">
            AND TO_DAYS(created_time) = TO_DAYS(#{date})
        </if>
        <if test="startTime != null">
            AND TO_DAYS(created_time) <![CDATA[>=]]> TO_DAYS(#{startTime})
        </if>
        <if test="endTime != null">
            AND TO_DAYS(created_time)  <![CDATA[<=]]> TO_DAYS(#{endTime})
        </if>
    </select>

    <select id="findCustomerRelationByUserIdOrPhone" resultMap="CustomerRelationDataPushModelMap">
        select
        #{param.addWay} addWay,
        t.userId,
        t.customerPhone,
        (case when t.isJoin = 1 and esdrId is not null and t.ecfFlag = 0 then 2
        when t.isJoin = 1 and esdrId is not null and ecfFlag = -1 then 3 else 1 end) addWechatStatus,
        (case t.scbrFlag when 0 then 2 when -1 then 3 else 1 end) joinGroupStatus,
        t.merchantId
        from (
        select
        sc.id id,
        sc.user_id userId,
        scm.customer_phone customerPhone,
        scm.status status,
        scm.is_join isJoin,
        t.scbrFlag scbrFlag,
        scm.merchant_id merchantId,
        ecf.del_flag ecfFlag,
        esdr.id esdrId
        from scrm_customer_merge scm
        left join scrm_customer sc on sc.one_id = scm.one_id and scm.del_flag = 0
        left join entmicro_customer_follow ecf on sc.id = ecf.customer_id
        left join entmicro_staff es on ecf.staff_id = es.id and es.del_flag = 0
        <if test="param.staffUserIdList != null and param.staffUserIdList.size > 0">
            and es.user_id in
            <foreach collection="param.staffUserIdList" item="staffUserId" open="(" separator="," close=")">
                #{staffUserId}
            </foreach>
        </if>
        left join entmicro_staff_depart_relation esdr on es.id = esdr.staff_id and esdr.del_flag = 0
        <if test="merchantIds != null and merchantIds.size > 0">
            and esdr.relevance_merchant in
            <foreach collection="merchantIds" item="merchantId" open="(" separator="," close=")">
                #{merchantId}
            </foreach>
        </if>
        left join scrm_customer_base_relation scbr on sc.id = scbr.customer_id
        left join (
        select scbr.customer_id customer_id,scbr.del_flag scbrFlag from scrm_customer_base_relation scbr
        left join scrm_customer_base scb on scbr.customer_base_id = scb.id and scb.del_flag = 0
        <where>
            <if test="param.chatIdList != null and param.chatIdList.size > 0">
                and scb.chat_id in
                <foreach collection="param.chatIdList" item="chatId" open="(" separator="," close=")">
                    #{chatId}
                </foreach>
            </if>
        </where>
        )t on sc.id = t.customer_id
        where scm.merchant_id in
        <foreach collection="merchantIds" item="merchantId" open="(" separator="," close=")">
            #{merchantId}
        </foreach>
        <choose>
            <when test="param.phoneList != null and param.phoneList.size > 0 and param.userIdList != null and param.userIdList.size > 0">
                and (sc.customer_phone in
                <foreach collection="param.phoneList" item="phone" open="(" separator="," close=")">
                    #{phone}
                </foreach>
                or sc.user_id in
                <foreach collection="param.userIdList" item="userId" open="(" separator="," close=")">
                    #{userId}
                </foreach>
                )
            </when>
            <when test="param.phoneList!= null and param.phoneList.size > 0">
                and sc.customer_phone in
                <foreach collection="param.phoneList" item="phone" open="(" separator="," close=")">
                    #{phone}
                </foreach>
            </when>
            <when test="param.userIdList!= null and param.userIdList.size > 0">
                and sc.user_id in
                <foreach collection="param.userIdList" item="userId" open="(" separator="," close=")">
                    #{userId}
                </foreach>
            </when>
        </choose>
        <!--        <if test="param.eventChatId != null">-->
        <!--            AND scb.chat_id = #{param.eventChatId}-->
        <!--        </if>-->
        having 1
        order by t.scbrFlag desc,esdr.id desc,ecf.del_flag desc
        )t group by t.id,t.merchantId
    </select>

    <select id="findCustomerStaffFollow" resultMap="BaseResultMap">
        select ecf.*
        from scrm_customer sc
        left join entmicro_customer_follow ecf on sc.id = ecf.customer_id and ecf.del_flag = 0
        left join entmicro_staff es on ecf.staff_id = es.id and es.del_flag = 0
        left join entmicro_staff_depart_relation esdr on es.id = esdr.staff_id and esdr.del_flag = 0
        left join scrm_customer_merge scm on sc.one_id = scm.one_id and scm.del_flag = 0
        where sc.del_flag = 0 and scm.customer_phone = #{customerPhone}
        and esdr.relevance_merchant in
        <foreach collection="merchantIdList" item="merchantId" open="(" separator="," close=")">
            #{merchantId}
        </foreach>
        <if test="staffUserIdList != null and staffUserIdList.size() >0">
            and es.user_id in
            <foreach collection="staffUserIdList" item="userId" open="(" separator="," close=")">
                #{userId}
            </foreach>
        </if>
    </select>
    <select id="selectCustomerFollowRemark" resultType="java.util.Map">
        select scm.customer_phone,ecf.description remark,101 as add_way,esdr.relevance_merchant merchant_id,
        sc.union_id,sc.user_id
        from scrm_customer sc
        left join scrm_customer_merge scm on sc.one_id = scm.one_id and scm.del_flag = 0
        left join entmicro_customer_follow ecf on sc.id = ecf.customer_id and ecf.del_flag = 0
        left join entmicro_staff es on ecf.staff_id = es.id and es.del_flag = 0
        left join entmicro_staff_depart_relation esdr on es.id = esdr.staff_id and esdr.del_flag = 0 and ecf.description is not null
        where sc.del_flag = 0 and ecf.description != ''
        <if test="merchantIdList != null and merchantIdList.size() >0">
            and esdr.relevance_merchant in
            <foreach collection="merchantIdList" item="merchantId" open="(" separator="," close=")">
                #{merchantId}
            </foreach>
        </if>
        <if test="updateTime != null">
            AND TO_DAYS(ecf.updated_time) <![CDATA[>=]]> TO_DAYS(#{updateTime})
        </if>
    </select>
</mapper>