<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yunzhi.marketing.mapper.TsActivityDailyMapper">

    <select id="getActivityDetail" resultType="com.yunzhi.marketing.entity.TsActivityDaily">
        SELECT
            a.statis_date,
            IFNULL(b.share_num, 0) as share_num,
            IFNULL(b.share_persons, 0) as share_persons,
            IFNULL(b.visit_num, 0) as visit_num,
            IFNULL(b.visit_persons, 0) as visit_persons,
            IFNULL(b.new_persons, 0) as new_persons,
            IFNULL(b.new_customers, 0) as new_customers
        FROM
        (
            SELECT
                DATE_FORMAT(
                    DATE_ADD( STR_TO_DATE( #{startDate}, '%Y-%m-%d' ), INTERVAL rownum DAY ), '%Y-%m-%d' ) AS statis_date
            FROM sequence
            WHERE rownum &lt;= DATEDIFF(STR_TO_DATE( #{endDate}, '%Y-%m-%d' ), STR_TO_DATE( #{startDate}, '%Y-%m-%d' ))
        ) a
        LEFT JOIN (
            SELECT
                t.statis_date,
                SUM(t.share_num) as share_num,
                SUM(t.share_persons) as share_persons,
                SUM(t.visit_num) as visit_num,
                SUM(t.visit_persons) as visit_persons,
                SUM(t.new_persons) as new_persons,
                SUM(t.new_customers) as new_customers
            FROM
                ts_activity_daily t
            WHERE
                t.org_id = #{orgId}
                AND t.statis_date BETWEEN #{startDate} AND #{endDate}

            <if test="buildingId != null and buildingId != ''">
                AND t.building_id = #{buildingId}
            </if>

            <if test="targetType != null and targetType != ''">
                AND t.target_type = #{targetType}
            </if>

            <if test="targetId != null and targetId != ''">
                AND t.target_id = #{targetId}
            </if>

            <if test="targetName != null and targetName != ''">
                AND t.target_name like CONCAT('%', #{targetName}, '%')
            </if>

            GROUP BY t.statis_date
        ) b ON a.statis_date = b.statis_date

        <if test="asc != null and asc == 'statis_date'">
            ORDER BY a.statis_date ASC
        </if>
        <if test="desc != null and desc == 'statis_date'">
            ORDER BY a.statis_date DESC
        </if>

        <if test="asc != null and asc != '' and asc != 'statis_date'">
            ORDER BY b.${asc} ASC
        </if>
        <if test="desc != null and desc != '' and desc != 'statis_date'">
            ORDER BY b.${desc} DESC
        </if>
    </select>

    <select id="getActivityExport" resultType="com.yunzhi.marketing.excel.ActivityDailyExport">
        SELECT
            t.statis_date,
            t.target_type,
            t.target_id,
            t.target_name,
            SUM(t.share_num) as share_num,
            SUM(t.share_persons) as share_persons,
            SUM(t.visit_num) as visit_num,
            SUM(t.visit_persons) as visit_persons,
            SUM(t.new_persons) as new_persons,
            SUM(t.new_customers) as new_customers
        FROM
            ts_activity_daily t
        WHERE
            t.org_id = #{orgId}
            AND t.statis_date BETWEEN #{startDate} AND #{endDate}

        <if test="buildingId != null and buildingId != ''">
            AND t.building_id = #{buildingId}
        </if>

        AND t.target_type = #{targetType}
        AND t.target_id = #{targetId}

        GROUP BY t.statis_date, t.target_type, t.target_id
        ORDER BY t.statis_date DESC
    </select>
</mapper>
