<?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.CustomerSourceEntity">
    <typeAlias alias="customerSourceEntity" type="com.house365.beans.entity.CustomerSourceEntity" />
    <resultMap id="customerSource" class="com.house365.beans.entity.CustomerSourceEntity">
        <result property="id" column="id"/>
        <result property="sourceName" column="source_name"/>
        <result property="sort" column="sort" />
        <result property="createTime" column="create_time"/>
        <result property="creater" column="creater"/>
        <result property="city" column="city"/>
        <result property="source" column="source"/>
        <result property="status" column="status"/>
        <result property="type" column="type"/>
    </resultMap>

    <!-- 用于select查询公用抽取的列 -->
    <sql id="customerSource.columns">
        <![CDATA[
        id, source_name, sort, create_time, creater, city, source, status,type
	    ]]>
    </sql>

    <!-- 新增记录 -->
    <insert id="save" parameterClass="customerSourceEntity">
        <![CDATA[
	        INSERT INTO customer_source (
	        	id, 
	        	source_name, 
	        	sort, 
	        	create_time, 
	        	creater, 
	        	city,
	        	source,
	        	type,
	        	status
	        ) VALUES (
	        	#id#, 
	        	#sourceName#, 
	        	#sort#, 
	        	#createTime#, 
	        	#creater#, 
	        	#city#,
	        	#source#,
	        	#type#,
	        	#status#
	        )
	    ]]>
        <selectKey resultClass="java.lang.Integer" type="post" keyProperty="id">
            SELECT LAST_INSERT_ID()
        </selectKey>
    </insert>

    <!-- 通过ID修改 -->
    <update id="update" parameterClass="customerSourceEntity">
        <![CDATA[
	        UPDATE customer_source SET
		        source_name = #sourceName#, 
		        sort = #sort#, 
		        create_time = #createTime#, 
		        creater = #creater#, 
		        city = #city#,
		        source = #source#,
		        type = #type#,
		        status = #status#
	        WHERE 
		        id = #id#
	    ]]>
    </update>

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

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

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

    <!-- 用于分页查询抽取的公共的条件 -->
    <sql id="customerSource.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.sourceName">
                    <![CDATA[
					source_name $searchFilters.sourceName.operation$ #searchFilters.sourceName.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.sort">
                    <![CDATA[
					sort $searchFilters.sort.operation$ #searchFilters.sort.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.createTime">
                    <![CDATA[
					create_time $searchFilters.createTime.operation$ #searchFilters.createTime.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.source">
                    <![CDATA[
					source $searchFilters.source.operation$ #searchFilters.source.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.creater">
                    <![CDATA[
					creater $searchFilters.creater.operation$ #searchFilters.creater.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.city">
                    <![CDATA[
					city $searchFilters.city.operation$ #searchFilters.city.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.status">
                    <![CDATA[
					status $searchFilters.status.operation$ #searchFilters.status.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.type">
                    <![CDATA[
					type $searchFilters.type.operation$ #searchFilters.type.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 customer_source
        <include refid="customerSource.queryPage" />
    </select>

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

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