<?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.microservices.system.mapper.SysUserDeptRoleMapper">

    <resultMap type="com.microservices.system.api.domain.SysUserDeptRole" id="SysUserDeptRoleResult">
        <result property="deptId" column="dept_id"/>
        <result property="userId" column="user_id"/>
        <result property="roleId" column="role_id"/>
        <result property="state" column="state"/>
        <association property="sysDept" column="dept_id" javaType="com.microservices.system.api.domain.SysDept"
                     resultMap="deptResult"/>
        <association property="sysRole" column="role_id" javaType="com.microservices.system.api.domain.SysRole"
                     resultMap="roleResult"/>
        <association property="sysUser" column="user_id" javaType="com.microservices.system.api.domain.SysUser"
                     resultMap="userResult"/>
    </resultMap>

    <resultMap id="userResult" type="com.microservices.system.api.domain.SysUser">
        <id property="userId" column="user_id"/>
        <result property="gitlinkUserId" column="gitlink_user_id"/>
        <result property="userName" column="user_name"/>
        <result property="nickName" column="nick_name"/>
        <result property="email" column="email"/>
        <result property="phonenumber" column="phonenumber"/>
        <result property="sex" column="sex"/>
        <result property="avatar" column="avatar"/>
        <result property="delFlag" column="del_flag"/>
        <result property="status" column="status"/>
    </resultMap>

    <resultMap id="deptResult" type="com.microservices.system.api.domain.SysDept">
        <id property="deptId" column="dept_id"/>
        <result property="parentId" column="parent_id"/>
        <result property="deptName" column="dept_name"/>
        <result property="ancestors" column="ancestors"/>
        <result property="orderNum" column="order_num"/>
        <result property="leader" column="leader"/>
        <result property="status" column="dept_status"/>
        <result property="deptType" column="dept_type"/>
    </resultMap>

    <resultMap id="roleResult" type="com.microservices.system.api.domain.SysRole">
        <id property="roleId" column="role_id"/>
        <result property="roleName" column="role_name"/>
        <result property="roleKey" column="role_key"/>
        <result property="roleSort" column="role_sort"/>
        <result property="dataScope" column="data_scope"/>
        <result property="status" column="role_status"/>
    </resultMap>
    <sql id="selectSysUserDeptRoleVo">
        select dept_id, user_id, role_id, state
        from sys_user_dept_role
    </sql>
    <sql id="selectSysUserDeptRoleDetailVo">
        select u.user_id,
               u.gitlink_user_id,
               u.user_name,
               u.nick_name,
               u.email,
               u.avatar,
               u.phonenumber,
               u.sex,
               u.status,
               u.del_flag,
               d.dept_id,
               d.parent_id,
               d.ancestors,
               d.dept_name,
               d.order_num,
               d.dept_type,
               d.leader,
               d.status as dept_status,
               r.role_id,
               r.role_name,
               r.role_key,
               r.role_sort,
               r.data_scope,
               r.status as role_status,
               udr.state
        from sys_user_dept_role udr
                 left join sys_dept d on udr.dept_id = d.dept_id
                 left join sys_role r on r.role_id = udr.role_id
                 left join sys_user u on u.user_id = udr.user_id
    </sql>


    <select id="selectSysUserDeptRoleList" parameterType="com.microservices.system.api.domain.SysUserDeptRole"
            resultMap="SysUserDeptRoleResult">
        <include refid="selectSysUserDeptRoleDetailVo"/>
        <where>
            <if test="deptId != null">and udr.dept_id=#{deptId}</if>
            <if test="userId != null">and udr.user_id=#{userId}</if>
            <if test="roleId != null">and udr.role_id=#{roleId}</if>
            and udr.state = 'normal'
        </where>
    </select>

    <select id="selectSysUserDeptRoleByDeptId" parameterType="Long" resultMap="SysUserDeptRoleResult">
        <include refid="selectSysUserDeptRoleVo"/>
        where dept_id = #{deptId}
        and state = 'normal'
    </select>
    <select id="selectSysUserDeptRoleListByDeptId" resultMap="SysUserDeptRoleResult">
        <include refid="selectSysUserDeptRoleDetailVo"/>
        <where>
            udr.dept_id = #{deptId}
            and u.del_flag = '0'
            <if test="userName != null and userName != ''">
                <choose>
                    <when test='userName.contains("_")'>
                        <bind name="contentTmp" value='userName.replaceAll("_","/_")'/>
                        AND u.user_name like concat('%', #{ contentTmp }, '%') escape '/'
                    </when>
                    <otherwise>
                        AND u.user_name like concat('%', #{userName}, '%')
                    </otherwise>
                </choose>
            </if>
            <if test="nickName != null and nickName != ''">
                <choose>
                    <when test='nickName.contains("_")'>
                        <bind name="contentTmp" value='nickName.replaceAll("_","/_")'/>
                        AND u.nick_name like concat('%', #{ contentTmp }, '%') escape '/'
                    </when>
                    <otherwise>
                        AND u.nick_name like concat('%', #{nickName}, '%')
                    </otherwise>
                </choose>
            </if>
            <if test="roleId != null and roleId != ''">
                AND r.role_id = ${roleId}
            </if>
            <if test="virtualRoleIdList != null">
                and r.role_id not in
                <foreach item="virtualRoleId" collection="virtualRoleIdList" open="(" separator="," close=")">
                    #{virtualRoleId}
                </foreach>
            </if>
            and udr.state = 'normal'
        </where>
    </select>
    <select id="selectSysUserDeptRoleListByUserId" resultMap="SysUserDeptRoleResult">
        <include refid="selectSysUserDeptRoleDetailVo"/>
        <where>
            udr.user_id = #{userId}
            and u.del_flag = '0'
            AND d.dept_id is not null
            AND r.role_id is not null
            <if test="deptName != null and deptName != ''">
                AND d.dept_name like concat('%', #{deptName}, '%')
            </if>
            <if test="roleName != null and roleName != ''">
                AND r.role_name like concat('%', #{roleName}, '%')
            </if>
            and udr.state = 'normal'
        </where>
        ORDER BY udr.dept_id
    </select>
    <select id="selectSysUserDeptRoleListByUserName" resultMap="SysUserDeptRoleResult">
        <include refid="selectSysUserDeptRoleDetailVo"/>
        <where>
            u.user_name = #{userName}
            and u.del_flag = '0'
            and udr.state = 'normal'
        </where>
    </select>
    <select id="selectSysUserDeptRoleByUserIdAndDeptId"
            resultMap="SysUserDeptRoleResult">
        <include refid="selectSysUserDeptRoleDetailVo"/>
        <where>
            udr.user_id = #{userId}
            and udr.dept_id = #{deptId}
            and udr.state = 'normal'
        </where>
    </select>
    <select id="selectSysUserDeptRoleByDeptIdAndRoleId"
            resultMap="SysUserDeptRoleResult">
        <include refid="selectSysUserDeptRoleDetailVo"/>
        <where>
            udr.role_id = #{roleId}
            and udr.dept_id = #{deptId}
            and udr.state = 'normal'
        </where>

    </select>
    <select id="countUserRoleByRoleId" resultType="java.lang.Integer">
        SELECT COUNT(1)
        FROM sys_user_dept_role
        WHERE role_id = #{roleId}
          and state = 'normal'
    </select>
    <select id="searchSysUserDeptRoleListByDeptId" resultMap="SysUserDeptRoleResult">
        <include refid="selectSysUserDeptRoleDetailVo"/>
        <where>
            udr.dept_id = #{deptId}
            <if test="search != null and search != ''">
                <choose>
                    <when test='search.contains("_")'>
                        <bind name="contentTmp" value='search.replaceAll("_","/_")'/>
                        AND (u.user_name like concat('%', #{ contentTmp }, '%') escape '/' OR u.nick_name like
                        concat('%', #{ contentTmp }, '%') escape '/')
                    </when>
                    <otherwise>
                        AND (u.user_name like concat('%', #{search}, '%') OR u.nick_name like concat('%', #{search},
                        '%'))
                    </otherwise>
                </choose>
            </if>
            and udr.state = 'normal'
        </where>
    </select>
    <select id="countUserDeptRole" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM `sys_user_dept_role` AS udr
                 LEFT JOIN sys_role AS sr ON sr.role_id = udr.`role_id`
        WHERE
            sr.`role_key` = #{roleKey}
          AND udr.user_id = #{userId}
          AND udr.dept_id = #{deptId}
          and udr.state = 'normal'
    </select>
    <select id="selectSysUserDeptRoleListByRoleId" resultMap="SysUserDeptRoleResult">
        <include refid="selectSysUserDeptRoleVo"/>
        WHERE role_id = #{roleId}
        and state = 'normal'
    </select>
    <select id="selectUserIdentityByAllStatus"
            resultMap="SysUserDeptRoleResult">
        <include refid="selectSysUserDeptRoleDetailVo"/>
        <where>
            udr.user_id = #{userId}
            and udr.dept_id = #{deptId}
        </where>
    </select>
    <select id="selectUserInDeptOrParentDeptHasPermsCount" resultType="java.lang.Integer">
        SELECT count(DISTINCT sm.menu_id)
        FROM sys_menu sm
                 INNER JOIN sys_role_menu srm ON sm.menu_id = srm.menu_id
                 INNER JOIN sys_user_dept_role sudr ON srm.role_id = sudr.role_id
                 CROSS JOIN (SELECT CONCAT(ancestors, ',', dept_id) AS dept_ancestors
                             FROM sys_dept
                             WHERE dept_id = #{deptId}) vars
        WHERE sudr.user_id = #{userId}
          AND FIND_IN_SET(sudr.dept_id, vars.dept_ancestors)
          AND sm.perms = #{perms};
    </select>

    <insert id="insertSysUserDeptRole" parameterType="com.microservices.system.api.domain.SysUserDeptRole">
        insert into sys_user_dept_role
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deptId != null">dept_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="roleId != null">role_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deptId != null">#{deptId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="roleId != null">#{roleId},</if>
        </trim>
    </insert>

    <update id="updateSysUserDeptRole" parameterType="com.microservices.system.api.domain.SysUserDeptRole">
        update sys_user_dept_role
        <trim prefix="SET" suffixOverrides=",">
            <if test="roleId != null">role_id = #{roleId},</if>
        </trim>
        where dept_id = #{deptId}
        and user_id = #{userId}
    </update>
    <update id="updateUserIdentifyListState">
        update sys_user_dept_role
        set state = #{state}
        where dept_id = #{deptId}
        and user_id in
        <foreach item="userId" collection="userIds" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </update>
    <update id="updateUserIdentifyStateByUserId">
        update sys_user_dept_role
        set state = #{state}
        where user_id = #{userId}
    </update>

    <delete id="deleteSysUserDeptRoleByDeptId" parameterType="Long">
        delete
        from sys_user_dept_role
        where dept_id = #{deptId}
    </delete>
    <delete id="deleteSysUserDeptRoleByDeptIdAndUserIds">
        delete from sys_user_dept_role where dept_id = #{deptId}
        and user_id in
        <foreach item="userId" collection="userIds" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>
    <delete id="deleteSysUserDeptRoleListByUserIdAndDeptIds">
        delete from sys_user_dept_role where user_id = #{userId}
        and dept_id in
        <foreach item="deptId" collection="deptIds" open="(" separator="," close=")">
            #{deptId}
        </foreach>
    </delete>
    <delete id="deleteSysUserDeptRole">
        delete
        from sys_user_dept_role
        where user_id = #{userId}
          and role_id = #{roleId}
          and dept_id = #{deptId}
    </delete>
</mapper>