<?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.SysDeptRoleMapper">

    <resultMap type="com.microservices.system.domain.SysDeptRole" id="SysDeptRoleResult">
        <result property="deptId" column="dept_id"/>
        <result property="roleId" column="role_id"/>
        <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"/>
    </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="selectSysDeptRoleVo">
        select dept_id, role_id
        from sys_dept_role
    </sql>

    <sql id="selectSysDeptRoleDetailVo">
        select sd.dept_id,
               sd.parent_id,
               sd.ancestors,
               sd.dept_name,
               sd.order_num,
               sd.dept_type,
               sd.leader,
               sd.status as dept_status,
               sr.role_id,
               sr.role_name,
               sr.role_key,
               sr.role_sort,
               sr.data_scope,
               sr.status as role_status
        from sys_dept_role sdr
                 left join sys_dept sd on sdr.dept_id = sd.dept_id
                 left join sys_role sr on sr.role_id = sdr.role_id
    </sql>

    <select id="selectSysDeptRoleList" parameterType="com.microservices.system.domain.SysDeptRole"
            resultMap="SysDeptRoleResult">
        <include refid="selectSysDeptRoleVo"/>
    </select>

    <select id="selectSysDeptRoleByDeptId" parameterType="Long" resultMap="SysDeptRoleResult">
        <include refid="selectSysDeptRoleVo"/>
        where dept_id = #{deptId}
    </select>
    <select id="selectSysDeptRoleListByDeptId" parameterType="Long" resultMap="SysDeptRoleResult">
        <include refid="selectSysDeptRoleDetailVo"/>
        where sdr.dept_id = #{deptId}
        <if test="roleName != null and roleName != ''">
            <choose>
                <when test='roleName.contains("_")'>
                    <bind name="contentTmp" value='roleName.replaceAll("_","/_")'/>
                    AND sr.role_name like concat('%', #{ contentTmp }, '%') escape '/'
                </when>
                <otherwise>
                    AND sr.role_name like concat('%', #{roleName}, '%')
                </otherwise>
            </choose>
        </if>
        order by sr.role_sort
    </select>
    <select id="selectSysDeptRoleByDeptIdAndRoleId" resultMap="SysDeptRoleResult">
        <include refid="selectSysDeptRoleVo"/>
        where dept_id = #{deptId}
        and role_id = #{roleId}
    </select>
    <select id="countDeptRoleByRoleId" resultType="java.lang.Integer">
        SELECT COUNT(1)
        FROM sys_dept_role
        WHERE role_id = #{roleId}
    </select>
    <select id="selectSysDeptRoleByDeptIdAndRoleName" resultMap="SysDeptRoleResult">
        <include refid="selectSysDeptRoleDetailVo"/>
        where sdr.dept_id=#{deptId}
        and sr.role_name=#{roleName}
    </select>

    <insert id="insertSysDeptRole" parameterType="com.microservices.system.domain.SysDeptRole">
        insert into sys_dept_role
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deptId != null">dept_id,</if>
            <if test="roleId != null">role_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deptId != null">#{deptId},</if>
            <if test="roleId != null">#{roleId},</if>
        </trim>
    </insert>

    <update id="updateSysDeptRole" parameterType="com.microservices.system.domain.SysDeptRole">
        update sys_dept_role
        <trim prefix="SET" suffixOverrides=",">
            <if test="roleId != null">role_id = #{roleId},</if>
        </trim>
        where dept_id = #{deptId}
    </update>

    <delete id="deleteSysDeptRoleByDeptId" parameterType="Long">
        delete
        from sys_dept_role
        where dept_id = #{deptId}
    </delete>

    <delete id="deleteSysDeptRoleByDeptIds" parameterType="String">
        delete from sys_dept_role where dept_id in
        <foreach item="deptId" collection="array" open="(" separator="," close=")">
            #{deptId}
        </foreach>
    </delete>
    <delete id="deleteSysRoleListForSysDept">
        delete from sys_dept_role where dept_id = #{deptId} and role_id in
        <foreach item="roleId" collection="roleIds" open="(" separator="," close=")">
            #{roleId}
        </foreach>
    </delete>
</mapper>