<?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="org.springblade.modules.business.monitor.mapper.WaterFlowRateHistoryMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="waterFlowRateResultMap"
               type="org.springblade.modules.business.monitor.entity.WaterFlowRateHistoryInfoEntity">
        <result column="mp_cd" property="mpcd"/>
        <result column="tm" property="tm"/>
        <result column="mp_q" property="mpq"/>
        <result column="acc_w" property="accw"/>
        <result column="in_stp_q" property="instpq"/>
        <result column="acc_pq_w" property="accpqw"/>
        <result column="spe_reg_data" property="speregdata"/>
        <result column="ts" property="ts"/>
        <result column="packet_uuid" property="packetuuid"/>
        <result column="hour_w" property="hourw"/>
    </resultMap>


    <resultMap id="waterFlowRateVOResultMap"
               type="org.springblade.modules.business.monitor.vo.WaterFlowRateHistoryInfoVO">
        <result column="mp_nm" property="mpnm"/>
    </resultMap>

    <resultMap id="orgWaterFlowCountVOResultMap"
               type="org.springblade.modules.business.monitor.vo.OrgWaterFlowCountInfoVO">
        <result column="accq" property="mpaccq"/>
        <result column="org_id" property="orgId"/>
    </resultMap>

    <resultMap id="consumerWaterFlowCountVOResultMap"
               type="org.springblade.modules.business.monitor.vo.ConsumerWaterFlowCountInfoVO">
        <result column="accq" property="mpaccq"/>
        <result column="wiu_cd" property="wiuCd"/>
    </resultMap>

    <select id="selectPage" resultMap="waterFlowRateVOResultMap">
        SELECT
        n.*,i.mp_nm
        FROM
        data_mp_qr n
        LEFT JOIN base_mp_info i ON i.mp_cd = n.mp_cd and i.is_deleted=0
        WHERE
        n.is_deleted = 0
        <if test="dto.mpcd!=null">
            and n.mp_cd = #{dto.mpcd}
        </if>
        order by n.tm desc
    </select>


    <select id="rtuUpDataCount" resultType="java.lang.Long">
        SELECT
        count( * )
        FROM
        (
        SELECT
        w.mp_cd
        FROM
        data_mp_qr w
        WHERE
        w.is_deleted = 0
        AND w.tm >= #{dto.beginTime}
        AND w.tm &lt; #{dto.endTime}
        GROUP BY
        w.mp_cd
        ) a
    </select>

    <select id="orgWaterUseCount" resultMap="orgWaterFlowCountVOResultMap">
        SELECT
        u.org_id,sum(q.mp_q) accq
        FROM data_mp_qr q
        LEFT JOIN base_wiust_mp_rel mp ON mp.mp_cd = q.mp_cd AND mp.is_deleted=0
        LEFT JOIN base_wiust_info site ON site.wiust_cd = mp.wiust_cd AND site.is_deleted=0
        LEFT JOIN base_wiu_wiust_rel ww ON ww.wiust_cd = site.wiust_cd AND ww.is_deleted=0
        LEFT JOIN base_wr_wiu_info u ON u.wiu_cd = ww.wiu_cd AND u.is_deleted=0
        LEFT JOIN blade_dept d ON d.id = u.org_id and d.is_deleted=0
        WHERE
        q.is_deleted = 0
        <if test="dto.beginTime!=null">
            AND q.tm >= #{dto.beginTime}
        </if>
        <if test="dto.endTime!=null">
            AND q.tm &lt; #{dto.endTime}
        </if>
        <if test="dto.orgId!=null">
            and (u.org_id =#{dto.orgId} or FIND_IN_SET(#{dto.orgId} ,d.ancestors) >0 )
        </if>
        GROUP BY u.org_id
    </select>

    <select id="consumerWaterUseCount" resultMap="consumerWaterFlowCountVOResultMap">
        SELECT
            u.wiu_cd,sum(q.mp_q) accq
        FROM data_mp_qr q
        LEFT JOIN base_wiust_mp_rel mp ON mp.mp_cd = q.mp_cd AND mp.is_deleted=0
        LEFT JOIN base_wiust_info site ON site.wiust_cd = mp.wiust_cd AND site.is_deleted=0
        LEFT JOIN base_wiu_wiust_rel ww ON ww.wiust_cd = site.wiust_cd AND ww.is_deleted=0
        LEFT JOIN base_wr_wiu_info u ON u.wiu_cd = ww.wiu_cd AND u.is_deleted=0
        WHERE
            q.is_deleted = 0
        <if test="dto.beginTime!=null">
            AND q.tm >= #{dto.beginTime}
        </if>
        <if test="dto.endTime!=null">
            AND q.tm &lt; #{dto.endTime}
        </if>
        GROUP BY u.wiu_cd
    </select>
</mapper>
