<?xml version="1.0" encoding="UTF-8"?>

<!--
  ~
  ~      Copyright (c) 2018-2025, lengleng All rights reserved.
  ~
  ~  Redistribution and use in source and binary forms, with or without
  ~  modification, are permitted provided that the following conditions are met:
  ~
  ~ Redistributions of source code must retain the above copyright notice,
  ~  this list of conditions and the following disclaimer.
  ~  Redistributions in binary form must reproduce the above copyright
  ~  notice, this list of conditions and the following disclaimer in the
  ~  documentation and/or other materials provided with the distribution.
  ~  Neither the name of the yifu4cloud.com developer nor the names of its
  ~  contributors may be used to endorse or promote products derived from
  ~  this software without specific prior written permission.
  ~  Author: lengleng (wangiegie@gmail.com)
  ~
  -->

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.yifu.cloud.plus.v1.permission.mapper.SysRoleInfoMapper">

  <resultMap id="sysRoleInfoMap" type="com.yifu.cloud.plus.v1.yifu.permission.entity.SysRoleInfo">
    <id property="fdId" column="fd_id"/>
    <result property="roleName" column="role_name"/>
    <result property="remark" column="remark"/>
    <result property="status" column="status"/>
    <result property="deleteFlag" column="delete_flag"/>
    <result property="dataAuth" column="data_auth"/>
    <result property="createBy" column="CREATE_BY"/>
    <result property="createName" column="CREATE_NAME"/>
    <result property="createTime" column="CREATE_TIME"/>
    <result property="updateBy" column="UPDATE_BY"/>
    <result property="updateTime" column="UPDATE_TIME"/>
  </resultMap>


	<resultMap id="HcmRoleMap" type="com.yifu.cloud.plus.v1.yifu.admin.api.vo.HcmRoleInfoVo">
		<result column="fd_id" property="roleId"/>
		<result column="role_name" property="roleName"/>
	</resultMap>
    <sql id="Base_Column_List">
                a.fd_id,
                a.role_name,
                a.remark,
                a.status,
                a.delete_flag,
                a.data_auth,
                a.CREATE_BY,
                a.CREATE_NAME,
                a.CREATE_TIME,
                a.UPDATE_BY,
                a.UPDATE_TIME
    </sql>
    <sql id="sysRoleInfo_where">
        <if test="sysRoleInfo != null">
                    <if test="sysRoleInfo.fdId != null and sysRoleInfo.fdId.trim() != ''">
                        AND a.fd_id = #{sysRoleInfo.fdId}
                    </if>
                    <if test="sysRoleInfo.roleName != null and sysRoleInfo.roleName.trim() != ''">
                        AND a.role_name like concat('%',#{sysRoleInfo.roleName},'%')
                    </if>
                    <if test="sysRoleInfo.remark != null and sysRoleInfo.remark.trim() != ''">
                        AND a.remark = #{sysRoleInfo.remark}
                    </if>
                    <if test="sysRoleInfo.status != null">
                        AND a.status = #{sysRoleInfo.status}
                    </if>
                    <if test="sysRoleInfo.deleteFlag != null">
                        AND a.delete_flag = #{sysRoleInfo.deleteFlag}
                    </if>
                    <if test="sysRoleInfo.dataAuth != null and sysRoleInfo.dataAuth.trim() != ''">
                        AND a.data_auth = #{sysRoleInfo.dataAuth}
                    </if>
                    <if test="sysRoleInfo.createBy != null and sysRoleInfo.createBy.trim() != ''">
                        AND a.CREATE_BY = #{sysRoleInfo.createBy}
                    </if>
                    <if test="sysRoleInfo.createName != null and sysRoleInfo.createName.trim() != ''">
                        AND a.CREATE_NAME = #{sysRoleInfo.createName}
                    </if>
                    <if test="sysRoleInfo.createTime != null">
                        AND a.CREATE_TIME = #{sysRoleInfo.createTime}
                    </if>
					<if test="sysRoleInfo.createTimeStart != null">
						AND a.CREATE_TIME <![CDATA[ >= ]]> #{sysRoleInfo.createTimeStart}
					</if>
					<if test="sysRoleInfo.createTimeEnd != null">
						AND a.CREATE_TIME <![CDATA[ <= ]]> #{sysRoleInfo.createTimeEnd}
					</if>
                    <if test="sysRoleInfo.updateBy != null and sysRoleInfo.updateBy.trim() != ''">
                        AND a.UPDATE_BY = #{sysRoleInfo.updateBy}
                    </if>
                    <if test="sysRoleInfo.updateTime != null">
                        AND a.UPDATE_TIME = #{sysRoleInfo.updateTime}
                    </if>
        </if>
    </sql>
    <!--sysRoleInfo简单分页查询-->
    <select id="getSysRoleInfoPage" resultMap="sysRoleInfoMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM sys_role_info a
        <where>
			a.delete_flag = 0
            <include refid="sysRoleInfo_where"/>
        </where>
		order by a.CREATE_TIME desc
    </select>

	<!-- 根据用户Id获取所有的角色的权限-->
    <select id="getAllRoleDataAuthByUserId" resultType="java.lang.String">
		SELECT
			a.data_auth
		FROM sys_role_info a
		left join sys_user_role_res r on r.role_id = a.fd_id
		where a.delete_flag = 0 and r.login_name = #{loginName}
    </select>

	<!-- 根据角色Id获取所有的角色的权限-->
    <select id="getRoleDataAuthByRoleId" resultType="java.lang.String">
		SELECT
			a.data_auth
		FROM sys_role_info a
		where a.delete_flag = 0 and a.fd_id in
		<foreach item="idStr" index="index" collection="roleIdList" open="(" separator="," close=")">
			#{idStr}
		</foreach>
    </select>

	<!-- 查找名称是否存在-->
    <select id="checkExistenceRole" resultType="java.lang.Integer">
		SELECT
			count(1)
		FROM sys_role_info a
		where a.delete_flag = 0 and a.role_name = #{roleName}
		<if test="roleId != null and roleId.trim() != ''">
			AND a.fd_id != #{roleId}
		</if>
    </select>


	<select id="getRoleList" resultMap="HcmRoleMap">
		SELECT
			a.fd_id,
			a.role_name
		FROM
			sys_role_info a
		where a.status = 0 and a.delete_flag = 0
	</select>
</mapper>
