<?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.yjxt.business.rain.mapper.YjRainDataMapper">

    <resultMap id="rtuRainDataVOResultMap" type="org.springblade.modules.yjxt.business.rain.vo.YjRainDataVO">
        <result column="rtu_code" property="rtuCode"/>
        <result column="tm" property="tm"/>
        <result column="drp" property="drp"/>
        <result column="rtu_name" property="rtuName"/>
        <result column="ad_city" property="adCity"/>
        <result column="ad_dist" property="adDist"/>
        <result column="from_time" property="fromTime"/>
        <result column="h1tm" property="h1tm"/>
        <result column="h1drp" property="h1drp"/>
        <result column="h3tm" property="h3tm"/>
        <result column="h3drp" property="h3drp"/>
        <result column="h6tm" property="h6tm"/>
        <result column="h6drp" property="h6drp"/>
        <result column="h12tm" property="h12tm"/>
        <result column="h12drp" property="h12drp"/>
        <result column="h24tm" property="h24tm"/>
        <result column="h24drp" property="h24drp"/>
        <result column="lng" property="lng"/>
        <result column="lat" property="lat"/>
    </resultMap>


    <select id="selectYjPage" resultMap="rtuRainDataVOResultMap">
        SELECT
        n.*,h1.tm AS h1tm,h1.drp AS h1drp,h3.tm AS h3tm,h3.drp AS h3drp,h6.tm AS h6tm,h6.drp AS h6drp,h12.tm AS
        h12tm,h12.drp AS h12drp,h24.tm AS h24tm,h24.drp AS h24drp,
        r.rtu_name,r.ad_city,r.ad_dist,r.lng,r.lat
        FROM
        data_att_st_prec_cache n
        LEFT JOIN data_att_st_prec_h1_cache h1 ON h1.rtu_code = n.rtu_code and h1.is_deleted=0
        LEFT JOIN data_att_st_prec_h3_cache h3 ON h3.rtu_code = n.rtu_code and h3.is_deleted=0
        LEFT JOIN data_att_st_prec_h6_cache h6 ON h6.rtu_code = n.rtu_code and h6.is_deleted=0
        LEFT JOIN data_att_st_prec_h12_cache h12 ON h12.rtu_code = n.rtu_code and h12.is_deleted=0
        LEFT JOIN data_att_st_prec_h24_cache h24 ON h24.rtu_code = n.rtu_code and h24.is_deleted=0
        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="rtuDataRainDTO.rtuName!=null ">
            and r.rtu_name LIKE concat(concat('%', #{rtuDataRainDTO.rtuName}), '%')
        </if>
        <if test="rtuDataRainDTO.rtuCode!=null">
            and n.rtu_code like concat(concat('%', #{rtuDataRainDTO.rtuCode}), '%')
        </if>
        <if test="rtuDataRainDTO.excludeCode!=null and rtuDataRainDTO.excludeCode==1">
            and a.exclude_ad = 0
        </if>
        <if test="rtuDataRainDTO.adCode!=null">
            and (a.adcd =#{rtuDataRainDTO.adCode} or  FIND_IN_SET(#{rtuDataRainDTO.adCode} ,a.ancestors) >0 )
        </if>
        <if test="rtuDataRainDTO.rainCountType!=null and rtuDataRainDTO.rainCountType == 0">
            and n.drp >0
        </if>
        <if test="rtuDataRainDTO.rainCountType!=null and rtuDataRainDTO.rainCountType == 1">
            and h1.drp >0
        </if>
        <if test="rtuDataRainDTO.rainCountType!=null and rtuDataRainDTO.rainCountType == 3">
            and h3.drp >0
        </if>
        <if test="rtuDataRainDTO.rainCountType!=null and rtuDataRainDTO.rainCountType == 6">
            and h6.drp >0
        </if>
        <if test="rtuDataRainDTO.rainCountType!=null and rtuDataRainDTO.rainCountType == 12">
            and h12.drp >0
        </if>
        <if test="rtuDataRainDTO.rainCountType!=null and rtuDataRainDTO.rainCountType == 24">
            and h24.drp >0
        </if>
        <choose>
            <when test="rtuDataRainDTO.order!=null and rtuDataRainDTO.order=='ascending'">
                <if test="rtuDataRainDTO.prop!=null and rtuDataRainDTO.prop=='tm'">
                    order by n.tm
                </if>
                <if test="rtuDataRainDTO.prop!=null and rtuDataRainDTO.prop=='drp'">
                    order by n.drp
                </if>
            </when>
            <when test="rtuDataRainDTO.order!=null and rtuDataRainDTO.order=='descending'">
                <if test="rtuDataRainDTO.prop!=null and rtuDataRainDTO.prop=='tm'">
                    order by n.tm desc
                </if>
                <if test="rtuDataRainDTO.prop!=null and rtuDataRainDTO.prop=='drp'">
                    order by n.drp desc
                </if>
            </when>
            <when test="rtuDataRainDTO.rainCountType!=null">
                <if test="rtuDataRainDTO.rainCountType == 0">
                    order by n.drp desc,n.tm desc
                </if>
                <if test="rtuDataRainDTO.rainCountType == 1">
                    order by h1.drp desc
                </if>
                <if test="rtuDataRainDTO.rainCountType == 3">
                    order by h3.drp desc
                </if>
                <if test="rtuDataRainDTO.rainCountType == 6">
                    order by h6.drp desc
                </if>
                <if test="rtuDataRainDTO.rainCountType == 12">
                    order by h12.drp desc
                </if>
                <if test="rtuDataRainDTO.rainCountType == 24">
                    order by h24.drp desc
                </if>
            </when>
            <otherwise>
                order by n.drp desc,n.tm desc
            </otherwise>
        </choose>
    </select>
</mapper>
