<?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="top.charles7c.cnadmin.monitor.mapper.LogMapper">
    <select id="selectDashboardTotal" resultType="top.charles7c.cnadmin.monitor.model.vo.DashboardTotalVO">
        SELECT
            (SELECT COUNT(*) FROM `sys_log`) AS pvCount,
            (SELECT COUNT(DISTINCT `client_ip`) FROM `sys_log`) AS ipCount,
            (SELECT COUNT(*) FROM `sys_log` WHERE DATE(`create_time`) = CURDATE()) AS todayPvCount,
            (SELECT COUNT(*) FROM `sys_log` WHERE DATE(`create_time`) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)) AS yesterdayPvCount
    </select>

    <select id="selectListDashboardAccessTrend"
            resultType="top.charles7c.cnadmin.monitor.model.vo.DashboardAccessTrendVO">
        SELECT
            DATE(`create_time`) AS date,
            COUNT(*) AS pvCount,
            COUNT(DISTINCT `client_ip`) AS ipCount
        FROM `sys_log`
        GROUP BY DATE(`create_time`)
        HAVING `date` != CURDATE()
        ORDER BY DATE(`create_time`) DESC
        LIMIT #{days}
    </select>

    <select id="selectListDashboardPopularModule"
            resultType="top.charles7c.cnadmin.monitor.model.vo.DashboardPopularModuleVO">
        SELECT
            `module`,
            COUNT(*) AS pvCount,
            SUM(CASE WHEN DATE(`create_time`) = CURDATE() THEN 1 ELSE 0 END) AS todayPvCount,
            SUM(CASE WHEN DATE(`create_time`) = DATE_SUB(CURDATE(), INTERVAL 1 DAY) THEN 1 ELSE 0 END) AS yesterdayPvCount
        FROM `sys_log`
        GROUP BY `module`
        HAVING `module` != '验证码' AND `module` != '登录'
        ORDER BY `pvCount` DESC
        LIMIT 10
    </select>

    <select id="selectListDashboardGeoDistribution" resultType="java.util.Map">
        SELECT
            `location` AS name,
            COUNT(DISTINCT `client_ip`) AS value
        FROM `sys_log`
        GROUP BY `location`
        ORDER BY COUNT(DISTINCT `client_ip`) DESC
        LIMIT 10
    </select>
</mapper>