<?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.CustomerStatusLogEntity">
    <typeAlias alias="projectSrouceParaPo" type="com.house365.beans.po.ProjectSrouceParaPo" />
    <typeAlias alias="customerStatusLogEntity" type="com.house365.beans.entity.CustomerStatusLogEntity" />
    <resultMap id="customerStatusLog" class="com.house365.beans.entity.CustomerStatusLogEntity">
        <result property="id" column="id" />
        <result property="fromStatusId" column="from_status_id" />
        <result property="toStatusId" column="to_status_id" />
        <result property="fromStatusName" column="from_status_name" />
        <result property="toStatusName" column="to_status_name" />
        <result property="remark" column="remark" />
        <result property="creater" column="creater" />
        <result property="createTime" column="create_time" />
        <result property="occurTime" column="occur_time" />
        <result property="customerId" column="customer_id" />
        <result property="projectId" column="project_id" />
        <result property="isRepeat" column="isRepeat" />
        <result property="nonStatistic" column="nonStatistic" />
        <result property="customerManagerId" column="customerManagerId" />
        <result property="customerManagerName" column="customerManagerName" />
        <result property="isDelete" column="is_delete" />
    </resultMap>

    <!-- 用于select查询公用抽取的列 -->
    <sql id="customerStatusLog.columns">
        <![CDATA[
        id, from_status_id, to_status_id, from_status_name, to_status_name, remark, creater, create_time,occur_time,
        customer_id, project_id,isRepeat,nonStatistic,customerManagerId, customerManagerName, is_delete
	    ]]>
    </sql>

    <!-- 新增记录 -->
    <insert id="save" parameterClass="customerStatusLogEntity">
        <![CDATA[
	        INSERT INTO customer_status_log (
	        	id, 
	        	from_status_id, 
	        	to_status_id, 
	        	from_status_name, 
	        	to_status_name, 
	        	remark, 
	        	creater, 
	        	create_time, 
	        	customer_id,
	        	project_id,
	        	isRepeat,
	        	nonStatistic,
	        	customerManagerId,
	        	customerManagerName,
	        	occur_time
	        ) VALUES (
	        	#id#, 
	        	#fromStatusId#, 
	        	#toStatusId#, 
	        	#fromStatusName#, 
	        	#toStatusName#, 
	        	#remark#, 
	        	#creater#, 
	        	#createTime#, 
	        	#customerId#,
	        	#projectId#,
	        	#isRepeat#,
	        	#nonStatistic#,
	        	#customerManagerId#,
	        	#customerManagerName#,
	        	#occurTime#
	        )
	    ]]>
        <selectKey resultClass="java.lang.Integer" type="post" keyProperty="id">
            SELECT LAST_INSERT_ID()
        </selectKey>
    </insert>

    <!-- 通过ID修改 -->
    <update id="update" parameterClass="customerStatusLogEntity">
        <![CDATA[
	        UPDATE customer_status_log SET
		        from_status_id = #fromStatusId#, 
		        to_status_id = #toStatusId#, 
		        from_status_name = #fromStatusName#, 
		        to_status_name = #toStatusName#, 
		        remark = #remark#, 
		        creater = #creater#, 
		        create_time = #createTime#, 
		        customer_id = #customerId#,
		        project_id = #projectId#,
		        isRepeat = #isRepeat#,
		        nonStatistic = #nonStatistic#,
		        customerManagerId = #customerManagerId#,
		        customerManagerName = #customerManagerName#
	        WHERE 
		        id = #id#
	    ]]>
    </update>

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

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

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

    <!-- 用于分页查询抽取的公共的条件 -->
    <sql id="customerStatusLog.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.fromStatusId">
                    <![CDATA[
					from_status_id $searchFilters.fromStatusId.operation$ #searchFilters.fromStatusId.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.toStatusId">
                    <![CDATA[
					to_status_id $searchFilters.toStatusId.operation$ #searchFilters.toStatusId.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.fromStatusName">
                    <![CDATA[
					from_status_name $searchFilters.fromStatusName.operation$ #searchFilters.fromStatusName.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.toStatusName">
                    <![CDATA[
					to_status_name $searchFilters.toStatusName.operation$ #searchFilters.toStatusName.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.remark">
                    <![CDATA[
					remark $searchFilters.remark.operation$ #searchFilters.remark.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.creater">
                    <![CDATA[
					creater $searchFilters.creater.operation$ #searchFilters.creater.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.createTime">
                    <![CDATA[
					create_time $searchFilters.createTime.operation$ #searchFilters.createTime.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.customerId">
                    <![CDATA[
					customer_id $searchFilters.customerId.operation$ #searchFilters.customerId.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.projectId">
                    <![CDATA[
					project_id $searchFilters.projectId.operation$ #searchFilters.projectId.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.isRepeat">
                    <![CDATA[
					isRepeat $searchFilters.isRepeat.operation$ #searchFilters.isRepeat.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.nonStatistic">
                    <![CDATA[
					nonStatistic $searchFilters.nonStatistic.operation$ #searchFilters.nonStatistic.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.customerManagerId">
                    <![CDATA[
					customerManagerId $searchFilters.customerManagerId.operation$ #searchFilters.customerManagerId.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.customerManagerName">
                    <![CDATA[
					customerManagerName $searchFilters.customerManagerName.operation$ #searchFilters.customerManagerName.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_status_log
        <include refid="customerStatusLog.queryPage" />
    </select>

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

    <!-- 查询所有记录 -->
    <select id="queryAll" resultMap="customerStatusLog">
        SELECT
        <include refid="customerStatusLog.columns" />
        <![CDATA[
		    FROM customer_status_log
	    ]]>
        <include refid="customerStatusLog.queryPage" />
    </select>
    <!--查询项目转化率-->
    <select id="getProjectTransform" resultClass="java.util.HashMap" parameterClass="java.util.HashMap">
        select to_status_id as status, count(*) num from customer_status_log a where a.to_status_id>0 and a.isRepeat=0
        and a.nonStatistic=1
        <isNotNull prepend="AND" property="projectId">
            project_id = #projectId#
        </isNotNull>
        <isNotNull prepend="AND" property="startTime">
            <![CDATA[	create_time >= #startTime# ]]>
        </isNotNull>
        <isNotNull prepend="AND" property="endTime">
            <![CDATA[	create_time < #endTime# ]]>
        </isNotNull>
        <isNotNull prepend="AND" property="isRepeat">
            isRepeat = #isRepeat#
        </isNotNull>
        <isNotNull prepend="AND" property="sameStatus">
            (from_status_id is null or to_status_id != from_status_id)
        </isNotNull>
        group by to_status_id order by status asc
    </select>

    <!--项目来源分析-->
    <select id="getProjectSource" resultClass="java.util.HashMap" parameterClass="projectSrouceParaPo">
        select cp.project_id projectId, count(*) num ,
        (select projectName from project p where p.id=cp.project_id) projectName,
        (select act_source from customer c where c.id=cp.customer_id) source
        from customer_status_log cp where cp.to_status_id>0 and cp.isRepeat=0 and cp.nonStatistic=1
        <isNotNull prepend="AND" property="isRepeat">
            cp.isRepeat = #isRepeat#
        </isNotNull>
        <isNotNull prepend="AND" property="status">
            <![CDATA[  cp.to_status_id = #status#  ]]>
        </isNotNull>
        <isNotEmpty prepend="AND" property="projectIds">
            cp.project_id in
            <iterate open="(" close=")" conjunction="," property="projectIds">
                <![CDATA[  #projectIds[]# ]]>
            </iterate>
        </isNotEmpty>
        <isNotNull prepend="AND" property="startTime">
            <![CDATA[	cp.create_time >= #startTime# ]]>
        </isNotNull>
        <isNotNull prepend="AND" property="endTime">
            <![CDATA[	cp.create_time < #endTime# ]]>
        </isNotNull>
        <isNotNull prepend="AND" property="sameStatus">
            (from_status_id is null or to_status_id != from_status_id)
        </isNotNull>
        group by projectName, source
    </select>

    <select id="backgetProjectSource" resultClass="java.util.HashMap" parameterClass="projectSrouceParaPo">
        select cp.project_name projectName,cp.project_id projectId, count(*) num ,
        (select act_source from customer c where c.id=cp.customer_id) source
        from customer_project cp
        <dynamic prepend="WHERE">
            <isNotNull prepend="AND" property="isRepeat">
                cp.is_repeat = #isRepeat#
            </isNotNull>
            <isNotNull prepend="AND" property="status">
                <![CDATA[  cp.status >= #status#  ]]>
            </isNotNull>
            <isNotEmpty prepend="AND" property="projectIds">
                cp.project_id in
                <iterate open="(" close=")" conjunction="," property="projectIds">
                    <![CDATA[  #projectIds[]# ]]>
                </iterate>
            </isNotEmpty>
            <isNotNull prepend="AND" property="startTime">
                <![CDATA[	cp.last_change_status_time >= #startTime# ]]>
            </isNotNull>
            <isNotNull prepend="AND" property="endTime">
                <![CDATA[	cp.last_change_status_time < #endTime# ]]>
            </isNotNull>
        </dynamic>
        group by project_name, source
    </select>

</sqlMap>
