<?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.CustomerProjectEntity">
    <typeAlias alias="customerProjectEntity" type="com.house365.beans.entity.CustomerProjectEntity" />
    <resultMap id="customerProject" class="com.house365.beans.entity.CustomerProjectEntity">
        <result property="id" column="id" />
        <result property="consultantName" column="consultant_name" />
        <result property="status" column="status" />
        <result property="consultantPhone" column="consultant_phone" />
        <result property="isRepeat" column="is_repeat" />
        <result property="recordedStatus" column="recorded_status" />
        <result property="recordedTime" column="recorded_time" />
        <result property="visitTime" column="visit_time" />
        <result property="loanStatus" column="loan_status" />
        <result property="creater" column="creater" />
        <result property="createTime" column="create_time" />
        <result property="projectId" column="project_id" />
        <result property="remark" column="remark" />
        <result property="customerId" column="customer_id" />
        <result property="requireArea" column="require_area" />
        <result property="requireHouse" column="require_house" />
        <result property="requireHouseType" column="require_house_type" />
        <result property="requirePrice" column="require_price" />
        <result property="updateTime" column="update_time" />
        <result property="updater" column="updater" />
        <result property="customerPhone" column="customer_phone" />
        <result property="contract" column="contract" />
        <result property="houseName" column="house_name" />
        <result property="projectName" column="project_name" />
        <result property="lastChangeStatusTime" column="last_change_status_time" />
    </resultMap>

    <!-- 用于select查询公用抽取的列 -->
    <sql id="customerProject.columns">
        <![CDATA[
        id, consultant_name, status, consultant_phone, is_repeat, recorded_status, recorded_time,
        visit_time, loan_status, create_time,creater, project_id, remark, customer_id, require_area, require_house,
        require_house_type, require_price, update_time, updater, customer_phone, contract, house_name, project_name,
        last_change_status_time
	    ]]>
    </sql>

    <!-- 新增记录 -->
    <insert id="save" parameterClass="customerProjectEntity">
        <![CDATA[
	        INSERT INTO customer_project (
	        	id, 
	        	consultant_name, 
	        	status, 
	        	consultant_phone, 
	        	is_repeat, 
	        	recorded_status, 
	        	recorded_time, 
	        	visit_time, 
	        	loan_status, 
	        	create_time, 
	        	project_id, 
	        	remark, 
	        	customer_id, 
	        	require_area, 
	        	require_house, 
	        	require_house_type, 
	        	require_price, 
	        	update_time, 
	        	updater,
	        	customer_phone,
	        	contract,
	        	house_name,
	        	creater,
	        	project_name,
	        	last_change_status_time
	        ) VALUES (
	        	#id#, 
	        	#consultantName#, 
	        	#status#, 
	        	#consultantPhone#, 
	        	#isRepeat#, 
	        	#recordedStatus#, 
	        	#recordedTime#, 
	        	#visitTime#, 
	        	#loanStatus#, 
	        	#createTime#, 
	        	#projectId#, 
	        	#remark#, 
	        	#customerId#, 
	        	#requireArea#, 
	        	#requireHouse#, 
	        	#requireHouseType#, 
	        	#requirePrice#, 
	        	#updateTime#, 
	        	#updater#,
	        	#customerPhone#,
	        	#contract#,
	        	#houseName#,
	        	#creater#,
	        	#projectName#,
	        	#lastChangeStatusTime#
	        )
	    ]]>
        <selectKey resultClass="java.lang.Integer" type="post" keyProperty="id">
            SELECT LAST_INSERT_ID()
        </selectKey>
    </insert>

    <!-- 通过ID修改 -->
    <update id="update" parameterClass="customerProjectEntity">
        <![CDATA[
	        UPDATE customer_project SET
		        consultant_name = #consultantName#, 
		        status = #status#, 
		        consultant_phone = #consultantPhone#, 
		        is_repeat = #isRepeat#, 
		        recorded_status = #recordedStatus#, 
		        recorded_time = #recordedTime#, 
		        visit_time = #visitTime#, 
		        loan_status = #loanStatus#, 
		        create_time = #createTime#, 
		        project_id = #projectId#, 
		        remark = #remark#, 
		        customer_id = #customerId#, 
		        require_area = #requireArea#, 
		        require_house = #requireHouse#, 
		        require_house_type = #requireHouseType#, 
		        require_price = #requirePrice#, 
		        update_time = #updateTime#, 
		        updater = #updater#,
		        customer_phone = #customerPhone#,
		        contract = #contract#,
		        house_name = #houseName#,
		        creater = #creater#,
		        project_name = #projectName#,
		        last_change_status_time = #lastChangeStatusTime#
	        WHERE 
		        id = #id#
	    ]]>
    </update>

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

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

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

    <!-- 用于分页查询抽取的公共的条件 -->
    <sql id="customerProject.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.consultantName">
                    <![CDATA[
					consultant_name $searchFilters.consultantName.operation$ #searchFilters.consultantName.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.status">
                    <![CDATA[
					status $searchFilters.status.operation$ #searchFilters.status.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.consultantPhone">
                    <![CDATA[
					consultant_phone $searchFilters.consultantPhone.operation$ #searchFilters.consultantPhone.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.isRepeat">
                    <![CDATA[
					is_repeat $searchFilters.isRepeat.operation$ #searchFilters.isRepeat.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.recordedStatus">
                    <![CDATA[
					recorded_status $searchFilters.recordedStatus.operation$ #searchFilters.recordedStatus.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.recordedTime">
                    <![CDATA[
					recorded_time $searchFilters.recordedTime.operation$ #searchFilters.recordedTime.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.visitTime">
                    <![CDATA[
					visit_time $searchFilters.visitTime.operation$ #searchFilters.visitTime.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.loanStatus">
                    <![CDATA[
					loan_status $searchFilters.loanStatus.operation$ #searchFilters.loanStatus.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.createTime">
                    <![CDATA[
					create_time $searchFilters.createTime.operation$ #searchFilters.createTime.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.projectId">
                    <![CDATA[
					project_id $searchFilters.projectId.operation$ #searchFilters.projectId.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.remark">
                    <![CDATA[
					remark $searchFilters.remark.operation$ #searchFilters.remark.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.customerId">
                    <![CDATA[
					customer_id $searchFilters.customerId.operation$ #searchFilters.customerId.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.customerIds">
                    <![CDATA[ customer_id in ]]>
                    <iterate prepend="" property="searchFilters.customerIds.value" open="(" close=")" conjunction=",">
                        <![CDATA[ #searchFilters.customerIds.value[]# ]]>
                    </iterate>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.requireArea">
                    <![CDATA[
					require_area $searchFilters.requireArea.operation$ #searchFilters.requireArea.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.requireHouse">
                    <![CDATA[
					require_house $searchFilters.requireHouse.operation$ #searchFilters.requireHouse.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.requireHouseType">
                    <![CDATA[
					require_house_type $searchFilters.requireHouseType.operation$ #searchFilters.requireHouseType.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.requirePrice">
                    <![CDATA[
					require_price $searchFilters.requirePrice.operation$ #searchFilters.requirePrice.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.customerPhone">
                    <![CDATA[
					customer_phone $searchFilters.customerPhone.operation$ #searchFilters.customerPhone.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.customerPhones">
                    customer_phone $searchFilters.customerPhones.operation$
                    <iterate open="(" close=")" conjunction="," property="searchFilters.customerPhones.value" >
                        #searchFilters.customerPhones.value[]#
                    </iterate>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.contract">
                    <![CDATA[
					contract $searchFilters.contract.operation$ #searchFilters.contract.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.updateTime">
                    <![CDATA[
					update_time $searchFilters.updateTime.operation$ #searchFilters.updateTime.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.updater">
                    <![CDATA[
					updater $searchFilters.updater.operation$ #searchFilters.updater.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.houseName">
                    <![CDATA[
					house_name $searchFilters.houseName.operation$ #searchFilters.houseName.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.creater">
                    <![CDATA[
					creater $searchFilters.creater.operation$ #searchFilters.creater.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.projectName">
                    <![CDATA[
					project_name $searchFilters.projectName.operation$ #searchFilters.projectName.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.lastChangeStatusTime">
                    <![CDATA[
					last_change_status_time $searchFilters.lastChangeStatusTime.operation$ #searchFilters.lastChangeStatusTime.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.notblankmanager">
                    <![CDATA[
					EXISTS (select id from customer where id=customer_id and manager_id is not null and manager_id != '' and is_delete = 0)
		            ]]>
                </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_project
        <include refid="customerProject.queryPage" />
    </select>

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

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

    <update id="updateName" parameterClass="java.util.HashMap">
        <![CDATA[
	        UPDATE customer_project SET
		        project_name = #name#
	        WHERE
		        project_id = #id#
	    ]]>
    </update>
</sqlMap>
