WeCustomerMapper.xml 9.04 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="com.linkwechat.wecom.mapper.WeCustomerMapper">

    <select id="findWeCustomerList" resultType="com.linkwechat.wecom.domain.WeCustomerList">
        SELECT
            wc.avatar,
            wc.`name` as customerName,
            wc.customer_type,
            if(wt.tag_type=1,GROUP_CONCAT(wt.`tag_id`),null ) as tagIds,
            if(wt.tag_type=1,GROUP_CONCAT(wt.`name`),null ) as tagNames,
            if(wt.tag_type=3,GROUP_CONCAT(wt.`tag_id`),null ) as personTagIds,
            if(wt.tag_type=3,GROUP_CONCAT(wt.`name`),null ) as personTagNames,
            wu.user_name,
            wc.external_userid,
            wc.first_user_id,
            wc.track_state,
            wc.add_method,
            wc.first_add_time,
            wc.birthday,
            wc.gender,
           (SELECT GROUP_CONCAT(wd.`name`) FROM we_department wd WHERE wd.id in (wu.department)) as departmentName
        FROM
        we_customer wc
        LEFT JOIN we_user wu ON wc.first_user_id=wu.user_id
        LEFT JOIN we_flower_customer_tag_rel wfctr ON wfctr.del_flag=0 AND wfctr.external_userid=wc.external_userid AND  wfctr.user_id=wc.first_user_id
        LEFT JOIN we_tag wt ON wt.tag_id=wfctr.tag_id
        <where>
            <if test="weCustomerList.gender !=null">
                AND wc.gender=#{weCustomerList.gender}
            </if>

            <if test="weCustomerList.externalUserid !=null">
                AND wc.external_userid=#{weCustomerList.externalUserid}
            </if>

            <if test="weCustomerList.departmentIds != null and weCustomerList.departmentIds !=''">
                <if test="weCustomerList.departmentIds.indexOf(',') != -1">
                    OR wu.department in
                    <foreach item="item" index="index" collection="weCustomerList.departmentIds.split(',')" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>

            </if>

            <if test="weCustomerList.delFlag != null">
                AND wc.del_flag = #{weCustomerList.delFlag}
            </if>
            <if test="weCustomerList.name != null and weCustomerList.name !=''">
                AND  wc.`name` like concat('%',#{weCustomerList.name,jdbcType=VARCHAR},'%')
            </if>
            <if test="weCustomerList.userIds !=null and weCustomerList.userIds !=''">
                AND wc.first_user_id in
                <foreach collection="weCustomerList.userIds.split(',')" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="weCustomerList.tagIds !=null and weCustomerList.tagIds !=''">
                AND wt.tag_id in
                <foreach collection="weCustomerList.tagIds.split(',')" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="weCustomerList.beginTime !=null and weCustomerList.beginTime != '' and weCustomerList.endTime !='' and weCustomerList.endTime != null">
                AND  date_format(wc.first_add_time,'%Y-%m-%d') BETWEEN #{weCustomerList.beginTime} AND #{weCustomerList.beginTime.endTime}
            </if>
        </where>
        GROUP BY wc.external_userid,wc.first_user_id
        ORDER BY wc.first_add_time desc
    </select>


    <select id="findWecustomerGroups" resultType="com.linkwechat.wecom.domain.WeCustomerDetail$Groups">
        SELECT
            wg.chat_id,
            wg.group_name,
            wgm.join_time,
            wgm.join_scene
        FROM
            we_group_member wgm
                LEFT JOIN we_group wg ON wg.chat_id = wgm.chat_id
        WHERE wgm.del_flag=0 and wg.del_flag=0. AND wgm.user_id=#{userId}
    </select>




    <select id="findCustomerByOperUseridAndCustomerId" resultType="com.linkwechat.wecom.domain.WeCustomerPortrait">
        SELECT
            wc.`name`,
            wc.other_descr as remark,
            wc.phone as remark_mobiles,
            wc.birthday,
            wc.email,
            wc.address,
            wc.qq,
            wc.position,
            wc.corp_name as remark_corp_name,
            wc.other_descr as description,
            wc.first_user_id as user_id,
            wc.avatar,
            wc.external_userid,
            wc.gender,
            GROUP_CONCAT(wt.`name`)	 as tagNames,
            GROUP_CONCAT(wt.`tag_id`) as tagIds
        FROM
            we_customer wc
       LEFT JOIN we_flower_customer_tag_rel wfcrf ON wfcrf.user_id = wc.first_user_id AND wfcrf.del_flag=0
       LEFT JOIN we_tag wt ON wt.tag_id = wfcrf.tag_id
        WHERE wc.external_userid=#{externalUserid} and  wc.first_user_id=#{userid}
    </select>


    <select id="countSocialConn" resultType="com.linkwechat.wecom.domain.WeCustomerSocialConn">
            SELECT
                    (
                        SELECT
                            count(*)
                        FROM
                            we_customer wfcr WHERE wfcr.external_userid=wc.external_userid
                    ) AS addEmployeNum,
                    (
                      SELECT
                        COUNT(*)
                      FROM
                       we_group_member wgm where  wgm.union_id=wc.external_userid
                    ) AS addGroupNum,
                    (
                        SELECT
                            COUNT(*)
                        FROM we_group_member where chat_id in (
                            SELECT
                                chat_id
                            FROM
                                `we_group_member` where union_id=wc.external_userid
                            ) and user_id=#{userid}
                    ) AS commonGroupNum
                    FROM we_customer wc WHERE wc.external_userid=#{externalUserid}  and wc.first_user_id=#{userid}
    </select>




    <select id="findCusertomerBelongUserInfo" resultType="com.linkwechat.wecom.domain.vo.CusertomerBelongUserInfo">
        SELECT
            wu.user_name as belongUserName,
            wu.user_name,
            wfcr.create_time
        FROM
            we_flower_customer_rel  wfcr
                LEFT JOIN   we_user wu ON wu.user_id=wfcr.user_id
        WHERE wfcr.external_userid=#{externalUserId}
        ORDER BY wfcr.create_time DESC
            LIMIT 1
    </select>



    <insert id="batchAddOrUpdate">
        INSERT INTO we_customer(
        external_userid,
        NAME,
        avatar,
        type,
        gender,
        unionid,
        birthday,
        corp_name,
        corp_full_name,
        position,
        is_open_chat,
        create_time,
        create_by,
        first_user_id,
        first_add_time,
        qq,
        email,
        address,
        phone,
        add_method
        ) values
        <foreach collection="weCustomers" item="item" index="index" separator=",">
            (#{item.externalUserid},#{item.name},#{item.avatar},#{item.type},#{item.gender}, #{item.unionid},#{item.birthday},
            #{item.corpName},#{item.corpFullName},#{item.position},#{item.isOpenChat}, #{item.createTime},
            #{item.createBy},#{item.firstUserId},#{item.firstAddTime},#{item.qq},#{item.email},#{item.address},#{item.phone},#{item.addMethod}
            )
        </foreach>
        ON DUPLICATE KEY UPDATE
        NAME=IF(we_customer.NAME != null, VALUES(NAME), we_customer.NAME),
        avatar=IF(we_customer.avatar != null, VALUES(avatar), we_customer.avatar),
        type=IF(we_customer.type != null, VALUES(type), we_customer.type),
        gender=IF(we_customer.gender != null, VALUES(gender), we_customer.gender),
        unionid=IF(we_customer.unionid != null, VALUES(unionid), we_customer.unionid),
        birthday=IF(we_customer.birthday != null,VALUES(birthday), we_customer.birthday),
        corp_name=IF(we_customer.corp_name != null, VALUES(corp_name), we_customer.corp_name),
        corp_full_name=IF(we_customer.corp_full_name != null, VALUES(corp_full_name), we_customer.corp_full_name),
        position=IF(we_customer.position != null, VALUES(position), we_customer.position),
        is_open_chat=IF(we_customer.is_open_chat != null, VALUES(is_open_chat), we_customer.is_open_chat),
        first_user_id=IF(we_customer.first_user_id != null, VALUES(first_user_id), we_customer.first_user_id),
        first_add_time=IF(we_customer.first_add_time != null, VALUES(first_add_time), we_customer.first_add_time),
        qq=IF(we_customer.qq != null, VALUES(qq), we_customer.qq),
        email=IF(we_customer.email != null, VALUES(email), we_customer.email),
        address=IF(we_customer.address != null, VALUES(address), we_customer.address),
        phone=IF(we_customer.phone != null, VALUES(phone), we_customer.phone),
        add_method=IF(we_customer.add_method != null, VALUES(add_method), we_customer.add_method);
    </insert>


    <select id="noRepeatCountCustomer" resultType="long">
      SELECT COUNT(DISTINCT external_userid) FROM we_customer where del_flag=0
    </select>






    
</mapper>