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

<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" 
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="com.house365.beans.entity.RoleEntity">
	<typeAlias alias="roleEntity"
		type="com.house365.beans.entity.RoleEntity" />
	<resultMap id="role"
		class="com.house365.beans.entity.RoleEntity">
		<result property="id" column="id" />
		<result property="code" column="code" />
		<result property="name" column="name" />
		<result property="deptId" column="deptId" />
		<result property="deptUrlPath" column="deptUrlPath" />
		<result property="enabled" column="enabled" />
		<result property="createUserid" column="createUserid" />
		<result property="createUsername" column="createUsername" />
		<result property="createTime" column="createTime" />
		<result property="createIp" column="createIp" />
		<result property="updateUserid" column="updateUserid" />
		<result property="updateUsername" column="updateUsername" />
		<result property="updateTime" column="updateTime" />
		<result property="updateIp" column="updateIp" />
		<result property="deptName" column="deptName" />
	</resultMap>

	<!-- 用于select查询公用抽取的列 -->
	<sql id="role.columns">
	    <![CDATA[
        id, code, name, deptId,deptUrlPath, enabled, createUserid, createUsername, createTime, createIp, updateUserid, updateUsername, updateTime, updateIp
	    ]]>
	</sql>

	<!-- 新增记录 -->
	<insert id="save" parameterClass="roleEntity">
	    <![CDATA[
	        INSERT INTO role (
	        	id, 
	        	code, 
	        	name, 
	        	deptId, 
	        	deptUrlPath,
	        	enabled, 
	        	createUserid, 
	        	createUsername, 
	        	createTime, 
	        	createIp, 
	        	updateUserid, 
	        	updateUsername, 
	        	updateTime, 
	        	updateIp
	        ) VALUES (
	        	#id#, 
	        	#code#, 
	        	#name#, 
	        	#deptId#, 
	        	#deptUrlPath#,
	        	#enabled#, 
	        	#createUserid#, 
	        	#createUsername#, 
	        	#createTime#, 
	        	#createIp#, 
	        	#updateUserid#, 
	        	#updateUsername#, 
	        	#updateTime#, 
	        	#updateIp#
	        )
	    ]]>
		<selectKey resultClass="java.lang.Integer" type="post"
			keyProperty="id">
			SELECT LAST_INSERT_ID()
		</selectKey>
	</insert>

	<!-- 通过ID修改 -->
	<update id="update" parameterClass="roleEntity">
	        UPDATE role 
				<dynamic prepend="SET">
				<isNotEmpty prepend="," property="code">
	        	  <![CDATA[
					code = #code#
		            ]]>
				</isNotEmpty>
	        	<isNotEmpty prepend="," property="name">
	        	  <![CDATA[
					name = #name#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="," property="deptId">
	        	  <![CDATA[
					deptId = #deptId#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="," property="deptUrlPath">
	        	  <![CDATA[
					deptUrlPath = #deptUrlPath#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="," property="enabled">
	        	  <![CDATA[
					enabled = #enabled#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="," property="createUserid">
	        	  <![CDATA[
					createUserid = #createUserid#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="," property="createUsername">
	        	  <![CDATA[
					createUsername = #createUsername#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="," property="createTime">
	        	  <![CDATA[
					createTime = #createTime#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="," property="createIp">
	        	  <![CDATA[
					createIp = #createIp#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="," property="updateUserid">
	        	  <![CDATA[
					updateUserid = #updateUserid#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="," property="updateUsername">
	        	  <![CDATA[
					updateUsername = #updateUsername#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="," property="updateTime">
	        	  <![CDATA[
					updateTime = #updateTime#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="," property="updateIp">
	        	  <![CDATA[
					updateIp = #updateIp#
		            ]]>
				</isNotEmpty>
				</dynamic>
	        WHERE 
		        id = #id#
	</update>

	<!-- 通过ID删除 -->
	<delete id="delete">
	    <![CDATA[
	        DELETE FROM role WHERE
	        id = #id#
	    ]]>
	</delete>

	<!-- 通过ID列表批量删除 -->
	<delete id="deleteByIds" parameterClass="java.lang.String">
	    <![CDATA[
	        DELETE FROM role WHERE
	        id IN ($ids$)
	    ]]>
	</delete>

	<!-- 通过ID查询 -->
	<select id="getById" resultMap="role">
		SELECT
		<include refid="role.columns" /> ,(select name from department d where d.id = r.deptId)  deptName
	    <![CDATA[
	        FROM role   r
	        WHERE 
		        id = #id#
	    ]]>
	</select>

	<!-- 用于分页查询抽取的公共的条件 -->
	<sql id="role.queryPage">
		<dynamic prepend="WHERE">
			<!-- 默认不开启实体作为条件 -->
			<!-- 
		    <isNotNull property="entity">
				<isNotEmpty prepend="AND" property="entity.id">
		            <![CDATA[
					id = #entity.id#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.code">
		            <![CDATA[
					code = #entity.code#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.name">
		            <![CDATA[
					name = #entity.name#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.dept">
		            <![CDATA[
					dept = #entity.dept#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.enabled">
		            <![CDATA[
					enabled = #entity.enabled#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.createUserid">
		            <![CDATA[
					createUserid = #entity.createUserid#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.createUsername">
		            <![CDATA[
					createUsername = #entity.createUsername#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.createTime">
		            <![CDATA[
					createTime = #entity.createTime#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.createIp">
		            <![CDATA[
					createIp = #entity.createIp#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.updateUserid">
		            <![CDATA[
					updateUserid = #entity.updateUserid#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.updateUsername">
		            <![CDATA[
					updateUsername = #entity.updateUsername#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.updateTime">
		            <![CDATA[
					updateTime = #entity.updateTime#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.updateIp">
		            <![CDATA[
					updateIp = #entity.updateIp#
		            ]]>
				</isNotEmpty>
			</isNotNull>
			 -->
		    <isNotNull property="searchFilters">
				<isNotEmpty prepend="AND" property="searchFilters.id">
		            <![CDATA[
					id $searchFilters.id.operation$ #searchFilters.id.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.code">
		            <![CDATA[
					code $searchFilters.code.operation$ #searchFilters.code.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.name">
		            <![CDATA[
					name $searchFilters.name.operation$ #searchFilters.name.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.deptId">
		            <![CDATA[
					deptId $searchFilters.deptId.operation$ #searchFilters.deptId.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.deptUrlPath">
		            <![CDATA[
					deptUrlPath $searchFilters.deptUrlPath.operation$ #searchFilters.deptUrlPath.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.enabled">
		            <![CDATA[
					enabled $searchFilters.enabled.operation$ #searchFilters.enabled.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.createUserid">
		            <![CDATA[
					createUserid $searchFilters.createUserid.operation$ #searchFilters.createUserid.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.createUsername">
		            <![CDATA[
					createUsername $searchFilters.createUsername.operation$ #searchFilters.createUsername.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.createTime">
		            <![CDATA[
					createTime $searchFilters.createTime.operation$ #searchFilters.createTime.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.createIp">
		            <![CDATA[
					createIp $searchFilters.createIp.operation$ #searchFilters.createIp.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.updateUserid">
		            <![CDATA[
					updateUserid $searchFilters.updateUserid.operation$ #searchFilters.updateUserid.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.updateUsername">
		            <![CDATA[
					updateUsername $searchFilters.updateUsername.operation$ #searchFilters.updateUsername.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.updateTime">
		            <![CDATA[
					updateTime $searchFilters.updateTime.operation$ #searchFilters.updateTime.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.updateIp">
		            <![CDATA[
					updateIp $searchFilters.updateIp.operation$ #searchFilters.updateIp.value#
		            ]]>
				</isNotEmpty>
			</isNotNull>
		</dynamic>
		<dynamic prepend="ORDER BY">
			<isNotNull property="orderColumn">
		        <![CDATA[
				$orderColumn$
				]]>
				<isNotNull property="orderMode">
				$orderMode$
				</isNotNull>
			</isNotNull>
		</dynamic>
	</sql>

	<!-- 为分页查询符合条件的记录数 -->
	<select id="queryByPage_count" resultClass="int">
		SELECT count(*) FROM role
		<include refid="role.queryPage" />
	</select>

	<!-- 分页查询指定页记录 -->
	<select id="queryByPage" resultMap="role">
		SELECT
		<include refid="role.columns" />,(select name from department d where d.id = r.deptId)  deptName
	    <![CDATA[
	        FROM role  r
	    ]]>
		<include refid="role.queryPage" />
		<![CDATA[
		    LIMIT #paging.startRecord#, #paging.pageSize#
        ]]>
	</select>

	<!-- 查询所有记录 -->
	<select id="queryAll" resultMap="role">
		SELECT
		<include refid="role.columns" />,(select name from department d where d.id = r.deptId)  deptName
	    <![CDATA[
		    FROM role r
	    ]]>
	    <include refid="role.queryPage" />
	</select>
	
	<select id="getRolesByUserId" resultClass="roleEntity">
		SELECT
		r.id, r.code, r.name, r.deptId,r.deptUrlPath, r.enabled
	    <![CDATA[
	        FROM role  r left join user_role ur on r.id = ur.roleId where ur.userId = #userId#
	    ]]>
	</select>
	
</sqlMap>