<?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="me.zhengjie.modules.system.mapper.DashboardMapper">

    <select id="getPieChatData" resultType="me.zhengjie.modules.system.domain.dto.DashboardDataVo">
        select d.brand text, count(1) value from bus_device d group by d.brand
    </select>

    <select id="getCardData" resultType="me.zhengjie.modules.system.domain.dto.DashboardDataVo">
        select 'user' text, count(*) value from sys_user
        UNION ALL
        select 'dept' text, count(*) value from sys_dept
        UNION ALL
        select 'role' text, count(*) value from sys_role
        UNION ALL
        select 'law' text, count(*) value from bus_user
    </select>

    <select id="getLineChartData" resultType="me.zhengjie.modules.system.domain.dto.DashboardDataVo">
        <if test="type == 'user'">
            SELECT dt.date_col text, sum(if(t.create_time is null, 0, 1)) value
            FROM (
                SELECT CURDATE() - INTERVAL a.num DAY AS date_col FROM (SELECT 0 AS num UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6) a
            ) dt LEFT JOIN sys_user t ON dt.date_col = DATE_FORMAT(t.create_time, '%Y-%m-%d')
            GROUP BY dt.date_col ORDER BY dt.date_col DESC
        </if>
        <if test="type == 'dept'">
            SELECT dt.date_col text, sum(if(t.create_time is null, 0, 1)) value
            FROM (
                SELECT CURDATE() - INTERVAL a.num DAY AS date_col FROM (SELECT 0 AS num UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6) a
            ) dt LEFT JOIN sys_dict t ON dt.date_col = DATE_FORMAT(t.create_time, '%Y-%m-%d')
            GROUP BY dt.date_col ORDER BY dt.date_col DESC
        </if>
        <if test="type == 'role'">
            SELECT dt.date_col text, sum(if(t.create_time is null, 0, 1)) value
            FROM (
                SELECT CURDATE() - INTERVAL a.num DAY AS date_col FROM (SELECT 0 AS num UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6) a
            ) dt LEFT JOIN sys_role t ON dt.date_col = DATE_FORMAT(t.create_time, '%Y-%m-%d')
            GROUP BY dt.date_col ORDER BY dt.date_col DESC
        </if>
        <if test="type == 'law'">
            SELECT dt.date_col text, sum(if(t.create_time is null, 0, 1)) value
            FROM (
                SELECT CURDATE() - INTERVAL a.num DAY AS date_col FROM (SELECT 0 AS num UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6) a
            ) dt LEFT JOIN bus_user t ON dt.date_col = DATE_FORMAT(t.create_time, '%Y-%m-%d')
            GROUP BY dt.date_col ORDER BY dt.date_col DESC
        </if>
    </select>

</mapper>