<?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.baseinfo.rtu.mapper.RtuInfoMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="rtuInfoResultMap" type="org.springblade.modules.baseinfo.rtu.entity.RtuInfoEntity">

        <result column="rtu_code" property="rtuCode"/>
        <result column="rtu_name" property="rtuName"/>
        <result column="lng" property="lng"/>
        <result column="lat" property="lat"/>
        <result column="org_id" property="orgId"/>

        <result column="ad_code" property="adCode"/>
        <result column="ad_name" property="adName"/>

        <result column="project_id" property="projectId"/>
        <result column="manage_company" property="manageCompany"/>

        <result column="rtu_kinds" property="rtuKinds"/>
        <result column="remark" property="remark"/>
        <result column="location_desc" property="locationDesc"/>

        <result column="is_rain" property="isRain"/>
        <result column="is_river" property="isRiver"/>
        <result column="is_res" property="isRes"/>
        <result column="is_video" property="isVideo"/>

        <result column="is_ground" property="isGround"/>
        <result column="is_satellite" property="isSatellite"/>

        <result column="rtu_brand" property="rtuBrand"/>
        <result column="rtu_model" property="rtuModel"/>
        <result column="rtu_replace_date" property="rtuReplaceDate"/>
        <result column="datagram_to_sheng" property="datagramToSheng"/>
        <result column="datagram_to_shi" property="datagramToShi"/>
        <result column="datagram_to_qu" property="datagramToQu"/>

        <result column="rain_sensor_brand" property="rainSensorBrand"/>
        <result column="rain_sensor_model" property="rainSensorModel"/>
        <result column="rain_sensor_replace_date" property="rainSensorReplaceDate"/>

        <result column="water_sensor_type" property="waterSensorType"/>
        <result column="water_sensor_brand" property="waterSensorBrand"/>
        <result column="water_sensor_model" property="waterSensorModel"/>
        <result column="water_sensor_replace_date" property="waterSensorReplaceDate"/>

        <result column="ground_sensor_type" property="groundSensorType"/>
        <result column="ground_sensor_brand" property="groundSensorBrand"/>
        <result column="ground_sensor_model" property="groundSensorModel"/>
        <result column="ground_sensor_replace_date" property="groundSensorReplaceDate"/>

        <result column="battery_model" property="batteryModel"/>
        <result column="battery_replace_date" property="batteryReplaceDate"/>

        <result column="sun_power_model" property="sunPowerModel"/>
        <result column="sun_power_controller_brand" property="sunPowerControllerBrand"/>
        <result column="sun_power_controller_model" property="sunPowerControllerModel"/>

        <result column="network_sim_id" property="networkSimId"/>
        <result column="network_payer" property="networkPayer"/>
        <result column="network_pay_end_date" property="networkPayEndDate"/>

        <result column="satellite_model" property="satelliteModel"/>

        <result column="ad_city" property="adCity"/>
        <result column="ad_dist" property="adDist"/>

    </resultMap>

    <!-- 通用查询映射结果 -->
    <resultMap id="rtuInfoVOResultMap" type="org.springblade.modules.baseinfo.rtu.vo.RtuInfoVO">

        <result column="rtu_code" property="rtuCode"/>
        <result column="rtu_name" property="rtuName"/>
        <result column="lng" property="lng"/>
        <result column="lat" property="lat"/>
        <result column="org_id" property="orgId"/>

        <result column="ad_code" property="adCode"/>
        <result column="ad_name" property="adName"/>

        <result column="project_id" property="projectId"/>
        <result column="manage_company" property="manageCompany"/>

        <result column="rtu_kinds" property="rtuKinds"/>
        <result column="remark" property="remark"/>
        <result column="location_desc" property="locationDesc"/>
        <result column="remark" property="remark"/>

        <result column="is_rain" property="isRain"/>
        <result column="is_river" property="isRiver"/>
        <result column="is_res" property="isRes"/>
        <result column="is_video" property="isVideo"/>
        <result column="is_ground" property="isGround"/>
        <result column="is_satellite" property="isSatellite"/>

        <result column="rtu_brand" property="rtuBrand"/>
        <result column="rtu_model" property="rtuModel"/>
        <result column="rtu_replace_date" property="rtuReplaceDate"/>
        <result column="datagram_to_sheng" property="datagramToSheng"/>
        <result column="datagram_to_shi" property="datagramToShi"/>
        <result column="datagram_to_qu" property="datagramToQu"/>

        <result column="rain_sensor_brand" property="rainSensorBrand"/>
        <result column="rain_sensor_model" property="rainSensorModel"/>
        <result column="rain_sensor_replace_date" property="rainSensorReplaceDate"/>

        <result column="water_sensor_type" property="waterSensorType"/>
        <result column="water_sensor_brand" property="waterSensorBrand"/>
        <result column="water_sensor_model" property="waterSensorModel"/>
        <result column="water_sensor_replace_date" property="waterSensorReplaceDate"/>

        <result column="ground_sensor_type" property="groundSensorType"/>
        <result column="ground_sensor_brand" property="groundSensorBrand"/>
        <result column="ground_sensor_model" property="groundSensorModel"/>
        <result column="ground_sensor_replace_date" property="groundSensorReplaceDate"/>

        <result column="battery_model" property="batteryModel"/>
        <result column="battery_replace_date" property="batteryReplaceDate"/>

        <result column="sun_power_model" property="sunPowerModel"/>
        <result column="sun_power_controller_brand" property="sunPowerControllerBrand"/>
        <result column="sun_power_controller_model" property="sunPowerControllerModel"/>

        <result column="network_sim_id" property="networkSimId"/>
        <result column="network_payer" property="networkPayer"/>
        <result column="network_pay_end_date" property="networkPayEndDate"/>

        <result column="satellite_model" property="satelliteModel"/>
        <result column="ad_city" property="adCity"/>
        <result column="ad_dist" property="adDist"/>

        <result column="adcd" property="adcd"/>
        <result column="adnm" property="adnm"/>
    </resultMap>


    <select id="selectPage" resultMap="rtuInfoVOResultMap">
        SELECT
        n.*
        FROM
        rtu_info n
        LEFT JOIN data_ad_cd_b a ON a.adcd = n.ad_code and a.is_deleted=0
        WHERE
        n.is_deleted = 0
        <if test="rtuInfoDTO.rtuName!=null">
            and n.rtu_name like concat(concat('%', #{rtuInfoDTO.rtuName}), '%')
        </if>
        <if test="rtuInfoDTO.rtuCode!=null">
            and n.rtu_code like concat(concat('%', #{rtuInfoDTO.rtuCode}), '%')
        </if>
        <if test="rtuInfoDTO.isRain!=null">
            and n.is_rain = #{rtuInfoDTO.isRain}
        </if>
        <if test="rtuInfoDTO.isRiver!=null">
            and (n.is_river = #{rtuInfoDTO.isRiver} or  n.is_res = #{rtuInfoDTO.isRiver})
        </if>
        <if test="rtuInfoDTO.isRes!=null">
            and n.is_res = #{rtuInfoDTO.isRes}
        </if>
        <if test="rtuInfoDTO.isGround!=null">
            and n.is_ground = #{rtuInfoDTO.isGround}
        </if>
        <if test="rtuInfoDTO.isVideo!=null">
            and n.is_video = #{rtuInfoDTO.isVideo}
        </if>
        <if test="rtuInfoDTO.excludeCode!=null and rtuInfoDTO.excludeCode==1">
            and a.exclude_ad = 0
        </if>
        <if test="rtuInfoDTO.adCode!=null">
            and (n.ad_code =#{rtuInfoDTO.adCode} or a.ancestors in (SELECT ancestors FROM data_ad_cd_b WHERE is_deleted = 0 and  ancestors
            like concat(concat('%', #{rtuInfoDTO.adCode}), '%')))
        </if>
        order by n.rtu_code
    </select>

    <select id="rtuCount" resultType="java.lang.Long">
        SELECT
        count(n.rtu_code)
        FROM
        rtu_info n
        LEFT JOIN data_ad_cd_b a ON a.adcd = n.ad_code and a.is_deleted=0
        WHERE
        n.is_deleted = 0
        <if test="rtuInfoDTO.excludeCode!=null and rtuInfoDTO.excludeCode==1">
            and a.exclude_ad = 0
        </if>
        <if test="rtuInfoDTO.adCode!=null">
            and (n.ad_code =#{rtuInfoDTO.adCode} or a.ancestors in (SELECT ancestors FROM data_ad_cd_b WHERE is_deleted = 0 and ancestors like
            concat(concat('%', #{rtuInfoDTO.adCode}), '%')))
        </if>
    </select>

    <select id="rtuRainCount" resultType="java.lang.Long">
        SELECT
        count(n.rtu_code)
        FROM
        rtu_info n
        LEFT JOIN data_ad_cd_b a ON a.adcd = n.ad_code and a.is_deleted=0
        WHERE
        n.is_deleted = 0 and n.is_rain = 1
        <if test="rtuInfoDTO.excludeCode!=null and rtuInfoDTO.excludeCode==1">
            and a.exclude_ad = 0
        </if>
        <if test="rtuInfoDTO.adCode!=null">
            and (n.ad_code =#{rtuInfoDTO.adCode} or a.ancestors in (SELECT ancestors FROM data_ad_cd_b WHERE is_deleted =0 and ancestors like
            concat(concat('%', #{rtuInfoDTO.adCode}), '%')))
        </if>
    </select>

    <select id="rtuRiverCount" resultType="java.lang.Long">
        SELECT
        count(n.rtu_code)
        FROM
        rtu_info n
        LEFT JOIN data_ad_cd_b a ON a.adcd = n.ad_code and a.is_deleted=0
        WHERE
        n.is_deleted = 0 and (n.is_river = 1 or n.is_res = 1)
        <if test="rtuInfoDTO.excludeCode!=null and rtuInfoDTO.excludeCode==1">
            and a.exclude_ad = 0
        </if>
        <if test="rtuInfoDTO.adCode!=null">
            and (n.ad_code =#{rtuInfoDTO.adCode} or a.ancestors in (SELECT ancestors FROM data_ad_cd_b WHERE is_deleted =0 and ancestors like
            concat(concat('%', #{rtuInfoDTO.adCode}), '%')))
        </if>
    </select>

    <select id="rtuGroundCount" resultType="java.lang.Long">
        SELECT
        count(n.rtu_code)
        FROM
        rtu_info n
        LEFT JOIN data_ad_cd_b a ON a.adcd = n.ad_code and a.is_deleted=0
        WHERE
        n.is_deleted = 0 and n.is_ground = 1
        <if test="rtuInfoDTO.excludeCode!=null and rtuInfoDTO.excludeCode==1">
            and a.exclude_ad = 0
        </if>
        <if test="rtuInfoDTO.adCode!=null">
            and (n.ad_code =#{rtuInfoDTO.adCode} or a.ancestors in (SELECT ancestors FROM data_ad_cd_b WHERE is_deleted =0 and ancestors like
            concat(concat('%', #{rtuInfoDTO.adCode}), '%')))
        </if>
    </select>

    <select id="getAdcd" resultMap="rtuInfoVOResultMap">
        SELECT
        n.*,a.adcd ,a.adnm
        FROM
        rtu_info n
        LEFT JOIN  data_mv_att_st_base b on b.st_code = n.rtu_code
        LEFT JOIN data_ad_cd_b a ON a.adcd = b.ad_code and a.is_deleted=0
        WHERE
        n.is_deleted = 0
        <if test="rtuCode!=null">
            and n.rtu_code = #{rtuCode}
        </if>
    </select>

    <select id="rtuList" resultMap="rtuInfoResultMap">
        SELECT
        n.*
        FROM
        rtu_info n
        LEFT JOIN data_ad_cd_b a ON a.adcd = n.ad_code and a.is_deleted=0
        WHERE
        n.is_deleted = 0
        <if test="rtuInfoDTO.isRain!=null">
            and n.is_rain = #{rtuInfoDTO.isRain}
        </if>
        <if test="rtuInfoDTO.isRiver!=null">
            and (n.is_river = #{rtuInfoDTO.isRiver} or  n.is_res = #{rtuInfoDTO.isRiver})
        </if>
        <if test="rtuInfoDTO.isRes!=null">
            and n.is_res = #{rtuInfoDTO.isRes}
        </if>
        <if test="rtuInfoDTO.isGround!=null">
            and n.is_ground = #{rtuInfoDTO.isGround}
        </if>
        <if test="rtuInfoDTO.isVideo!=null">
            and n.is_video = #{rtuInfoDTO.isVideo}
        </if>
        <if test="rtuInfoDTO.excludeCode!=null and rtuInfoDTO.excludeCode==1">
            and a.exclude_ad = 0
        </if>
        <if test="rtuInfoDTO.adCode!=null">
            and (n.ad_code =#{rtuInfoDTO.adCode} or a.ancestors in (SELECT ancestors FROM data_ad_cd_b WHERE is_deleted =0 and ancestors
            like concat(concat('%', #{rtuInfoDTO.adCode}), '%')))
        </if>
        order by n.rtu_code
    </select>
</mapper>
