<?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.tofly.xrtygis.custom.mapper.ExportAboutMapper">

    <select id="selectHistory" resultType="map">
        select SCADA_TIME, ITNUM_VAL ITVAL
        from TF_YWPN_SCADAHISTORY_R_VIEW
        where ITCD = #{itcd}
          and to_char(scada_time, 'yyyy-mm-dd') between #{start} and #{end}
    </select>

    <select id="viewWaterInfoByDay" resultType="map">
        select to_char(SCADA_TIME, 'yyyy-mm-dd') time,
        nvl(ITNUM_VAL,0) qsl,
        nvl((select ITNUM_VAL
        from TF_YWPN_SCADAHISTORY_R_VIEW R
        where R.ITCD = #{wsItcd}
        and to_char(R.SCADA_TIME, 'yyyy-mm-dd') = to_char(V.SCADA_TIME, 'yyyy-mm-dd')),0) gsl
        from TF_YWPN_SCADAHISTORY_R_VIEW V
        where ITCD = #{wiItcd}
        <if test="startTime != null and endTime != null">
            and to_char(SCADA_TIME,'yyyy-mm-dd') between #{startTime} and #{endTime}
        </if>

        <if test="startTime == null and endTime == null">
            and SCADA_TIME between add_months(sysdate,-3) and sysdate
        </if>

        order by time
    </select>

    <select id="viewWaterInfoByMonth" resultType="map">
        select to_char(SCADA_TIME, 'yyyy-mm') time,
        nvl(sum(ITNUM_VAL),0) qsl,
        nvl(sum((select ITNUM_VAL
        from TF_YWPN_SCADAHISTORY_R_VIEW R
        where R.ITCD = #{wsItcd}
        and to_char(R.SCADA_TIME, 'yyyy-mm-dd') = to_char(V.SCADA_TIME, 'yyyy-mm-dd'))),0) gsl
        from TF_YWPN_SCADAHISTORY_R_VIEW V
        where ITCD = #{wiItcd}
        <if test="startTime != null and endTime != null">
            and to_char(SCADA_TIME,'yyyy-mm') between #{startTime} and #{endTime}
        </if>
        <if test="startTime == null and endTime == null">
            and SCADA_TIME between add_months(sysdate,-3) and sysdate
        </if>
        group by to_char(SCADA_TIME, 'yyyy-mm')
        order by time
    </select>

    <select id="viewWaterContrast" resultType="map">
        SELECT time, nvl(sumqsl, 0) qsl, nvl(sumgsl, 0) gsl
        FROM (SELECT SUM(qsl) sumqsl, SUM(gsl) sumgsl, time
              FROM (select sum(ITNUM_VAL)                     qsl,
                           TO_CHAR(SCADA_TIME, 'yyyy-MM') time,
                           (select V.ITNUM_VAL
                            from TF_YWPN_SCADAHISTORY_R_VIEW V
                            where to_char(R.SCADA_TIME, 'yyyy-mm-dd') = to_char(V.SCADA_TIME, 'yyyy-mm-dd')
                              and V.ITCD = #{wiItcd})     gsl
                    from TF_YWPN_SCADAHISTORY_R_VIEW R
                    WHERE to_char(SCADA_TIME, 'yyyy-mm') BETWEEN #{startTime} and #{endTime}
                      and ITCD = #{wsItcd}
                    GROUP BY SCADA_TIME)
              GROUP BY time)
        WHERE time LIKE #{month}
    </select>

</mapper>