<?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.hzs.system.sys.mapper.SysRoleMapper">

    <resultMap type="com.hzs.common.domain.system.sys.SysRole" id="SysRoleResult">
        <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="status"/>
        <result property="delFlag" column="DEL_FLAG"/>
        <result property="pkCreator" column="PK_CREATOR"/>
        <result property="creationTime" column="CREATION_TIME"/>
        <result property="pkModified" column="PK_MODIFIED"/>
        <result property="modifiedTime" column="MODIFIED_TIME"/>
    </resultMap>

    <!-- 根据条件分页查询角色数据 -->
    <select id="selectRoleList" resultMap="SysRoleResult">
        select * from sys_role r
        where r.del_flag = 0
        <if test="param.roleName != null and param.roleName != ''">
            AND r.role_name like #{param.roleName} || '%'
        </if>
        <if test="param.roleKey != null and param.roleKey != ''">
            AND r.role_key like #{param.roleKey} || '%'
        </if>
        <if test="param.status != null">
            AND r.status = #{param.status}
        </if>
        <if test="param.creationTime != null and param.creationTime.length > 1">
            and r.creation_time >= #{param.creationTime[0]}
            and #{param.creationTime[1]} >= r.creation_time
        </if>
        order by r.role_sort
    </select>

    <!-- 通过角色ID查询角色 -->
    <select id="selectRoleById" parameterType="Long" resultMap="SysRoleResult">
        select *
        from sys_role r
        where r.role_id = #{roleId}
          and r.del_flag = 0
    </select>

    <!-- 校验角色名称是否唯一 -->
    <select id="checkRoleNameUnique" parameterType="String" resultMap="SysRoleResult">
        select *
        from sys_role r
        where r.role_name = #{roleName}
          and r.del_flag = 0
          and rownum = 1
    </select>

    <!-- 校验角色权限是否唯一 -->
    <select id="checkRoleKeyUnique" parameterType="String" resultMap="SysRoleResult">
        select *
        from sys_role r
        where r.role_key = #{roleKey}
          and r.del_flag = 0
          and rownum = 1
    </select>

    <!-- 新增角色信息 -->
    <insert id="insertRole" parameterType="com.hzs.common.domain.system.sys.SysRole"
            useGeneratedKeys="true" keyProperty="roleId">
        <selectKey resultType="java.lang.Long" order="BEFORE" keyProperty="roleId">
            SELECT SYS_ROLE_ROLE_ID_SEQ.nextval AS role_id FROM dual
        </selectKey>
        insert into SYS_ROLE (
        <if test="roleId != null and roleId != 0">role_id,</if>
        <if test="roleName != null and roleName != ''">role_name,</if>
        <if test="roleKey != null and roleKey != ''">role_key,</if>
        <if test="roleSort != null">role_sort,</if>
        <if test="dataScope != null">data_scope,</if>
        <if test="status != null">status,</if>
        <if test="pkCreator != null">pk_creator,</if>
        creation_time
        ) values (
        <if test="roleId != null and roleId != 0">#{roleId},</if>
        <if test="roleName != null and roleName != ''">#{roleName},</if>
        <if test="roleKey != null and roleKey != ''">#{roleKey},</if>
        <if test="roleSort != null">#{roleSort},</if>
        <if test="dataScope != null">#{dataScope},</if>
        <if test="status != null">#{status},</if>
        <if test="pkCreator != null ">#{pkCreator},</if>
        sysdate
        )
    </insert>

    <!-- 修改角色信息 -->
    <update id="updateRole" parameterType="com.hzs.common.domain.system.sys.SysRole">
        update sys_role
        <set>
            <if test="roleName != null and roleName != ''">role_name = #{roleName},</if>
            <if test="roleKey != null and roleKey != ''">role_key = #{roleKey},</if>
            <if test="roleSort != null">role_sort = #{roleSort},</if>
            <if test="dataScope != null">data_scope = #{dataScope},</if>
            <if test="status != null ">status = #{status},</if>
            <if test="pkModified != null">PK_MODIFIED = #{pkModified},</if>
            MODIFIED_TIME = sysdate
        </set>
        where role_id = #{roleId}
    </update>

    <!-- 批量删除角色信息 -->
    <delete id="deleteRoleByIds" parameterType="Long">
        update sys_role
        set del_flag = 1 ,
        PK_MODIFIED = #{userId},
        MODIFIED_TIME = sysdate
        where role_id in
        <foreach collection="roleIds" item="roleId" open="(" separator="," close=")">
            #{roleId}
        </foreach>
    </delete>

</mapper>
