<?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.MarketingActivityEntity">
    <typeAlias alias="marketingActivityEntity" type="com.house365.beans.entity.MarketingActivityEntity"/>

    <resultMap id="marketingActivity" class="com.house365.beans.entity.MarketingActivityEntity">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="status" column="status"/>
        <result property="type" column="type"/>
        <result property="cycle" column="cycle"/>
        <result property="expectExtractCount" column="expect_extract_count"/>
        <result property="projectName" column="project_name"/>
        <result property="mobileType" column="mobile_type"/>
        <result property="applyStartTime" column="apply_start_time"/>
        <result property="applyEndTime" column="apply_end_time"/>
        <result property="applySource" column="apply_source"/>
        <result property="activeType" column="active_type"/>
        <result property="totalPriceLower" column="total_price_lower"/>
        <result property="totalPriceHigher" column="total_price_higher"/>
        <result property="areaLower" column="area_lower"/>
        <result property="areaHigher" column="area_higher"/>
        <result property="returnVisitTimeStart" column="return_visit_time_start"/>
        <result property="returnVisitTimeEnd" column="return_visit_time_end"/>
        <result property="returnVisitStatus" column="return_visit_status"/>
        <result property="returnVisitResult" column="return_visit_result"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="finishTime" column="finish_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="gainCount" column="gain_count"/>
        <result property="overTimeCount" column="over_time_count"/>
        <result property="visitBackCount" column="visit_back_count"/>
        <result property="waitVisitCount" column="wait_visit_count"/>
        <result property="boughtCount" column="bought_count"/>
        <result property="applySourceName" column="apply_source_name"/>
        <result property="activeTypeName" column="active_type_name"/>
        <result property="returnVisitStatusName" column="return_visit_status_name"/>
        <result property="protectCount" column="protectCount"/>
        <result property="createName" column="create_name"/>
        <result property="city" column="city"/>
        <result property="block" column="block"/>
        <result property="blockName" column="blockName"/>
        <result property="district" column="district"/>
        <result property="districtName" column="districtName"/>
        <result property="channel" column="channel"/>
        <result property="channelName" column="channelName"/>
        <result property="actName" column="actName"/>
        <result property="mobile" column="mobile"/>
        <result property="callNum" column="callNum"/>

        <result property="seeProjectName" column="see_project_name"/>
        <result property="seeStartTime" column="see_start_time"/>
        <result property="seeEndTime" column="see_end_time"/>
        <result property="seeCount" column="see_count"/>
        <result property="buildingId" column="projectId"/>
        <result property="browseProjectId" column="seeProjectId"/>
    </resultMap>

    <!-- 用于select查询公用抽取的列 -->
    <sql id="marketingActivity.columns">
        <![CDATA[

        id, name, status, type, cycle, expect_extract_count, project_name, mobile_type, apply_start_time,
        apply_end_time, apply_source, active_type, total_price_lower, total_price_higher, area_lower,
        area_higher, return_visit_time_start, return_visit_time_end, return_visit_status, return_visit_result,
        create_by, create_time, update_by, update_time, gain_count, over_time_count, visit_back_count,
        wait_visit_count, bought_count, apply_source_name, active_type_name, return_visit_status_name,finish_time,protectCount,
        create_name,city,block,blockName,district,districtName,channel,channelName,actName,mobile,callNum,see_project_name,
        see_start_time,see_end_time,see_count,projectId,seeProjectId

        ]]>
    </sql>

    <!-- 新增记录 -->
    <insert id="save" parameterClass="marketingActivityEntity">
        <![CDATA[
	        INSERT INTO marketing_activity (
	        	id,
	        	name,
	        	status,
	        	type,
	        	cycle,
	        	expect_extract_count,
	        	project_name,
	        	mobile_type,
	        	apply_start_time,
	        	apply_end_time,
	        	apply_source,
	        	active_type,
	        	total_price_lower,
	        	total_price_higher,
	        	area_lower,
	        	area_higher,
	        	return_visit_time_start,
	        	return_visit_time_end,
	        	return_visit_status,
	        	return_visit_result,
	        	create_by,
	        	create_time,
	        	update_by,
	        	update_time,
	        	gain_count,
	        	over_time_count,
	        	visit_back_count,
	        	wait_visit_count,
	        	bought_count,
	        	apply_source_name,
	        	active_type_name,
	        	return_visit_status_name,
	        	finish_time,
	        	protectCount,
	        	create_name,
	        	city,block,blockName,district,districtName,channel,channelName,actName,mobile,callNum
	        ) VALUES (
	        	#id#,
	        	#name#,
	        	#status#,
	        	#type#,
	        	#cycle#,
	        	#expectExtractCount#,
	        	#projectName#,
	        	#mobileType#,
	        	#applyStartTime#,
	        	#applyEndTime#,
	        	#applySource#,
	        	#activeType#,
	        	#totalPriceLower#,
	        	#totalPriceHigher#,
	        	#areaLower#,
	        	#areaHigher#,
	        	#returnVisitTimeStart#,
	        	#returnVisitTimeEnd#,
	        	#returnVisitStatus#,
	        	#returnVisitResult#,
	        	#createBy#,
	        	#createTime#,
	        	#updateBy#,
	        	#updateTime#,
	        	#gainCount#,
	        	#overTimeCount#,
	        	#visitBackCount#,
	        	#waitVisitCount#,
	        	#boughtCount#,
	        	#applySourceName#,
	        	#activeTypeName#,
	        	#returnVisitStatusName#,
	        	#finishTime#,
	        	#protectCount#,
	        	#createName#,
	        	#city#,#block#,#blockName#,#district#,#districtName#,#channel#,#channelName#,#actName#,#mobile#,#callNum#
	        )
	    ]]>
        <selectKey resultClass="java.lang.Integer" type="post"
                   keyProperty="id">
            SELECT LAST_INSERT_ID()
        </selectKey>
    </insert>

    <!-- 通过ID修改 -->
    <update id="update" parameterClass="marketingActivityEntity">
        <![CDATA[

	        UPDATE marketing_activity SET
		        name = #name#,
		        status = #status#,
		        type = #type#,
		        cycle = #cycle#,
		        expect_extract_count = #expectExtractCount#,
		        project_name = #projectName#,
		        mobile_type = #mobileType#,
		        apply_start_time = #applyStartTime#,
		        apply_end_time = #applyEndTime#,
		        apply_source = #applySource#,
		        active_type = #activeType#,
		        total_price_lower = #totalPriceLower#,
		        total_price_higher = #totalPriceHigher#,
		        area_lower = #areaLower#,
		        area_higher = #areaHigher#,
		        return_visit_time_start = #returnVisitTimeStart#,
		        return_visit_time_end = #returnVisitTimeEnd#,
		        return_visit_status = #returnVisitStatus#,
		        return_visit_result = #returnVisitResult#,
		        create_by = #createBy#,
		        create_time = #createTime#,
		        update_by = #updateBy#,
		        update_time = #updateTime#,
		        gain_count = #gainCount#,
		        over_time_count = #overTimeCount#,
		        visit_back_count = #visitBackCount#,
		        wait_visit_count = #waitVisitCount#,
		        bought_count = #boughtCount#,
		        apply_source_name = #applySourceName#,
		        active_type_name = #activeTypeName#,
		        return_visit_status_name = #returnVisitStatusName#,
		        finish_time = #finishTime#,protectCount=#protectCount#,
		        create_name = #createName#,
		        city=#city#,
		        block=#block#,blockName=#blockName#,district=#district#,districtName=#districtName#,
		        channel=#channel#,channelName=#channelName#,actName=#actName#,mobile=#mobile#,callNum=#callNum#,
		        see_start_time = #seeStartTime#,see_end_time = #seeEndTime#,
		        see_project_name = #seeProjectName#,see_count = #seeCount#,
		        projectId = #buildingId#, seeProjectId = #browseProjectId#
	        WHERE
		        id = #id#
        ]]>
    </update>

    <!-- 通过ID删除 -->
    <delete id="delete">
        <![CDATA[

	        DELETE FROM marketing_activity WHERE
	        id = #id#

        ]]>
    </delete>

    <!-- 通过ID列表批量删除 -->
    <delete id="deleteByIds" parameterClass="java.lang.String">
        <![CDATA[

	        DELETE FROM marketing_activity WHERE
	        id IN ($ids$)

        ]]>
    </delete>

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

    <!-- 用于分页查询抽取的公共的条件 -->
    <sql id="marketingActivity.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.name">
                    <![CDATA[
					name $searchFilters.name.operation$ #searchFilters.name.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>
                <isNotEmpty prepend="AND" property="searchFilters.cycle">
                    <![CDATA[
					cycle $searchFilters.cycle.operation$ #searchFilters.cycle.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.expectExtractCount">
                    <![CDATA[
					expect_extract_count $searchFilters.expectExtractCount.operation$ #searchFilters.expectExtractCount.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.projectName">
                    <![CDATA[
					project_name $searchFilters.projectName.operation$ #searchFilters.projectName.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.mobileType">
                    <![CDATA[
					mobile_type $searchFilters.mobileType.operation$ #searchFilters.mobileType.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.applyStartTime">
                    <![CDATA[
					apply_start_time $searchFilters.applyStartTime.operation$ #searchFilters.applyStartTime.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.applyEndTime">
                    <![CDATA[
					apply_end_time $searchFilters.applyEndTime.operation$ #searchFilters.applyEndTime.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.applySource">
                    <![CDATA[
					apply_source $searchFilters.applySource.operation$ #searchFilters.applySource.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.activeType">
                    <![CDATA[
					active_type $searchFilters.activeType.operation$ #searchFilters.activeType.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.totalPriceLower">
                    <![CDATA[
					total_price_lower $searchFilters.totalPriceLower.operation$ #searchFilters.totalPriceLower.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.totalPriceHigher">
                    <![CDATA[
					total_price_higher $searchFilters.totalPriceHigher.operation$ #searchFilters.totalPriceHigher.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.areaLower">
                    <![CDATA[
					area_lower $searchFilters.areaLower.operation$ #searchFilters.areaLower.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.areaHigher">
                    <![CDATA[
					area_higher $searchFilters.areaHigher.operation$ #searchFilters.areaHigher.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.returnVisitTimeStart">
                    <![CDATA[
					return_visit_time_start $searchFilters.returnVisitTimeStart.operation$ #searchFilters.returnVisitTimeStart.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.returnVisitTimeEnd">
                    <![CDATA[
					return_visit_time_end $searchFilters.returnVisitTimeEnd.operation$ #searchFilters.returnVisitTimeEnd.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.returnVisitStatus">
                    <![CDATA[
					return_visit_status $searchFilters.returnVisitStatus.operation$ #searchFilters.returnVisitStatus.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.returnVisitResult">
                    <![CDATA[
					return_visit_result $searchFilters.returnVisitResult.operation$ #searchFilters.returnVisitResult.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.createBy">
                    <![CDATA[
					create_by $searchFilters.createBy.operation$ #searchFilters.createBy.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.createTime">
                    <![CDATA[
					create_time $searchFilters.createTime.operation$ #searchFilters.createTime.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.finishTime">
                    <![CDATA[
					finish_time $searchFilters.finishTime.operation$ #searchFilters.finishTime.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.updateBy">
                    <![CDATA[
					update_by $searchFilters.updateBy.operation$ #searchFilters.updateBy.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.updateTime">
                    <![CDATA[
					update_time $searchFilters.updateTime.operation$ #searchFilters.updateTime.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.gainCount">
                    <![CDATA[
					gain_count $searchFilters.gainCount.operation$ #searchFilters.gainCount.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.city">
                    <![CDATA[
					city $searchFilters.city.operation$ #searchFilters.city.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.overTimeCount">
                    <![CDATA[
					over_time_count $searchFilters.overTimeCount.operation$ #searchFilters.overTimeCount.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.visitBackCount">
                    <![CDATA[
					visit_back_count $searchFilters.visitBackCount.operation$ #searchFilters.visitBackCount.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.waitVisitCount">
                    <![CDATA[
					wait_visit_count $searchFilters.waitVisitCount.operation$ #searchFilters.waitVisitCount.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.boughtCount">
                    <![CDATA[
					bought_count $searchFilters.boughtCount.operation$ #searchFilters.boughtCount.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.applySourceName">
                    <![CDATA[
					apply_source_name $searchFilters.applySourceName.operation$ #searchFilters.applySourceName.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.activeTypeName">
                    <![CDATA[
					active_type_name $searchFilters.activeTypeName.operation$ #searchFilters.activeTypeName.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.returnVisitStatusName">
                    <![CDATA[
					return_visit_status_name $searchFilters.returnVisitStatusName.operation$ #searchFilters.returnVisitStatusName.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.userId">
                    <![CDATA[
					 (create_by = $searchFilters.userId.value$ or create_by in (select id from user where depturlpath like (select concat(depturlpath,'/%') from user where id = $searchFilters.userId.value$)))
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.block">
                    <![CDATA[
					block $searchFilters.block.operation$ #searchFilters.block.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.blockName">
                    <![CDATA[
					blockName $searchFilters.blockName.operation$ #searchFilters.blockName.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.district">
                    <![CDATA[
					district $searchFilters.district.operation$ #searchFilters.district.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.districtName">
                    <![CDATA[
					districtName $searchFilters.districtName.operation$ #searchFilters.districtName.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.channel">
                    <![CDATA[
					channel $searchFilters.channel.operation$ #searchFilters.channel.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.channelName">
                    <![CDATA[
					channelName $searchFilters.channelName.operation$ #searchFilters.channelName.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.actName">
                    <![CDATA[
					actName $searchFilters.actName.operation$ #searchFilters.actName.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.mobile">
                    <![CDATA[
					mobile $searchFilters.mobile.operation$ #searchFilters.mobile.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.callNum">
                    <![CDATA[
					callNum $searchFilters.callNum.operation$ #searchFilters.callNum.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 marketing_activity
        <include refid="marketingActivity.queryPage"/>
    </select>

    <select id="queryProtectCount" resultClass="int">
        <![CDATA[

        select count(0) from market_customer a,marketing_activity b where NOW() < b.finish_time
        and a.active_id=b.id and a.phone=#phone# and b.status=1 and a.status != 3

        ]]>
    </select>

    <select id="queryAllPullCount" resultClass="int">
        <![CDATA[
        select sum(gain_count) from marketing_activity
	    ]]>
        <include refid="marketingActivity.queryPage"/>
    </select>

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

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

    <select id="queryCustomerCountByActive" resultClass="int">
        <![CDATA[

        select count(0) from market_customer where active_id=$activeId$

        ]]>
    </select>

    <update id="setDisable" parameterClass="int">
        update marketing_activity set status=2 where id=$id$
    </update>

    <update id="addCallBackCount" parameterClass="int">
        update marketing_activity set visit_back_count=IFNULL(visit_back_count,0)+1 where id=$id$
    </update>

    <update id="addWaitVisitCount" parameterClass="int">
        update marketing_activity set wait_visit_count=IFNULL(wait_visit_count,0)+1 where id=$id$
    </update>

    <update id="addBughtCount" parameterClass="int">
        update marketing_activity set bought_count=IFNULL(bought_count,0)+1 where id=$id$
    </update>

    <update id="addOverTimeCount" parameterClass="int">
        update marketing_activity set over_time_count=IFNULL(over_time_count,0)+1 where id=$id$
    </update>

</sqlMap>
