<?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.goldenwater.yjjt.mapper.YjjtStatisticsMapper">

    <resultMap type="com.goldenwater.yjjt.domain.YjjtYearlyDataStatistics" id="YearlyDataResult">
        <result property="year" column="year" />
        <result property="addvcd" column="addvcd" />
        <result property="dataCount" column="dataCount" />
        <result property="monitorCount" column="monitorCount" />
    </resultMap>

    <resultMap type="com.goldenwater.yjjt.domain.YjjtStatisticsDTO" id="UnitYearlyResult">
        <result property="year" column="year" />
        <result property="unitName" column="unitName" />
        <result property="dataCount" column="dataCount" />
        <result property="monitorCount" column="monitorCount" />
    </resultMap>

    <resultMap type="com.goldenwater.yjjt.domain.YjjtStatisticsDTO" id="StationCountResult">
        <result property="year" column="year" />
        <result property="unitName" column="unitName" />
        <result property="stationCount" column="stationCount" />
    </resultMap>

    <select id="selectDailyStatistics" resultType="com.goldenwater.yjjt.domain.YjjtDailyStatistics">
        select '无锡' as unitName, 'WX' as unitCode,
               (select count(*) from YJJT_SL_DATA where ADDVCD = 'WX' and trunc(SPT) = trunc(SYSDATE)) +
               (select count(*) from YJJT_SZ_DATA where ADDVCD = 'WX' and trunc(SPT) = trunc(SYSDATE)) as todayCount,
               0 as weekCount, 0 as monthCount
        from dual
        union all
        select '江苏' as unitName, 'JS' as unitCode,
               (select count(*) from YJJT_SL_DATA where ADDVCD = 'JS' and trunc(SPT) = trunc(SYSDATE)) +
               (select count(*) from YJJT_SZ_DATA where ADDVCD = 'JS' and trunc(SPT) = trunc(SYSDATE)) as todayCount,
               0 as weekCount, 0 as monthCount
        from dual
        union all
        select '上海' as unitName, 'SH' as unitCode,
               (select count(*) from YJJT_SL_DATA where ADDVCD = 'SH' and trunc(SPT) = trunc(SYSDATE)) +
               (select count(*) from YJJT_SZ_DATA where ADDVCD = 'SH' and trunc(SPT) = trunc(SYSDATE)) as todayCount,
               0 as weekCount, 0 as monthCount
        from dual
    </select>

    <select id="selectWeeklyStatistics" resultType="com.goldenwater.yjjt.domain.YjjtDailyStatistics">
        select '无锡' as unitName, 'WX' as unitCode, 0 as todayCount,
               (select count(*) from YJJT_SL_DATA where ADDVCD = 'WX' and SPT >= trunc(SYSDATE - to_char(SYSDATE, 'D') + 1)) +
               (select count(*) from YJJT_SZ_DATA where ADDVCD = 'WX' and SPT >= trunc(SYSDATE - to_char(SYSDATE, 'D') + 1)) as weekCount,
               0 as monthCount
        from dual
        union all
        select '江苏' as unitName, 'JS' as unitCode, 0 as todayCount,
               (select count(*) from YJJT_SL_DATA where ADDVCD = 'JS' and SPT >= trunc(SYSDATE - to_char(SYSDATE, 'D') + 1)) +
               (select count(*) from YJJT_SZ_DATA where ADDVCD = 'JS' and SPT >= trunc(SYSDATE - to_char(SYSDATE, 'D') + 1)) as weekCount,
               0 as monthCount
        from dual
        union all
        select '上海' as unitName, 'SH' as unitCode, 0 as todayCount,
               (select count(*) from YJJT_SL_DATA where ADDVCD = 'SH' and SPT >= trunc(SYSDATE - to_char(SYSDATE, 'D') + 1)) +
               (select count(*) from YJJT_SZ_DATA where ADDVCD = 'SH' and SPT >= trunc(SYSDATE - to_char(SYSDATE, 'D') + 1)) as weekCount,
               0 as monthCount
        from dual
    </select>

    <select id="selectMonthlyStatistics" resultType="com.goldenwater.yjjt.domain.YjjtDailyStatistics">
        select '无锡' as unitName, 'WX' as unitCode, 0 as todayCount, 0 as weekCount,
               (select count(*) from YJJT_SL_DATA where ADDVCD = 'WX' and trunc(SPT, 'MM') = trunc(SYSDATE, 'MM')) +
               (select count(*) from YJJT_SZ_DATA where ADDVCD = 'WX' and trunc(SPT, 'MM') = trunc(SYSDATE, 'MM')) as monthCount
        from dual
        union all
        select '江苏' as unitName, 'JS' as unitCode, 0 as todayCount, 0 as weekCount,
               (select count(*) from YJJT_SL_DATA where ADDVCD = 'JS' and trunc(SPT, 'MM') = trunc(SYSDATE, 'MM')) +
               (select count(*) from YJJT_SZ_DATA where ADDVCD = 'JS' and trunc(SPT, 'MM') = trunc(SYSDATE, 'MM')) as monthCount
        from dual
        union all
        select '上海' as unitName, 'SH' as unitCode, 0 as todayCount, 0 as weekCount,
               (select count(*) from YJJT_SL_DATA where ADDVCD = 'SH' and trunc(SPT, 'MM') = trunc(SYSDATE, 'MM')) +
               (select count(*) from YJJT_SZ_DATA where ADDVCD = 'SH' and trunc(SPT, 'MM') = trunc(SYSDATE, 'MM')) as monthCount
        from dual
    </select>

    <!-- 年度水量统计 -->
    <select id="selectYearlySlStatisticsData" resultMap="YearlyDataResult">
        select extract(year from spt) as year, addvcd,
               count(*) as dataCount,
               count(distinct cast(spt as date) || stcd) as monitorCount
        from YJJT_SL_DATA
        <where>
            <if test="year != null and year != ''">
                and extract(year from spt) = #{year}
            </if>
        </where>
        group by extract(year from spt), addvcd
        order by year desc
    </select>

    <!-- 年度水质统计 -->
    <select id="selectYearlySzStatisticsData" resultMap="YearlyDataResult">
        select extract(year from spt) as year, addvcd,
               count(*) as dataCount,
               count(distinct cast(spt as date) || stcd) as monitorCount
        from YJJT_SZ_DATA
        <where>
            <if test="year != null and year != ''">
                and extract(year from spt) = #{year}
            </if>
        </where>
        group by extract(year from spt), addvcd
        order by year desc
    </select>

    <!-- 年度合计统计 -->
    <select id="selectYearlyStatisticsData" resultMap="YearlyDataResult">
        select extract(year from spt) as year, addvcd,
               count(*) as dataCount,
               count(distinct cast(spt as date) || stcd) as monitorCount
        from (
            select spt, addvcd, stcd from YJJT_SL_DATA
            union all
            select spt, addvcd, stcd from YJJT_SZ_DATA
        ) t
        <where>
            <if test="year != null and year != ''">
                and extract(year from spt) = #{year}
            </if>
        </where>
        group by extract(year from spt), addvcd
        order by year desc
    </select>

    <select id="selectUnitYearlyStatistics" resultMap="UnitYearlyResult">
        select extract(year from spt) as year,
               (case addvcd when 'WX' then '无锡' when 'JS' then '江苏' when 'SH' then '上海' else addvcd end) as unitName,
               count(*) as dataCount,
               count(distinct cast(spt as date) || stcd) as monitorCount
        from (
            select spt, addvcd, stcd from YJJT_SL_DATA where addvcd = #{unitCode}
            union all
            select spt, addvcd, stcd from YJJT_SZ_DATA where addvcd = #{unitCode}
        ) t
        group by extract(year from spt), addvcd
        order by year desc
    </select>

    <select id="selectStationCountByYear" resultMap="StationCountResult">
        select year,
               (case when jcdw like '%太湖流域水文水资源监测中心%' then '无锡'
                     when jcdw like '%江苏省水文水资源勘测局%' then '江苏'
                     when jcdw like '%上海市水文总站%' then '上海'
                     else jcdw end) as unitName,
               count(distinct stcd) as stationCount
        from YJJT_STATION
        where (jcdw like '%太湖流域水文水资源监测中心%' and #{unitCode} = 'WX')
           or (jcdw like '%江苏省水文水资源勘测局%' and #{unitCode} = 'JS')
           or (jcdw like '%上海市水文总站%' and #{unitCode} = 'SH')
        group by year,
                 (case when jcdw like '%太湖流域水文水资源监测中心%' then '无锡'
                       when jcdw like '%江苏省水文水资源勘测局%' then '江苏'
                       when jcdw like '%上海市水文总站%' then '上海'
                       else jcdw end)
        order by year desc
    </select>

    <select id="selectTodayDataCount" resultType="java.lang.Integer">
        select count(*) from (
            select 1 from YJJT_SL_DATA where ADDVCD = #{unitCode} and trunc(SPT) = trunc(SYSDATE)
            union all
            select 1 from YJJT_SZ_DATA where ADDVCD = #{unitCode} and trunc(SPT) = trunc(SYSDATE)
        ) t
    </select>

    <select id="selectWeekDataCount" resultType="java.lang.Integer">
        select count(*) from (
            select 1 from YJJT_SL_DATA where ADDVCD = #{unitCode} and SPT >= trunc(SYSDATE - to_char(SYSDATE, 'D') + 1)
            union all
            select 1 from YJJT_SZ_DATA where ADDVCD = #{unitCode} and SPT >= trunc(SYSDATE - to_char(SYSDATE, 'D') + 1)
        ) t
    </select>

    <select id="selectMonthDataCount" resultType="java.lang.Integer">
        select count(*) from (
            select 1 from YJJT_SL_DATA where ADDVCD = #{unitCode} and trunc(SPT, 'MM') = trunc(SYSDATE, 'MM')
            union all
            select 1 from YJJT_SZ_DATA where ADDVCD = #{unitCode} and trunc(SPT, 'MM') = trunc(SYSDATE, 'MM')
        ) t
    </select>
</mapper>
