<?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.YjjtQuebaoMapper">

    <resultMap type="com.goldenwater.yjjt.domain.YjjtQuebaoStatistics" id="QuebaoResult">
        <result property="year" column="year" />
        <result property="stcd" column="stcd" />
        <result property="stnm" column="stnm" />
        <result property="sttp" column="sttp" />
        <result property="rvnm" column="rvnm" />
        <result property="stlc" column="stlc" />
        <result property="jcdw" column="jcdw" />
        <result property="jcpc" column="jcpc" />
        <result property="jcxm" column="jcxm" />
        <result property="addvcd" column="addvcd" />
        <result property="dataCount" column="dataCount" />
        <result property="reportStatus" column="reportStatus" />
    </resultMap>

    <select id="selectQuebaoStationList" parameterType="com.goldenwater.yjjt.domain.YjjtQuebaoStatistics" resultMap="QuebaoResult">
        SELECT s.year, s.stcd, s.stnm, s.sttp, s.rvnm, s.stlc, s.jcdw, s.jcpc, s.jcxm, s.addvcd,
               NVL(t.dataCount, 0) as dataCount,
               CASE WHEN NVL(t.dataCount, 0) = 0 THEN '缺报' ELSE '正常' END as reportStatus
        FROM YJJT_STATION s
        LEFT JOIN (
            SELECT stcd, to_char(spt, 'YYYY') as year, count(*) as dataCount
            FROM (
                SELECT stcd, spt FROM YJJT_SL_DATA
                UNION ALL
                SELECT stcd, spt FROM YJJT_SZ_DATA
            ) combined
            GROUP BY stcd, to_char(spt, 'YYYY')
        ) t ON s.stcd = t.stcd AND s.year = t.year
        WHERE s.del_flag = '0'
        <if test="query.year != null and query.year != ''">
            AND s.year = #{query.year}
        </if>
        <if test="query.addvcd != null and query.addvcd != ''">
            AND s.addvcd = #{query.addvcd}
        </if>
        <if test="query.jcxm != null and query.jcxm != ''">
            AND s.jcxm = #{query.jcxm}
        </if>
        <if test="query.stnm != null and query.stnm != ''">
            AND s.stnm LIKE CONCAT('%', #{query.stnm}, '%')
        </if>
        ORDER BY s.year DESC, s.stcd ASC
    </select>

    <select id="selectQuebaoStationListForExport" resultMap="QuebaoResult">
        SELECT s.year, s.stcd, s.stnm, s.sttp, s.rvnm, s.stlc, s.jcdw, s.jcpc, s.jcxm, s.addvcd,
               NVL(t.dataCount, 0) as dataCount,
               CASE WHEN NVL(t.dataCount, 0) = 0 THEN '缺报' ELSE '正常' END as reportStatus
        FROM YJJT_STATION s
        LEFT JOIN (
            SELECT stcd, to_char(spt, 'YYYY') as year, count(*) as dataCount
            FROM (
                SELECT stcd, spt FROM YJJT_SL_DATA
                UNION ALL
                SELECT stcd, spt FROM YJJT_SZ_DATA
            ) combined
            GROUP BY stcd, to_char(spt, 'YYYY')
        ) t ON s.stcd = t.stcd AND s.year = t.year
        WHERE s.del_flag = '0'
        <if test="query.year != null and query.year != ''">
            AND s.year = #{query.year}
        </if>
        <if test="query.addvcd != null and query.addvcd != ''">
            AND s.addvcd = #{query.addvcd}
        </if>
        <if test="query.jcxm != null and query.jcxm != ''">
            AND s.jcxm = #{query.jcxm}
        </if>
        <if test="query.stnm != null and query.stnm != ''">
            AND s.stnm LIKE CONCAT('%', #{query.stnm}, '%')
        </if>
        ORDER BY s.year DESC, s.stcd ASC
    </select>
</mapper>
