<?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.ProjectSourceStatisticsEntity">
    <typeAlias alias="projectSrouceParaPo" type="com.house365.beans.po.ProjectSrouceParaPo" />
    <typeAlias alias="projectSrouceRePo" type="com.house365.beans.po.ProjectSrouceRePo" />
    <typeAlias alias="projectSourceStatisticsEntity" type="com.house365.beans.entity.ProjectSourceStatisticsEntity" />
    <resultMap id="projectSourceStatistics" class="com.house365.beans.entity.ProjectSourceStatisticsEntity">
        <result property="id" column="id" />
        <result property="projectId" column="projectId" />
        <result property="projectName" column="projectName" />
        <result property="objectType" column="objectType" />
        <result property="customerStatus" column="customerStatus" />
        <result property="customerNum" column="customerNum" />
        <result property="sourceId" column="sourceId" />
        <result property="sourceName" column="sourceName" />
        <result property="city" column="city" />
        <result property="cityName" column="cityName" />
        <result property="dayBaseRatio" column="dayBaseRatio" />
        <result property="createTime" column="createTime" />
    </resultMap>

    <!-- 用于select查询公用抽取的列 -->
    <sql id="projectSourceStatistics.columns">
        <![CDATA[
        id, projectId, projectName, customerStatus, customerNum, sourceId, sourceName, city, cityName, dayBaseRatio, createTime, objectType
	    ]]>
    </sql>

    <!-- 新增记录 -->
    <insert id="save" parameterClass="projectSourceStatisticsEntity">
        <![CDATA[
	        INSERT INTO project_source_statistics (
	        	id, 
	        	projectId, 
	        	projectName, 
	        	customerStatus, 
	        	customerNum, 
	        	sourceId, 
	        	sourceName, 
	        	city, 
	        	cityName, 
	        	dayBaseRatio, 
	        	createTime,
	        	objectType
	        ) VALUES (
	        	#id#, 
	        	#projectId#, 
	        	#projectName#, 
	        	#customerStatus#, 
	        	#customerNum#, 
	        	#sourceId#, 
	        	#sourceName#, 
	        	#city#, 
	        	#cityName#, 
	        	#dayBaseRatio#, 
	        	#createTime#,
	        	#objectType#
	        )
	    ]]>
        <selectKey resultClass="java.lang.Integer" type="post" keyProperty="id">
            SELECT LAST_INSERT_ID()
        </selectKey>
    </insert>

    <!-- 通过ID修改 -->
    <update id="update" parameterClass="projectSourceStatisticsEntity">
        <![CDATA[
	        UPDATE project_source_statistics SET
		        projectId = #projectId#, 
		        projectName = #projectName#, 
		        customerStatus = #customerStatus#, 
		        customerNum = #customerNum#, 
		        sourceId = #sourceId#, 
		        sourceName = #sourceName#, 
		        city = #city#, 
		        cityName = #cityName#, 
		        dayBaseRatio = #dayBaseRatio#, 
		        createTime = #createTime#,
		        objectType = #objectType#
	        WHERE 
		        id = #id#
	    ]]>
    </update>

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

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

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

    <!-- 用于分页查询抽取的公共的条件 -->
    <sql id="projectSourceStatistics.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.projectId">
                    <![CDATA[
					projectId $searchFilters.projectId.operation$ #searchFilters.projectId.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.projectIds">
                    projectId $searchFilters.projectIds.operation$
                    <iterate open="(" close=")" conjunction="," property="searchFilters.projectIds.value">
                        #searchFilters.projectIds.value[]#
                    </iterate>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.projectName">
                    <![CDATA[
					projectName $searchFilters.projectName.operation$ #searchFilters.projectName.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.objectType">
                    <![CDATA[
					objectType $searchFilters.objectType.operation$ #searchFilters.objectType.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.customerStatus">
                    <![CDATA[
					customerStatus $searchFilters.customerStatus.operation$ #searchFilters.customerStatus.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.customerNum">
                    <![CDATA[
					customerNum $searchFilters.customerNum.operation$ #searchFilters.customerNum.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.sourceId">
                    <![CDATA[
					sourceId $searchFilters.sourceId.operation$ #searchFilters.sourceId.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.sourceName">
                    <![CDATA[
					sourceName $searchFilters.sourceName.operation$ #searchFilters.sourceName.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.city">
                    <![CDATA[
					city $searchFilters.city.operation$ #searchFilters.city.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.cityName">
                    <![CDATA[
					cityName $searchFilters.cityName.operation$ #searchFilters.cityName.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.dayBaseRatio">
                    <![CDATA[
					dayBaseRatio $searchFilters.dayBaseRatio.operation$ #searchFilters.dayBaseRatio.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.createTime">
                    <![CDATA[
					createTime $searchFilters.createTime.operation$ #searchFilters.createTime.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 project_source_statistics
        <include refid="projectSourceStatistics.queryPage" />
    </select>

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

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

    <select id="queryByDay" parameterClass="projectSrouceParaPo" resultClass="projectSrouceRePo">
        select project_id as projectId,DATE_FORMAT(create_time,'%Y%m%d') time,
        (select act_source from customer c where c.id=cp.customer_id) sourceName,
        count(project_id) count ,
        (select projectName from project p where p.id=cp.project_id) projectName
        from customer_status_log cp
        <dynamic prepend="WHERE">
            <isNotNull prepend="AND" property="isRepeat">
                cp.to_status_id>0 and cp.isRepeat=0 and cp.nonStatistic=1
            </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>
        </dynamic>
        group by projectId,sourceName,time order by projectId,sourceName,time
    </select>

    <select id="queryByWeek" parameterClass="projectSrouceParaPo" resultClass="projectSrouceRePo">
        select project_id as projectId,DATE_FORMAT(create_time,'%Y%u') time,
        (select act_source from customer c where c.id=cp.customer_id) sourceName,
        count(project_id) count ,
        (select projectName from project p where p.id=cp.project_id) projectName
        from customer_status_log cp
        <dynamic prepend="WHERE">
            <isNotNull prepend="AND" property="isRepeat">
                cp.to_status_id>0 and cp.isRepeat=0 and cp.nonStatistic=1
            </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>
        </dynamic>
        group by projectId,sourceName,time order by projectId,sourceName,time
    </select>

    <select id="queryByMonth" parameterClass="projectSrouceParaPo" resultClass="projectSrouceRePo">
        select project_id as projectId,DATE_FORMAT(create_time,'%Y%m') time,
        (select act_source from customer c where c.id=cp.customer_id) sourceName,
        count(project_id) count ,
        (select projectName from project p where p.id=cp.project_id) projectName
        from customer_status_log cp
        <dynamic prepend="WHERE">
            <isNotNull prepend="AND" property="isRepeat">
                cp.to_status_id>0 and cp.isRepeat=0 and cp.nonStatistic=1
            </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>
        </dynamic>
        group by projectId,sourceName,time order by projectId,sourceName,time
    </select>

    <select id="backqueryByDay" parameterClass="projectSrouceParaPo" resultClass="projectSrouceRePo">
        select project_id as projectId,DATE_FORMAT(last_change_status_time,'%Y%m%d') time,
        (select act_source from customer c where c.id=cp.customer_id) sourceName,
        count(project_id) count ,
        (select projectName from project p where p.id=cp.project_id) projectName
        from customer_project cp
        <dynamic prepend="WHERE">
            <isNotNull prepend="AND" property="isRepeat">
                cp.to_status_id>0 and cp.isRepeat=0 and cp.nonStatistic=1
            </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 projectId,sourceName,time order by projectId,sourceName,time
    </select>

    <select id="backqueryByWeek" parameterClass="projectSrouceParaPo" resultClass="projectSrouceRePo">
        select project_id as projectId,DATE_FORMAT(last_change_status_time,'%Y%u') time,
        (select act_source from customer c where c.id=cp.customer_id) sourceName,
        count(project_id) count ,
        (select projectName from project p where p.id=cp.project_id) projectName
        from customer_project cp
        <dynamic prepend="WHERE">
            <isNotNull prepend="AND" property="isRepeat">
                cp.to_status_id>0 and cp.isRepeat=0 and cp.nonStatistic=1
            </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 projectId,sourceName,time order by projectId,sourceName,time
    </select>

    <select id="backqueryByMonth" parameterClass="projectSrouceParaPo" resultClass="projectSrouceRePo">
        select project_id as projectId,DATE_FORMAT(last_change_status_time,'%Y%m') time,
        (select act_source from customer c where c.id=cp.customer_id) sourceName,
        count(project_id) count ,
        (select projectName from project p where p.id=cp.project_id) projectName
        from customer_project cp
        <dynamic prepend="WHERE">
            <isNotNull prepend="AND" property="isRepeat">
                cp.to_status_id>0 and cp.isRepeat=0 and cp.nonStatistic=1
            </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 projectId,sourceName,time order by projectId,sourceName,time
    </select>

    <sql id="projectSourceStatistics.queryPage1">
        <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.projectId">
                    <![CDATA[
					projectId $searchFilters.projectId.operation$ #searchFilters.projectId.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.projectIds">
                    projectId $searchFilters.projectIds.operation$
                    <iterate open="(" close=")" conjunction="," property="searchFilters.projectIds.value">
                        #searchFilters.projectIds.value[]#
                    </iterate>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.projectName">
                    <![CDATA[
					projectName $searchFilters.projectName.operation$ #searchFilters.projectName.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.objectType">
                    <![CDATA[
					objectType $searchFilters.objectType.operation$ #searchFilters.objectType.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.customerStatus">
                    <![CDATA[
					customerStatus $searchFilters.customerStatus.operation$ #searchFilters.customerStatus.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.customerNum">
                    <![CDATA[
					customerNum $searchFilters.customerNum.operation$ #searchFilters.customerNum.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.sourceId">
                    <![CDATA[
					sourceId $searchFilters.sourceId.operation$ #searchFilters.sourceId.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.sourceName">
                    <![CDATA[
					sourceName $searchFilters.sourceName.operation$ #searchFilters.sourceName.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.city">
                    <![CDATA[
					city $searchFilters.city.operation$ #searchFilters.city.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.cityName">
                    <![CDATA[
					cityName $searchFilters.cityName.operation$ #searchFilters.cityName.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.dayBaseRatio">
                    <![CDATA[
					dayBaseRatio $searchFilters.dayBaseRatio.operation$ #searchFilters.dayBaseRatio.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.createTime">
                    <![CDATA[
					createTime $searchFilters.createTime.operation$ #searchFilters.createTime.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.andCreateTime">
                    <![CDATA[
					createTime $searchFilters.andCreateTime.operation$ #searchFilters.andCreateTime.value#
		            ]]>
                </isNotEmpty>
            </isNotNull>
        </dynamic>
    </sql>

    <select id="statisticCustomer" resultMap="projectSourceStatistics">
        <![CDATA[
        select a.*, (
        select IFNULL(sum(customernum),0) customerNum1 from user_source_statistics a1
        where a1.cityname=a.cityname and a1.customerStatus=a.customerStatus and a1.projectName=a.projectName
        and DATE_FORMAT(DATE_ADD(a1.createTime,INTERVAL 1 DAY),'%Y%m%d')=DATE_FORMAT(a.createTime,'%Y%m%d')
        ) prenum from (
        SELECT min(id) id,cityname,customerStatus,DATE_FORMAT(createTime,'%Y%m%d') createTime,projectName,sum(customernum) customerNum
        FROM project_source_statistics ]]>
        <include refid="projectSourceStatistics.queryPage1" />
        <![CDATA[
        group by cityname,customerStatus,DATE_FORMAT(createTime,'%Y%m%d'),projectName
        ]]>
        <dynamic prepend="ORDER BY">
            <isNotNull property="orderColumn">
                <![CDATA[
				$orderColumn$
				]]>
                <isNotNull property="orderMode">
                    $orderMode$
                </isNotNull>
            </isNotNull>
        </dynamic>
        <![CDATA[
		    LIMIT #paging.startRecord#, #paging.pageSize#
        ]]>
        ) a
    </select>

    <select id="countStatisticCustomer" resultClass="int">
        select count(0) from (
        SELECT min(id) id,cityname,customerStatus,createTime,projectName,sum(customernum) customerNum
        FROM project_source_statistics
        <include refid="projectSourceStatistics.queryPage1" />
        <![CDATA[
        group by cityname,customerStatus,DATE_FORMAT(createTime,'%Y%m%d'),projectName
        ) a
        ]]>
    </select>

    <update id="updateProjectName">
        update project_source_statistics b inner join (select id,projectname from project) a on a.id=b.projectid
        set b.projectName=a.projectname
    </update>

    <update id="updateSourceName">
        update project_source_statistics b inner join (select id,source_name from customer_source) a on a.id=b.sourceid
        set b.sourcename=a.source_name
    </update>
    <update id="updateCityName">
        update project_source_statistics b inner join (select descripition,name from department) a on a.descripition=b.city
        set b.cityname=a.name
    </update>

    <delete id="deleteByDate">
        delete from project_source_statistics where createTime = #startDateStr#
    </delete>

</sqlMap>
