<?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.LocalReportEntity">
    <typeAlias alias="localReportEntity" type="com.house365.beans.entity.LocalReportEntity" />
    <typeAlias alias="reportDailyPo" type="com.house365.beans.po.ReportDailyPo"/>
    <resultMap id="localReport" class="com.house365.beans.entity.LocalReportEntity">
        <result property="id" column="id" />
        <result property="projectId" column="project_id" />
        <result property="channelId" column="channel_id" />
        <result property="departId" column="depart_id" />
        <result property="userId" column="user_id" />
        <result property="name" column="name" />
        <result property="record" column="record" />
        <result property="visit" column="visit" />
        <result property="buy" column="buy" />
        <result property="score" column="score" />
        <result property="city" column="city" />
        <result property="status" column="status" />
        <result property="creater" column="creater" />
        <result property="createTime" column="create_time" />
        <result property="lastModifier" column="last_modifier" />
        <result property="updateTime" column="update_time" />
        <result property="reportDate" column="report_date" />
        <result property="buyorder" column="buyorder" />
    </resultMap>

    <!-- 用于select查询公用抽取的列 -->
    <sql id="localReport.columns">
        <![CDATA[
        id, channel_id, depart_id, name, record, visit, buy, score, city, status, creater,
        create_time, last_modifier, update_time, report_date, buyorder, user_id, project_id
	    ]]>
    </sql>

    <!-- 新增记录 -->
    <insert id="save" parameterClass="localReportEntity">
        <![CDATA[
	        INSERT INTO local_report (
	        	id, 
	        	channel_id, 
	        	depart_id, 
	        	name, 
	        	record, 
	        	visit, 
	        	buy, 
	        	score, 
	        	city, 
	        	status, 
	        	creater, 
	        	create_time, 
	        	last_modifier, 
	        	update_time, 
	        	report_date, 
	        	buyorder,
	        	user_id,
	        	project_id
	        ) VALUES (
	        	#id#, 
	        	#channelId#, 
	        	#departId#, 
	        	#name#, 
	        	#record#, 
	        	#visit#, 
	        	#buy#, 
	        	#score#, 
	        	#city#, 
	        	#status#, 
	        	#creater#, 
	        	#createTime#, 
	        	#lastModifier#, 
	        	#updateTime#, 
	        	#reportDate#, 
	        	#buyorder#,
	        	#userId#,
	        	#projectId#
	        )
	    ]]>
        <selectKey resultClass="java.lang.Integer" type="post" keyProperty="id">
            SELECT LAST_INSERT_ID()
        </selectKey>
    </insert>

    <!-- 通过ID修改 -->
    <update id="update" parameterClass="localReportEntity">
        <![CDATA[
	        UPDATE local_report SET
		        channel_id = #channelId#, 
		        depart_id = #departId#, 
		        name = #name#, 
		        record = #record#, 
		        visit = #visit#, 
		        buy = #buy#, 
		        score = #score#, 
		        city = #city#, 
		        status = #status#, 
		        creater = #creater#, 
		        create_time = #createTime#, 
		        last_modifier = #lastModifier#, 
		        update_time = #updateTime#, 
		        report_date = #reportDate#, 
		        buyorder = #buyorder#,
		        user_id = #userId#,
		        project_id = #projectId#
	        WHERE 
		        id = #id#
	    ]]>
    </update>

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

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

    <delete id="cleanDayReportByProject">
        <![CDATA[
	        DELETE FROM local_report WHERE
	        project_id = #projectId# and date_format(report_date,'%Y-%m-%d') = #reportDate#
	    ]]>
    </delete>

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

    <!-- 用于分页查询抽取的公共的条件 -->
    <sql id="localReport.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.channelId">
                    <![CDATA[
					channel_id $searchFilters.channelId.operation$ #searchFilters.channelId.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.departId">
                    <![CDATA[
					depart_id $searchFilters.departId.operation$ #searchFilters.departId.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.projectId">
                    <![CDATA[
					project_id $searchFilters.projectId.operation$ #searchFilters.projectId.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.name">
                    <![CDATA[
					name $searchFilters.name.operation$ #searchFilters.name.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.record">
                    <![CDATA[
					record $searchFilters.record.operation$ #searchFilters.record.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.visit">
                    <![CDATA[
					visit $searchFilters.visit.operation$ #searchFilters.visit.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.buy">
                    <![CDATA[
					buy $searchFilters.buy.operation$ #searchFilters.buy.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.score">
                    <![CDATA[
					score $searchFilters.score.operation$ #searchFilters.score.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.city">
                    <![CDATA[
					city $searchFilters.city.operation$ #searchFilters.city.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.status">
                    <![CDATA[
					status $searchFilters.status.operation$ #searchFilters.status.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.lastModifier">
                    <![CDATA[
					last_modifier $searchFilters.lastModifier.operation$ #searchFilters.lastModifier.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.updateTime">
                    <![CDATA[
					update_time $searchFilters.updateTime.operation$ #searchFilters.updateTime.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.reportDate">
                    <![CDATA[
					report_date $searchFilters.reportDate.operation$ #searchFilters.reportDate.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.buyorder">
                    <![CDATA[
					buyorder $searchFilters.buyorder.operation$ #searchFilters.buyorder.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.userId">
                    <![CDATA[
					user_id $searchFilters.userId.operation$ #searchFilters.userId.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 local_report
        <include refid="localReport.queryPage" />
    </select>

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

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

    <select id="queryFormVORecord" resultClass="com.house365.beans.vo.LocalReportFormVo">
        select t1.*,d.name AS departName from (
        SELECT
        l.user_id AS userId,
        l.name AS name,
        rc.name AS channelName,
        l.channel_id AS channelId,
        l.depart_id AS departId,
        record,
        visit,
        buy,
        score,
        buyorder,
        rc.channel_type as channelType
        FROM
        local_report l,
        report_channel rc
        WHERE
        rc.id = l.channel_id
        AND rc.city = #city#
        AND DATE_FORMAT(l.report_date, '%Y-%m-%d') = #reportDate#
        AND l.project_id = #projectId#
        ORDER BY channelid , departid , name) t1 left join department d on t1.departId = d.id;
    </select>

    <select id="queryFormVO" resultClass="com.house365.beans.vo.LocalReportFormVo">
        select u.id as userId,u.realName as name,rc.name as channelName,rc.id as channelId,d.id as departId,d.name as
        departName,
        0 as record, 0 as visit, 0 as buy, 0 as score,0 as buyorder
        from user u,report_channel rc,report_channel_depart_rel rcdr,department d
        where u.enabled=1 and rc.id=rcdr.channel_id and rcdr.depart_id=d.id and u.deptId=d.id and rc.status=1 and rc.city=#city#
        order by channelid,departid,name
    </select>

    <select id="countStatistic" resultClass="int">
        select count(0) from (
        SELECT
        l.user_id AS userId,
        l.name AS name,
        rc.name AS channelName,
        l.channel_id AS channelId,
        l.depart_id AS departId,
        l.project_id as projectId,
        <isNotEmpty property="searchFilters.departId">
            $searchFilters.departId.value$ as stid,
        </isNotEmpty>
        SUM(record) record,
        SUM(visit) visit,
        SUM(buy) buy,
        SUM(score) score,
        SUM(buyorder) buyorder,
        $searchFilters.period.value$ as reportDate
        FROM
        local_report l, report_channel rc
        where rc.id = l.channel_id
        <dynamic>
            <isNotEmpty prepend="AND" property="searchFilters.channelId">
                l.channel_id in ($searchFilters.channelId.value$)
            </isNotEmpty>
            <isNotEmpty prepend="AND" property="searchFilters.projectId">
                l.project_id in ($searchFilters.projectId.value$)
            </isNotEmpty>
            <isNotEmpty prepend="AND" property="searchFilters.departId">
                l.depart_id in (select id from department where urlPath like (select concat(urlpath,'%') from department where id=$searchFilters.departId.value$))
            </isNotEmpty>
            <isNotEmpty prepend="AND" property="searchFilters.userId">
                l.user_id in ($searchFilters.userId.value$)
            </isNotEmpty>
            <isNotEmpty prepend="AND" property="searchFilters.createTime">
                <![CDATA[
					l.report_date >= #searchFilters.createTime.value#
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="AND" property="searchFilters.andCreateTime">
                <![CDATA[
					l.report_date < #searchFilters.andCreateTime.value#
		            ]]>
            </isNotEmpty>

            <isNotEmpty property="searchFilters.groupby">
                GROUP BY $searchFilters.groupby.value$
            </isNotEmpty>
        </dynamic>
        ) a
    </select>

    <select id="statistic" resultClass="com.house365.beans.vo.LocalReportFormVo">
        select t1.*,d.name AS departName from (
        SELECT
        l.user_id AS userId,
        l.name AS name,
        rc.name AS channelName,
        l.channel_id AS channelId,
        l.depart_id AS departId,
        l.project_id AS projectId,
        p.projectname,
        <isNotEmpty property="searchFilters.departId">
            $searchFilters.departId.value$ as stid,
        </isNotEmpty>
        <isEmpty property="searchFilters.departId">
            l.depart_id as stid,
        </isEmpty>
        SUM(record) record,
        SUM(visit) visit,
        SUM(buy) buy,
        SUM(score) score,
        SUM(buyorder) buyorder
        <isNotEmpty property="searchFilters.period">
            , $searchFilters.period.value$ AS reportDate
        </isNotEmpty>
        FROM
        local_report l,
        report_channel rc,
        project p
        WHERE rc.id = l.channel_id AND l.project_id = p.id
        <dynamic>
            <isNotEmpty prepend="AND" property="searchFilters.channelId">
                l.channel_id in ($searchFilters.channelId.value$)
            </isNotEmpty>
            <isNotEmpty prepend="AND" property="searchFilters.projectId">
                l.project_id in ($searchFilters.projectId.value$)
            </isNotEmpty>
            <isNotEmpty prepend="AND" property="searchFilters.departId">
                l.depart_id in (select id from department where urlPath like (select concat(urlpath,'%') from department where id=$searchFilters.departId.value$))
            </isNotEmpty>
            <isNotEmpty prepend="AND" property="searchFilters.userId">
                l.user_id in ($searchFilters.userId.value$)
            </isNotEmpty>
            <isNotEmpty prepend="AND" property="searchFilters.createTime">
                <![CDATA[
					l.report_date >= #searchFilters.createTime.value#
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="AND" property="searchFilters.andCreateTime">
                <![CDATA[
					l.report_date < #searchFilters.andCreateTime.value#
		            ]]>
            </isNotEmpty>
            <isNotEmpty property="searchFilters.groupby">
                GROUP BY $searchFilters.groupby.value$
            </isNotEmpty>
        </dynamic>
        ORDER BY DATE_FORMAT(l.report_date, '%Y%m%d') desc,project_id, channel_id, stid
        ) t1 left join department d on t1.stid=d.id left join user u on t1.userId=u.id
        LIMIT #paging.startRecord#, #paging.pageSize#
    </select>

    <!--<select id="statistic" resultClass="com.house365.beans.vo.LocalReportFormVo">-->
        <!--select t1.*,d.name AS departName from (-->
        <!--SELECT-->
        <!--l.user_id AS userId,-->
        <!--l.name AS name,-->
        <!--rc.name AS channelName,-->
        <!--l.channel_id AS channelId,-->
        <!--l.depart_id AS departId,-->
        <!--l.project_id AS projectId,-->
        <!--p.projectname,-->
        <!--SUM(record) record,-->
        <!--SUM(visit) visit,-->
        <!--SUM(buy) buy,-->
        <!--SUM(score) score,-->
        <!--SUM(buyorder) buyorder,-->
        <!--DATE_FORMAT(l.report_date, '%Y%m%d') AS reportDate-->
        <!--FROM-->
        <!--local_report l,-->
        <!--report_channel rc,-->
        <!--project p-->
        <!--WHERE-->
        <!--rc.id = l.channel_id-->
        <!--AND l.project_id = p.id-->
        <!--<dynamic>-->
            <!--<isNotEmpty prepend="AND" property="searchFilters.channelId">-->
                <!--l.channel_id in ($searchFilters.channelId.value$)-->
            <!--</isNotEmpty>-->
            <!--<isNotEmpty prepend="AND" property="searchFilters.projectId">-->
                <!--l.project_id in ($searchFilters.projectId.value$)-->
            <!--</isNotEmpty>-->
            <!--<isNotEmpty prepend="AND" property="searchFilters.departId">-->
                <!--l.depart_id in ($searchFilters.departId.value$)-->
            <!--</isNotEmpty>-->
            <!--<isNotEmpty prepend="AND" property="searchFilters.userId">-->
                <!--l.user_id in ($searchFilters.userId.value$)-->
            <!--</isNotEmpty>-->
            <!--<isNotEmpty prepend="AND" property="searchFilters.createTime">-->
                <!--<![CDATA[-->
					<!--l.report_date >= #searchFilters.createTime.value#-->
		            <!--]]>-->
            <!--</isNotEmpty>-->
            <!--<isNotEmpty prepend="AND" property="searchFilters.andCreateTime">-->
                <!--<![CDATA[-->
					<!--l.report_date < #searchFilters.andCreateTime.value#-->
		            <!--]]>-->
            <!--</isNotEmpty>-->
            <!--<isNotEmpty property="searchFilters.groupby">-->
                <!--GROUP BY $searchFilters.groupby.value$-->
            <!--</isNotEmpty>-->
        <!--</dynamic>-->
        <!--ORDER BY channelid , departid , name-->
        <!--) t1 left join department d on t1.departId=d.id-->
        <!--LIMIT #paging.startRecord#, #paging.pageSize#-->
    <!--</select>-->

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