<?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="org.jeecg.modules.adweb.dmp.mapper.GADailyReportMapper">

    <select id="getPeriodicStats" resultType="org.jeecg.modules.adweb.dmp.vo.report.PeriodicStatsVO">
        SELECT
        tp.period AS period,
        COALESCE(SUM(d.total_users), 0) AS total_users,
        COALESCE(SUM(d.page_views), 0) AS page_views
        FROM
        (
        SELECT 'today' AS period
        UNION ALL SELECT 'yesterday'
        UNION ALL SELECT 'thisWeek'
        UNION ALL SELECT 'lastWeek'
        UNION ALL SELECT 'thisMonth'
        UNION ALL SELECT 'lastMonth'
        UNION ALL SELECT 'allTime'
        ) tp
        LEFT JOIN
        adweb_dmp_ga_daily_report d
        ON
        d.site_code = #{siteCode}
        AND (
        (tp.period = 'today' AND DATE(d.date) = CURDATE()) OR
        (tp.period = 'yesterday' AND DATE(d.date) = CURDATE() - INTERVAL 1 DAY) OR
        (tp.period = 'thisWeek' AND YEARWEEK(d.date, 1) = YEARWEEK(CURDATE(), 1)) OR
        (tp.period = 'lastWeek' AND YEARWEEK(d.date, 1) = YEARWEEK(CURDATE() - INTERVAL 1 WEEK, 1)) OR
        (tp.period = 'thisMonth' AND YEAR(d.date) = YEAR(CURDATE()) AND MONTH(d.date) = MONTH(CURDATE())) OR
        (tp.period = 'lastMonth' AND YEAR(d.date) = YEAR(CURDATE() - INTERVAL 1 MONTH) AND MONTH(d.date) =
        MONTH(CURDATE() - INTERVAL 1 MONTH)) OR
        (tp.period = 'allTime')
        )
        GROUP BY
        tp.period;
    </select>
</mapper>