<?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.check.process.mapper.RtuOrderProcessMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="orderProcessInfoResultMap"
               type="org.springblade.modules.business.check.process.entity.OrderProcessInfoEntity">


        <result column="order_id" property="orderId"/>
        <result column="process_desc" property="processDesc"/>
        <result column="order_status_key" property="orderStatusKey"/>

        <result column="rtu_replace" property="rtuReplace"/>
        <result column="rtu_desc" property="rtuDesc"/>
        <result column="rtu_status" property="rtuStatus"/>
        <result column="rtu_brand" property="rtuBrand"/>
        <result column="rtu_model" property="rtuModel"/>
        <result column="rain_sensor_replace" property="rainSensorReplace"/>
        <result column="rain_sensor_desc" property="rainSensorDesc"/>
        <result column="rain_sensor_status" property="rainSensorStatus"/>
        <result column="rain_sensor_active" property="rainSensorActive"/>
        <result column="rain_sensor_brand" property="rainSensorBrand"/>
        <result column="rain_sensor_model" property="rainSensorModel"/>
        <result column="water_sensor_replace" property="waterSensorReplace"/>
        <result column="water_sensor_desc" property="waterSensorDesc"/>
        <result column="water_level_sensor_status" property="waterLevelSensorStatus"/>
        <result column="water_level_sensor_active" property="waterLevelSensorActive"/>
        <result column="water_sensor_brand" property="waterSensorBrand"/>
        <result column="water_sensor_model" property="waterSensorModel"/>
        <result column="ground_sensor_replace" property="groundSensorReplace"/>
        <result column="ground_sensor_desc" property="groundSensorDesc"/>
        <result column="ground_water_sensor_status" property="groundWaterSensorStatus"/>
        <result column="ground_water_sensor_active" property="groundWaterSensorActive"/>
        <result column="ground_sensor_brand" property="groundSensorBrand"/>
        <result column="ground_sensor_model" property="groundSensorModel"/>
        <result column="battery_replace" property="batteryReplace"/>
        <result column="sun_power_replace" property="sunPowerReplace"/>
        <result column="power_desc" property="powerDesc"/>
        <result column="power_status" property="powerStatus"/>

        <result column="network_overdue" property="networkOverdue"/>
        <result column="network_desc" property="networkDesc"/>
        <result column="network_status" property="networkStatus"/>
        <result column="network_active" property="networkActive"/>

        <result column="satellite_desc" property="satelliteDesc"/>
        <result column="satellite_status" property="satelliteStatus"/>
        <result column="satellite_active" property="satelliteActive"/>

        <result column="base_before_photo1_take" property="baseBeforePhoto1Take"/>
        <result column="base_before_photo1_url" property="baseBeforePhoto1Url"/>
        <result column="base_before_photo2_take" property="baseBeforePhoto2Take"/>
        <result column="base_before_photo2_url" property="baseBeforePhoto2Url"/>
        <result column="base_after_photo1_take" property="baseAfterPhoto1Take"/>
        <result column="base_after_photo1_url" property="baseAfterPhoto1Url"/>
        <result column="base_after_photo2_take" property="baseAfterPhoto2Take"/>
        <result column="base_after_photo2_url" property="baseAfterPhoto2Url"/>

        <result column="rtu_photo_take" property="rtuPhotoTake"/>
        <result column="rtu_photo_url" property="rtuPhotoUrl"/>
        <result column="rain_sensor_photo_take" property="rainSensorPhotoTake"/>
        <result column="rain_sensor_photo_url" property="rainSensorPhotoUrl"/>
        <result column="water_sensor_photo_take" property="waterSensorPhotoTake"/>
        <result column="water_sensor_photo_url" property="waterSensorPhotoUrl"/>
        <result column="ground_sensor_photo_take" property="groundSensorPhotoTake"/>
        <result column="ground_sensor_photo_url" property="groundSensorPhotoUrl"/>
        <result column="battery_photo_take" property="batteryPhotoTake"/>
        <result column="battery_photo_url" property="batteryPhotoUrl"/>
        <result column="sun_photo_take" property="sunPhotoTake"/>
        <result column="sun_photo_url" property="sunPhotoUrl"/>
        <result column="network_signal_status" property="networkSignalStatus"/>
        <result column="photo_supplement_submit_status" property="photoSupplementSubmitStatus"/>

    </resultMap>

    <resultMap id="orderProcessInfoVOResultMap"
               type="org.springblade.modules.business.check.process.vo.OrderProcessInfoVO">

        <result column="order_id" property="orderId"/>
        <result column="process_desc" property="processDesc"/>
        <result column="order_status_key" property="orderStatusKey"/>
        <result column="rtu_replace" property="rtuReplace"/>
        <result column="rtu_desc" property="rtuDesc"/>
        <result column="rtu_status" property="rtuStatus"/>
        <result column="rtu_brand" property="rtuBrand"/>
        <result column="rtu_model" property="rtuModel"/>
        <result column="rain_sensor_replace" property="rainSensorReplace"/>
        <result column="rain_sensor_desc" property="rainSensorDesc"/>
        <result column="rain_sensor_status" property="rainSensorStatus"/>
        <result column="rain_sensor_active" property="rainSensorActive"/>
        <result column="rain_sensor_brand" property="rainSensorBrand"/>
        <result column="rain_sensor_model" property="rainSensorModel"/>
        <result column="water_sensor_replace" property="waterSensorReplace"/>
        <result column="water_sensor_desc" property="waterSensorDesc"/>
        <result column="water_level_sensor_status" property="waterLevelSensorStatus"/>
        <result column="water_level_sensor_active" property="waterLevelSensorActive"/>
        <result column="water_sensor_brand" property="waterSensorBrand"/>
        <result column="water_sensor_model" property="waterSensorModel"/>
        <result column="ground_sensor_replace" property="groundSensorReplace"/>

        <result column="ground_sensor_desc" property="groundSensorDesc"/>
        <result column="ground_water_sensor_status" property="groundWaterSensorStatus"/>
        <result column="ground_water_sensor_active" property="groundWaterSensorActive"/>
        <result column="ground_sensor_brand" property="groundSensorBrand"/>
        <result column="ground_sensor_model" property="groundSensorModel"/>
        <result column="battery_replace" property="batteryReplace"/>
        <result column="sun_power_replace" property="sunPowerReplace"/>
        <result column="power_desc" property="powerDesc"/>
        <result column="power_status" property="powerStatus"/>
        <result column="network_overdue" property="networkOverdue"/>
        <result column="network_desc" property="networkDesc"/>
        <result column="network_status" property="networkStatus"/>
        <result column="network_active" property="networkActive"/>
        <result column="satellite_desc" property="satelliteDesc"/>
        <result column="satellite_status" property="satelliteStatus"/>
        <result column="satellite_active" property="satelliteActive"/>
        <result column="base_before_photo1_take" property="baseBeforePhoto1Take"/>
        <result column="base_before_photo1_url" property="baseBeforePhoto1Url"/>
        <result column="base_before_photo2_take" property="baseBeforePhoto2Take"/>
        <result column="base_before_photo2_url" property="baseBeforePhoto2Url"/>
        <result column="base_after_photo1_take" property="baseAfterPhoto1Take"/>
        <result column="base_after_photo1_url" property="baseAfterPhoto1Url"/>
        <result column="base_after_photo2_take" property="baseAfterPhoto2Take"/>
        <result column="base_after_photo2_url" property="baseAfterPhoto2Url"/>
        <result column="rtu_photo_take" property="rtuPhotoTake"/>
        <result column="rtu_photo_url" property="rtuPhotoUrl"/>
        <result column="rain_sensor_photo_take" property="rainSensorPhotoTake"/>
        <result column="rain_sensor_photo_url" property="rainSensorPhotoUrl"/>
        <result column="water_sensor_photo_take" property="waterSensorPhotoTake"/>
        <result column="water_sensor_photo_url" property="waterSensorPhotoUrl"/>
        <result column="ground_sensor_photo_take" property="groundSensorPhotoTake"/>
        <result column="ground_sensor_photo_url" property="groundSensorPhotoUrl"/>
        <result column="battery_photo_take" property="batteryPhotoTake"/>
        <result column="battery_photo_url" property="batteryPhotoUrl"/>
        <result column="sun_photo_take" property="sunPhotoTake"/>
        <result column="sun_photo_url" property="sunPhotoUrl"/>
        <result column="rtu_name" property="rtuName"/>
        <result column="rtu_code" property="rtuCode"/>
        <result column="real_name" property="orderProcessUserName"/>
        <result column="dict_value" property="orderStatusName"/>
        <result column="order_confirm_user" property="orderConfirmUser"/>
        <result column="network_signal_status" property="networkSignalStatus"/>
        <result column="photo_supplement_submit_status" property="photoSupplementSubmitStatus"/>
        <result column="ad_city" property="adCity"/>
        <result column="ad_dist" property="adDist"/>

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

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

        <result column="warn_id" property="warnId"/>

    </resultMap>

    <select id="selectList" resultMap="orderProcessInfoVOResultMap">
        SELECT
        p.*,u.real_name,o.order_confirm_user,r.is_rain,r.is_river,r.is_res,r.is_ground,r.is_satellite,r.rtu_name,o.rtu_code
        FROM
        rtu_failure_check_order_process p
        LEFT JOIN rtu_failure_check_order o ON o.id = p.order_id and o.is_deleted=0
        LEFT JOIN rtu_info r ON r.rtu_code = o.rtu_code and r.is_deleted=0
        LEFT JOIN blade_user u ON u.id = p.update_user and u.is_deleted=0
        WHERE
        p.is_deleted = 0
        <if test="checkOrderInfoDTO.id!=null">
            and p.order_id =#{checkOrderInfoDTO.id}
        </if>
        order by p.create_time
    </select>

    <select id="selectReportList" resultMap="orderProcessInfoVOResultMap">
        SELECT
        p.*,u.real_name,o.order_confirm_user,r.is_rain,r.is_river,r.is_res,r.is_ground,r.is_satellite,r.rtu_name,o.rtu_code,o.warn_id,a.adnm AS ad_city
        FROM
        rtu_failure_check_order_process p
        LEFT JOIN rtu_failure_check_order o ON o.id = p.order_id and o.is_deleted=0
        LEFT JOIN rtu_info r ON r.rtu_code = o.rtu_code and r.is_deleted=0
        LEFT JOIN blade_user u ON u.id = p.update_user and u.is_deleted=0
        LEFT JOIN data_ad_cd_b a ON a.adcd = r.ad_code and a.is_deleted=0
        WHERE
        p.is_deleted = 0
        AND o.process_user is not null
        <if test="checkOrderInfoDTO.reportStartDate!=null">
            and p.create_time &gt;= #{checkOrderInfoDTO.reportStartDate}
        </if>
        <if test="checkOrderInfoDTO.reportEndDate!=null">
            and p.create_time &lt; #{checkOrderInfoDTO.reportEndDate}
        </if>
        <if test="checkOrderInfoDTO.orderStatusKey!=null">
            and p.order_status_key = #{checkOrderInfoDTO.orderStatusKey}
        </if>
        <if test="checkOrderInfoDTO.excludeCode!=null and checkOrderInfoDTO.excludeCode==1">
            and a.exclude_ad = 0
        </if>
        <if test="checkOrderInfoDTO.adCode!=null">
            and (a.adcd =#{checkOrderInfoDTO.adCode} or a.ancestors in (SELECT ancestors FROM data_ad_cd_b WHERE is_deleted =0 and ancestors  like concat(concat('%', #{checkOrderInfoDTO.adCode}), '%')))
        </if>
        order by a.adnm ,r.rtu_code
    </select>


    <select id="selectPage" resultMap="orderProcessInfoVOResultMap">
        SELECT
        p.*,u.real_name,r.is_rain,r.is_river,r.is_res,r.is_ground,r.is_satellite
        FROM
        rtu_failure_check_order_process p
        LEFT JOIN blade_user u ON u.id = p.update_user and u.is_deleted=0
        LEFT JOIN rtu_failure_check_order o ON o.id = p.order_id and o.is_deleted=0
        LEFT JOIN rtu_info r ON r.rtu_code = o.rtu_code and r.is_deleted=0
        WHERE
        p.is_deleted = 0
        <if test="checkOrderInfoDTO.id!=null">
            and p.order_id =#{checkOrderInfoDTO.id}
        </if>
        order by p.create_time desc
    </select>

    <select id="selectTodayProcessList" resultMap="orderProcessInfoVOResultMap">
        SELECT
        p.*,r.rtu_code,r.rtu_name,u.real_name,d.dict_value,r.ad_city,r.ad_dist,a.adcd,a.ancestors
        FROM
        rtu_failure_check_order_process p
        LEFT JOIN rtu_failure_check_order o ON o.id=p.order_id
        LEFT JOIN rtu_info r ON r.rtu_code = o.rtu_code
        LEFT JOIN data_ad_cd_b a ON a.adcd = r.ad_code and a.is_deleted=0
        LEFT JOIN blade_user u ON u.id=p.create_user
        LEFT JOIN blade_dict_biz d ON p.order_status_key=d.id
        WHERE
        p.is_deleted = 0
        <if test="processInfoDTO.createTime!=null">
            and   DATE_FORMAT(p.create_time,'%Y-%m-%d') = DATE_FORMAT(#{processInfoDTO.createTime},'%Y-%m-%d')
        </if>
        <if test="processInfoDTO.rtuCode!=null">
            and r.rtu_code like concat(concat('%', #{processInfoDTO.rtuCode}), '%')
        </if>
        <if test="processInfoDTO.rtuName!=null">
            and r.rtu_name like concat(concat('%', #{processInfoDTO.rtuName}), '%')
        </if>
        <if test="processInfoDTO.excludeCode!=null and processInfoDTO.excludeCode==1">
            and a.exclude_ad = 0
        </if>
        <if test="processInfoDTO.adCode!=null">
            and (a.adcd =#{processInfoDTO.adCode} or a.ancestors in (SELECT ancestors FROM data_ad_cd_b WHERE is_deleted =0 and ancestors  like concat(concat('%', #{processInfoDTO.adCode}), '%')))
        </if>
        order by p.create_time desc
        limit 10
    </select>


    <select id="selectTodayProcessPage" resultMap="orderProcessInfoVOResultMap">
        SELECT
        p.*,r.rtu_code,r.rtu_name,u.real_name,d.dict_value,r.ad_city,r.ad_dist,a.adcd,a.ancestors
        FROM
        rtu_failure_check_order_process p
        LEFT JOIN rtu_failure_check_order o ON o.id=p.order_id
        LEFT JOIN rtu_info r ON r.rtu_code = o.rtu_code
        LEFT JOIN data_ad_cd_b a ON a.adcd = r.ad_code and a.is_deleted=0
        LEFT JOIN blade_user u ON u.id=p.create_user
        LEFT JOIN blade_dict_biz d ON p.order_status_key=d.id
        WHERE
        p.is_deleted = 0
        <if test="processInfoDTO.createTime!=null">
            and   DATE_FORMAT(p.create_time,'%Y-%m-%d') = DATE_FORMAT(#{processInfoDTO.createTime},'%Y-%m-%d')
        </if>
        <if test="processInfoDTO.rtuCode!=null">
            and r.rtu_code like concat(concat('%', #{processInfoDTO.rtuCode}), '%')
        </if>
        <if test="processInfoDTO.rtuName!=null">
            and r.rtu_name like concat(concat('%', #{processInfoDTO.rtuName}), '%')
        </if>
        <if test="processInfoDTO.excludeCode!=null and processInfoDTO.excludeCode==1">
            and a.exclude_ad = 0
        </if>
        <if test="processInfoDTO.adCode!=null">
            and (a.adcd =#{processInfoDTO.adCode} or a.ancestors in (SELECT ancestors FROM data_ad_cd_b WHERE is_deleted =0 and ancestors  like concat(concat('%', #{processInfoDTO.adCode}), '%')))
        </if>
        order by p.create_time desc
    </select>


    <select id="todayProcessCount" resultType="java.lang.Long">
        select  count(*) from
        ( SELECT
        o.rtu_code
        FROM
        rtu_failure_check_order_process p
        LEFT JOIN rtu_failure_check_order o ON o.id=p.order_id
        LEFT JOIN rtu_info r ON r.rtu_code = o.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
        p.is_deleted = 0
        <if test="processInfoDTO.createTime!=null">
            and   DATE_FORMAT(p.create_time,'%Y-%m-%d') = DATE_FORMAT(#{processInfoDTO.createTime},'%Y-%m-%d')
        </if>
        <if test="processInfoDTO.excludeCode!=null and processInfoDTO.excludeCode==1">
            and a.exclude_ad = 0
        </if>
        <if test="processInfoDTO.adCode!=null">
            and (a.adcd =#{processInfoDTO.adCode} or a.ancestors in (SELECT ancestors FROM data_ad_cd_b WHERE is_deleted =0 and ancestors  like concat(concat('%', #{processInfoDTO.adCode}), '%')))
        </if>
        group by o.rtu_code ) s

    </select>


    <select id="selectNoNetworkSignalPage" resultMap="orderProcessInfoVOResultMap">
        SELECT
        p.*,u.real_name,o.rtu_code,r.rtu_name,r.ad_city,r.ad_dist
        FROM
        rtu_failure_check_order_process p
        LEFT JOIN blade_user u ON u.id = p.update_user and u.is_deleted=0
        LEFT JOIN rtu_failure_check_order o ON o.id=p.order_id
        LEFT JOIN rtu_info r ON r.rtu_code = o.rtu_code
        LEFT JOIN data_ad_cd_b a ON a.adcd = r.ad_code and a.is_deleted=0
        WHERE
        p.is_deleted = 0
        and p.network_signal_status = 1
        <if test="processInfoDTO.rtuName!=null">
            and r.rtu_name like concat(concat('%', #{processInfoDTO.rtuName}), '%')
        </if>
        <if test="processInfoDTO.rtuCode!=null">
            and r.rtu_code like concat(concat('%', #{processInfoDTO.rtuCode}), '%')
        </if>
        <if test="processInfoDTO.excludeCode!=null and processInfoDTO.excludeCode==1">
            and a.exclude_ad = 0
        </if>
        <if test="processInfoDTO.adCode!=null">
            and (r.ad_code =#{processInfoDTO.adCode} or a.ancestors in (SELECT ancestors FROM data_ad_cd_b WHERE
            is_deleted =0 and ancestors
            like concat(concat('%', #{processInfoDTO.adCode}), '%')))
        </if>
        order by  p.photo_supplement_submit_status ASC,p.create_time ASC,o.rtu_code ASC

    </select>

</mapper>
