<?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.miniapp.commonmapper.WxCustomerMapper">

    <select id="queryCustomerByUser" resultMap="customerTag">
        SELECT ur.nick_name,
               ur.mobile,
               ur.union_id,
               ur.service_time,
               ur.last_login_time lastAccessTime,
               ut.tag_name,
               ut.tag_id,
               cut.user_tag_id as customeTagId,
               cut.tag_name as customTagName
        FROM  user ur
                  LEFT JOIN user_tag ut ON ur.union_id=ut.union_id
                  left join custom_user_tag cut on cut.union_id = ur.union_id and cut.userid = #{workUserId}
        WHERE ur.service_id = #{userId}
        ORDER BY ut.create_time asc
    </select>


    <resultMap id="customerTag" type="com.gree.mall.miniapp.bean.tag.UserWxCustomerBean">
        <id column="union_id" property="unionId"/>
        <result column="nick_name" property="nickName"/>
        <result column="mobile" property="mobile"/>
        <result column="lastAccessTime" property="lastAccessTime"/>
        <result column="create_time" property="createTime"/>
        <collection property="tags" ofType="com.gree.mall.miniapp.bean.tag.UserWxCustomerTagBean">
            <id column="tag_id" property="tagId"/>
            <result column="tag_name" property="tagName"/>
        </collection>
        <collection property="customtags" ofType="com.gree.mall.miniapp.bean.tag.UserWxCustomerTagBean" >
            <id column="customeTagId" property="tagId"/>
            <result column="customTagName" property="tagName"/>
        </collection>
    </resultMap>


    <select id="queryTagList" resultMap="tagList">
        select tg.tag_group_id,
               tg.tag_group_name,
               ta.tag_id,
               ta.name,
               if(ut.union_id = #{unionId}, 1, 0) as tagged
        from tag_group tg
                 left join tag ta on ta.group_id = tg.tag_group_id
                 left join user_tag ut on ta.tag_id = ut.tag_id
        where ta.deleted =0 and tg.deleted =0
        ORDER BY tg.sort_num desc,ta.sort_num desc,tg.create_time asc
    </select>


    <resultMap id="tagList" type="com.gree.mall.miniapp.bean.tag.TagGroupBean">
        <id column="tag_group_id" property="tagGroupId"/>
        <result column="tag_group_name" property="tagGroupName"/>
        <collection property="tags" ofType="com.gree.mall.miniapp.bean.tag.UserWxCustomerTagBean">
            <id column="tag_id" property="tagId"/>
            <result column="name" property="tagName"/>
            <result column="tagged" property="tagged"/>
        </collection>
    </resultMap>

    <select id="queryCustomTagList" resultType="java.lang.String">
        select tag_name from custom_user_tag WHERE union_id =#{unionId} and userid = #{workUserId} ORDER BY create_time asc
    </select>



    <update id="updateMpMaRela">
       update mp_ma_rela a join user b on a.union_id=b.union_id
        set a.ma_open_id = b.open_id,a.ma_user_name=b.nick_name,a.ma_phone=b.mobile,a.ma_user_id=b.user_id
        where a.ma_open_id=''
    </update>

    <select id="queryNoticeParam" resultType="com.gree.mall.miniapp.bean.notice.NoticeParamBean">
        select
            b.*,
            a.mp_open_id
        from mp_ma_rela a
        join notice_param b on a.ma_phone=b.mobile
        where b.mobile !='' and b.create_time >= #{time}
        and b.status=0 and b.type = #{type}
        and b.err_num &lt; #{errNum}
    </select>


</mapper>