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

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


    <resultMap id="rtuRiverDataVOResultMap" type="org.springblade.modules.business.data.vo.RtuDataRiverVO">
        <result column="rtu_code" property="rtuCode"/>
        <result column="tm" property="tm"/>
        <result column="z" property="z"/>
        <result column="ad_city" property="adCity"/>
        <result column="ad_dist" property="adDist"/>
        <result column="from_time" property="fromTime"/>
    </resultMap>

    <resultMap id="rtuWaterDataVOResultMap" type="org.springblade.modules.business.data.vo.RtuDataRiverVO">
        <result column="rtu_code" property="rtuCode"/>
        <result column="tm" property="tm"/>
        <result column="z" property="z"/>
        <result column="rz" property="rz"/>
        <result column="ad_city" property="adCity"/>
        <result column="ad_dist" property="adDist"/>
        <result column="from_time" property="fromTime"/>
    </resultMap>

    <select id="selectPage" resultMap="rtuRiverDataVOResultMap">
        SELECT
        n.*,
        r.rtu_name,r.ad_city,r.ad_dist
        FROM
        data_att_st_waco_ware_cache n
        LEFT JOIN  rtu_info r ON n.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
        n.is_deleted = 0
        <if test="riverDTO.rtuName!=null ">
            and r.rtu_name LIKE concat(concat('%', #{riverDTO.rtuName}), '%')
        </if>
        <if test="riverDTO.rtuCode!=null">
            and n.rtu_code like concat(concat('%', #{riverDTO.rtuCode}), '%')
        </if>
        <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>
        order by r.ad_code,r.rtu_code
    </select>

    <select id="selectWaterPage" resultMap="rtuWaterDataVOResultMap">
        SELECT r.rtu_code,r.rtu_name,res.rz,w.z,res.tm rsvrTm,w.tm,r.ad_city,r.ad_dist
        FROM rtu_info r
        LEFT JOIN data_att_st_res_ware_cache res  on res.rtu_code = r.rtu_code and res.is_deleted=0
        LEFT JOIN data_att_st_waco_ware_cache w on w.rtu_code = r.rtu_code 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.rtuName!=null ">
            and r.rtu_name LIKE concat(concat('%', #{riverDTO.rtuName}), '%')
        </if>
        <if test="riverDTO.rtuCode!=null">
            and r.rtu_code like concat(concat('%', #{riverDTO.rtuCode}), '%')
        </if>
        <if test="riverDTO.adCode!=null">
            and (r.ad_code =#{riverDTO.adCode} or a.ancestors in (SELECT ancestors FROM data_ad_cd_b WHERE ancestors  like concat(concat('%', #{riverDTO.adCode}), '%')))
        </if>
        <choose>
            <when test="riverDTO.order!=null and riverDTO.order=='ascending'">
                <if test="riverDTO.prop!=null ">
                    order by res.tm,w.tm
                </if>
            </when>
            <when test="riverDTO.order!=null and riverDTO.order=='descending'">
                <if test="riverDTO.prop!=null ">
                    order by res.tm desc,w.tm desc
                </if>
            </when>
            <otherwise>
                order by r.ad_code,r.rtu_code
            </otherwise>
        </choose>
    </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_cache 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_cache 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>
