<?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.BaseTreeEntity">
	<typeAlias alias="baseTreeEntity"
		type="com.house365.beans.entity.BaseTreeEntity" />
	<resultMap id="baseTree"
		class="com.house365.beans.entity.BaseTreeEntity">
		<result property="id" column="id" />
		<result property="name" column="name" />
		<result property="urlPath" column="urlPath" />
		<result property="status" column="status" />
		<result property="parentId" column="parentId" />
		<result property="orderNum" column="orderNum" />
		<result property="fullPath" column="fullPath" />
		<result property="hierarchy" column="hierarchy" />
		<result property="descripition" column="descripition" />
		<result property="createTime" column="createTime" />
		<result property="updateTime" column="updateTime" />
		<result property="creator" column="creator" />
		<result property="updater" column="updater" />
		<result property="sourceId" column="sourceId" />
		<result property="sourceType" column="sourceType" />
	</resultMap>

	<!-- 用于select查询公用抽取的列 -->
	<sql id="baseTree.columns">
	    <![CDATA[
        id, name, urlPath, status, parentId, orderNum, fullPath, hierarchy, descripition, createTime, updateTime, creator, updater, sourceId, sourceType
	    ]]>
	</sql>

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

	<!-- 通过ID修改 -->
	<update id="update" parameterClass="baseTreeEntity">
	    <![CDATA[
	        UPDATE base_tree SET
		        name = #name#, 
		        urlPath = #urlPath#, 
		        status = #status#, 
		        parentId = #parentId#, 
		        orderNum = #orderNum#, 
		        fullPath = #fullPath#, 
		        hierarchy = #hierarchy#, 
		        descripition = #descripition#, 
		        createTime = #createTime#, 
		        updateTime = #updateTime#, 
		        creator = #creator#, 
		        updater = #updater#, 
		        sourceId = #sourceId#, 
		        sourceType = #sourceType#
	        WHERE 
		        id = #id#
	    ]]>
	</update>

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

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

    <delete id="clean">
        DELETE FROM base_tree WHERE sourceType=#sourceType#
    </delete>

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

	<!-- 用于分页查询抽取的公共的条件 -->
	<sql id="baseTree.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.status">
		            <![CDATA[
					status = #entity.status#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.parentId">
		            <![CDATA[
					parentId = #entity.parentId#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.orderNum">
		            <![CDATA[
					orderNum = #entity.orderNum#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.fullPath">
		            <![CDATA[
					fullPath = #entity.fullPath#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.hierarchy">
		            <![CDATA[
					hierarchy = #entity.hierarchy#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.descripition">
		            <![CDATA[
					descripition = #entity.descripition#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.createTime">
		            <![CDATA[
					createTime = #entity.createTime#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.updateTime">
		            <![CDATA[
					updateTime = #entity.updateTime#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.creator">
		            <![CDATA[
					creator = #entity.creator#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.updater">
		            <![CDATA[
					updater = #entity.updater#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.sourceId">
		            <![CDATA[
					sourceId = #entity.sourceId#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.sourceType">
		            <![CDATA[
					sourceType = #entity.sourceType#
		            ]]>
				</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.status">
		            <![CDATA[
					status $searchFilters.status.operation$ #searchFilters.status.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.fullPath">
		            <![CDATA[
					fullPath $searchFilters.fullPath.operation$ #searchFilters.fullPath.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.creator">
		            <![CDATA[
					creator $searchFilters.creator.operation$ #searchFilters.creator.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.updater">
		            <![CDATA[
					updater $searchFilters.updater.operation$ #searchFilters.updater.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.sourceId">
		            <![CDATA[
					sourceId $searchFilters.sourceId.operation$ #searchFilters.sourceId.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.sourceType">
		            <![CDATA[
					sourceType $searchFilters.sourceType.operation$ #searchFilters.sourceType.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 base_tree
		<include refid="baseTree.queryPage" />
	</select>

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

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

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

</sqlMap>
