<?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.order.mapper.RtuCheckOrderMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="checkOrderInfoResultMap"
               type="org.springblade.modules.business.check.order.entity.CheckOrderInfoEntity">
        <result column="rtu_code" property="rtuCode"/>
        <result column="order_desc" property="orderDesc"/>
        <result column="order_type" property="orderType"/>
        <result column="order_from_key" property="orderFromKey"/>
        <result column="order_status_key" property="orderStatusKey"/>
        <result column="order_confirm_user" property="orderConfirmUser"/>
        <result column="warn_id" property="warnId"/>
        <result column="order_close" property="orderClose"/>
        <result column="order_confirm" property="orderConfirm"/>
        <result column="order_confirm_time" property="orderConfirmTime"/>
        <result column="contact_user" property="contactUser"/>
        <result column="contact_phone" property="contactPhone"/>
        <result column="order_process_id" property="orderProcessId"/>
        <result column="order_photos" property="orderPhotos"/>
        <result column="order_videos" property="orderVideos"/>
        <result column="batch_id" property="batchId"/>
        <result column="order_rate" property="orderRate"/>
        <result column="order_complete_time" property="orderCompleteTime"/>
        <result column="longitude" property="longitude"/>
        <result column="latitude" property="latitude"/>
        <result column="address" property="address"/>
        <result column="org_id" property="orgId"/>
    </resultMap>

    <!-- 通用查询映射结果 -->
    <resultMap id="checkOrderInfoVOResultMap" type="org.springblade.modules.business.check.order.vo.CheckOrderInfoVO">
        <result column="rtu_code" property="rtuCode"/>
        <result column="order_desc" property="orderDesc"/>
        <result column="order_type" property="orderType"/>
        <result column="order_from_key" property="orderFromKey"/>
        <result column="order_status_key" property="orderStatusKey"/>
        <result column="order_confirm_user" property="orderConfirmUser"/>
        <result column="warn_id" property="warnId"/>
        <result column="order_close" property="orderClose"/>
        <result column="order_confirm" property="orderConfirm"/>
        <result column="order_confirm_time" property="orderConfirmTime"/>
        <result column="lng" property="lng"/>
        <result column="lat" property="lat"/>
        <result column="location_desc" property="locationDesc"/>
        <result column="rtu_name" property="rtuName"/>
        <result column="real_name" property="processorName"/>
        <result column="ad_city" property="adCity"/>
        <result column="ad_dist" property="adDist"/>
        <result column="contact_user" property="contactUser"/>
        <result column="contact_phone" property="contactPhone"/>
        <result column="contact_user_name" property="contactUserName"/>
        <result column="ancestors" property="ancestors"/>
        <result column="adcd" property="adCode"/>
        <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_video" property="isVideo"/>
        <result column="create_order_person_name" property="createOrderPersonName"/>
        <result column="order_process_id" property="orderProcessId"/>
        <result column="order_photos" property="orderPhotos"/>
        <result column="order_videos" property="orderVideos"/>
        <result column="batch_id" property="batchId"/>
        <result column="order_rate" property="orderRate"/>
        <result column="order_complete_time" property="orderCompleteTime"/>
        <result column="longitude" property="longitude"/>
        <result column="latitude" property="latitude"/>
        <result column="address" property="address"/>
        <result column="org_id" property="orgId"/>
    </resultMap>

    <resultMap id="checkOrderCountVOResultMap" type="org.springblade.modules.business.check.order.vo.CheckOrderCountVO">
        <result column="real_name" property="servicePersonName"/>
        <result column="order_count" property="orderCount"/>
        <result column="processing_order_count" property="processingOrderCount"/>
    </resultMap>

    <select id="allOrders" resultMap="checkOrderInfoVOResultMap">
        SELECT
        o.*,u.real_name,u3.phone AS contact_phone,i.rtu_name,i.lng,i.lat,i.location_desc,i.ad_city,i.ad_dist,u2.real_name AS contact_user_name ,u3.real_name create_order_person_name
        FROM
        rtu_failure_check_order o
        LEFT JOIN rtu_info i ON i.rtu_code = o.rtu_code and i.is_deleted=0
        LEFT JOIN data_ad_cd_b a ON a.adcd = i.ad_code and a.is_deleted=0
        LEFT JOIN blade_user u ON u.id = o.update_user and u.is_deleted=0
        LEFT JOIN blade_user u2 ON u2.id = o.contact_user and u2.is_deleted=0
        LEFT JOIN blade_user u3 ON u3.id = o.create_user and u3.is_deleted=0
        WHERE
        o.is_deleted = 0
        <if test="checkOrderInfoDTO.orgId!=null">
            and o.org_id = #{checkOrderInfoDTO.orgId}
        </if>
        <if test="checkOrderInfoDTO.rtuCode!=null">
            and o.rtu_code like concat(concat('%', #{checkOrderInfoDTO.rtuCode}), '%')
        </if>
        <if test="checkOrderInfoDTO.rtuName!=null">
            and i.rtu_name like concat(concat('%', #{checkOrderInfoDTO.rtuName}), '%')
        </if>
        <if test="checkOrderInfoDTO.contactUser!=null">
            and o.contact_user = #{checkOrderInfoDTO.contactUser}
        </if>
        <if test="checkOrderInfoDTO.orderConfirm!=null">
            and o.order_confirm = #{checkOrderInfoDTO.orderConfirm}
        </if>
        <if test="checkOrderInfoDTO.orderClose!=null">
            and o.order_close = #{checkOrderInfoDTO.orderClose}
        </if>
        <if test="checkOrderInfoDTO.adCode!=null">
            and (a.adcd =#{checkOrderInfoDTO.adCode} or a.ancestors in (SELECT ancestors FROM data_ad_cd_b WHERE
            ancestors like concat(concat('%', #{checkOrderInfoDTO.adCode}), '%')))
        </if>
        order by o.create_time ,o.order_confirm ,o.order_close
    </select>

    <select id="selectPage" resultMap="checkOrderInfoVOResultMap">
        SELECT
        o.*,u.real_name,i.rtu_name,i.is_rain,i.is_river,i.is_res,i.is_ground,i.is_video,i.lng,i.lat,i.location_desc,i.ad_city,i.ad_dist,u2.real_name AS contact_user_name,a.adcd,a.ancestors,u2.real_name create_order_persion_name,u2.phone AS contact_phone
        FROM
        rtu_failure_check_order o
        LEFT JOIN rtu_info i ON i.rtu_code = o.rtu_code and i.is_deleted=0
        LEFT JOIN data_warning_info w ON w.rtu_code = o.rtu_code and w.is_deleted = 0 and w.warning_kind=1
        LEFT JOIN data_ad_cd_b a ON a.adcd = i.ad_code and a.is_deleted=0
        LEFT JOIN blade_user u ON u.id = o.update_user and u.is_deleted=0
        LEFT JOIN blade_user u2 ON u2.id = o.create_user and u2.is_deleted=0
        LEFT JOIN blade_dict_biz b ON b.id = o.order_status_key and b.is_deleted=0
        WHERE
        o.is_deleted = 0
        <if test="checkOrderInfoDTO.rtuCode!=null">
            and o.rtu_code like concat(concat('%', #{checkOrderInfoDTO.rtuCode}), '%')
        </if>
        <if test="checkOrderInfoDTO.rtuName!=null">
            and i.rtu_name like concat(concat('%', #{checkOrderInfoDTO.rtuName}), '%')
        </if>
        <if test="checkOrderInfoDTO.contactUser!=null">
            and o.contact_user = #{checkOrderInfoDTO.contactUser}
        </if>
        <if test="checkOrderInfoDTO.orderConfirm!=null">
            and o.order_confirm = #{checkOrderInfoDTO.orderConfirm}
        </if>
        <if test="checkOrderInfoDTO.orderClose!=null">
            and o.order_close = #{checkOrderInfoDTO.orderClose}
        </if>
        <if test="checkOrderInfoDTO.orderStatusKey!=null">
            and o.order_status_key = #{checkOrderInfoDTO.orderStatusKey}
        </if>
        <if test="checkOrderInfoDTO.adCode!=null">
            and (a.adcd =#{checkOrderInfoDTO.adCode} or a.ancestors in (SELECT ancestors FROM data_ad_cd_b WHERE
            ancestors like concat(concat('%', #{checkOrderInfoDTO.adCode}), '%')))
        </if>
        order by b.dict_key,o.update_time desc
    </select>

    <select id="selectWarnPage" resultMap="checkOrderInfoVOResultMap">
        SELECT
        o.*,u.real_name,i.rtu_name,i.is_rain,i.is_river,i.is_res,i.is_ground,i.is_video,i.lng,i.lat,i.location_desc,i.ad_city,i.ad_dist,u2.real_name AS contact_user_name,a.adcd,a.ancestors
        FROM
        rtu_failure_check_order o
        LEFT JOIN rtu_info i ON i.rtu_code = o.rtu_code and i.is_deleted=0
        LEFT JOIN data_warning_info w ON w.rtu_code = o.rtu_code and w.is_deleted = 0
        LEFT JOIN data_ad_cd_b a ON a.adcd = i.ad_code and a.is_deleted=0
        LEFT JOIN blade_user u ON u.id = o.update_user and u.is_deleted=0
        LEFT JOIN blade_user u2 ON u2.id = o.contact_user and u2.is_deleted=0
        WHERE
        o.is_deleted = 0
        and w.warning_kind = #{checkOrderInfoDTO.warnKind}
        <if test="checkOrderInfoDTO.rtuCode!=null">
            and o.rtu_code like concat(concat('%', #{checkOrderInfoDTO.rtuCode}), '%')
        </if>
        <if test="checkOrderInfoDTO.rtuName!=null">
            and i.rtu_name like concat(concat('%', #{checkOrderInfoDTO.rtuName}), '%')
        </if>
        <if test="checkOrderInfoDTO.contactUser!=null">
            and o.contact_user = #{checkOrderInfoDTO.contactUser}
        </if>
        <if test="checkOrderInfoDTO.orderConfirm!=null">
            and o.order_confirm = #{checkOrderInfoDTO.orderConfirm}
        </if>
        <if test="checkOrderInfoDTO.orderClose!=null">
            and o.order_close = #{checkOrderInfoDTO.orderClose}
        </if>
        <if test="checkOrderInfoDTO.adCode!=null">
            and (a.adcd =#{checkOrderInfoDTO.adCode} or a.ancestors in (SELECT ancestors FROM data_ad_cd_b WHERE
            ancestors like concat(concat('%', #{checkOrderInfoDTO.adCode}), '%')))
        </if>
        order by o.order_confirm ,o.order_close,o.create_time
    </select>

    <select id="selectMyPage" resultMap="checkOrderInfoVOResultMap">
        SELECT
        o.*,u.real_name,i.rtu_name,i.lng,i.lat,i.location_desc,i.ad_city,i.ad_dist,u2.real_name AS contact_user_name,a.adcd,a.ancestors
        FROM
        rtu_failure_check_order o
        LEFT JOIN rtu_info i ON i.rtu_code = o.rtu_code and i.is_deleted=0
        LEFT JOIN data_ad_cd_b a ON a.adcd = i.ad_code and a.is_deleted=0
        LEFT JOIN blade_user u ON u.id = o.update_user and u.is_deleted=0
        LEFT JOIN blade_user u2 ON u2.id = o.contact_user and u2.is_deleted=0
        WHERE
        o.is_deleted = 0
        <if test="checkOrderInfoDTO.rtuCode!=null">
            and o.rtu_code like concat(concat('%', #{checkOrderInfoDTO.rtuCode}), '%')
        </if>
        <if test="checkOrderInfoDTO.rtuName!=null">
            and i.rtu_name like concat(concat('%', #{checkOrderInfoDTO.rtuName}), '%')
        </if>
        <if test="checkOrderInfoDTO.contactUser!=null">
            and o.contact_user = #{checkOrderInfoDTO.contactUser}
        </if>
        <if test="checkOrderInfoDTO.orderConfirm!=null">
            and o.order_confirm = #{checkOrderInfoDTO.orderConfirm}
        </if>
        <if test="checkOrderInfoDTO.orderClose!=null">
            and o.order_close = #{checkOrderInfoDTO.orderClose}
        </if>
        <if test="checkOrderInfoDTO.adCode!=null">
            and (a.adcd =#{checkOrderInfoDTO.adCode} or a.ancestors in (SELECT ancestors FROM data_ad_cd_b WHERE
            ancestors like concat(concat('%', #{checkOrderInfoDTO.adCode}), '%')))
        </if>
        order by o.order_confirm_time  desc
    </select>

    <select id="selectTodoList" resultMap="checkOrderInfoVOResultMap">
        SELECT
        o.*,u.real_name create_order_persion_name,i.rtu_name,i.lng,i.lat,i.location_desc,i.ad_city,i.ad_dist
        FROM
        rtu_failure_check_order o
        LEFT JOIN rtu_info i ON i.rtu_code = o.rtu_code and i.is_deleted=0
        LEFT JOIN blade_user u ON u.id = o.update_user and u.is_deleted=0
        WHERE
        o.is_deleted = 0
        <if test="checkOrderInfoDTO.rtuCode!=null">
            and o.rtu_code like concat(concat('%', #{checkOrderInfoDTO.rtuCode}), '%')
        </if>
        <if test="checkOrderInfoDTO.rtuName!=null">
            and i.rtu_name like concat(concat('%', #{checkOrderInfoDTO.rtuName}), '%')
        </if>
        <if test="checkOrderInfoDTO.contactUser!=null">
            and o.contact_user = #{checkOrderInfoDTO.contactUser}
        </if>
        <if test="checkOrderInfoDTO.orderStatusKey!=null">
            and o.order_status_key = #{checkOrderInfoDTO.orderStatusKey}
        </if>

        order by o.create_time  desc
    </select>


    <select id="selectConfirmDelayPage" resultMap="checkOrderInfoVOResultMap">
        SELECT
        o.*,u.real_name,i.rtu_name,i.lng,i.lat,i.location_desc,i.ad_city,i.ad_dist,u2.real_name AS contact_user_name,a.adcd,a.ancestors
        FROM
        rtu_failure_check_order o
        LEFT JOIN rtu_info i ON i.rtu_code = o.rtu_code and i.is_deleted=0
        LEFT JOIN data_ad_cd_b a ON a.adcd = i.ad_code and a.is_deleted=0
        LEFT JOIN blade_user u ON u.id = o.update_user and u.is_deleted=0
        LEFT JOIN blade_user u2 ON u2.id = o.contact_user and u2.is_deleted=0
        WHERE
        o.is_deleted = 0
        and o.order_confirm = 0
        and (now() > date_add(o.create_time ,interval #{checkOrderInfoDTO.orderComfireDelayTime} minute))
        <if test="checkOrderInfoDTO.rtuCode!=null">
            and o.rtu_code like concat(concat('%', #{checkOrderInfoDTO.rtuCode}), '%')
        </if>
        <if test="checkOrderInfoDTO.rtuName!=null">
            and i.rtu_name like concat(concat('%', #{checkOrderInfoDTO.rtuName}), '%')
        </if>
        <if test="checkOrderInfoDTO.adCode!=null">
            and (a.adcd =#{checkOrderInfoDTO.adCode} or a.ancestors in (SELECT ancestors FROM data_ad_cd_b WHERE
            ancestors like concat(concat('%', #{checkOrderInfoDTO.adCode}), '%')))
        </if>
        order by o.create_time
    </select>

    <select id="orderCount" resultType="java.lang.Long">
        SELECT
        count(*)
        FROM
        rtu_failure_check_order o
        LEFT JOIN rtu_info i ON o.rtu_code = i.rtu_code and i.is_deleted=0
        LEFT JOIN data_ad_cd_b a ON a.adcd = i.ad_code and a.is_deleted=0
        WHERE
        o.is_deleted = 0
        <if test="checkOrderInfoDTO.orgId!=null">
            and o.org_id = #{checkOrderInfoDTO.orgId}
        </if>
        <if test="checkOrderInfoDTO.orderStatusKey!=null">
            and o.order_status_key = #{checkOrderInfoDTO.orderStatusKey}
        </if>
        <if test="checkOrderInfoDTO.orderClose!=null">
            and o.order_close = #{checkOrderInfoDTO.orderClose}
        </if>
        <if test="checkOrderInfoDTO.orderConfirm!=null">
            and o.order_confirm = #{checkOrderInfoDTO.orderConfirm}
        </if>
        <if test="checkOrderInfoDTO.contactUser!=null">
            and o.contact_user = #{checkOrderInfoDTO.contactUser}
        </if>
        <if test="checkOrderInfoDTO.adCode!=null">
            and (a.adcd =#{checkOrderInfoDTO.adCode} or a.ancestors in (SELECT ancestors FROM data_ad_cd_b WHERE
            ancestors like concat(concat('%', #{checkOrderInfoDTO.adCode}), '%')))
        </if>
    </select>

    <select id="unCloseOrderCount" resultType="java.lang.Long">
        SELECT
        count(*)
        FROM
        rtu_failure_check_order o
        LEFT JOIN rtu_info i ON i.rtu_code = o.rtu_code and i.is_deleted=0
        LEFT JOIN data_ad_cd_b a ON a.adcd = i.ad_code and a.is_deleted=0
        WHERE
        o.is_deleted = 0
        and o.order_close =0
        and o.order_confirm=1
        <if test="checkOrderInfoDTO.adCode!=null">
            and (a.adcd =#{checkOrderInfoDTO.adCode} or a.ancestors in (SELECT ancestors FROM data_ad_cd_b WHERE
            ancestors like concat(concat('%', #{checkOrderInfoDTO.adCode}), '%')))
        </if>
        order by o.create_time asc
    </select>

    <select id="unconfirmOrderList" resultMap="checkOrderInfoVOResultMap">
        SELECT
        o.*,u.real_name,i.rtu_name,i.lng,i.lat,i.location_desc,i.ad_city,i.ad_dist,u3.real_name create_order_persion_name
        FROM
        rtu_failure_check_order o
        LEFT JOIN rtu_info i ON i.rtu_code = o.rtu_code and i.is_deleted=0
        LEFT JOIN data_ad_cd_b a ON a.adcd = i.ad_code and a.is_deleted=0
        LEFT JOIN blade_user u ON u.id = o.update_user and u.is_deleted=0
        LEFT JOIN blade_user u3 ON u3.id = o.create_user and u3.is_deleted=0
        WHERE
        o.is_deleted = 0
        and o.order_confirm = 0
        <if test="checkOrderInfoDTO.rtuCode!=null">
            and o.rtu_code like concat(concat('%', #{checkOrderInfoDTO.rtuCode}), '%')
        </if>
        <if test="checkOrderInfoDTO.rtuName!=null">
            and i.rtu_name like concat(concat('%', #{checkOrderInfoDTO.rtuName}), '%')
        </if>
        <if test="checkOrderInfoDTO.adCode!=null">
            and (a.adcd =#{checkOrderInfoDTO.adCode} or a.ancestors in (SELECT ancestors FROM data_ad_cd_b WHERE
            ancestors like concat(concat('%', #{checkOrderInfoDTO.adCode}), '%')))
        </if>
        order by o.create_time asc limit 10
    </select>


    <select id="unconfirmOrderCount" resultType="java.lang.Long">
        SELECT
        count(*)
        FROM
        rtu_failure_check_order o
        LEFT JOIN rtu_info i ON i.rtu_code = o.rtu_code and i.is_deleted=0
        LEFT JOIN data_ad_cd_b a ON a.adcd = i.ad_code and a.is_deleted=0
        WHERE
        o.is_deleted = 0
        and o.order_confirm = 0
        <if test="checkOrderInfoDTO.adCode!=null">
            and (a.adcd =#{checkOrderInfoDTO.adCode} or a.ancestors in (SELECT ancestors FROM data_ad_cd_b WHERE
            ancestors like concat(concat('%', #{checkOrderInfoDTO.adCode}), '%')))
        </if>
    </select>

    <select id="orderReportCount" resultMap="checkOrderCountVOResultMap">
        SELECT u.real_name,u.id,o.order_count,o2.processing_order_count FROM blade_user u LEFT JOIN
        (SELECT order_confirm_user,count(id) as order_count FROM rtu_failure_check_order WHERE is_deleted=0 AND
        order_confirm_user is not null AND order_confirm =1 GROUP BY order_confirm_user) o
        on u.id = o.order_confirm_user
        LEFT JOIN
        (SELECT order_confirm_user,count(id) as processing_order_count FROM rtu_failure_check_order WHERE is_deleted=0
        AND
        order_confirm_user is not null AND order_confirm =1 AND order_close=0 GROUP BY order_confirm_user) o2
        on u.id = o2.order_confirm_user
        WHERE u.is_deleted =0
        <if test="checkOrderInfoDTO.orgId!=null">
            AND u.dept_id = #{checkOrderInfoDTO.orgId}
        </if>
    </select>


</mapper>
