<?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.UserRoleEntity">
	<typeAlias alias="userRoleEntity"
		type="com.house365.beans.entity.UserRoleEntity" />
	<resultMap id="userRole"
		class="com.house365.beans.entity.UserRoleEntity">
		<result property="id" column="id" />
		<result property="userId" column="userId" />
		<result property="roleId" column="roleId" />
		<result property="createTime" column="createTime" />
		<result property="createUserId" column="createUserId" />
	</resultMap>

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

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

	<!-- 通过ID修改 -->
	<update id="update" parameterClass="userRoleEntity">
	    <![CDATA[
	        UPDATE user_role SET
		        userId = #userId#, 
		        roleId = #roleId#, 
		        createTime = #createTime#, 
		        createUserId = #createUserId#
	        WHERE 
		        id = #id#
	    ]]>
	</update>

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

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

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

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

	<!-- 分页查询指定页记录 -->
	<select id="queryByPage" resultClass="userRoleEntity">
		SELECT
		 ur.id, ur.userId, ur.roleId, ur.createTime, ur.createUserId, r.code as roleCode,r.name as roleName
	    <![CDATA[
	        FROM user_role ur left join role r on ur.roleId = r.id 
	    ]]>
		<include refid="userRole.queryPage" />
		<![CDATA[
		    LIMIT #paging.startRecord#, #paging.pageSize#
        ]]>
	</select>

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