<?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.continew.admin.system.mapper.LogMapper">
    <select id="selectLogPage" resultType="top.continew.admin.system.model.resp.log.LogResp">
        SELECT
            t1.id,
            t1.description,
            t1.module,
            t1.time_taken,
            t1.ip,
            t1.address,
            t1.browser,
            t1.os,
            t1.status,
            t1.error_msg,
            t1.create_user,
            t1.create_time,
            t2.nickname AS createUserString
        FROM sys_log AS t1
        LEFT JOIN sys_user AS t2 ON t2.id = t1.create_user
        ${ew.customSqlSegment}
    </select>

    <select id="selectLogList" resultType="top.continew.admin.system.model.resp.log.LogResp">
        SELECT
            t1.id,
            t1.description,
            t1.module,
            t1.time_taken,
            t1.ip,
            t1.address,
            t1.browser,
            t1.os,
            t1.status,
            t1.error_msg,
            t1.create_user,
            t1.create_time,
            t2.nickname AS createUserString
        FROM sys_log AS t1
        LEFT JOIN sys_user AS t2 ON t2.id = t1.create_user
        ${ew.customSqlSegment}
    </select>

    <select id="selectDashboardTotal" resultType="top.continew.admin.system.model.resp.DashboardTotalResp">
        SELECT
            (SELECT COUNT(*) FROM sys_log) AS pvCount,
            (SELECT COUNT(DISTINCT ip) FROM sys_log) AS ipCount,
            (SELECT COUNT(*) FROM sys_log WHERE DATE(create_time) = CURRENT_DATE) AS todayPvCount,
            (SELECT COUNT(*) FROM sys_log WHERE DATE(create_time) = CURRENT_DATE - 1) AS yesterdayPvCount
    </select>

    <select id="selectListDashboardAccessTrend"
            resultType="top.continew.admin.system.model.resp.DashboardAccessTrendResp">
        SELECT
            DATE(create_time) AS date,
            COUNT(*) AS pvCount,
            COUNT(DISTINCT ip) AS ipCount
        FROM sys_log
        WHERE DATE(create_time) != CURRENT_DATE
        GROUP BY DATE(create_time)
        ORDER BY DATE(create_time) DESC
        LIMIT #{days}
    </select>

    <select id="selectListDashboardPopularModule"
            resultType="top.continew.admin.system.model.resp.DashboardPopularModuleResp">
        SELECT
            module,
            COUNT(*) AS pvCount,
            SUM(CASE WHEN DATE(create_time) = CURRENT_DATE THEN 1 ELSE 0 END) AS todayPvCount,
            SUM(CASE WHEN DATE(create_time) = CURRENT_DATE - 1 THEN 1 ELSE 0 END) AS yesterdayPvCount
        FROM sys_log
        WHERE module != '验证码' AND module != '登录'
        GROUP BY module
        ORDER BY pvCount DESC
        LIMIT 10
    </select>

    <select id="selectListDashboardGeoDistribution" resultType="java.util.Map">
        SELECT
            CASE
                WHEN POSITION(' ' IN address) > 0 THEN SUBSTRING(address FROM 1 FOR POSITION(' ' IN address) - 1)
                ELSE address
            END AS name,
            COUNT(DISTINCT ip) AS value
        FROM sys_log
        GROUP BY name
        ORDER BY value DESC
        LIMIT 10
    </select>
</mapper>
