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

	<resultMap type="Role" id="roleResultMap">
		<id column="ROLE_ID" property="ROLE_ID" />
		<result column="ROLE_NAME" property="ROLE_NAME" />
		<result column="RIGHTS" property="RIGHTS" />
		<result column="SORT" property="SORT" />
	</resultMap>

	<!--表名 -->
	<sql id="tableName">
		SYS_ROLE
	</sql>

	<!-- 字段 -->
	<sql id="Field">
		ROLE_ID,
		ROLE_NAME,
		RIGHTS,
		PARENT_ID,
		ADD_QX,
		DEL_QX,
		EDIT_QX,
		CHA_QX,
		RNUMBER,
		SORT,
		ISMAIN,
	    HAS_PLS
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
        #{ROLE_ID},
        #{ROLE_NAME},
        #{RIGHTS},
        #{PARENT_ID},
        #{ADD_QX},
        #{DEL_QX},
        #{EDIT_QX},
        #{CHA_QX},
        #{RNUMBER},
        #{SORT},
        #{ISMAIN},
        #{HAS_PLS}
    </sql>

	<!-- 通过id查找 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include>
		where ROLE_ID = #{ROLE_ID}
	</select>

	<!-- 通过id查找 返回角色对象)-->
	<select id="getRoleById" parameterType="String" resultMap="roleResultMap">
		select * from
		<include refid="tableName"></include>
		where ROLE_ID=#{ROLE_ID}
	</select>

	<!-- 通过编码查找 -->
	<select id="getRoleByRnumber" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include>
		where RNUMBER = #{RNUMBER}
	</select>

	<!-- 列出此组下的角色 -->
	<select id="listAllRolesByPId" resultMap="roleResultMap">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include>
		where
			PARENT_ID = #{ROLE_ID}
		ORDER BY SORT
	</select>

	<!-- 列出此组下的角色 -->
	<select id="listAllRolesById" resultMap="roleResultMap">
		select
		f.*
		from
		<include refid="tableName"></include> f
		left join sys_role p on p.ROLE_ID = f.PARENT_ID
		where p.ISMAIN = '1'
		ORDER BY SORT
	</select>

	<select id="getPdByParIdAndName" parameterType="pd" resultType="pd">
		select *
		from
			sys_role
		where
			PARENT_ID = #{PARENT_ID}
			and ROLE_NAME = #{NAME}
	</select>

	<!-- 添加 -->
	<insert id="add" parameterType="pd">
		insert into
		<include refid="tableName"></include>
		(
		<include refid="Field"></include>
		) values (
		<include refid="FieldValue"></include>
		)
	</insert>

	<!-- 保存修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set ROLE_NAME = #{ROLE_NAME}

		<if test="SORT != null and SORT != ''">
			,SORT = #{SORT}
		</if>
		<if test="ISMAIN != null and ISMAIN != ''">
			,ISMAIN = #{ISMAIN}
		</if>
		<if test="HAS_PLS != null and HAS_PLS != ''">
			,HAS_PLS = #{HAS_PLS}
		</if>
		where ROLE_ID = #{ROLE_ID}
	</update>

	<!-- 删除角色  -->
	<delete id="deleteRoleById" parameterType="String">
		delete from
		<include refid="tableName"></include>
		where ROLE_ID=#{ROLE_ID}
	</delete>

	<!-- 给当前角色附加菜单权限  -->
	<update id="updateRoleRights" parameterType="role">
		update
		<include refid="tableName"></include>
		set RIGHTS=#{RIGHTS}
		where ROLE_ID=#{ROLE_ID}
	</update>

	<!-- 给全部子角色加菜单权限 -->
	<update id="setAllRights" parameterType="pd">
		update
		<include refid="tableName"></include>
		set RIGHTS=#{rights}
		where PARENT_ID=#{ROLE_ID}
	</update>

	<!-- 新增权限 -->
	<update id="addQx" parameterType="pd">
		update
		<include refid="tableName"></include>
		set ADD_QX=#{value}
		where ROLE_ID=#{ROLE_ID}
	</update>

	<!-- 删除权限 -->
	<update id="delQx" parameterType="pd">
		update
		<include refid="tableName"></include>
		set DEL_QX=#{value}
		where ROLE_ID=#{ROLE_ID}
	</update>

	<!-- 修改权限 -->
	<update id="editQx" parameterType="pd">
		update
		<include refid="tableName"></include>
		set EDIT_QX=#{value}
		where ROLE_ID=#{ROLE_ID}
	</update>

	<!-- 查看权限 -->
	<update id="chaQx" parameterType="pd">
		update
		<include refid="tableName"></include>
		set CHA_QX=#{value}
		where ROLE_ID=#{ROLE_ID}
	</update>

	<update id="updateRoleNavs" parameterType="pd">
		update
		<include refid="tableName"></include>
		set NAV_QX=#{value}
		where ROLE_ID=#{ROLE_ID}
	</update>

	<!-- 通过角色ID数组获取角色列表 -->
	<select id="listAllRolesByArryROLE_ID" parameterType="String" resultType="Role" >
		select
			RNUMBER
		from
			<include refid="tableName"></include>
		where
			ROLE_ID in
  			 <foreach item="item" index="index" collection="array" open="(" separator="," close=")">
                  #{item}
             </foreach>
	</select>

	<!-- 角色列表(弹窗选择用) -->
	<select id="roleWindowlistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include>
		where 1=1
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					ROLE_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				or
					RNUMBER LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				)
		</if>
		<if test="pd.ROLE_ID != null and pd.ROLE_ID != ''">
			and PARENT_ID = #{pd.ROLE_ID}
		</if>
		ORDER BY RNUMBER
	</select>

	<!-- 查询角色分组list -->
	<select id="getGroupList" parameterType="pd" resultType="pd">
		select
			<include refid="Field"></include>,
			0 NUMBER
		from
			<include refid="tableName"></include>
		where
			PARENT_ID = '0'
			ORDER BY SORT
	</select>

	<!-- 查询分组内的角色 -->
	<select id="getRoleListByPid" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
		1 = 1
		<if test="PARENT_ID != null and PARENT_ID != ''">
			and f.PARENT_ID = #{PARENT_ID}
		</if>

		<if test="ISMAIN != null and ISMAIN != ''">
			and f.ISMAIN = #{ISMAIN}
		</if>

	</select>

	<!-- 通过上级ID和是否主角色查找 -->
	<select id="getRoleByPidAndMain" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include>
		where
			PARENT_ID = #{PARENT_ID}
			AND ISMAIN = #{ISMAIN}
	</select>

	<!--代理机构管理-查询角色组-->
	<select id="getGroupListForAgency" parameterType="pd" resultType="pd">
		select
			sr.ROLE_ID,
			sr.ROLE_NAME,
			sr.RIGHTS,
			sr.PARENT_ID,
			sr.ADD_QX,
			sr.DEL_QX,
			sr.EDIT_QX,
			sr.CHA_QX,
			sr.RNUMBER,
			sr.SORT,
			sr.ISMAIN,
			0 NUMBER
		from
		    sys_role sr
		where
		    sr.PARENT_ID = '0' and sr.ISMAIN = '0'
	</select>

	<select id="getMarketingGroupList" resultType="com.zcloud.entity.PageData">
		select
		<include refid="Field"></include>,
		0 NUMBER
		from
		<include refid="tableName"></include>
		where
		PARENT_ID = '0'
		and ISMAIN = '0'
		ORDER BY SORT
	</select>
</mapper>
