<?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.DepartmentEntity">
	<typeAlias alias="departmentEntity"
		type="com.house365.beans.entity.DepartmentEntity" />
	<resultMap id="department"
		class="com.house365.beans.entity.DepartmentEntity">
		<result property="id" column="id" />
		<result property="name" column="name" />
		<result property="urlPath" column="urlPath" />
		<result property="type" column="type" />
		<result property="parentId" column="parentId" />
		<result property="orderNum" column="orderNum" />
		<result property="root" column="root" />
		<result property="hierarchy" column="hierarchy" />
		<result property="descripition" column="descripition" />
		<result property="createTime" column="createTime" />
		<result property="updateTime" column="updateTime" />
	</resultMap>

	<!-- 用于select查询公用抽取的列 -->
	<sql id="department.columns">
	    <![CDATA[
        id, name, urlPath, type, parentId, orderNum, root,hierarchy,descripition, createTime, updateTime
	    ]]>
	</sql>

	<!-- 新增记录 -->
	<insert id="save" parameterClass="departmentEntity">
	    <![CDATA[
	        INSERT INTO department (
	        	id, 
	        	name, 
	        	urlPath, 
	        	type, 
	        	parentId, 
	        	orderNum, 
	        	root, 
	        	hierarchy,
	        	descripition,
	        	createTime, 
	        	updateTime
	        ) VALUES (
	        	#id#, 
	        	#name#, 
	        	#urlPath#, 
	        	#type#, 
	        	#parentId#, 
	        	#orderNum#, 
	        	#root#,
	        	#hierarchy#,
	        	#descripition#, 
	        	#createTime#, 
	        	#updateTime#
	        )
	    ]]>
		<selectKey resultClass="java.lang.Integer" type="post"
			keyProperty="id">
			SELECT LAST_INSERT_ID()
		</selectKey>
	</insert>

	<!-- 通过ID修改 -->
	<update id="update" parameterClass="departmentEntity">
	    UPDATE department 
	        	<dynamic prepend="SET">
	        	<isNotEmpty prepend="," property="name">
		            <![CDATA[
					name = #name#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="," property="urlPath">
		            <![CDATA[
					urlPath = #urlPath#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="," property="type">
		            <![CDATA[
					type = #type#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="," property="parentId">
		            <![CDATA[
					parentId = #parentId#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="," property="orderNum">
		            <![CDATA[
					orderNum = #orderNum#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="," property="root">
		            <![CDATA[
					root = #root#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="," property="hierarchy">
		            <![CDATA[
					hierarchy = #hierarchy#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="," property="descripition">
		            <![CDATA[
					descripition = #descripition#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="," property="createTime">
		            <![CDATA[
					createTime = #createTime#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="," property="updateTime">
		            <![CDATA[
					updateTime = #updateTime#
		            ]]>
				</isNotEmpty>
			    </dynamic>
			        WHERE 
				        id = #id#
	</update>

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

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

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

	<!-- 用于分页查询抽取的公共的条件 -->
	<sql id="department.queryPage">
		<dynamic prepend="WHERE">
			<!-- 默认不开启实体作为条件 -->
			<!-- 
		    <isNotNull property="entity">
				<isNotEmpty prepend="AND" property="entity.id">
		            <![CDATA[
					id = #entity.id#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.name">
		            <![CDATA[
					name = #entity.name#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.urlPath">
		            <![CDATA[
					urlPath = #entity.urlPath#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.type">
		            <![CDATA[
					type = #entity.type#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.parentId">
		            <![CDATA[
					parentId = #entity.parentId#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.order">
		            <![CDATA[
					order = #entity.order#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.root">
		            <![CDATA[
					root = #entity.root#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.createTime">
		            <![CDATA[
					createTime = #entity.createTime#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.updateTime">
		            <![CDATA[
					updateTime = #entity.updateTime#
		            ]]>
				</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.name">
		            <![CDATA[
					name $searchFilters.name.operation$ #searchFilters.name.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.urlPath">
		            <![CDATA[
					urlPath $searchFilters.urlPath.operation$ #searchFilters.urlPath.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.type">
		            <![CDATA[
					type $searchFilters.type.operation$ #searchFilters.type.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.parentId">
		            <![CDATA[
					parentId $searchFilters.parentId.operation$ #searchFilters.parentId.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.orderNum">
		            <![CDATA[
					orderNum $searchFilters.orderNum.operation$ #searchFilters.orderNum.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.root">
		            <![CDATA[
					root $searchFilters.root.operation$ #searchFilters.root.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.hierarchy">
		            <![CDATA[
					hierarchy $searchFilters.hierarchy.operation$ #searchFilters.hierarchy.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.descripition">
		            <![CDATA[
					descripition $searchFilters.descripition.operation$ #searchFilters.descripition.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.createTime">
		            <![CDATA[
					createTime $searchFilters.createTime.operation$ #searchFilters.createTime.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.updateTime">
		            <![CDATA[
					updateTime $searchFilters.updateTime.operation$ #searchFilters.updateTime.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.ids">
					id in
					<iterate open="(" close=")" conjunction="," property="searchFilters.ids.value">
						<![CDATA[  #searchFilters.ids.value[]# ]]>
					</iterate>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.rushRule">
					<![CDATA[
					id not in (select department_id from rush_customer_rule_department_rel)
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.rushRuleId">
					<![CDATA[
					id not in (select department_id from rush_customer_rule_department_rel where rush_customer_rule_id != #searchFilters.rushRuleId.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 department
		<include refid="department.queryPage" />
	</select>

	<!-- 分页查询指定页记录 -->
	<select id="queryByPage" resultMap="department">
		SELECT
		<include refid="department.columns" />
	    <![CDATA[
	        FROM department 
	    ]]>
		<include refid="department.queryPage" />
		<![CDATA[
		    LIMIT #paging.startRecord#, #paging.pageSize#
        ]]>
	</select>

	<!-- 查询所有记录 -->
	<select id="queryAll" resultMap="department">
		SELECT
		<include refid="department.columns" />
	    <![CDATA[
		    FROM department
	    ]]>
	    <include refid="department.queryPage" />
	</select>
	
	 <!-- 查询所有记录 -->
    <select id="findChildrenListByPath" resultMap="department" parameterClass="java.util.LinkedList">
        SELECT
        <include refid="department.columns"/>
        <![CDATA[
		    FROM department
	    ]]>
        <dynamic prepend="where">
            <iterate open="(" close=")" conjunction="or">
                <![CDATA[ urlPath  like CONCAT(#paths[]#,'%') ]]>
            </iterate>
        </dynamic>
    </select>
</sqlMap>
