<!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.WaterMakingAnalysisMapper">
    <select id="monthNrw" resultType="map">
        select round(CXC, 4)               nrw,
               extract(month from UP_DATE) month
        from TF_YWPN_COSTRLWATER_W
        where extract(year from UP_DATE) = #{year}
          and MARK = 1
        order by month
    </select>

    <select id="yearNrwPlan" resultType="double">
        select round(((sum(GSL_TOTAL) +
                       sum(GSL_QBJ) -
                       sum(SSL_TOTAL)) /
                      nullif(sum(GSL_TOTAL) + sum(GSL_QBJ), 0) *
                      100), 4) nrw
        from TF_YWPN_COSTBGWATER_W tbg
        where substr(MONTH, 1, 4) = #{year}
        group by substr(MONTH, 1, 4)
    </select>

    <select id="yearNrw" resultType="double">
        select round(((sum(GSL_TOTAL) +
                       sum(GSL_QBJ) -
                       sum(SSL_TOTAL)) /
                      nullif(sum(GSL_TOTAL) + sum(GSL_QBJ), 0) *
                      100), 4) nrw
        from TF_YWPN_COSTRLWATER_W
        where extract(year from UP_DATE
                  ) = #{year}
          and MARK = '1'
        group by extract(year from UP_DATE)
    </select>

    <select id="monthLeakage" resultType="map">
        select round(LSL, 4)               leakage,
               extract(month from UP_DATE) month
        from TF_YWPN_COSTRLWATER_W
        where extract(year from UP_DATE) = #{year}
          and MARK = '1'
        order by month
    </select>

    <select id="yearLeakage" resultType="double">
        select round(((sum(GSL_TOTAL) + sum(GSL_QBJ) -
                       sum(SSL_TOTAL) - sum(WSPSL)) /
                      nullif(sum(GSL_TOTAL) + sum(GSL_QBJ), 0) * 100), 4) leakage
        from TF_YWPN_COSTRLWATER_W
        where extract(year from UP_DATE) = #{year}
          and MARK = '1'
        group by extract(year from UP_DATE
                     )
    </select>

    <select id="yearLeakagePlan" resultType="double">
        select round(((sum(GSL_TOTAL) + sum(GSL_QBJ) -
                       sum(SSL_TOTAL) - sum(WSPSL)) /
                      nullif(sum(GSL_TOTAL) + sum(GSL_QBJ), 0) * 100), 4) leakage
        from TF_YWPN_COSTBGWATER_W tbg
        where substr(MONTH, 1, 4) = #{year}
        group by substr(MONTH, 1, 4)
    </select>

    <select id="monSupSellWater" resultType="map">
        select round(GSL_TOTAL, 4) gsl,
               round(SSL_TOTAL, 4) ssl,
               extract(month from UP_DATE
                   )               month
        from TF_YWPN_COSTRLWATER_W
        where MARK = '1'
          and extract(year from UP_DATE) = #{year}
        order by month
    </select>

    <select id="monSupSellWaterPlan" resultType="map">
        select round(GSL_TOTAL, 4) gsl, round(SSL_TOTAL, 4) ssl, MONTH
        from TF_YWPN_COSTBGWATER_W tbg
        where substr(MONTH, 1, 4) = #{year}
    </select>

    <select id="yearSupSellWater" resultType="map">
        select round(sum(GSL_TOTAL), 4) gsl,
               round(sum(SSL_TOTAL), 4) ssl
        from TF_YWPN_COSTRLWATER_W
        where MARK = '1'
          and extract(year from UP_DATE) = #{year}
        group by extract(year from UP_DATE)
    </select>

    <select id="yearSupSellWaterPlan" resultType="map">
        select round(sum(GSL_TOTAL), 4) gsl, round(sum(SSL_TOTAL), 4) ssl
        from TF_YWPN_COSTBGWATER_W tbg
        where substr(MONTH, 1, 4) = #{year}
        group by substr(MONTH, 1, 4)
    </select>
</mapper>