<?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">
<!-- UIWS system 이식: 부서-권한 매핑(tb_uiws_dept_role). -->
<mapper namespace="com.zioinfo.esn.uiws.system.mapper.DeptRoleMapper">

    <select id="findRoleIdsByDeptId" resultType="string">
        SELECT role_id FROM tb_uiws_dept_role WHERE dept_id = #{deptId} ORDER BY role_id ASC
    </select>

    <select id="exists" resultType="boolean">
        SELECT EXISTS(SELECT 1 FROM tb_uiws_dept_role WHERE dept_id = #{deptId} AND role_id = #{roleId})
    </select>

    <select id="existsByRoleId" resultType="boolean">
        SELECT EXISTS(SELECT 1 FROM tb_uiws_dept_role WHERE role_id = #{roleId})
    </select>

    <insert id="insert">
        INSERT INTO tb_uiws_dept_role (dept_id, role_id, created_by, created_at)
        VALUES (#{deptId}, #{roleId}, #{actor}, now())
        ON CONFLICT (dept_id, role_id) DO NOTHING
    </insert>

    <delete id="delete">
        DELETE FROM tb_uiws_dept_role WHERE dept_id = #{deptId} AND role_id = #{roleId}
    </delete>
</mapper>
