<?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.yeejoin.amos.knowledgebase.face.orm.dao.StatisticsRecordMapper">

    <update id="deleteAll">
        truncate table knowlege_statistics_record
    </update>

    <!-- 灾情数量同比环比 -->
    <select id="selectDisasterCount" resultType="java.util.Map">
        SELECT
          (SELECT
            COUNT(1)
          FROM
            knowlege_statistics_record
          WHERE record_name = "DISASTER_RECORD"
            AND DATE(disaster_time) BETWEEN DATE_ADD(
              CURDATE(),
              INTERVAL - DAY(CURDATE()) + 1 DAY
            )
            AND LAST_DAY(CURDATE())) AS currentMonthCount,
          (SELECT
            COUNT(1)
          FROM
            knowlege_statistics_record
          WHERE record_name = "DISASTER_RECORD"
            AND DATE(disaster_time) BETWEEN DATE_SUB(
              CURDATE() - DAY(CURDATE()) + 1,
              INTERVAL 1 MONTH
            )
            AND LAST_DAY(DATE_SUB(NOW(), INTERVAL 1 MONTH))) AS lastMonthCount,
          (SELECT
            COUNT(1)
          FROM
            knowlege_statistics_record
          WHERE record_name = "DISASTER_RECORD"
            AND DATE(disaster_time) BETWEEN DATE_SUB(
              CURDATE() - DAY(CURDATE()) + 1,
              INTERVAL 2 MONTH
            )
            AND LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH))) AS last2MonthCount,
          (SELECT
            COUNT(1)
          FROM
            knowlege_statistics_record
          WHERE record_name = "DISASTER_RECORD"
            AND DATE(disaster_time) BETWEEN DATE_SUB(
              CURDATE() - DAY(CURDATE()) + 1,
              INTERVAL 1 YEAR
            )
            AND LAST_DAY(DATE_SUB(CURDATE(), INTERVAL 1 YEAR))) AS lastYearCurrentMonthCount,
          (SELECT
            COUNT(1)
          FROM
            knowlege_statistics_record
          WHERE record_name = "DISASTER_RECORD"
            AND DATE(disaster_time) BETWEEN DATE_SUB(
              DATE_SUB(
                CURDATE() - DAY(CURDATE()) + 1,
                INTERVAL 1 MONTH
              ),
              INTERVAL 1 YEAR
            )
            AND LAST_DAY(
              DATE_SUB(
                DATE_SUB(CURDATE(), INTERVAL 1 YEAR),
                INTERVAL 1 MONTH
              )
            )) AS lastYearLastMonthCount,
          (SELECT
            COUNT(1)
          FROM
            knowlege_statistics_record
          WHERE record_name = "DISASTER_RECORD"
            AND DATE(disaster_time) BETWEEN CONCAT(
              YEAR(NOW()),
              '-',
              LPAD((QUARTER(CURDATE()) - 1) * 3+1, 2, '0'),
              '-01'
            )
            AND LAST_DAY(
              CONCAT(
                YEAR(NOW()),
                '-',
                LPAD(QUARTER(NOW()) * 3, 2, 0),
                '-01'
              )
            )) AS currentQuarterCount,
          (SELECT
            COUNT(1)
          FROM
            knowlege_statistics_record
          WHERE record_name = "DISASTER_RECORD"
            AND DATE(disaster_time) BETWEEN DATE_SUB(
              CONCAT(
                YEAR(NOW()),
                '-',
                LPAD((QUARTER(CURDATE()) - 1) * 3+1, 2, '0'),
                '-01'
              ),
              INTERVAL 1 QUARTER
            )
            AND DATE_SUB(
              CONCAT(
                YEAR(NOW()),
                '-',
                LPAD((QUARTER(CURDATE()) - 1) * 3+1, 2, '0'),
                '-01'
              ),
              INTERVAL 1 DAY
            )) AS lastQuarterCount,
          (SELECT
            COUNT(1)
          FROM
            knowlege_statistics_record
          WHERE record_name = "DISASTER_RECORD"
            AND DATE(disaster_time) BETWEEN DATE_SUB(
              CONCAT(
                YEAR(NOW()),
                '-',
                LPAD((QUARTER(CURDATE()) - 1) * 3+1, 2, '0'),
                '-01'
              ),
              INTERVAL 1 YEAR
            )
            AND DATE_SUB(
              LAST_DAY(
                CONCAT(
                  YEAR(NOW()),
                  '-',
                  LPAD(QUARTER(NOW()) * 3, 2, 0),
                  '-01'
                )
              ),
              INTERVAL 1 YEAR
            )) AS lastYearCurrentQuarterCount,
          (SELECT
            COUNT(1)
          FROM
            knowlege_statistics_record
          WHERE record_name = "DISASTER_RECORD"
            AND DATE(disaster_time) BETWEEN CONCAT(YEAR(CURDATE()), '-01-01')
            AND CONCAT(YEAR(CURDATE()), '-12-31')) AS currentYearCount,
          (SELECT
            COUNT(1)
          FROM
            knowlege_statistics_record
          WHERE record_name = "DISASTER_RECORD"
            AND DATE(disaster_time) BETWEEN CONCAT(
              YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR)),
              '-01-01'
            )
            AND CONCAT(
              YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR)),
              '-12-31'
            )) AS lastYearCount
    </select>

    <!-- 燃烧物质分类查询 -->
    <select id="selectCategoryByName" resultType="java.util.Map"  parameterType="java.lang.String">
        SELECT
          category_value category, count(1) quantity
        FROM
          knowlege_statistics_record
        WHERE record_name = #{recordName}
        group by category_value
    </select>
    
    <select id="selectCountByNameAndDateRange" resultType="java.util.Map" parameterType="java.util.Map">
        SELECT
          DATE_FORMAT(disaster_time, #{format}) AS period,
          SUM(COUNT_VALUE) quantity
        FROM
          knowlege_statistics_record
        WHERE record_name = #{recordName}
          <if test="dateRangeLeft != null">
            AND DATE_FORMAT(disaster_time, #{format}) <![CDATA[ >= ]]> DATE_FORMAT(#{dateRangeLeft}, #{format})
          </if>
          <if test="dateRangeRight != null">
            AND DATE_FORMAT(disaster_time, #{format}) <![CDATA[ <= ]]> DATE_FORMAT(#{dateRangeRight}, #{format})
          </if>
        GROUP BY period
        ORDER BY period ASC
    </select>
</mapper>
