<?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.RolePermissionEntity">
	<typeAlias alias="rolePermissionEntity"
		type="com.house365.beans.entity.RolePermissionEntity" />
	<resultMap id="rolePermission"
		class="com.house365.beans.entity.RolePermissionEntity">
		<result property="id" column="id" />
		<result property="roleId" column="roleId" />
		<result property="permissionId" column="permissionId" />
		<result property="createTime" column="createTime" />
		<result property="createUserid" column="createUserid" />
	</resultMap>

	<!-- 用于select查询公用抽取的列 -->
	<sql id="rolePermission.columns">
	    <![CDATA[
        id, roleId, permissionId, createTime, createUserid
	    ]]>
	</sql>

	<!-- 新增记录 -->
	<insert id="save" parameterClass="rolePermissionEntity">
	    <![CDATA[
	        INSERT INTO role_permission (
	        	id, 
	        	roleId, 
	        	permissionId, 
	        	createTime, 
	        	createUserid
	        ) VALUES (
	        	#id#, 
	        	#roleId#, 
	        	#permissionId#, 
	        	#createTime#, 
	        	#createUserid#
	        )
	    ]]>
		<selectKey resultClass="java.lang.Integer" type="post"
			keyProperty="id">
			SELECT LAST_INSERT_ID()
		</selectKey>
	</insert>

	<!-- 通过ID修改 -->
	<update id="update" parameterClass="rolePermissionEntity">
	    <![CDATA[
	        UPDATE role_permission SET
		        roleId = #roleId#, 
		        permissionId = #permissionId#, 
		        createTime = #createTime#, 
		        createUserid = #createUserid#
	        WHERE 
		        id = #id#
	    ]]>
	</update>

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

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

	<!-- 通过ID查询 -->
	<select id="getById" resultMap="rolePermission">
		SELECT
		<include refid="rolePermission.columns" />
	    <![CDATA[
	        FROM role_permission 
	        WHERE 
		        id = #id#
	    ]]>
	</select>

	<!-- 用于分页查询抽取的公共的条件 -->
	<sql id="rolePermission.queryPage">
		<dynamic prepend="WHERE">
			<!-- 默认不开启实体作为条件 -->
			<!-- 
		    <isNotNull property="entity">
				<isNotEmpty prepend="AND" property="entity.id">
		            <![CDATA[
					id = #entity.id#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.roleId">
		            <![CDATA[
					roleId = #entity.roleId#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.permissionId">
		            <![CDATA[
					permissionId = #entity.permissionId#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.createTime">
		            <![CDATA[
					createTime = #entity.createTime#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.createUserid">
		            <![CDATA[
					createUserid = #entity.createUserid#
		            ]]>
				</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.roleId">
		            <![CDATA[
					roleId $searchFilters.roleId.operation$ #searchFilters.roleId.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.permissionId">
		            <![CDATA[
					permissionId $searchFilters.permissionId.operation$ #searchFilters.permissionId.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.createTime">
		            <![CDATA[
					createTime $searchFilters.createTime.operation$ #searchFilters.createTime.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.createUserid">
		            <![CDATA[
					createUserid $searchFilters.createUserid.operation$ #searchFilters.createUserid.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_permission
		<include refid="rolePermission.queryPage" />
	</select>

	<!-- 分页查询指定页记录 -->
	<select id="queryByPage" resultClass="rolePermissionEntity">
		SELECT
		 rp.id, rp.roleId, rp.permissionId, rp.createTime, rp.createUserid,p.name as permissionName,p.code as permissionCode,p.hierarchy
	    <![CDATA[
	        FROM role_permission rp  left join permission p on rp.permissionId = p.id
	    ]]>
		<include refid="rolePermission.queryPage" />
		<![CDATA[
		    LIMIT #paging.startRecord#, #paging.pageSize#
        ]]>
	</select>

	<!-- 查询所有记录 -->
	<select id="queryAll" resultMap="rolePermission">
		SELECT
		<include refid="rolePermission.columns" />
	    <![CDATA[
		    FROM role_permission
	    ]]>
	    <include refid="rolePermission.queryPage" />
	</select>
</sqlMap>