<?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.ics.system.mapper.RoleMapper">

	<resultMap type="com.ics.system.domain.Role" id="RoleResult">
		<id     property="id"       	column="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="isDefault" 	column="is_default"		/>
		<result property="version"      column="version"        />
        <result property="deleteFlag"   column="delete_flag"    />
		<result property="createBy"     column="create_by"      />
		<result property="createTime"   column="create_time"    />
		<result property="updateBy"     column="update_by"      />
		<result property="updateTime"   column="update_time"    />
		<result property="remark"       column="remark"         />
		<result property="tenantId"     column="tenant_id"      />
	</resultMap>
	
	<sql id="selectRoleContactVo">
        SELECT DISTINCT r.id,
						r.role_name,
						r.role_key,
						r.role_sort,
						r.data_scope,
						r.status,
						r.is_default,
						r.create_time,
						r.remark
		FROM   sys_role r
			   LEFT JOIN sys_user_role ur
					  ON ur.role_id = r.id
			   LEFT JOIN sys_user u
					  ON u.id = ur.user_id
			   LEFT JOIN sys_dept d
					  ON u.dept_id = d.id
    </sql>
    
    <sql id="selectRoleVo">
		SELECT r.id,
			   r.role_name,
			   r.role_key,
			   r.role_sort,
			   r.data_scope,
			   r.status,
			   r.is_default,
			   r.create_time,
			   r.remark
		FROM   sys_role r
	</sql>
	
	<select id="selectRoleList" parameterType="com.ics.system.domain.Role" resultMap="RoleResult">
		<include refid="selectRoleContactVo"/>
		<where>
			<if test="roleName != null and roleName != ''">
				AND r.role_name LIKE concat('%', #{roleName}, '%')
			</if>
			<if test="status != null and status != ''">
				AND r.status = #{status}
			</if>
			<if test="roleKey != null and roleKey != ''">
				AND r.role_key LIKE concat('%', #{roleKey}, '%')
			</if>
			<if test="dataScope != null and dataScope != ''">
				AND r.data_scope = #{dataScope}
			</if>
			<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
				AND date_format(r.create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
			</if>
			<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
				AND date_format(r.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
			</if>

			<!-- 数据范围过滤 -->
			${params.dataScope}
		</where>

	</select>

	<select id="selectRolesByUserId" parameterType="Long" resultMap="RoleResult">
		<include refid="selectRoleContactVo"/>
		WHERE ur.user_id = #{userId}
	</select>
	
	<select id="selectRoleById" parameterType="Long" resultMap="RoleResult">
		<include refid="selectRoleVo"/>
		WHERE r.id = #{id}
	</select>
	
	<select id="checkRoleNameUnique" parameterType="String" resultMap="RoleResult">
		<include refid="selectRoleVo"/>
		 WHERE r.role_name=#{roleName}
	</select>
	
	<select id="checkRoleKeyUnique" parameterType="String" resultMap="RoleResult">
		<include refid="selectRoleVo"/>
		 WHERE r.role_key=#{roleKey}
	</select>

	<select id="findDefault" resultMap="RoleResult">
		<include refid="selectRoleVo"/>
		WHERE r.is_default = TRUE
	</select>

	<select id="findParkAdminRole" resultMap="RoleResult">
		<include refid="selectRoleVo"/>
		WHERE r.role_key = 'park_admin'
	</select>

	<delete id="deleteRoleById" parameterType="Long">
 		DELETE FROM sys_role WHERE id = #{id}
 	</delete>
 	
 	<delete id="deleteRoleByIds" parameterType="Long">
		DELETE FROM sys_role WHERE id IN
 		<foreach collection="array" item="id" open="(" separator="," close=")">
 			#{id}
        </foreach> 
 	</delete>
 	
 	<insert id="insertRole" parameterType="com.ics.system.domain.Role" useGeneratedKeys="true" keyProperty="id">
 		INSERT INTO sys_role(
 			<if test="id != null and id != 0">id,</if>
 			<if test="roleName != null and roleName != ''">role_name,</if>
 			<if test="roleKey != null and roleKey != ''">role_key,</if>
 			<if test="roleSort != null and roleSort != ''">role_sort,</if>
 			<if test="dataScope != null and dataScope != ''">data_scope,</if>
 			<if test="status != null and status != ''">status,</if>
			<if test="isDefault != null">is_default,</if>
 			<if test="remark != null and remark != ''">remark,</if>
 		    <if test="tenantId != null and tenantId != ''">tenant_id,</if>
 			<if test="createBy != null and createBy != ''">create_by,</if>
 			create_time
 		) VALUES (
 			<if test="id != null and id != 0">#{id},</if>
 			<if test="roleName != null and roleName != ''">#{roleName},</if>
 			<if test="roleKey != null and roleKey != ''">#{roleKey},</if>
 			<if test="roleSort != null and roleSort != ''">#{roleSort},</if>
 			<if test="dataScope != null and dataScope != ''">#{dataScope},</if>
 			<if test="status != null and status != ''">#{status},</if>
			<if test="isDefault != null">#{isDefault},</if>
 			<if test="remark != null and remark != ''">#{remark},</if>
 		    <if test="tenantId != null">#{tenantId},</if>
 			<if test="createBy != null and createBy != ''">#{createBy},</if>
 			sysdate()
 		)
	</insert>

	<update id="updateRole" parameterType="com.ics.system.domain.Role">
		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 and roleSort != ''">role_sort = #{roleSort},</if>
			<if test="dataScope != null and dataScope != ''">data_scope = #{dataScope},</if>
			<if test="status != null and status != ''">status = #{status},</if>
			<if test="isDefault != null ">is_default = #{isDefault},</if>
			<if test="remark != null">remark = #{remark},</if>
			<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
			update_time = sysdate()
		</set>
		WHERE id = #{id}
	</update>

	<update id="clearDefault">
        UPDATE sys_role SET is_default = FALSE WHERE is_default = TRUE
    </update>

</mapper> 