<?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.cyksj.mapper.corp.CorpTagGroupMapper">

    <resultMap type="com.cyksj.model.views.CorpTagGroupView" id="groupTag">
        <result column="group_id" property="groupId"/>
        <result column="group_name" property="groupName"/>

        <collection property="tagList" column="groupId=group_id" ofType="com.cyksj.model.entity.CorpTag" select="getCorpTagListByGroupId"/>

    </resultMap>

    <select id="getOneCorpGroupTag" resultMap="groupTag">
        select * from corp_tag_group
        <where>
            <if test="groupId != null and groupId != ''">
                group_id = #{groupId,jdbcType=VARCHAR}
            </if>
            <if test="groupName != null and groupName != ''">
                and group_name = #{groupName,jdbcType=VARCHAR}
            </if>
        </where>
        limit 1
    </select>

    <select id="getCorpTagListByGroupId" resultType="com.cyksj.model.entity.CorpTag">
        select * from corp_tag where group_id = #{groupId}
    </select>

    <select id="getCorpTagList" resultMap="groupTag">
        select * from corp_tag_group
        <where>
            <if test="groupName!=null and groupName != ''">
                group_name like concat('%',#{groupName},'%')
            </if>
        </where>
        order by id desc
    </select>

    <select id="getCorpTagBackView" resultType="com.cyksj.model.views.CorpTagBackView">
        select * from (select cg.id as group_id,
        cg.group_id as groupIdStr,
        cg.group_name,
        cg.update_time
        from corp_tag_group cg
        <where>
            <if test="groupName != null">
                cg.group_name like '%${groupName}%'
            </if>
        </where>
        )s
        order by group_id desc
    </select>

    <select id="fillCorpGroupTagInfo" resultType="com.cyksj.model.views.CorpTagBackView">
        select count(distinct ct.id)        as tagSize,
               count(distinct corp_user_id) as numOfCustomer
        from corp_tag ct
                 left join corp_user_tag cut on cut.tag_id = ct.tag_id
                 left join corp_user_follow_relation cufr on cufr.id = cut.relation_id
        where ct.group_id = #{groupId}
          and cufr.wx_corp_id = #{wxCorpId}
    </select>
</mapper>