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

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

    <select id="selectList" resultMap="rtuRiverDataEntityResultMap">
        SELECT
        w.*
        FROM
        data_att_st_waco_ware w
        WHERE
        w.is_deleted = 0
        <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="rtuRiverDataEntityResultMap">
        SELECT
        w.*
        FROM
        data_att_st_waco_ware w
        WHERE
        w.is_deleted = 0
        <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>

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