<?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.mapper.IndicatorCorrectionMapper">

    <resultMap id="indicatorCorrectionMap" type="com.tofly.xrtygis.entity.IndicatorCorrection">
        <id property="id" column="ID"/>
        <result property="reviseDate" column="REVISE_DATE"/>
        <result property="dmaId" column="DMA_ID"/>
        <result property="dmaName" column="DMA_NAME"/>
        <result property="dmaCode" column="DMA_CODE"/>
        <result property="dmaLevel" column="DMA_LEVEL"/>
        <result property="jmcbZgsBl" column="JMCB_ZGS_BL"/>
        <result property="jmcbXzz" column="JMCB_XZZ"/>
        <result property="npjylXzz" column="NPJYL_XZZ"/>
        <result property="zddtsdXzz" column="ZDDTSD_XZZ"/>
        <result property="beforLeak" column="BEFOR_LEAK"/>
        <result property="afterLeak" column="AFTER_LEAK"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="differenceLeak" column="DIFFERENCE_LEAK"/>
        <result property="updateTime" column="UPDATE_TIME"/>
        <result property="updateUser" column="UPDATE_USER"/>
        <result property="dnLengthVal" column="DN_LENGTH_VAL"/>
        <result property="waterSupply" column="WATER_SUPPLY"/>
        <result property="pipeLength" column="PIPE_LENGTH"/>
        <result property="minWaterSupply" column="MIN_WATER_SUPPLY"/>
        <result property="waterSupplyRate" column="WATER_SUPPLY_RATE"/>
        <association property="createUserName" column="CREATE_USER"
                     select="com.tofly.xrtygis.mapper.IFileMapper.getUserName"/>
        <association property="updateUserName" column="UPDATE_USER"
                     select="com.tofly.xrtygis.mapper.IFileMapper.getUserName"/>
        <association property="dmaLevelName" column="DMA_LEVEL" select="getDmaLevleName"/>
    </resultMap>

    <select id="getDmaLevleName" resultType="string">
        SELECT decode(#{dmaLevle},'1','一级','2','二级','3','三级') as dmaLevleName FROM dual
    </select>

    <select id="getDmaUserWaterConsumption" resultType="com.tofly.xrtygis.entity.vo.DmaCalculatePramVo">
        SELECT  t1.id dmaId,NVL(SUM(sl), 0) userWaterConsumption
        FROM TF_DMA_WATER_W q1
                 LEFT JOIN TF_YWPN_DMAREGION_W t1
                           on q1.DMA_ID = T1.ID
                 LEFT JOIN TF_YWPN_WATERMETER_W t2
                           on t2.id = q1.WATER_METER_ID
                 LEFT JOIN TF_YWPN_TABLELINKUSER_W t3
                           on t2.ID = t3.TABLE_ID
                 LEFT JOIN YY_QFMX@YXDBLINK t4
                           on t3.KHBH = t4.YHBH
                 LEFT JOIN bm_sj@YXDBLINK t5
                           on t4.sjbm = t5.bm
        WHERE
            jsrq >= #{startTime}
          AND jsrq &lt;= #{endTime}
        group by t1.id
    </select>

    <select id="getTotalUserWaterConsumption" resultType="java.math.BigDecimal">
        SELECT
            NVL (SUM(sl), 0) AS sl
        FROM
            bm_sj@YXDBLINK A
                LEFT JOIN yy_qfmx@YXDBLINK b ON A .bm = b.sjbm
        WHERE
            1 = 1
          AND jsrq >= #{startTime}
          AND jsrq &lt;= #{endTime}
    </select>

    <select id="getDmaWaterSupply" resultType="com.tofly.xrtygis.entity.vo.DmaCalculatePramVo">
        SELECT t1.id dmaId ,t2.ISOUT isOut,nvl(sum(NEW_VALUE),0) waterSupply
        FROM TF_YWPN_DMAREGION_W t1 LEFT JOIN TF_YWPN_DMAANDOBSER_W T2
                                              on t1.id = t2.DMA_ID
                                    LEFT JOIN TF_YWPN_DMATABLE_W T3
                                              ON t2.TABLE_ID = t3.ID
                                    LEFT JOIN TF_INDEX_ALLOCATION t4
                                              on t3.DEVICE_ID = t4.DEVICE_ID
                                    LEFT JOIN SCADA_LJ_FLOW t5
                                              on t4.VARIABLE_CODE = t5.CODE
                                    LEFT JOIN (SELECT code,max(SCADA_TIME) strDate
                                               FROM SCADA_LJ_FLOW
                                               WHERE to_char(SCADA_TIME, 'yyyy-mm-dd') = #{dateTime}
                                               GROUP BY CODE) t6 on t5.CODE = t6.CODE
        WHERE
            t4.TYPE = #{nightFlowAllIndex}
          and t5.SCADA_TIME  = t6.strDate
        group by t1.id,t2.ISOUT
    </select>
    <select id="getDmaInstantaneousMinWaterSupply" resultType="com.tofly.xrtygis.entity.vo.DmaMinWaterSupplyVo">
        SELECT t1.id dmaId , nvl(min(min),0) minWaterSupply
        FROM TF_YWPN_DMAREGION_W t1 LEFT JOIN TF_YWPN_DMAANDOBSER_W T2
                                              on t1.id = t2.DMA_ID
                                    LEFT JOIN TF_YWPN_DMATABLE_W T3
                                              ON t2.TABLE_ID = t3.ID
                                    LEFT JOIN TF_INDEX_ALLOCATION t4
                                              on t3.DEVICE_ID = t4.DEVICE_ID
                                    LEFT JOIN STATISTICS_SCADA t5
                                              on t4.VARIABLE_CODE = t5.CODE
        WHERE
            t4.TYPE = #{flowAllIndex}
          and t5.STATISTICS_TYPE = 0
          and to_char(t5.START_TIME, 'yyyy-mm-dd') = #{dateTime}
        group by t1.id
    </select>
    <select id="getLostMonthWaterSupply" resultType="com.tofly.xrtygis.entity.vo.DmaCalculatePramVo">
        SELECT t1.id dmaId ,t2.ISOUT isOut,nvl(sum(OLD_VALUE),0) waterSupply
        FROM TF_YWPN_DMAREGION_W t1 LEFT JOIN TF_YWPN_DMAANDOBSER_W T2
                                              on t1.id = t2.DMA_ID
                                    LEFT JOIN TF_YWPN_DMATABLE_W T3
                                              ON t2.TABLE_ID = t3.ID
                                    LEFT JOIN TF_INDEX_ALLOCATION t4
                                              on t3.DEVICE_ID = t4.DEVICE_ID
                                    LEFT JOIN SCADA_LJ_FLOW t5
                                              on t4.VARIABLE_CODE = t5.CODE
                                    LEFT JOIN (SELECT code,min(SCADA_TIME) strDate
                                               FROM SCADA_LJ_FLOW
                                               WHERE  to_char(SCADA_TIME, 'yyyy-mm-dd') = #{dateTime}
                                               GROUP BY CODE) t6 on t5.CODE = t6.CODE
        WHERE
            t4.TYPE = #{flowAllIndex}
          and t5.SCADA_TIME  = t6.strDate
        group by t1.id,t2.ISOUT
    </select>

    <select id="getTotalWaterSupply" resultType="com.tofly.xrtygis.entity.vo.DmaCalculatePramVo">
        SELECT t1.id dmaId ,t2.ISOUT isOut,nvl(sum(NEW_VALUE),0) waterSupply
        FROM TF_YWPN_DMAREGION_W t1 LEFT JOIN TF_YWPN_DMAANDOBSER_W T2
                                              on t1.id = t2.DMA_ID
                                    LEFT JOIN TF_YWPN_DMATABLE_W T3
                                              ON t2.TABLE_ID = t3.ID
                                    LEFT JOIN TF_INDEX_ALLOCATION t4
                                              on t3.DEVICE_ID = t4.DEVICE_ID
                                    LEFT JOIN SCADA_LJ_FLOW t5
                                              on t4.VARIABLE_CODE = t5.CODE
                                    LEFT JOIN (SELECT code,max(SCADA_TIME) strDate
                                               FROM SCADA_LJ_FLOW
                                               WHERE to_char(SCADA_TIME, 'yyyy-mm-dd') = #{dateTime}
                                               GROUP BY CODE) t6 on t5.CODE = t6.CODE
        WHERE
            t4.TYPE = #{nightFlowAllIndex}
          and t1.PARTITION_LEVEL = '1'
          and t5.SCADA_TIME  = t6.strDate
        group by t1.id,t2.ISOUT
    </select>

    <select id="getLostMonthTotalWaterSupply" resultType="com.tofly.xrtygis.entity.vo.DmaCalculatePramVo">
        SELECT t1.id dmaId ,t2.ISOUT isOut,nvl(sum(OLD_VALUE),0) waterSupply
        FROM TF_YWPN_DMAREGION_W t1 LEFT JOIN TF_YWPN_DMAANDOBSER_W T2
                                              on t1.id = t2.DMA_ID
                                    LEFT JOIN TF_YWPN_DMATABLE_W T3
                                              ON t2.TABLE_ID = t3.ID
                                    LEFT JOIN TF_INDEX_ALLOCATION t4
                                              on t3.DEVICE_ID = t4.DEVICE_ID
                                    LEFT JOIN SCADA_LJ_FLOW t5
                                              on t4.VARIABLE_CODE = t5.CODE
                                    LEFT JOIN (SELECT code,min(SCADA_TIME) strDate
                                               FROM SCADA_LJ_FLOW
                                               WHERE  to_char(SCADA_TIME, 'yyyy-mm-dd') = #{dateTime}
                                               GROUP BY CODE) t6 on t5.CODE = t6.CODE
        WHERE
            t4.TYPE = #{nightFlowAllIndex}
          and t1.PARTITION_LEVEL = '1'
          and t5.SCADA_TIME  = t6.strDate
        group by t1.id,t2.ISOUT
    </select>

    <select id="getDNLength" resultType="com.tofly.xrtygis.entity.vo.DmaCalculatePramVo">
        SELECT t1.id dmaId, round(sum(t2.PIPE_LEN)/1000,0) dNLength
        FROM TF_YWPN_DMAREGION_W t1 LEFT JOIN TF_YWPN_DMAREGIONUPIPE_W t2
                                              on t1.id = t2.DMA_ID
        WHERE
            t2.VILLAGE_TYPE >=75
        group by t1.id
    </select>

    <select id="getRemoteWaterSupply" resultType="com.tofly.xrtygis.entity.vo.DmaCalculatePramVo">
        SELECT t1.id dmaId, round(sum(t2.now_Val),0) remoteWaterSupply
        FROM TF_YWPN_DMAREGION_W t1 LEFT JOIN TF_DMA_REMOTE_METER t2
                                              on t1.id = t2.DMA_ID
        WHERE
            t2.start_Date between #{startDate} and #{endDate}
        group by t1.id
    </select>

    <select id="getTotalRemoteWaterSupply" resultType="java.math.BigDecimal">
        SELECT round(sum(t2.now_Val),0) remoteWaterSupply
        FROM TF_YWPN_DMAREGION_W t1 LEFT JOIN TF_DMA_REMOTE_METER t2
                                              on t1.id = t2.DMA_ID
        WHERE
            t2.start_Date between #{startDate} and #{endDate}
          and t1.PARTITION_LEVEL = '1'
    </select>

    <select id="getDmaYearPressure" resultType="com.tofly.xrtygis.entity.vo.DmaCalculatePramVo">
        SELECT t1.id dmaId,round(avg(t5.AVE) ,0) yearPressure
        FROM TF_YWPN_DMAREGION_W t1 LEFT JOIN TF_YWPN_DMAANDOBSER_W T2
                                              on t1.id = t2.DMA_ID
                                    LEFT JOIN TF_YWPN_DMATABLE_W T3
                                              ON t2.TABLE_ID = t3.ID
                                    LEFT JOIN TF_INDEX_ALLOCATION t4
                                              on t3.DEVICE_ID = t4.DEVICE_ID
                                    LEFT JOIN STATISTICS_SCADA t5
                                              on t4.VARIABLE_CODE = t5.CODE
        WHERE
            t5.STATISTICS_TYPE = 3
          and  to_char(t5.START_TIME, 'yyyy') = #{year}
        group by t1.id
    </select>

    <select id="getLeakageGdInfo" resultType="com.tofly.xrtygis.entity.vo.DmaCalculatePramVo">
        SELECT t1.id dmaId, round(sum(CASE WHEN t3.lcjd = 45 THEN 1 ELSE 0 END),0) leakageOverVal ,round(count(t3.id),0) leakageVal
        FROM TF_YWPN_DMAREGION_W t1 LEFT JOIN TF_YWPN_LEAK_INFO_W t2
                                              on t1.id = t2.DMA_ID
                                    LEFT JOIN TF_YWPN_GDINFO_W t3
                                              on t2.GDINFO_ID = t3.id
        WHERE
            t3.DJSJ BETWEEN #{startTime} and #{endTime}
        GROUP BY t1.id
    </select>

    <select id="getTotalLeakageGdInfo" resultType="com.tofly.xrtygis.entity.vo.DmaCalculatePramVo">
        SELECT round(sum(CASE WHEN t3.lcjd = 45 THEN 1 ELSE 0 END),0) leakageOverVal ,round(count(t3.id),0) leakageVal
        FROM  TF_YWPN_LEAK_INFO_W t2 LEFT JOIN TF_YWPN_GDINFO_W t3
                                               on t2.GDINFO_ID = t3.id
        WHERE
            t3.DJSJ BETWEEN #{startTime} and #{endTime}
    </select>

    <select id="getTotalPlanPipeLength" resultType="java.math.BigDecimal">
        SELECT round(nvl(sum(TOTAL_LENGTH),0),2)
        FROM TF_YWPN_PLAN_W t1
        WHERE
            ((PLAN_BEGINDATE >= #{startTime} and PLAN_ENDDATE &lt;= #{endTime})
                or (PLAN_BEGINDATE >= #{endTime} and PLAN_ENDDATE &lt;= #{startTime}))
          and TOTAL_LENGTH !=0
    </select>

    <select id="getPlanTotalDistanceLength" resultType="java.math.BigDecimal">
        SELECT  round(nvl(sum(PLAN_TOTAL_DISTANCE),0),2)
        FROM TF_YWPN_GPSCALCULATE_W t1
        WHERE
            GPS_DATE >= #{startTime} and GPS_DATE &lt;= #{endTime}
    </select>
</mapper>
