<?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.system.SysRoleDao">
    <select id="findById" parameterType="java.lang.Integer" resultType="com.api.model.system.SysRole">
        select * from sys_role where id = #{id}
    </select>

    <select id="findByOrganizationId" parameterType="java.lang.Integer" resultType="com.api.model.system.SysRole">
        SELECT r.* from sys_organization as o,sys_organization_role as sor,sys_role r
        WHERE o.id = sor.organization_id AND r.id = sor.role_id AND o.id = #{organizationId}
    </select>

    <select id="findByIdentityId" parameterType="java.lang.Integer" resultType="com.api.model.system.SysRole">
        select r.* FROM sys_identity as i,sys_identity_role as ir,sys_role as r
        WHERE i.id = ir.identity_id AND r.id = ir.role_id and i.id = #{positionId}
    </select>

    <select id="roleList" parameterType="java.lang.Integer" resultType="com.api.vo.system.VoRole">
        select * from sys_role where parent_id = #{parentId}
    </select>

    <select id="findNameByRoleId" parameterType="java.lang.Integer" resultType="java.lang.String">
        select name from sys_role where id = #{id}
    </select>
    <select id="countUserByRoleId" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select count(*) from sys_user where FIND_IN_SET(#{roleId},role_id) > 0
    </select>
    <select id="findByRoleId" parameterType="java.lang.Integer" resultType="com.api.vo.businessManagement.VoRoleFBI">
        select sr1.*,sr2.name as parent_name
        from sys_role as sr1
        LEFT JOIN sys_role as sr2 on sr1.parent_id = sr2.id
        where sr1.id = #{roleId}
    </select>

    <insert id="insertRole" parameterType="com.api.model.system.SysRole">
        insert into sys_role(
            name,
            code,
            parent_id,
            action_id,
            create_id,
            create_date
        )values (
            #{name},
            #{code},
            #{parentId},
            #{actionId},
            #{createId},
            #{createDate}
        )
    </insert>

    <delete id="deleteRole" parameterType="java.lang.Integer">
        delete from sys_role where id = #{roleId}
    </delete>
    <delete id="deleteRoleJurisdictionByRoleId" parameterType="java.lang.Integer">
        delete from sys_role_jurisdiction where role_id = #{roleId}
    </delete>


    <update id="updateRole" parameterType="com.api.model.system.SysRole">
        update sys_role
        <set>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="code != null and code != ''">
                code = #{code},
            </if>
            <if test="parentId != null">
                parent_id = #{parentId},
            </if>
            modify_id = #{modifyId},
            modify_date = #{modifyDate}
        </set>
        where id = #{id}
    </update>



</mapper>