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

    <!-- 通用查询映射结果 -->
    <resultMap id="rtuRainDataResultMap" type="org.springblade.modules.business.data.entity.RtuDataRainStoreEntity">
        <result column="rtu_code" property="rtuCode"/>
        <result column="tm" property="tm"/>
        <result column="drp" property="drp"/>
        <result column="intv" property="intv"/>
        <result column="from_time" property="fromTime"/>
    </resultMap>

    <select id="rtuUpDataCount" resultType="java.lang.Long">
        SELECT
        count( * )
        FROM
        (
        SELECT
        w.rtu_code
        FROM
        data_att_st_prec w
        LEFT JOIN rtu_info r ON w.rtu_code = r.rtu_code
        AND r.is_deleted = 0
        LEFT JOIN data_ad_cd_b a ON a.adcd = r.ad_code
        AND a.is_deleted = 0
        WHERE
        w.is_deleted = 0
        AND w.tm >= #{rainDTO.beginTime}
        AND w.tm &lt; #{rainDTO.endTime}
        <if test="rainDTO.excludeCode!=null and rainDTO.excludeCode==1">
            and a.exclude_ad = 0
        </if>
        <if test="rainDTO.adCode!=null">
            and (a.adcd =#{rainDTO.adCode} or a.ancestors in (SELECT ancestors FROM data_ad_cd_b WHERE is_deleted =0 and
            ancestors like concat(concat('%', #{rainDTO.adCode}), '%')))
        </if>
        GROUP BY
        w.rtu_code
        ) a
    </select>

    <select id="rainDrpCount" resultType="java.lang.Double">
        SELECT
        sum(w.drp)
        FROM
        data_att_st_prec w
        WHERE
        w.is_deleted = 0
        AND w.intv = 0.05 AND w.drp is not null
        <if test="rainDTO.beginTime!=null">
            AND w.tm >= #{rainDTO.beginTime}
        </if>
        <if test="rainDTO.endTime!=null">
            AND w.tm &lt; #{rainDTO.endTime}
        </if>
        <if test="rainDTO.rtuCode!=null">
            and w.rtu_code = #{rainDTO.rtuCode}
        </if>
    </select>

    <select id="selectList" resultMap="rtuRainDataResultMap">
        SELECT
        w.*
        FROM
        data_att_st_prec w
        WHERE
        w.is_deleted = 0
        AND w.drp is not null
        <if test="dto.intv!=null">
            AND w.intv = #{dto.intv}
        </if>
        <if test="dto.intv==null">
            AND w.intv = 0.05
        </if>
        <if test="dto.beginTime!=null">
            AND w.tm >= #{dto.beginTime}
        </if>
        <if test="dto.endTime!=null">
            AND w.tm &lt; #{dto.endTime}
        </if>
        <if test="dto.rtuCode!=null">
            and w.rtu_code = #{dto.rtuCode}
        </if>
        order by w.tm
    </select>

    <select id="selectLast" resultMap="rtuRainDataResultMap">
        SELECT
        w.*
        FROM
        data_att_st_prec w
        WHERE
        w.is_deleted = 0
        AND w.drp is not null
        <if test="dto.endTime!=null">
            AND w.tm &lt; #{dto.endTime}
        </if>
        <if test="dto.rtuCode!=null">
            and w.rtu_code = #{dto.rtuCode}
        </if>
        order by w.tm desc limit 1
    </select>
</mapper>
