<?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.ReportMarketingEntity">
	<typeAlias alias="reportMarketingEntity"
			   type="com.house365.beans.entity.ReportMarketingEntity"/>
	<typeAlias alias="reportMarketingVo" type="com.house365.beans.vo.ReportMarketingFormVo"/>
	<resultMap id="reportMarketingVo"
			   class="com.house365.beans.vo.ReportMarketingFormVo">
		<result property="id" column="id"/>
		<result property="callCount" column="call_count"/>
		<result property="connectCount" column="connect_count"/>
		<result property="intentBuyCount" column="intent_buy_count"/>
		<result property="visitedCount" column="visited_count"/>
		<result property="identificationChipsCount" column="identification_chips_count"/>
		<result property="identificationChipsProject" column="identification_chips_project"/>
		<result property="subscriptionCount" column="subscription_count"/>
		<result property="subscriptionProject" column="subscription_project"/>
		<result property="source" column="source"/>
		<result property="reportTime" column="report_time"/>
		<result property="departmentId" column="department_id"/>
		<result property="departmentName" column="department_name"/>
		<result property="userId" column="user_id"/>
		<result property="userName" column="user_name"/>
		<result property="city" column="city"/>
	</resultMap>
	<resultMap id="reportMarketing"
			   class="com.house365.beans.entity.ReportMarketingEntity">
		<result property="id" column="id"/>
		<result property="callCount" column="call_count"/>
		<result property="connectCount" column="connect_count"/>
		<result property="intentBuyCount" column="intent_buy_count"/>
		<result property="visitedCount" column="visited_count"/>
		<result property="visitedProject" column="visited_project"/>
		<result property="twiceVisitedCount" column="twice_visited_count"/>
		<result property="twiceVisitedProject" column="twice_visited_project"/>
		<result property="identificationChipsCount" column="identification_chips_count"/>
		<result property="identificationChipsProject" column="identification_chips_project"/>
		<result property="subscriptionCount" column="subscription_count"/>
		<result property="subscriptionProject" column="subscription_project"/>
		<result property="source" column="source"/>
		<result property="createTime" column="create_time"/>
		<result property="departmentId" column="department_id"/>
		<result property="departmentName" column="department_name"/>
		<result property="userId" column="user_id"/>
		<result property="userName" column="user_name"/>
		<result property="reportTime" column="report_time"/>
		<result property="city" column="city"/>
	</resultMap>

	<!-- 用于select查询公用抽取的列 -->
	<sql id="reportMarketing.columns">
		<![CDATA[
        id, call_count, connect_count, intent_buy_count, visited_count,visited_project,twice_visited_count,twice_visited_project, identification_chips_count,
        identification_chips_project, subscription_count, subscription_project,
        source, create_time, department_id, department_name, user_id, user_name, report_time,city
	    ]]>
	</sql>

	<!-- 新增记录 -->
	<insert id="save" parameterClass="reportMarketingEntity">
		<![CDATA[
	        INSERT INTO report_marketing (
	        	id,
	        	call_count,
	        	connect_count,
	        	intent_buy_count,
	        	visited_count,
	        	visited_project,
	        	twice_visited_count,
	        	twice_visited_project,
	        	identification_chips_count,
	        	identification_chips_project,
	        	subscription_count,
	        	subscription_project,
	        	source,
	        	create_time,
	        	department_id,
	        	department_name,
	        	user_id,
	        	user_name,
	        	report_time,
	        	city
	        ) VALUES (
	        	#id#,
	        	#callCount#,
	        	#connectCount#,
	        	#intentBuyCount#,
	        	#visitedCount#,
	        	#visitedProject#,
	        	#twiceVisitedCount#,
	        	#twiceVisitedProject#,
	        	#identificationChipsCount#,
	        	#identificationChipsProject#,
	        	#subscriptionCount#,
	        	#subscriptionProject#,
	        	#source#,
	        	#createTime#,
	        	#departmentId#,
	        	#departmentName#,
	        	#userId#,
	        	#userName#,
	        	#reportTime#,
	        	#city#
	        )
	    ]]>
		<selectKey resultClass="java.lang.Integer" type="post"
				   keyProperty="id">
			SELECT LAST_INSERT_ID()
		</selectKey>
	</insert>

	<!-- 通过ID修改 -->
	<update id="update" parameterClass="reportMarketingEntity">
	    <![CDATA[
	        UPDATE report_marketing SET
		        call_count = #callCount#,
		        connect_count = #connectCount#,
		        intent_buy_count = #intentBuyCount#,
		        visited_count = #visitedCount#,
		        visited_project = #visitedProject#,
		        twice_visited_count = #twiceVisitedCount#,
		        twice_visited_project = #twiceVisitedProject#,
		        identification_chips_count = #identificationChipsCount#, 
		        identification_chips_project = #identificationChipsProject#, 
		        subscription_count = #subscriptionCount#,
		        subscription_project = #subscriptionProject#, 
		        source = #source#,
		        create_time = #createTime#, 
		        department_id = #departmentId#, 
		        department_name = #departmentName#, 
		        user_id = #userId#, 
		        user_name = #userName#, 
		        report_time = #reportTime#,
		        city = #city#
	        WHERE
		        id = #id#
	    ]]>
	</update>

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

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

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

	<!-- 通过userID查询日报信息 -->
	<select id="getByUserId" resultMap="reportMarketing">
		SELECT
		<include refid="reportMarketing.columns"/>
		<![CDATA[
	        FROM report_marketing
	        WHERE
		        user_id = #userId#
		        and report_time = #reportTime#
	    ]]>
	</select>

	<!-- 通过userID和日期修改 -->
	<update id="updateByUserId" parameterClass="reportMarketingEntity">
		<![CDATA[
	        UPDATE report_marketing SET
		        call_count = #callCount#,
		        connect_count = #connectCount#,
		        intent_buy_count = #intentBuyCount#,
		        visited_count = #visitedCount#,
		        visited_project = #visitedProject#,
		        twice_visited_count = #twiceVisitedCount#,
		        twice_visited_project = #twiceVisitedProject#,
		        identification_chips_count = #identificationChipsCount#,
		        identification_chips_project = #identificationChipsProject#,
		        subscription_count = #subscriptionCount#,
		        subscription_project = #subscriptionProject#,
		        source = #source#,
		        create_time = #createTime#,
		        department_id = #departmentId#,
		        department_name = #departmentName#,
		        user_id = #userId#,
		        user_name = #userName#,
		        report_time = #reportTime#,
		        city = #city#
	        WHERE
		        user_id = #userId#
		    and report_time = #reportTime#
	    ]]>
	</update>

	<!-- 用于分页查询抽取的公共的条件 -->
	<sql id="reportMarketing.queryPage">
		<dynamic prepend="WHERE">
			<!-- 默认不开启实体作为条件 -->
			<!--
		    <isNotNull property="entity">
				<isNotEmpty prepend="AND" property="entity.id">
		            <![CDATA[
					id = #entity.id#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.callCount">
		            <![CDATA[
					call_count = #entity.callCount#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.connectCount">
		            <![CDATA[
					connect_count = #entity.connectCount#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.intentBuyCount">
		            <![CDATA[
					intent_buy_count = #entity.intentBuyCount#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.visitedCount">
		            <![CDATA[
					visited_count = #entity.visitedCount#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.identificationChipsCount">
		            <![CDATA[
					identification_chips_count = #entity.identificationChipsCount#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.identificationChipsProject">
		            <![CDATA[
					identification_chips_project = #entity.identificationChipsProject#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.identificationChipsProjectId">
		            <![CDATA[
					identification_chips_project_id = #entity.identificationChipsProjectId#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.subscriptionCount">
		            <![CDATA[
					subscription_count = #entity.subscriptionCount#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.subscriptionProject">
		            <![CDATA[
					subscription_project = #entity.subscriptionProject#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.subscriptionProjectId">
		            <![CDATA[
					subscription_project_id = #entity.subscriptionProjectId#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.source">
		            <![CDATA[
					source = #entity.source#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.createTime">
		            <![CDATA[
					create_time = #entity.createTime#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.departmentId">
		            <![CDATA[
					department_id = #entity.departmentId#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.departmentName">
		            <![CDATA[
					department_name = #entity.departmentName#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.userId">
		            <![CDATA[
					user_id = #entity.userId#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.userName">
		            <![CDATA[
					user_name = #entity.userName#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.reportTime">
		            <![CDATA[
					report_time = #entity.reportTime#
		            ]]>
				</isNotEmpty>
			</isNotNull>
			 -->
			<isNotNull property="searchFilters">
				<isNotEmpty prepend="AND" property="searchFilters.id">
					<![CDATA[
					id $searchFilters.id.operation$ #searchFilters.id.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.callCount">
					<![CDATA[
					call_count $searchFilters.callCount.operation$ #searchFilters.callCount.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.connectCount">
					<![CDATA[
					connect_count $searchFilters.connectCount.operation$ #searchFilters.connectCount.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.intentBuyCount">
					<![CDATA[
					intent_buy_count $searchFilters.intentBuyCount.operation$ #searchFilters.intentBuyCount.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.visitedCount">
					<![CDATA[
					visited_count $searchFilters.visitedCount.operation$ #searchFilters.visitedCount.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.identificationChipsCount">
					<![CDATA[
					identification_chips_count $searchFilters.identificationChipsCount.operation$ #searchFilters.identificationChipsCount.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.identificationChipsProject">
					<![CDATA[
					identification_chips_project $searchFilters.identificationChipsProject.operation$ #searchFilters.identificationChipsProject.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.subscriptionCount">
					<![CDATA[
					subscription_count $searchFilters.subscriptionCount.operation$ #searchFilters.subscriptionCount.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.subscriptionProject">
					<![CDATA[
					subscription_project $searchFilters.subscriptionProject.operation$ #searchFilters.subscriptionProject.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.source">
					<![CDATA[
					source $searchFilters.source.operation$ #searchFilters.source.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.createTime">
					<![CDATA[
					create_time $searchFilters.createTime.operation$ #searchFilters.createTime.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.departmentId">
					<![CDATA[
					department_id $searchFilters.departmentId.operation$ #searchFilters.departmentId.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.departmentName">
					<![CDATA[
					department_name $searchFilters.departmentName.operation$ #searchFilters.departmentName.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.userId">
					<![CDATA[
					user_id $searchFilters.userId.operation$ #searchFilters.userId.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.userIds">
					<![CDATA[ user_id in ]]>
					<iterate prepend="" property="searchFilters.userIds.value" open="(" close=")" conjunction=",">
						<![CDATA[ #searchFilters.userIds.value[]# ]]>
					</iterate>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.userName">
					<![CDATA[
					user_name $searchFilters.userName.operation$ #searchFilters.userName.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.reportTime">
					<![CDATA[
					report_time $searchFilters.reportTime.operation$ #searchFilters.reportTime.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.endReportTime">
					<![CDATA[
					report_time $searchFilters.endReportTime.operation$ #searchFilters.endReportTime.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.city">
					<![CDATA[
					city $searchFilters.city.operation$ #searchFilters.city.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 report_marketing
		<include refid="reportMarketing.queryPage"/>
	</select>

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

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

	<select id="getDailies" resultClass="reportDailyPo">
		select
		DATE_FORMAT( report_time, '%Y-%m-%d' ) time
		from
		report_marketing
		<dynamic prepend="WHERE">
			<isNotEmpty prepend="AND" property="startTime">
				<![CDATA[
					report_time >= #startTime#
				 ]]>
			</isNotEmpty>
			<isNotEmpty prepend="AND" property="endTime">
				<![CDATA[
					report_time <= #endTime#
				]]>
			</isNotEmpty>
			<isNotEmpty prepend="AND" property="userIds">
				<![CDATA[ user_id in ]]>
				<iterate prepend="" property="userIds" open="(" close=")" conjunction=",">
					<![CDATA[ #userIds[]# ]]>
				</iterate>
			</isNotEmpty>
			<isNotEmpty prepend="AND" property="city">
				<![CDATA[
					city = #city#
				]]>
			</isNotEmpty>
		</dynamic>
		group by time;
	</select>

	<select id="countStatistic" resultClass="int">
		select count(0) from (
		select
		rm.id
		from
		report_marketing rm,
		department d
		<dynamic prepend="WHERE">
			rm.department_id = d.id
			<isNotEmpty prepend="AND" property="searchFilters.departIds">
				<iterate prepend="AND" property="searchFilters.departIds.value" open="(" close=")" conjunction="or">
					<![CDATA[d.urlPath like (select concat(urlpath,'%') from department where id=$searchFilters.departIds.value[]$)]]>
				</iterate>
			</isNotEmpty>
			<isNotEmpty prepend="AND" property="searchFilters.userId">
				<![CDATA[user_id in ($searchFilters.userId.value$)]]>
			</isNotEmpty>
			<isNotEmpty prepend="AND" property="searchFilters.createTime">
				<![CDATA[
					report_time >= #searchFilters.createTime.value#
		            ]]>
			</isNotEmpty>
			<isNotEmpty prepend="AND" property="searchFilters.andCreateTime">
				<![CDATA[
					report_time < #searchFilters.andCreateTime.value#
		            ]]>
			</isNotEmpty>
			<isNotEmpty property="searchFilters.groupby">
				GROUP BY $searchFilters.groupby.value$
			</isNotEmpty>
		</dynamic>
		) a
	</select>

	<select id="statistic" resultMap="reportMarketingVo">
		select
		rm.id,
		rm.department_id,
		rm.department_name,
		rm.user_id,
		rm.user_name,
		sum( rm.call_count ) call_count,
		SUM( rm.connect_count ) connect_count,
		SUM( rm.intent_buy_count ) intent_buy_count,
		SUM( rm.visited_count ) visited_count,
		SUM( rm.identification_chips_count ) identification_chips_count,
		SUM( rm.source ) source,
		rm.identification_chips_project,
		SUM( rm.subscription_count ) subscription_count,
		rm.subscription_project,
		rm.city
		<isNotEmpty property="searchFilters.period">
			,$searchFilters.period.value$ AS report_time
		</isNotEmpty>
		from
		report_marketing rm,
		department d
		<dynamic prepend="WHERE">
			rm.department_id = d.id
			<isNotEmpty prepend="AND" property="searchFilters.departIds">
				<iterate prepend="AND" property="searchFilters.departIds.value" open="(" close=")" conjunction="or">
					<![CDATA[d.urlPath like (select concat(urlpath,'%') from department where id=$searchFilters.departIds.value[]$)]]>
				</iterate>
			</isNotEmpty>
			<isNotEmpty prepend="AND" property="searchFilters.userId">
				<![CDATA[user_id in ($searchFilters.userId.value$)]]>
			</isNotEmpty>
			<isNotEmpty prepend="AND" property="searchFilters.createTime">
				<![CDATA[
					report_time >= #searchFilters.createTime.value#
		            ]]>
			</isNotEmpty>
			<isNotEmpty prepend="AND" property="searchFilters.andCreateTime">
				<![CDATA[
					report_time < #searchFilters.andCreateTime.value#
		            ]]>
			</isNotEmpty>
			<isNotEmpty property="searchFilters.groupby">
				GROUP BY $searchFilters.groupby.value$
			</isNotEmpty>
		</dynamic>
		ORDER BY report_time desc, department_id , user_name
	</select>
</sqlMap>