<?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.CrmSourceEntity">
	<typeAlias alias="crmSourceEntity"
			   type="com.house365.beans.entity.CrmSourceEntity"/>
	<resultMap id="crmSource"
			   class="com.house365.beans.entity.CrmSourceEntity">
		<result property="id" column="id" />
		<result property="sourceId" column="source_id" />
		<result property="sourceName" column="source_name" />
		<result property="status" column="status" />
		<result property="categoryId" column="category_id" />
		<result property="categoryName" column="category_name" />
		<result property="categoryPid" column="category_pid" />
		<result property="createTime" column="create_time" />
		<result property="creater" column="creater" />
	</resultMap>

	<!-- 用于select查询公用抽取的列 -->
	<sql id="crmSource.columns">
		<![CDATA[
        id, source_id, source_name, status, category_id, category_name, category_pid, create_time, creater
	    ]]>
	</sql>

	<!-- 新增记录 -->
	<insert id="save" parameterClass="crmSourceEntity">
		<![CDATA[
	        INSERT INTO crm_source1 (
	        	id,
	        	source_id,
	        	source_name,
	        	status,
	        	category_id,
	        	category_name,
	        	category_pid,
	        	create_time,
	        	creater
	        ) VALUES (
	        	#id#,
	        	#sourceId#,
	        	#sourceName#,
	        	#status#,
	        	#categoryId#,
	        	#categoryName#,
	        	#categoryPid#,
	        	#createTime#,
	        	#creater#
	        )
	    ]]>
		<selectKey resultClass="java.lang.Integer" type="post"
				   keyProperty="id">
			SELECT LAST_INSERT_ID()
		</selectKey>
	</insert>

	<!-- 通过ID修改 -->
	<update id="update" parameterClass="crmSourceEntity">
		<![CDATA[
	        UPDATE crm_source1 SET
		        source_id = #sourceId#,
		        source_name = #sourceName#,
		        status = #status#,
		        category_id = #categoryId#,
		        category_name = #categoryName#,
		        category_pid = #categoryPid#,
		        create_time = #createTime#,
		        creater = #creater#
	        WHERE
		        id = #id#
	    ]]>
	</update>

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

	<delete id="truncateTable">
		truncate table crm_source1
	</delete>

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

    <select id="getListByCategory" resultMap="crmSource">
        SELECT * FROM crm_source1 where category_id in (
          SELECT sourceid FROM base_tree where urlpath regexp #categoryReg#
        )
    </select>

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

	<!-- 用于分页查询抽取的公共的条件 -->
	<sql id="crmSource.queryPage">
		<dynamic prepend="WHERE">
			<isNotNull property="searchFilters">
				<isNotEmpty prepend="AND" property="searchFilters.id">
					<![CDATA[
					id $searchFilters.id.operation$ #searchFilters.id.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.sourceId">
					<![CDATA[
					source_id $searchFilters.sourceId.operation$ #searchFilters.sourceId.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.sourceName">
					<![CDATA[
					source_name $searchFilters.sourceName.operation$ #searchFilters.sourceName.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.status">
					<![CDATA[
					status $searchFilters.status.operation$ #searchFilters.status.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.categoryId">
					<![CDATA[
					category_id $searchFilters.categoryId.operation$ #searchFilters.categoryId.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.categoryName">
					<![CDATA[
					category_name $searchFilters.categoryName.operation$ #searchFilters.categoryName.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.categoryPid">
					<![CDATA[
					category_pid $searchFilters.categoryPid.operation$ #searchFilters.categoryPid.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.createTime">
					<![CDATA[
					create_time $searchFilters.createTime.operation$ #searchFilters.createTime.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.creater">
					<![CDATA[
					creater $searchFilters.creater.operation$ #searchFilters.creater.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 crm_source1
		<include refid="crmSource.queryPage" />
	</select>

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

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


</sqlMap>
