<?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.hotent.uc.dao.RoleDao">
	<resultMap id="BaseResultMap" type="com.hotent.uc.model.Role">
		<id property="id" column="ID_" jdbcType="VARCHAR"/>
		<result property="name" column="NAME_" jdbcType="VARCHAR"/>
		<result property="code" column="CODE_" jdbcType="VARCHAR"/>
		<result property="enabled" column="ENABLED_" jdbcType="NUMERIC"/>
		<result property="description" column="DESCRIPTION_" jdbcType="VARCHAR"/>
		
		<result property="isDelete" column="IS_DELE_" jdbcType="VARCHAR"/>
		<result property="version" column="VERSION_" jdbcType="NUMERIC"/>
		<result property="updateTime" column="UPDATE_TIME_" jdbcType="TIMESTAMP"/>
		<result property="createBy" column="CREATE_BY_" jdbcType="VARCHAR"/>
		<result property="updateBy" column="UPDATE_BY_" jdbcType="VARCHAR"/>
		<result property="createOrgId" column="CREATE_ORG_ID_" jdbcType="VARCHAR"/>
		<result property="createTime" column="create_time_" jdbcType="TIMESTAMP"/>
	</resultMap>
	
	<parameterMap id="Role" type="com.hotent.uc.model.Role" />
	
	<sql id="Base_Column_List">
		ID_,NAME_,CODE_,ENABLED_,DESCRIPTION_,UPDATE_TIME_,IS_DELE_,VERSION_,CREATE_BY_,UPDATE_BY_,CREATE_ORG_ID_
	</sql>

	<update id="updateRole" parameterType="com.hotent.uc.model.Role">
		UPDATE UC_ROLE SET NAME_ = #{name},CODE_=#{code},ENABLED_=#{enabled},DESCRIPTION_=#{description},IS_DELE_=#{isDelete}
		WHERE ID_ = #{id}
	</update>
	
	<select id="getByCode"   parameterType="java.lang.String" resultMap="BaseResultMap">
		SELECT <include refid="Base_Column_List" /> FROM UC_ROLE 
		WHERE 
		CODE_=#{code} AND IS_DELE_!='1'
	</select>

	<select id="getAllByCode"   parameterType="java.lang.String" resultMap="BaseResultMap">
		SELECT <include refid="Base_Column_List" /> FROM UC_ROLE
		WHERE
		CODE_=#{code}
	</select>
	
	
	<delete id="removePhysical" >
		DELETE FROM UC_ROLE WHERE IS_DELE_='1'
	</delete>
	
	<select id="getListByUserId" parameterType="java.util.Map" resultMap="BaseResultMap">
		SELECT r.ID_,r.NAME_,r.CODE_  from UC_USER_ROLE ur  INNER JOIN UC_USER u on ur.USER_ID_=u.ID_
		INNER JOIN UC_ROLE  r on  ur.ROLE_ID_=r.ID_
		and ur.USER_ID_= #{userId}
		WHERE r.IS_DELE_!='1' AND ur.IS_DELE_!='1'
		<if test="!roleIds.isEmpty()">
			UNION SELECT r1.ID_, r1.NAME_, r1.CODE_ FROM uc_role r1 where  r1.ID_ IN
			<foreach collection="roleIds" separator="," open="(" close=")" item="roleId">
				#{roleId}
			</foreach>
			AND r1.IS_DELE_ != '1'
		</if>
	</select>

	<select id="getListByAccount" parameterType="java.lang.String" resultMap="BaseResultMap" >
		SELECT DISTINCT r.* ,u.* FROM UC_ROLE r LEFT JOIN uc_user_role ur
		ON r.ID_ = ur.ROLE_ID_  LEFT JOIN uc_user u ON u.ID_ = ur.USER_ID_ WHERE
			u.ACCOUNT_ = #{account}  AND u.IS_DELE_!='1' AND ur.IS_DELE_!='1'
		AND r.IS_DELE_!='1'
	</select>

	<select id="getByIds" resultMap="BaseResultMap">
		SELECT r.* from uc_role r WHERE
		<if test="ids.isEmpty()">1=2</if>
		<if test="!ids.isEmpty()">
			r.ID_ IN
			<foreach collection="ids" separator="," open="(" close=")"  item="id">
				#{id}
			</foreach>
		</if>
		AND r.IS_DELE_ != '1'
	</select>

	<select id="getRoleByIdsAndExtOrgId" parameterType="string" resultType="map">
		SELECT distinct  r.ID_, r.NAME_, r.CODE_, o.ID_ as ORG_ID_, o.NAME_ as ORG_NAME_, o.PATH_NAME_ as ORG_PATH_  from uc_role r, uc_org_auth_ext t, UC_ORG o
		where t.AUTH_TYPE_ = '1' and r.ID_ = t.ROLE_ID_ and o.ID_ = t.ORG_ID_
		and t.ORG_ID_ in (
			SELECT u.ID_ from UC_ORG u WHERE u.IS_DELE_='0' and u.PATH_ like '${orgPath}%'
		)
		<if test="!ids.isEmpty()">
			and r.ID_ IN
			<foreach collection="ids" separator="," open="(" close=")"  item="id">
				#{id}
			</foreach>
		</if>
		AND r.IS_DELE_ = '0' AND o.IS_DELE_ = '0'
		order by o.PATH_, o.ID_, r.ID_
	</select>
	
	<select id="getOrgRoleList" resultMap="BaseResultMap">
		select r.* from uc_role r INNER JOIN uc_org_role o ON r.ID_ = o.ROLE_ID_ 
		where r.IS_DELE_!='1' AND o.IS_DELE_!='1'
		<if test="orgId!=null">
			and o.ORG_ID_ = #{orgId}
		</if>
		<if test="orgCode!=null">
			and o.CODE_ = #{orgCode}
		</if>
		<if test="roleCode!=null">
			and r.CODE_ = #{roleCode}
		</if>
		<if test="roleId!=null">
			and r.ID_ = #{roleId}
		</if>
		<if test="isInherit!=null">
			and o.IS_INHERIT_ = #{isInherit}
		</if>
	</select>

	<select id="getOrgRoleListNotCode" parameterType="java.lang.String" resultMap="BaseResultMap" >
		SELECT <include refid="Base_Column_List" />
		FROM uc_role WHERE CODE_ != #{code} and IS_DELE_='0'
	</select>

	<select id="getCountByCode" parameterType="java.lang.String" resultType="java.lang.Integer">
		SELECT count(1) FROM UC_ROLE
		WHERE CODE_ = #{code} and IS_DELE_='0'
	</select>

	<select id="queryRoleByExtOrgPath" parameterType="java.lang.String" resultMap="BaseResultMap" >
		SELECT * FROM (
			SELECT ro.*
			FROM uc_role ro, uc_org_auth_ext ext
			where ro.ID_ = ext.ROLE_ID_ and ro.IS_DELE_ = 0 and ro.ENABLED_ = 1
			and ext.ORG_ID_ in (
				select org.ID_ from uc_org org where org.PATH_ like CONCAT(#{path}, '%')
			)
		) t
		<if test="ew.customSqlSegment!=null">
			${ew.customSqlSegment}
		</if>
	</select>
</mapper>