<?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="com.ygtx.gxt.mapper.GxtOrderHourMapper">

    <resultMap type="com.ygtx.gxt.domain.OrderScoreInfo" id="OrderScoreInfoResult">
        <result property="orderType"    column="order_type"    />
        <result property="id"    column="id"    />
        <result property="workOrderProjectNo"    column="work_order_project_no"    />
        <result property="workOrderStatus"    column="work_order_status"    />
        <result property="gxtCenterId"    column="gxt_center_id"    />
        <result property="gxtCenter"    column="gxt_center"    />
        <result property="pcsStationId"    column="pcs_station_id"    />
        <result property="pcsStationPid"    column="pcs_station_pid"    />
        <result property="pcsStationName"    column="pcs_station_name"    />
        <result property="pcsDeviceId"    column="pcs_device_id"    />
        <result property="pcsDeviceName"    column="pcs_device_name"    />
        <result property="brand"    column="brand"    />
        <result property="model"    column="model"    />
        <result property="assignTime"    column="assign_time"    />
        <result property="assignUserId"    column="assign_user_id"    />
        <result property="assignUserName"    column="assign_user_name"    />
        <result property="acceptTime"    column="accept_time"    />
        <result property="acceptUserId"    column="accept_user_id"    />
        <result property="acceptUserName"    column="accept_user_name"    />
        <result property="realStartTime"    column="real_start_time"    />
        <result property="realEndTime"    column="real_end_time"    />
        <result property="teamLeaderId"    column="team_leader_id"    />
        <result property="teamLeaderName"    column="team_leader_name"    />
        <result property="workGroupMemberId"    column="work_group_member_id"    />
        <result property="workGroupMemberName"    column="work_group_member_name"    />
        <result property="score"    column="score"    />
        <result property="reviewContent"    column="review_content"    />
        <result property="suspendReason"    column="suspend_reason"    />
        <result property="restartTime"    column="restart_time"    />
        <result property="priorityType"    column="priority_type"    />
        <result property="workEndTime"    column="work_end_time"    />
        <result property="scoringStatus"    column="scoring_status"    />
        <result property="workSummary"    column="work_summary"    />
        <result property="maintenanceType"    column="maintenance_type"    />
        <result property="inspectionType"    column="inspection_type"    />
        <!-- 新增字段映射 -->
        <result property="planHour"    column="plan_hour"    />
        <result property="faultCode"    column="fault_code"    />
        <result property="faultDesc"    column="fault_desc"    />
        <result property="occurTime"    column="occur_time"    />
        <result property="suspendTime"    column="suspend_time"    />
        <result property="pauseReason"    column="pause_reason"    />
        <result property="pauseTime"    column="pause_time"    />
        <result property="completeTime"    column="complete_time"    />
        <result property="status"    column="status"    />
        <result property="reviewScoreNum"    column="review_score_num"    />
        <result property="finalCoefficient"    column="final_coefficient"    />
        <result property="planStartTime" column="plan_start_time" />
        <result property="planEndTime" column="plan_end_time" />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <select id="selectUnionOrderList" resultMap="OrderScoreInfoResult">
        SELECT * FROM (
            SELECT
                1 as order_type,
                id as id,
                work_order_project_no as work_order_project_no,
                work_order_status as work_order_status,
                gxt_center_id as gxt_center_id,
                gxt_center as gxt_center,
                pcs_station_id as pcs_station_id,
                pcs_station_pid as pcs_station_pid,
                pcs_station_name as pcs_station_name,
                pcs_device_id as pcs_device_id,
                pcs_device_name as pcs_device_name,
                brand as brand,
                model as model,
                assign_time as assign_time,
                assign_user_id as assign_user_id,
                assign_user_name as assign_user_name,
                accept_time as accept_time,
                accept_user_id as accept_user_id,
                accept_user_name as accept_user_name,
                real_start_time as real_start_time,
                real_end_time as real_end_time,
                team_leader_id as team_leader_id,
                team_leader_name as team_leader_name,
                work_group_member_id as work_group_member_id,
                work_group_member_name as work_group_member_name,
                score as score,
                review_content as review_content,
                suspend_reason as suspend_reason,
                restart_time as restart_time,
                priority_type as priority_type,
                real_end_time as work_end_time,
                scoring_status as scoring_status,
                content as work_summary,
                maintenance_type as maintenance_type,
                null as inspection_type,
                null as plan_start_time,
                null as plan_end_time,
                plan_hour as plan_hour,
                fault_code as fault_code,
                fault_desc as fault_desc,
                occur_time as occur_time,
                null as suspend_time,
                null as pause_reason,
                null as pause_time,
                null as complete_time,
                review_score_num as review_score_num,
                final_coefficient as final_coefficient,
                create_time as create_time,
                update_time as update_time
            FROM gxt_repair_order t
            <where>
                <if test="repairOrder != null">
                    <if test="keyword != null and keyword != ''">
                        AND (work_order_project_no LIKE CONCAT('%', #{keyword}, '%') OR pcs_device_name LIKE CONCAT('%', #{keyword}, '%'))
                    </if>
                    <if test="repairOrder.workOrderProjectNo != null and repairOrder.workOrderProjectNo != ''"> and work_order_project_no = #{repairOrder.workOrderProjectNo}</if>
                    <if test="repairOrder.workOrderStatus != null and repairOrder.workOrderStatus != ''"> and work_order_status = #{repairOrder.workOrderStatus}</if>
                    <if test="repairOrder.gxtCenterId != null"> and gxt_center_id = #{repairOrder.gxtCenterId}</if>
                    <if test="repairOrder.gxtCenter != null and repairOrder.gxtCenter != ''"> and gxt_center = #{repairOrder.gxtCenter}</if>
                    <if test="repairOrder.pcsStationId != null"> and pcs_station_id = #{repairOrder.pcsStationId}</if>
                    <if test="repairOrder.pcsStationName != null and repairOrder.pcsStationName != ''"> and pcs_station_name = #{repairOrder.pcsStationName}</if>
                    <if test="repairOrder.pcsDeviceId != null"> and pcs_device_id = #{repairOrder.pcsDeviceId}</if>
                    <if test="repairOrder.pcsDeviceName != null and repairOrder.pcsDeviceName != ''"> and pcs_device_name like concat('%', #{repairOrder.pcsDeviceName}, '%')</if>
                    <if test="repairOrder.teamLeaderName != null and repairOrder.teamLeaderName != ''"> and team_leader_name like concat('%', #{repairOrder.teamLeaderName}, '%')</if>
                    <if test="repairOrder.scoringStatus != null"> and scoring_status = #{repairOrder.scoringStatus}</if>
                    <if test="repairOrder.params.beginOccurTime != null"> AND occur_time &gt;= #{repairOrder.params.beginOccurTime}</if>
                    <if test="repairOrder.params.endOccurTime != null"> AND occur_time &lt;= #{repairOrder.params.endOccurTime}</if>

                    <if test="repairOrder.params.beginCompleteTime != null"> AND t.id IN (SELECT DISTINCT order_id FROM gxt_repair_order_flow WHERE action_type IN ('complete','auto_finalize') AND action_time &gt;= #{repairOrder.params.beginCompleteTime})</if>
                    <if test="repairOrder.params.endCompleteTime != null"> AND t.id IN (SELECT DISTINCT order_id FROM gxt_repair_order_flow WHERE action_type IN ('complete','auto_finalize') AND action_time &lt;= #{repairOrder.params.endCompleteTime})</if>
                    <!-- 时间范围查询条件 - 使用结束时间 -->
                    <if test="repairOrder.params.beginRealEndTime != null"> AND real_end_time &gt;= #{repairOrder.params.beginRealEndTime}</if>
                    <if test="repairOrder.params.endRealEndTime != null"> AND real_end_time &lt;= #{repairOrder.params.endRealEndTime}</if>
                    <!-- 业务特定数据权限过滤条件 -->
                    <if test="repairOrder != null and repairOrder.params.businessDataScope != null and repairOrder.params.businessDataScope != ''">
                        ${repairOrder.params.businessDataScope}
                    </if>
                </if>
                and NULLIF(scoring_status, '') IS NOT NULL and work_order_status IN ('completed','archived')
            </where>

            UNION ALL

            SELECT
                2 as order_type,
                id as id,
                work_order_project_no as work_order_project_no,
                work_order_status as work_order_status,
                gxt_center_id as gxt_center_id,
                gxt_center as gxt_center,
                pcs_station_id as pcs_station_id,
                pcs_station_pid as pcs_station_pid,
                pcs_station_name as pcs_station_name,
                pcs_device_id as pcs_device_id,
                pcs_device_name as pcs_device_name,
                brand as brand,
                model as model,
                assign_time as assign_time,
                assign_user_id as assign_user_id,
                assign_user_name as assign_user_name,
                accept_time as accept_time,
                accept_user_id as accept_user_id,
                accept_user_name as accept_user_name,
                real_start_time as real_start_time,
                real_end_time as real_end_time,
                team_leader_id as team_leader_id,
                team_leader_name as team_leader_name,
                work_group_member_id as work_group_member_id,
                work_group_member_name as work_group_member_name,
                score as score,
                review_content as review_content,
                suspend_reason as suspend_reason,
                restart_time as restart_time,
                priority_type as priority_type,
                real_end_time as work_end_time,
                scoring_status as scoring_status,
                real_content as work_summary,
                null as maintenance_type,
                inspection_type as inspection_type,
                plan_start_time as plan_start_time,
                plan_end_time as plan_end_time,
                plan_hour as plan_hour,
                fault_code as fault_code,
                fault_desc as fault_desc,
                null as occur_time,
                suspend_time as suspend_time,
                pause_reason as pause_reason,
                pause_time as pause_time,
                complete_time as complete_time,
                review_score_num as review_score_num,
                final_coefficient as final_coefficient,
                create_time as create_time,
                update_time as update_time
            FROM gxt_work_order t
            <where>
                <if test="workOrder != null">
                    <if test="keyword != null and keyword != ''">
                        AND (work_order_project_no LIKE CONCAT('%', #{keyword}, '%') OR pcs_device_name LIKE CONCAT('%', #{keyword}, '%'))
                    </if>
                    <if test="workOrder.workOrderProjectNo != null and workOrder.workOrderProjectNo != ''"> and work_order_project_no = #{workOrder.workOrderProjectNo}</if>
                    <if test="workOrder.workOrderStatus != null and workOrder.workOrderStatus != ''"> and work_order_status = #{workOrder.workOrderStatus}</if>
                    <if test="workOrder.gxtCenterId != null"> and gxt_center_id = #{workOrder.gxtCenterId}</if>
                    <if test="workOrder.gxtCenter != null and workOrder.gxtCenter != ''"> and gxt_center = #{workOrder.gxtCenter}</if>
                    <if test="workOrder.pcsStationId != null"> and pcs_station_id = #{workOrder.pcsStationId}</if>
                    <if test="workOrder.pcsStationName != null and workOrder.pcsStationName != ''"> and pcs_station_name = #{workOrder.pcsStationName}</if>
                    <if test="workOrder.pcsDeviceId != null"> and pcs_device_id = #{workOrder.pcsDeviceId}</if>
                    <if test="workOrder.pcsDeviceName != null and workOrder.pcsDeviceName != ''"> and pcs_device_name like concat('%', #{workOrder.pcsDeviceName}, '%')</if>
                    <if test="workOrder.teamLeaderName != null and workOrder.teamLeaderName != ''"> and team_leader_name like concat('%', #{workOrder.teamLeaderName}, '%')</if>
                    <if test="workOrder.scoringStatus != null"> and scoring_status = #{workOrder.scoringStatus}</if>
                    <if test="workOrder.params.beginCreateTime != null"> AND create_time &gt;= #{workOrder.params.beginCreateTime}</if>
                    <if test="workOrder.params.endCreateTime != null"> AND create_time &lt;= #{workOrder.params.endCreateTime}</if>

                    <if test="workOrder.params.beginCompleteTime != null"> AND t.id IN (SELECT DISTINCT order_id FROM gxt_work_order_flow WHERE action_type IN ('complete','auto_finalize')  AND action_time &gt;= #{workOrder.params.beginCompleteTime})</if>
                    <if test="workOrder.params.endCompleteTime != null"> AND t.id IN (SELECT DISTINCT order_id FROM gxt_work_order_flow WHERE action_type IN ('complete','auto_finalize') AND action_time &lt;= #{workOrder.params.endCompleteTime})</if>
                    <!-- 时间范围查询条件 - 使用结束时间 -->
                    <if test="workOrder.params.beginRealEndTime != null"> AND real_end_time &gt;= #{workOrder.params.beginRealEndTime}</if>
                    <if test="workOrder.params.endRealEndTime != null"> AND real_end_time &lt;= #{workOrder.params.endRealEndTime}</if>
                    <!-- 业务特定数据权限过滤条件 -->
                    <if test="workOrder != null and workOrder.params.businessDataScope != null and workOrder.params.businessDataScope != ''">
                        ${workOrder.params.businessDataScope}
                    </if>
                </if>
                and NULLIF(scoring_status, '') IS NOT NULL and work_order_status IN ('completed','archived')
            </where>
        ) t
        <where>
            <!-- 根据工单类型筛选 -->
            <if test="orderType != null and orderType != ''">
                and order_type = #{orderType}
            </if>
        </where>
        ORDER BY create_time DESC
    </select>
    
    <!-- 查询维修工单挂起时长记录 -->
    <select id="selectRepairOrderSuspensionTime" resultType="int">
        SELECT 
            COALESCE(total_before_start_suspend_seconds, 0) as beforeStartSeconds,
            COALESCE(total_in_work_suspend_seconds, 0) as inWorkSeconds,
            COALESCE(total_night_auto_suspend_seconds, 0) as nightAutoSeconds
        FROM gxt_repair_order_flow_records_next 
        WHERE order_id = #{orderId}
    </select>
    
    <!-- 查询维保工单挂起时长记录 -->
    <select id="selectWorkOrderSuspensionTime" resultType="int">
        SELECT 
            COALESCE(total_before_start_suspend_seconds, 0) as beforeStartSeconds,
            COALESCE(total_in_work_suspend_seconds, 0) as inWorkSeconds,
            COALESCE(total_night_auto_suspend_seconds, 0) as nightAutoSeconds
        FROM gxt_work_order_flow_records_next 
        WHERE order_id = #{orderId}
    </select>

</mapper>