<?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.gree.mall.manager.commonmapper.WxCustomerMapper">
    <select id="queryCustomerDetail" resultMap="CustomerMap">
        select s.*, utg.tag_id, utg.tag_name
        from (SELECT eu.*, ut.follow_createtime, ur.work_name as relatedUserName, ur.work_user_id as relatedUserId
              from external_user eu
                       left join user ur on
                  eu.union_id = ur.union_id
                       left join
                   user_tag ut on eu.union_id = ut.union_id
              where eu.add_type = 1
                and eu.external_user_id = #{externalUserId}
              GROUP BY eu.external_user_id
              order by ut.follow_createtime ASC) s
                 LEFT JOIN user_tag utg on utg.union_id = s.union_id
    </select>


    <select id="queryTagUserIdList" resultType="java.lang.String">
        select ur.user_id
        from user_tag ut
        left JOIN user ur on ut.union_id = ur.union_id
        WHERE
        ur.company_wechat_id = #{companyWechatId}
        and ut.tag_id in
        <foreach item="item" index="index" collection="couponTags" open="(" separator="," close=")">
            #{item.tagId}
        </foreach>
        GROUP BY ur.user_id
    </select>

    <select id="queryCustomerByUser" resultType="com.gree.mall.manager.bean.user.UserWxCustomerBean">
        SELECT ur.nick_name,
               ur.mobile,
               ur.union_id,
               fu.create_time
        FROM enterprise_wechat_worker eww
                 LEFT JOIN follow_user fu ON (eww.wechat_user_id = fu.user_id and eww.corp_id = fu.corp_id)
                 LEFT JOIN external_user eu ON (fu.external_user_id = eu.external_user_id and fu.corp_id = eu.corp_id)
                 LEFT JOIN user ur on ur.union_id = eu.union_id
        WHERE eww.wechat_user_id = #{workUserId}
          and eww.corp_id = #{corpId}
        group by ur.union_id
        ORDER BY fu.create_time asc
    </select>

    <select id="queryCustomerList" resultMap="CustomerMap">
        select s.*, utg.tag_id, utg.tag_name
        from (SELECT eu.*, ut.follow_createtime
        from external_user eu
        left join user ur on
        eu.union_id = ur.union_id
        left join
        user_tag ut on eu.union_id = ut.union_id
        where eu.add_type = 1

        <if test="keyword!=null and keyword!=''">
            and (eu.name like concat('%',#{keyword,jdbcType=VARCHAR},'%') or ur.nick_name like
            concat('%',#{keyword,jdbcType=VARCHAR},'%') or ur.mobile like concat('%',#{keyword,jdbcType=VARCHAR},'%') )
        </if>

        <if test="startTime!=null">
            and ut.follow_createtime >=#{startTime}
        </if>

        <if test="endTime!=null">
            and ut.follow_createtime &lt;>=#{endTime}
        </if>
        <if test="tagIds!=null and tagIds.length>0">
            and ut.tag_id in
            <foreach item="item" index="index" collection="tagIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>

        GROUP BY eu.external_user_id
        order by ut.follow_createtime ASC
        limit #{pageNum},#{pageSize}) s
        LEFT JOIN user_tag utg on utg.union_id = s.union_id


    </select>
    <select id="queryCustomerCount" resultType="java.lang.Integer">
        SELECT count(distinct eu.external_user_id)
        from external_user eu
        left join user ur on
        eu.union_id = ur.union_id
        left join
        user_tag ut on eu.union_id = ut.union_id
        where eu.add_type = 1

        <if test="keyword!=null and keyword!=''">
            and (eu.name like concat('%',#{keyword,jdbcType=VARCHAR},'%') or ur.nick_name like
            concat('%',#{keyword,jdbcType=VARCHAR},'%') or ur.mobile like concat('%',#{keyword,jdbcType=VARCHAR},'%') )
        </if>

        <if test="startTime!=null">
            and ut.follow_createtime >=#{startTime}
        </if>

        <if test="endTime!=null">
            and ut.follow_createtime &lt;>=#{endTime}
        </if>
        <if test="tagIds!=null and tagIds.length>0">
            and ut.tag_id in
            <foreach item="item" index="index" collection="tagIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </select>

    <resultMap id="CustomerMap" type="com.gree.mall.manager.bean.wxCustomer.WxCustomerBean">
        <id column="external_user_id" property="externalUserId"/>
        <result column="union_id" property="unionId"/>
        <result column="name" property="name"/>
        <result column="position" property="position"/>
        <result column="corp_name" property="corpName"/>
        <result column="corp_full_name" property="corpFullName"/>
        <result column="avatar" property="avatar"/>
        <result column="type" property="type"/>
        <result column="gender" property="gender"/>
        <result column="add_type" property="addType"/>
        <result column="external_profile" property="externalProfile"/>
        <result column="relatedUserName" property="relatedUserName"/>
        <result column="relatedUserId" property="relatedUserId"/>
        <result column="create_time" property="createTime"/>
        <collection property="tagList" ofType="com.gree.mall.manager.bean.wxCustomer.SimpleTagBean">
            <id column="tag_id" property="tagId"/>
            <result column="tagName" property="tag_name"/>
        </collection>
    </resultMap>
</mapper>