<?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="ink.wgink.service.group.dao.IGroupUserDao">

    <cache flushInterval="3600000"/>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `sys_group_user` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `group_id` char(36) NOT NULL COMMENT '组ID',
            `user_id` char(36) NOT NULL,
            PRIMARY KEY (`id`),
            KEY `user_id_idx` (`user_id`) USING BTREE,
            KEY `group_id_idx` (`group_id`) USING BTREE
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    </update>

    <!-- 新增 -->
    <insert id="save" parameterType="map" flushCache="true">
        INSERT INTO sys_group_user(
            group_id,
            user_id
        ) VALUES(
            #{groupId},
            #{userId}
        )
    </insert>

    <!-- 删除 -->
    <delete id="delete" parameterType="map" flushCache="true">
        DELETE FROM
            sys_group_user
        WHERE
            1 = 1
        <if test="groupId != null and groupId != ''">
        AND
            group_id = #{groupId}
        </if>
        <if test="userId != null and userId != ''">
        AND
            user_id = #{userId}
        </if>
        <if test="userIds != null and userIds.size > 0">
        AND
            user_id IN
            <foreach collection="userIds" index="index" open="(" separator="," close=")">
                #{userIds[${index}]}
            </foreach>
        </if>
    </delete>

    <!-- 用户ID列表 -->
    <select id="listUserId" parameterType="map" resultType="java.lang.String" useCache="true">
        SELECT
            user_id
        FROM
            sys_group_user
        WHERE
        <if test="groupId != null and groupId != ''">
            group_id = #{groupId}
        </if>
        <if test="groupIds != null and groupIds.size > 0">
            group_id IN (
            <foreach collection="groupIds" index="index" open="(" separator="," close=")">
                #{groupIds[${index}]}
            </foreach>
            )
        </if>
    </select>

    <!-- 用户ID列表 -->
    <select id="listGroupUserId" parameterType="map" resultType="java.lang.String" useCache="true">
        SELECT
            user_id
        FROM
            sys_group_user
        WHERE
            1 = 1
        GROUP BY
            user_id
    </select>

    <!-- 组ID列表 -->
    <select id="listGroupId" parameterType="map" resultType="java.lang.String">
        SELECT
            group_id
        FROM
            sys_group_user
        WHERE
        <if test="userId != null and userId != ''">
            user_id = #{userId}
        </if>
        <if test="userIds != null and userIds.size > 0">
            user_id IN (
            <foreach collection="userIds" index="index" open="(" separator="," close=")">
                #{userIds[${index}]}
            </foreach>
            )
        </if>
    </select>

    <!-- 用户列表 -->
    <select id="listUser" parameterType="map" resultMap="ink.wgink.service.user.dao.IUserDao.userDTO" useCache="false">
        SELECT
            user_id,
            user_username,
            user_name,
            user_phone,
            user_email,
            user_type,
            user_state
        FROM
            sys_user t1
        WHERE
            t1.is_delete = 0
        AND
            t1.user_username != 'admin'
        <if test="keywords != null and keywords != ''">
            AND (
            user_username LIKE CONCAT('%', #{keywords}, '%')
            OR
            user_name LIKE CONCAT('%', #{keywords}, '%')
            OR
            user_phone LIKE CONCAT('%', #{keywords}, '%')
            OR
            user_email LIKE CONCAT('%', #{keywords}, '%')
            )
        </if>
        <if test="userType != null">
        AND
            t1.user_type = #{userType}
        </if>
        <if test="userState != null">
        AND
            t1.user_state = #{userState}
        </if>
        <if test="excludeUserType != null and excludeUserType != ''">
        AND
            t1.user_type != #{excludeUserType}
        </if>
        <if test="groupId != null and groupId != ''">
        AND
            t1.user_id IN (
                SELECT
                    user_id
                FROM
                    sys_group_user st1
                WHERE
                    st1.group_id = #{groupId}
            )
        </if>
        <if test="groupIds != null and groupIds.size > 0">
        AND
            t1.user_id IN (
                SELECT
                    user_id
                FROM
                    sys_group_user st1
                WHERE
                    st1.group_id IN
                <foreach collection="groupIds" index="index" open="(" separator="," close=")">
                    #{groupIds[${index}]}
                </foreach>
            )
        </if>
        <if test="excludeGroupId != null and excludeGroupId != ''">
        AND
            t1.user_id NOT IN (
                SELECT
                    user_id
                FROM
                    sys_group_user st1
                WHERE
                    st1.group_id = #{excludeGroupId}
            )
        </if>
        <if test="excludeGroupIds != null and excludeGroupIds.size > 0">
        AND
            t1.user_id NOT IN (
                SELECT
                    user_id
                FROM
                    sys_group_user st1
                WHERE
                    st1.group_id IN
                <foreach collection="excludeGroupIds" index="index" open="(" separator="," close=")">
                    #{excludeGroupIds[${index}]}
                </foreach>
            )
        </if>
    </select>
    
</mapper>