<?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.role.dao.IRoleUserDao">

    <cache flushInterval="3600000"/>

    <resultMap id="roleUserDTO" type="ink.wgink.pojo.dtos.role.RoleUserDTO" extends="ink.wgink.service.user.dao.IUserDao.userDTO">
        <result column="role_id" property="roleId"/>
        <result column="user_id" property="userId"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `sys_role_user` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `role_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 `role_id_idx` (`role_id`) USING BTREE
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    </update>

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

    <!-- 删除 -->
    <delete id="delete" parameterType="map" flushCache="true">
        DELETE FROM
            sys_role_user
        WHERE
            1 = 1
        <if test="roleId != null and roleId != ''">
        AND
            role_id = #{roleId}
        </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_role_user
        <where>
        <if test="roleId != null and roleId != ''">
            role_id = #{roleId}
        </if>
        <if test="roleIds != null and roleIds.size > 0">
        AND
            role_id IN
            <foreach collection="roleIds" index="index" open="(" separator="," close=")">
                #{roleIds[${index}]}
            </foreach>
        </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>
        </where>
    </select>

    <!-- 角色ID列表 -->
    <select id="listRoleId" parameterType="map" resultType="java.lang.String" useCache="true">
        SELECT
            role_id
        FROM
            sys_role_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>

    <!-- 用户ID列表 -->
    <select id="listGroupUserId" parameterType="map" resultType="java.lang.String" useCache="false">
        SELECT
            user_id
        FROM
            sys_role_user
        WHERE
            1 = 1
        GROUP BY
            user_id
    </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="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="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="roleId != null and roleId != ''">
        AND
            t1.user_id IN (
                SELECT
                    st1.user_id
                FROM
                    sys_role_user st1
                WHERE
                    role_id = #{roleId}
            )
        </if>
        <if test="roleIds != null and roleIds.size > 0">
        AND
            t1.user_id IN (
                SELECT
                    user_id
                FROM
                    sys_role_user st1
                WHERE
                    st1.role_id IN
                <foreach collection="roleIds" index="index" open="(" separator="," close=")">
                    #{roleIds[${index}]}
                </foreach>
            )
        </if>
        <if test="noRole != null and noRole == true">
        AND
            t1.user_id NOT IN (
                SELECT
                    st3.user_id
                FROM
                    sys_role_user st3
            )
        </if>
        <if test="excludeRoleId != null and excludeRoleId != ''">
        AND
            t1.user_id NOT IN (
                SELECT
                    user_id
                FROM
                    sys_role_user st1
                WHERE
                    st1.role_id = #{excludeRoleId}
            )
        </if>
        <if test="excludeRoleIds != null and excludeRoleIds.size > 0">
        AND
            t1.user_id NOT IN (
                SELECT
                    user_id
                FROM
                    sys_role_user st1
                WHERE
                    st1.role_id IN
                <foreach collection="excludeRoleIds" index="index" open="(" separator="," close=")">
                    #{excludeRoleIds[${index}]}
                </foreach>
            )
        </if>
    </select>

    <!-- 用户列表 -->
    <select id="listUserPO" parameterType="map" resultMap="ink.wgink.service.user.dao.IUserDao.userPO" useCache="false">
        SELECT
            user_id,
            user_password,
            user_username,
            user_name,
            user_phone,
            user_email,
            user_ukey,
            user_ukey_electronic_secret_key,
            user_type,
            user_state,
            user_expired_date,
            user_avatar,
            user_longitude,
            user_latitude,
            last_login_address,
            LEFT(last_login_time, 19) last_login_time,
            login_type,
            LEFT(gmt_password_modified, 19) gmt_password_modified,
            remarks,
            LEFT(gmt_create, 19) gmt_create
        FROM
            sys_user t1
        WHERE
            t1.is_delete = 0
        <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="roleId != null and roleId != ''">
        AND
            t1.user_id IN (
                SELECT
                    st1.user_id
                FROM
                    sys_role_user st1
                WHERE
                    role_id = #{roleId}
            )
        </if>
        <if test="roleIds != null and roleIds.size > 0">
        AND
            t1.user_id IN (
                SELECT
                    user_id
                FROM
                    sys_role_user st1
                WHERE
                    st1.role_id IN
                <foreach collection="roleIds" index="index" open="(" separator="," close=")">
                    #{roleIds[${index}]}
                </foreach>
            )
        </if>
        <if test="noRole != null and noRole == true">
        AND
            t1.user_id NOT IN (
                SELECT
                    st3.user_id
                FROM
                    sys_role_user st3
            )
        </if>
        <if test="excludeRoleId != null and excludeRoleId != ''">
        AND
            t1.user_id NOT IN (
                SELECT
                    user_id
                FROM
                    sys_role_user st1
                WHERE
                    st1.role_id = #{excludeRoleId}
            )
        </if>
        <if test="excludeRoleIds != null and excludeRoleIds.size > 0">
        AND
            t1.user_id NOT IN (
                SELECT
                    user_id
                FROM
                    sys_role_user st1
                WHERE
                    st1.role_id IN
                <foreach collection="excludeRoleIds" index="index" open="(" separator="," close=")">
                    #{excludeRoleIds[${index}]}
                </foreach>
            )
        </if>
    </select>

    <!-- 部门用户列表 -->
    <select id="listRoleUser" parameterType="map" resultMap="roleUserDTO" useCache="false">
        SELECT
            t1.user_id,
            t1.user_username,
            t1.user_name,
            t1.user_phone,
            t1.user_email,
            t1.user_type,
            t1.user_state,
            jt1.role_id,
            jt1.user_sort
        FROM
            sys_user t1
        LEFT JOIN
            sys_role_user jt1
        ON
            t1.user_id = jt1.user_id
        WHERE
            t1.is_delete = 0
        AND
            t1.user_username != 'admin'

        <if test="userType != null and userType != ''">
        AND
            t1.user_type = #{userType}
        </if>
        <if test="excludeUserType != null and excludeUserType != ''">
        AND
            t1.user_type != #{excludeUserType}
        </if>
        <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="roleId != null and roleId != ''">
        AND
            t1.user_id IN (
                SELECT
                    st1.user_id
                FROM
                    sys_role_user st1
                WHERE
                    role_id = #{roleId}
            )
        </if>
        <if test="roleId != null and roleId != ''">
        AND
            t1.user_id IN (
                SELECT
                    st2.user_id
                FROM
                    sys_role_user st2
                WHERE
                    role_id = #{roleId}
            )
        </if>
        <if test="noRole != null and noRole">
        AND
            t1.user_id NOT IN (
                SELECT
                    st3.user_id
                FROM
                    sys_role_user st3
            )
        </if>
        <if test="noRole != null and noRole">
        AND
            t1.user_id NOT IN (
                SELECT
                    st4.user_id
                FROM
                    sys_role_user st4
            )
        </if>
    </select>

    <!-- 部门用户列表 -->
    <select id="listDepartmentUser" parameterType="map" resultMap="ink.wgink.service.department.dao.IDepartmentUserDao.departmentUserDTO" useCache="false">
        SELECT
            t1.user_id,
            t1.user_username,
            t1.user_name,
            t1.user_phone,
            t1.user_email,
            t1.user_type,
            t1.user_state,
            jt1.department_id,
            jt1.user_sort
        FROM
            sys_user t1
        LEFT JOIN
            sys_department_user jt1
        ON
            t1.user_id = jt1.user_id
        WHERE
            t1.is_delete = 0
        AND
            t1.user_username != 'admin'
        <if test="userType != null and userType != ''">
        AND
            t1.user_type = #{userType}
        </if>
        <if test="excludeUserType != null and excludeUserType != ''">
        AND
            t1.user_type != #{excludeUserType}
        </if>
        <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="departmentId != null and departmentId != ''">
        AND
            t1.user_id IN (
            SELECT
            st1.user_id
            FROM
            sys_department_user st1
            WHERE
            department_id = #{departmentId}
            )
        </if>
        <if test="roleId != null and roleId != ''">
        AND
            t1.user_id IN (
            SELECT
            st2.user_id
            FROM
            sys_role_user st2
            WHERE
            role_id = #{roleId}
            )
        </if>
        <if test="noDepartment != null and noDepartment">
        AND
            t1.user_id NOT IN (
            SELECT
            st3.user_id
            FROM
            sys_department_user st3
            )
        </if>
        <if test="noRole != null and noRole">
        AND
            t1.user_id NOT IN (
            SELECT
            st4.user_id
            FROM
            sys_role_user st4
            )
        </if>
    </select>

</mapper>