<?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.api.manage.dao.businessManagement.FunctionAuthorityDao">
    <select id="list" parameterType="com.api.model.businessManagement.SearchFunctionAuthority" resultType="com.api.vo.businessManagement.VoFunctionAuthority">
        select su.*,so.name as organization_name,si.name as position_name
        from sys_user as su,sys_organization as so,sys_identity as si
        <where>
            su.organization_id = so.id
            and su.position_id = si.id
            and su.is_delete = 1
            <if test="organizationId != null">
                and su.organization_id_path like concat('%:',#{organizationId},':%')
            </if>
            <if test="roleId != null">
                and FIND_IN_SET(#{roleId},su.role_id) > 0
            </if>
        </where>
    </select>

    <select id="listJurisdiction" parameterType="com.api.model.businessManagement.RoleIdAndParentId" resultType="com.api.vo.businessManagement.VoListJurisdiction">
        select sj.*,
        (case srj.id is null when true then 0 else 1 end) as is_check
        from sys_jurisdiction as sj LEFT JOIN sys_role_jurisdiction as srj
        on sj.id = srj.jurisdiction_id and srj.role_id = #{roleId}
        where sj.parent_id = #{parentId}
        order By sj.sort
    </select>
    <select id="countByRoleIdJurisdictionId" parameterType="com.api.model.businessManagement.RoleIdAndJurisdictionId" resultType="java.lang.Integer">
        select count(*) from sys_role_jurisdiction where role_id = #{roleId} and jurisdiction_id = #{jurisdictionId}
    </select>

    <delete id="deleteJurisdictionByRoleId" parameterType="java.lang.Integer">
        delete from sys_role_jurisdiction where role_id = #{roleId}
    </delete>
    <delete id="deleteByRoleIdJurisdictionId" parameterType="com.api.model.businessManagement.RoleIdAndJurisdictionId">
        delete from sys_role_jurisdiction where role_id = #{roleId} and jurisdiction_id = #{jurisdictionId}
    </delete>

    <insert id="insertJurisdiction" parameterType="com.api.model.businessManagement.RoleIdAndJurisdictionId">
        insert into sys_role_jurisdiction(
            role_id,
            jurisdiction_id,
            action_id
        )values (
            #{roleId},
            #{jurisdictionId},
            1
        )
    </insert>

</mapper>