WeCustomerMapper.xml 12.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="com.linkwechat.wecom.mapper.WeCustomerMapper">
    
    <resultMap type="WeCustomer" id="WeCustomerResult">
        <id property="externalUserid"    column="external_userid"    />
        <result property="name"    column="name"    />
        <result property="avatar"    column="avatar"    />
        <result property="type"    column="type"    />
        <result property="gender"    column="gender"    />
        <result property="unionid"    column="unionid"    />
        <result property="birthday"    column="birthday"    />
        <result property="corpName"    column="corp_name"    />
        <result property="corpFullName"    column="corp_full_name"    />
        <result property="position"    column="position"    />
        <result property="userName"    column="user_name"    />
        <result property="userId"    column="user_id"    />
        <result property="isOpenChat"    column="is_open_chat" jdbcType="INTEGER"  />
        <collection property="weFlowerCustomerRels" ofType="com.linkwechat.wecom.domain.WeFlowerCustomerRel">
            <result property="id"    column="wfcrId"    />
            <result property="userId"    column="user_id"    />
            <result property="remark"    column="remark"    />
            <result property="description"    column="description"    />
            <result property="createTime"    column="create_time"    />
            <result property="remarkCorpName"    column="remark_corp_name"    />
            <result property="operUserid"    column="oper_userid"    />
            <result property="addWay"    column="add_way"    />
            <result property="state"    column="state"    />
            <result property="status"    column="status"    />
            <result property="userName" column="user_name"/>
            <result property="department" column="department"/>
            <collection property="weFlowerCustomerTagRels" javaType="ArrayList"
                        column="{id = wfcrId}" select="selectWeCustomerTags">
            </collection>
        </collection>
    </resultMap>
    
    
    <resultMap id="weFlowerCustomerRelsMapper" type="com.linkwechat.wecom.domain.WeFlowerCustomerTagRel">
        <result property="flowerCustomerRelId"    column="flower_customer_rel_id"    />
        <result property="tagId"    column="tag_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="tagName" column="tagName"/>
    </resultMap>


    <sql id="selectWeCustomerVo">
        select external_userid, name, avatar, type, gender, unionid, birthday, corp_name, corp_full_name, position, is_open_chat from we_customer
    </sql>
    <sql id="selectWeCustomerByTagVo">
        select
        wc.external_userid,
        wc.name,
        wc.avatar,
        wc.type,
        wc.gender,
        wc.unionid,
        wc.birthday,
        wc.corp_name,
        wc.corp_full_name,
        wc.position,
        wc.is_open_chat,
        wt.tag_id
        from
            we_customer wc
        left join we_flower_customer_rel wfcr on
            wc.external_userid = wfcr.external_userid
            and wfcr.status = 0
        left join we_flower_customer_tag_rel wfctr on
            wfctr.flower_customer_rel_id = wfcr.id
        right join we_tag wt on
        wt.tag_id = wfctr.tag_id
        and wt.status  = 0
    </sql>

    <select id="selectWeCustomerList" parameterType="WeCustomer" resultMap="WeCustomerResult">
        SELECT
             DISTINCT wc.external_userid,
            wc.`name`,
            wc.avatar,
            wc.type,
            wc.gender,
            wc.unionid,
            wc.birthday,
            wc.corp_name,
            wc.corp_full_name,
            wc.position,
            wc.is_open_chat,
            wfcr.id as wfcrId,
            wfcr.user_id,
            wfcr.remark,
            wfcr.description,
            wfcr.create_time,
            wfcr.remark_corp_name,
            wfcr.oper_userid,
            wfcr.add_way,
            wfcr.state,
            wfcr.status,
            wu.user_name,
            (SELECT GROUP_CONCAT(wd.`name`) FROM we_department wd WHERE wd.id=wu.department) as department
            FROM
            we_customer wc
        LEFT JOIN we_flower_customer_rel wfcr ON wc.external_userid=wfcr.external_userid
        LEFT JOIN we_user wu ON wu.user_id=wfcr.user_id
        LEFT JOIN we_flower_customer_tag_rel wfcrf ON wfcrf.flower_customer_rel_id = wfcr.id
        LEFT JOIN we_tag wt ON wt.tag_id = wfcrf.tag_id
        <where>
            <if test="name != null and name !=''">
               AND wc.name like concat('%', #{name}, '%')
            </if>
            <if test="userIds != null and userIds !=''">
                <if test="userIds.indexOf(',') != -1">
                    AND  wfcr.user_id in
                    <foreach item="item" index="index" collection="userIds.split(',')" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
                <if test="userIds.indexOf(',') == -1">
                    AND  wfcr.user_id=#{userIds}
                </if>
            </if>
            <if test="beginTime != null and beginTime != ''"><!-- 开始时间检索 -->
                    AND date_format(wfcr.create_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')
            </if>

             <if test="endTime != null and endTime != ''"><!-- 结束时间检索 -->
                   AND date_format(wfcr.create_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
             </if>

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

             <if test="status !=null and status !=''">
                  AND wfcr.status=#{status}
             </if>
            <if test="isOpenChat !=null ">
                AND wc.is_open_chat=#{isOpenChat}
            </if>
        </where>
        ORDER BY wfcr.create_time DESC
    </select>


    <select id="selectWeCustomerTags" resultMap="weFlowerCustomerRelsMapper">
        SELECT
                wfcrf.flower_customer_rel_id,
                wt.tag_id,
               wt.`name` as tagName
        FROM
            we_flower_customer_tag_rel wfcrf
        LEFT JOIN we_tag wt ON wt.tag_id = wfcrf.tag_id
        where wt.status=0 and  wfcrf.flower_customer_rel_id=#{id}
    </select>













    
    <select id="selectWeCustomerById" parameterType="String" resultType="WeCustomer">
        <include refid="selectWeCustomerVo"/>
        where external_userid = #{externalUserId}
    </select>
        
    <insert id="insertWeCustomer" parameterType="WeCustomer">
        insert into we_customer
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="externalUserid != null">external_userid,</if>
            <if test="name != null">name,</if>
            <if test="avatar != null">avatar,</if>
            <if test="type != null">type,</if>
            <if test="gender != null">gender,</if>
            <if test="unionid != null">unionid,</if>
            <if test="birthday != null">birthday,</if>
            <if test="corpName != null">corp_name,</if>
            <if test="corpFullName != null">corp_full_name,</if>
            <if test="position != null">position,</if>
            <if test="isOpenChat != null">is_open_chat</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="externalUserid != null">#{externalUserid},</if>
            <if test="name != null">#{name},</if>
            <if test="avatar != null">#{avatar},</if>
            <if test="type != null">#{type},</if>
            <if test="gender != null">#{gender},</if>
            <if test="unionid != null">#{unionid},</if>
            <if test="birthday != null">#{birthday},</if>
            <if test="corpName != null">#{corpName},</if>
            <if test="corpFullName != null">#{corpFullName},</if>
            <if test="position != null">#{position},</if>
            <if test="isOpenChat != null">#{isOpenChat}</if>
         </trim>
    </insert>

    <update id="updateWeCustomer" parameterType="WeCustomer">
        update we_customer
        <trim prefix="SET" suffixOverrides=",">
            <if test="externalUserid != null">external_userid = #{externalUserid},</if>
            <if test="name != null">name = #{name},</if>
            <if test="avatar != null">avatar = #{avatar},</if>
            <if test="type != null">type = #{type},</if>
            <if test="gender != null">gender = #{gender},</if>
            <if test="unionid != null">unionid = #{unionid},</if>
            <if test="birthday != null">birthday = #{birthday},</if>
            <if test="corpName != null">corp_name = #{corpName},</if>
            <if test="corpFullName != null">corp_full_name = #{corpFullName},</if>
            <if test="position != null">position = #{position},</if>
            <if test="isOpenChat != null">is_open_chat = #{isOpenChat}</if>
        </trim>
        where external_userid = #{externalUserid}
    </update>

    <delete id="deleteWeCustomerById" parameterType="String">
        delete from we_customer where external_userid = #{externalUserId}
    </delete>

    <delete id="deleteWeCustomerByIds">
        delete from we_customer where external_userid in
        <foreach item="item" collection="array" open="(" separator="," close=")">
            #{item}
        </foreach>
    </delete>


    <select id="getCustomersByUserId" resultMap="WeCustomerResult">
          SELECT
                  wc.external_userid,
                  wc.`name`,
                  wc.avatar,
                  wc.type,
                  wc.gender,
                  wc.unionid,
                  wc.birthday,
                  wc.corp_name,
                  wc.corp_full_name,
                  wc.position,
                  wc.is_open_chat,
                  wfcr.id as wfcrId,
                  wfcr.user_id,
                  wfcr.remark,
                  wfcr.description,
                  wfcr.create_time,
                  wfcr.remark_corp_name,
                  wfcr.oper_userid,
                  wfcr.add_way,
                  wfcr.state,
                  wu.user_name,
                  wfcrf.flower_customer_rel_id,
                  wt.tag_id,
                  wt.create_time,
                  wt.`name` as tagName,
                  (SELECT GROUP_CONCAT(wd.`name`) FROM we_department wd WHERE wd.id=wu.department) as department
        FROM
            we_customer wc
        LEFT JOIN we_flower_customer_rel wfcr ON wc.external_userid=wfcr.external_userid
        LEFT JOIN we_user wu ON wu.user_id=wfcr.user_id
        LEFT JOIN we_flower_customer_tag_rel wfcrf ON wfcrf.flower_customer_rel_id = wfcr.id
        LEFT JOIN we_tag wt ON wt.tag_id = wfcrf.tag_id
        WHERE wc.external_userid=#{externalUserid}
    </select>


    <select id="getCustomerByTag" parameterType="java.util.ArrayList" resultType="com.linkwechat.wecom.domain.WeUser">
        <include refid="selectWeCustomerByTagVo"/>
        <where>
            and wt.tag_id in
            <foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </where>
    </select>
    
</mapper>