<?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.iflytek.astron.console.toolkit.mapper.group.GroupUserMapper">
    <delete id="deleteByTagIdAndUidList">
        delete from group_user
        where uid = #{uid} and tag_id = #{tagId} and user_id in
        <foreach collection="uids" item="uid" separator="," open="(" close=")">#{uid}</foreach>
    </delete>
    <delete id="deleteByUidList">
        delete from group_user
        where uid = #{uid} and user_id in
        <foreach collection="uids" item="uid" separator="," open="(" close=")">#{uid}</foreach>
    </delete>
    <delete id="deleteExcludeTagIds">
        delete from group_user where uid = #{uid} and user_id = #{userId}
        <if test="tagIds != null and tagIds.size()>0">
            and tag_id not in <foreach collection="tagIds" item="tagId" open="(" close=")" separator=",">#{tagId}</foreach>
        </if>
    </delete>

    <select id="listUserByTagId" resultType="com.iflytek.astron.console.toolkit.entity.vo.group.GroupUserTagVO">
        select t1.user_id as uid, t2.login, t2.nickname , t2.email ,t1.tagIds, t1.tagNames from (
        select gu.user_id, GROUP_CONCAT(gu.tag_id) as tagIds, GROUP_CONCAT(gt.name) as tagNames  from group_user gu  left join group_tag gt on gu.tag_id = gt.id
        where gu.uid  = #{uid}  group by gu.user_id
        HAVING FIND_IN_SET(#{tagId}, tagIds) >0
        ) t1 left join `system_user` t2 on t1.user_id = t2.id
        <where>
            <if test="content!=null and content!=''">
                (instr(t2.login, #{content}) or instr(t2.nickname, #{content}))
            </if>
        </where>
    </select>
</mapper>
