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

    <resultMap id="dmaandobserMap" type="com.tofly.xrtygis.entity.Dmaandobser">
        <id property="id" column="ID"/>
        <result property="dmaId" column="DMA_ID"/>
        <result property="tableId" column="TABLE_ID"/>
        <result property="isout" column="ISOUT"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="createTime" column="CREATE_TIME"/>
        <association property="createUserName" column="CREATE_USER"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"></association>
    </resultMap>
    <insert id="insertBatch">
        insert into TF_YWPN_DMAANDOBSER_W (ID, DMA_ID , TABLE_ID , ISOUT , CREATE_USER , CREATE_TIME)
        <foreach collection="list" item="l" separator="union all">
            select
            #{l.id},
            #{l.dmaId},
            #{l.tableId},
            #{l.isout},
            #{l.createUser},
            #{l.createTime}
            from dual
        </foreach>
    </insert>

    <select id="getDiffByDeviceDay" resultType="map">
        SELECT date_format(t1.start_time, '%Y-%m-%d') scada_time,
               t3.id device_id,
               SUM(IFNULL((t1.max - t1.min), 0)) diff_value
        FROM tf_ywpn_statisticsscada_w t1
        LEFT JOIN TF_INDEX_ALLOCATION t2 ON t1.code = t2.variable_code
        LEFT JOIN TF_DEVICE_ARCHIVE_MANAGE t3 ON t3.id = t2.DEVICE_ID
        WHERE
            t1.statistics_type = 1
            AND t2.type = '2'
        <if test="startDate != null and endDate != null">
            AND DATE_FORMAT(t1.start_time,'%Y-%m-%d %H:%i:%s') BETWEEN #{startDate} AND #{endDate}
        </if>
        GROUP BY t3.id, DATE_FORMAT(t1.start_time, '%Y-%m-%d')
    </select>

    <select id="getDmaStatistical" resultType="com.tofly.xrtygis.entity.DmaStatistical">
        SELECT
            to_char(a.STATISTICAL_DATE,'yyyy-mm') STATISTICAL_DATE_STR,
            a.DMA_ID,
            SUM( a.BILLING_WATER_VOLUME ) BILLING_WATER_VOLUME,
            SUM( a.FREE_WATER_VOLUME ) FREE_WATER_VOLUME,
            SUM( a.OUTFLOW_WATER_VOLUME ) OUTFLOW_WATER_VOLUME,
            SUM( a.LOSS_WATER_VOLUME ) LOSS_WATER_VOLUME,
            SUM( a.OTHER_WATER_VOLUME ) OTHER_WATER_VOLUME,
            SUM( a.REMOTE_WATER ) REMOTE_WATER,
            case WHEN(NVL(COUNT(*), 0)) != 0 THEN round(SUM( a.LEAKAGE_RATE ) / COUNT(*),2) ELSE 0 END LEAKAGE_RATE,
            case WHEN(NVL(COUNT(*), 0)) != 0 THEN round(SUM( a.POOR_PRODUCTION_RATE ) / COUNT(*),2) ELSE 0 END POOR_PRODUCTION_RATE,
            SUM( a.WATER_SUPPLY ) WATER_SUPPLY,
            SUM( a.USER_WATER ) USER_WATER,
            SUM( a.BILLING_WATER_VOLUME + a.USER_WATER ) CHARGE_LEGAL_WATER,
            SUM( a.LEAKAGE_OVER_VAL ) LEAKAGE_OVER_VAL,
            SUM( a.LEAKAGE_VAL ) LEAKAGE_VAL,
            SUM( a.DN_LENGTH_VAL ) DN_LENGTH_VAL,
            case WHEN(NVL(COUNT(*), 0)) != 0 THEN round(SUM( a.YEAR_PRESSURE_VAL ) / COUNT(*),2) ELSE 0 END YEAR_PRESSURE_VAL,
            SUM( a.LOSS_WATER_VOLUME + a.OTHER_WATER_VOLUME ) APPARENT_LEAKAGE_VAL,
            SUM( a.OTHER_WATER_VOLUME + a.LOSS_WATER_VOLUME + a.OUTFLOW_WATER_VOLUME ) LEAKAGE_SUM_VOLUME,
            SUM( a.USER_WATER + a.FREE_WATER_VOLUME ) LEGAL_WATER_VOLUME
        FROM
            TF_DMA_STATISTICAL a
        WHERE
            1=1
        <if test="startDate != null and endDate != null">
            AND TO_CHAR(a.STATISTICAL_DATE, 'yyyy-MM-dd HH24:mi:ss') BETWEEN #{startDate} AND #{endDate}
        </if>
        <if test="dmaIdList != null">
            AND a.DMA_ID IN
            <foreach item="item" index="index" collection="dmaIdList" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY a.DMA_ID, to_char(a.STATISTICAL_DATE,'yyyy-mm')
    </select>

</mapper>
