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

    <!-- 通用查询映射结果 -->
    <resultMap id="waterLevelDataEntityResultMap" type="org.springblade.modules.business.data.entity.RtuDataWaterLevelStoreEntity">
        <result column="rtu_code" property="rtuCode"/>
        <result column="tm" property="tm"/>
        <result column="water_level" property="waterLevel"/>
        <result column="is_river" property="isRiver"/>
        <result column="is_res" property="isRes"/>
        <result column="from_time" property="fromTime"/>
    </resultMap>

    <select id="selectList" resultMap="waterLevelDataEntityResultMap">
        SELECT
        w.*
        FROM
        data_att_st_river_reservoir_level 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="waterLevelDataEntityResultMap">
        SELECT
        w.*
        FROM
        data_att_st_river_reservoir_level 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
        n.rtu_code
        FROM
        data_att_st_river_reservoir_level n
        LEFT JOIN rtu_info r ON r.rtu_code = n.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
        n.is_deleted = 0
        AND n.tm >= #{dto.beginTime}
        AND n.tm &lt; #{dto.endTime}
        <if test="dto.isRiver!=null">
            and n.is_river = #{dto.isRiver}
        </if>
        <if test="dto.isRes!=null">
            and n.is_res = #{dto.isRes}
        </if>
        <if test="dto.excludeCode!=null and dto.excludeCode==1">
            and a.exclude_ad = 0
        </if>
        <if test="dto.adCode!=null">
            and (a.adcd =#{dto.adCode} or a.ancestors in (SELECT ancestors FROM data_ad_cd_b WHERE ancestors  like concat(concat('%', #{dto.adCode}), '%')))
        </if>
        GROUP BY
        n.rtu_code
        ) a
    </select>
</mapper>
