<?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.GxtOrderScoreMapper">

    <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"    />
        <result property="feedbackReason"    column="feedback_reason"    />
        <result property="confirmStatus"    column="confirm_status"    />
        <!-- 申诉理由 - 仅维修工单有此字段 -->
        <result property="appealReason"    column="appeal_reason"    />
        <!-- 评分退回理由 -->
        <result property="scoreReturnReason"    column="score_return_reason"    />
        <result property="orderEntryType"    column="order_entry_type"    />

        <result property="issueHour"    column="issueHour"    />
        <result property="acceptHour"    column="acceptHour"    />
        <result property="prepareHour"    column="prepareHour"    />
        <result property="suspendHour"    column="suspendHour"    />
        <result property="workHour"    column="workHour"    />
        <result property="restartHour"    column="restartHour"    />
        <result property="downtimeHour"    column="downtimeHour"    />
        <result property="wlryNum"    column="wlry_num"    />
        <result property="wwryNum"    column="wwry_num"    />
        <result property="workPermitNum"    column="work_permit_num"    />
        <result property="misOrderNo"    column="misOrderNo"    />
        <result property="faultBarcode"    column="faultBarcode"    />
        <result property="realFailureReason"    column="realFailureReason"    />
        <result property="lostPower"    column="lostPower"    />
        <result property="resetMethod"    column="resetMethod"    />
        <result property="extraWork"    column="extraWork"    />
        <result property="workArea"    column="workArea"    />
        <result property="itemCompletionFactorSum"    column="itemCompletionFactorSum"    />
        <result property="itemCompletionFactor"    column="itemCompletionFactor"    />
        <result property="selfRatingTime"    column="selfRatingTime"    />
        <result property="reviewRatingTime"    column="reviewRatingTime"    />
        <result property="finalizeMethod"    column="finalizeMethod"    />
        <result property="teamLeaderScore"    column="teamLeaderScore"    />
    </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,
                feedback_reason as feedback_reason,
                confirm_status as confirm_status,
                create_time as create_time,
                update_time as update_time,
                appeal_reason as appeal_reason,
                score_return_reason as score_return_reason
            FROM gxt_repair_order t
            <where>
                <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 like concat('%', #{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 repairOrder.scoringStatus != ''"> and scoring_status in
                    <foreach collection="repairOrder.scoringStatus.split(',')" item="status" open="(" close=")" separator=",">
                        #{status}
                    </foreach>
                </if>

                <if test="repairOrder.orderType != null and repairOrder.orderType != ''"> and 1 = #{repairOrder.orderType}</if>
                and NULLIF(scoring_status, '') IS NOT NULL AND work_order_status != 'invalid'
                <!-- 业务特定数据权限过滤条件 -->
                <if test="repairOrder.params.businessDataScope != null and repairOrder.params.businessDataScope != ''">
                    ${repairOrder.params.businessDataScope}
                </if>
            </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,
                feedback_reason as feedback_reason,
                confirm_status as confirm_status,
                create_time as create_time,
                update_time as update_time,
                null as appeal_reason,
                score_return_reason as score_return_reason
            FROM gxt_work_order t
            <where>
                <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 like concat('%', #{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 workOrder.scoringStatus != ''"> and scoring_status in
                    <foreach collection="workOrder.scoringStatus.split(',')" item="status" open="(" close=")" separator=",">
                        #{status}
                    </foreach>
                </if>
                <if test="workOrder.orderType != null and workOrder.orderType != ''"> and 2 = #{workOrder.orderType}</if>
                and NULLIF(scoring_status, '') IS NOT NULL AND work_order_status != 'invalid'
                <!-- 业务特定数据权限过滤条件 -->
                <if test="workOrder.params.businessDataScope != null and workOrder.params.businessDataScope != ''">
                    ${workOrder.params.businessDataScope}
                </if>
            </where>
        ) t
        ORDER BY COALESCE(update_time, create_time) DESC, create_time DESC
    </select>

    <select id="selectMobileUnionOrderList" resultMap="OrderScoreInfoResult">
        SELECT * FROM (
        SELECT
        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,
        feedback_reason as feedback_reason,
        confirm_status as confirm_status,
        create_time as create_time,
        update_time as update_time,
        appeal_reason as appeal_reason,
        null as order_entry_type
        FROM gxt_repair_order t
        <where>
            <if test="keyword != null and keyword != ''">
                and (work_order_project_no like concat('%', #{keyword}, '%') or pcs_device_name like concat('%', #{keyword}, '%') or mis_order_no = #{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 in
                <foreach collection="repairOrder.workOrderStatus.split(',')" item="status" open="(" close=")" separator=",">
                    #{status}
                </foreach>
            </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.orderType != null and repairOrder.orderType != ''"> and 1 = #{repairOrder.orderType}</if>
            <if test="repairOrder.workPermitNum != null and repairOrder.workPermitNum != ''"> and work_permit_num = #{repairOrder.workPermitNum}</if>
            <!-- 超时工单条件：accept_time为空，并且当前时间超过assign_time并且超过时间大于配置值 -->
            <if test="repairOrder.params.overdueCondition != null and repairOrder.params.overdueCondition == 'overdue'">
                and work_order_status = 'assigned'
                and update_time &lt; NOW()
                and TIMESTAMPDIFF(MINUTE, update_time, NOW()) > (
                SELECT config_value FROM sys_config WHERE config_key = 'gxt.order.timeout' LIMIT 1
                )
            </if>
            <!-- 业务特定数据权限过滤条件 -->
            <if test="repairOrder.params.businessDataScope != null and repairOrder.params.businessDataScope != ''">
                ${repairOrder.params.businessDataScope}
            </if>
            <!-- 限制待下发工单只能创建人本部门查看 -->
            <if test="repairOrder.params.currentUser != null and repairOrder.params.currentUser != ''">
                and ((t.work_order_status != 'to_issue' and t.work_order_status != 'accept_return') or t.create_by in
                    (select user_name from sys_user t1
                     left join sys_dept t2 on t1.dept_id = t2.dept_id
                     where t2.dept_id = (select dept_id from sys_user where user_name = #{repairOrder.params.currentUser,jdbcType=VARCHAR} and del_flag='0')))
            </if>
        </where>

        UNION ALL

        SELECT
        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,
        feedback_reason as feedback_reason,
        confirm_status as confirm_status,
        create_time as create_time,
        update_time as update_time,
        null as appeal_reason,
        order_entry_type
        FROM gxt_work_order t
        <where>
            <if test="keyword != null and keyword != ''">
                and (work_order_project_no like concat('%', #{keyword}, '%') or pcs_device_name like concat('%', #{keyword}, '%') or mis_no = #{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 in
                <foreach collection="workOrder.workOrderStatus.split(',')" item="status" open="(" close=")" separator=",">
                    #{status}
                </foreach>
            </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.orderType != null and workOrder.orderType != ''"> and 2 = #{workOrder.orderType}</if>
            <if test="workOrder.workPermitNum != null and workOrder.workPermitNum != ''"> and work_permit_num = #{workOrder.workPermitNum}</if>
            <!-- 超时工单条件：accept_time为空，并且当前时间超过assign_time并且超过时间大于配置值 -->
            <if test="workOrder.params.overdueCondition != null and workOrder.params.overdueCondition == 'overdue'">
                and work_order_status = 'assigned'
                and update_time &lt; NOW()
                and TIMESTAMPDIFF(MINUTE, update_time, NOW()) > (
                SELECT config_value FROM sys_config WHERE config_key = 'gxt.order.timeout' LIMIT 1
                )
            </if>
            <!-- 业务特定数据权限过滤条件 -->
            <if test="workOrder.params.businessDataScope != null and workOrder.params.businessDataScope != ''">
                ${workOrder.params.businessDataScope}
            </if>
            <!-- 限制待下发工单只能创建人本部门查看 -->
            <if test="workOrder.params.currentUser != null and workOrder.params.currentUser != ''">
                and ((t.work_order_status != 'to_issue' and t.work_order_status != 'accept_return') or t.create_by in
                (select user_name from sys_user t1
                left join sys_dept t2 on t1.dept_id = t2.dept_id
                where t2.dept_id = (select dept_id from sys_user where user_name = #{workOrder.params.currentUser,jdbcType=VARCHAR} and del_flag='0')))
            </if>
        </where>
        ) t
        ORDER BY COALESCE(update_time, create_time) DESC, create_time DESC
    </select>

    <select id="selectOrderScoreStatistics" resultType="map">
        SELECT 
            COALESCE(SUM(CASE WHEN order_type = 2 THEN scoreResult ELSE 0 END), 0) AS maintenanceScore,
            COALESCE(SUM(CASE WHEN order_type = 1 THEN scoreResult ELSE 0 END), 0) AS repairScore,
            COUNT(CASE WHEN order_type = 2 THEN 1 END) AS maintenanceCount,
            COUNT(CASE WHEN order_type = 1 THEN 1 END) AS repairCount
        FROM (
            SELECT
                2 as order_type,
                wrk.score as scoreResult
            FROM gxt_work_order t
            JOIN gxt_work_order_person wrk ON t.id = wrk.order_id 
                <if test="workOrder.params.currentUserId != null">
                    AND wrk.user_id = #{workOrder.params.currentUserId}
                </if>
            <where>
                t.scoring_status IS NOT NULL AND t.work_order_status != 'invalid'

                <if test="month != null and month != ''">
                    AND DATE_FORMAT(t.create_time, '%Y-%m') = #{month}
                </if>
                <!-- 业务特定数据权限过滤条件 -->
                <if test="workOrder.params.businessDataScope != null and workOrder.params.businessDataScope != ''">
                    ${workOrder.params.businessDataScope}
                </if>
                AND (wrk.score IS NOT NULL AND wrk.score != 0)
            </where>

            UNION ALL

            SELECT
                1 as order_type,
                rep.score as scoreResult
            FROM gxt_repair_order t
            JOIN gxt_repair_order_person rep ON t.id = rep.order_id
                <if test="repairOrder.params.currentUserId != null">
                    AND rep.user_id = #{repairOrder.params.currentUserId}
                </if>
            <where>
                t.scoring_status IS NOT NULL AND t.work_order_status != 'invalid'

                <if test="month != null and month != ''">
                    AND DATE_FORMAT(t.occur_time, '%Y-%m') = #{month}
                </if>
                <!-- 业务特定数据权限过滤条件 -->
                <if test="repairOrder.params.businessDataScope != null and repairOrder.params.businessDataScope != ''">
                    ${repairOrder.params.businessDataScope}
                </if>
                AND (rep.score IS NOT NULL AND rep.score != 0)
            </where>
        ) t
    </select>

    <select id="selectUnionExportOrderList" resultMap="OrderScoreInfoResult">
        SELECT * FROM (
        SELECT
        1 as order_type,
        work_order_project_no as work_order_project_no,
        (SELECT dict.dict_label FROM sys_dict_data dict
        WHERE dict.dict_type = 'gxt_work_order_status' AND dict.dict_value = t.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,
        t.score as score,
        (SELECT GROUP_CONCAT(CONCAT(p.nick_name, ':',
            CASE 
                WHEN p.score = ROUND(p.score, 0) THEN CAST(ROUND(p.score, 0) AS CHAR)
                ELSE TRIM(TRAILING '.0' FROM CAST(ROUND(p.score, 1) AS CHAR))
            END
        ) SEPARATOR ',')
         FROM gxt_repair_order_person p
         WHERE p.order_id = t.id AND p.is_leader = 0 AND p.score IS NOT NULL) as workGroupMemberScore,
        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,
        t.feedback_reason as feedback_reason,
        t.confirm_status as confirm_status,
        t.create_time as create_time,
        t.update_time as update_time,
        t.appeal_reason as appeal_reason,
        t.score_return_reason as score_return_reason,
        t.wwry_num as wwry_num,
        t.wlry_num as wlry_num,
        t.work_permit_num as work_permit_num,
        t.mis_order_no as misOrderNo,
        t.fault_barcode as faultBarcode,
        t.real_failure_reason as realFailureReason,
        t.lost_power as lostPower,
        (SELECT dict.dict_label FROM sys_dict_data dict
        WHERE dict.dict_type = 'gxt_reset_method' AND dict.dict_value = t.reset_method) as resetMethod,
        extra_work as extraWork,
        (SELECT GROUP_CONCAT(dict.dict_label SEPARATOR ',')
         FROM sys_dict_data dict 
         WHERE dict.dict_type = 'gxt_work_area' 
         AND FIND_IN_SET(dict.dict_value, t.work_area)) as workArea,
        null as itemCompletionFactorSum,
        null as itemCompletionFactor,
        f1.action_time as selfRatingTime,
        f2.action_time as reviewRatingTime,
        (SELECT dict.dict_label FROM sys_dict_data dict
        WHERE dict.dict_type = 'gxt_finalize_method' AND dict.dict_value = t.finalize_method) as finalizeMethod,
        p.score as teamLeaderScore,
        ROUND(TIMESTAMPDIFF( SECOND, t.occur_time, t.assign_time ) / 3600,2) AS issueHour,
        ROUND(( TIMESTAMPDIFF( SECOND, t.assign_time, t.accept_time ) - IFNULL(gq.total_night_auto_suspend_seconds, 0))/ 3600,2) AS acceptHour,
        ROUND((TIMESTAMPDIFF( SECOND, t.accept_time, t.real_start_time ) - IFNULL(gq.total_before_start_suspend_seconds, 0)) / 3600,2) AS prepareHour,
        ROUND(TIMESTAMPDIFF( SECOND, t.occur_time, t.restart_time ) / 3600,2) AS downtimeHour,
        ROUND(TIMESTAMPDIFF( SECOND, t.real_end_time, t.restart_time ) / 3600,2) AS restartHour,
        ROUND((TIMESTAMPDIFF( SECOND, t.real_start_time, t.real_end_time ) - IFNULL(gq.total_in_work_suspend_seconds, 0)) / 3600,2) AS workHour,
        ROUND(CASE WHEN gq.order_id IS NOT NULL THEN (gq.total_before_start_suspend_seconds + gq.total_in_work_suspend_seconds + gq.total_night_auto_suspend_seconds)/3600 ELSE 0 END,2) AS suspendHour
        FROM gxt_repair_order t
        LEFT JOIN gxt_repair_order_flow_records_next gq ON gq.order_id = t.id
        LEFT JOIN gxt_repair_order_person p ON p.order_id = t.id and p.is_leader = 1
        LEFT JOIN gxt_repair_order_flow f1 ON f1.order_id = t.id and f1.action_type = 'selfRating'
        LEFT JOIN gxt_repair_order_flow f2 ON f2.order_id = t.id and f2.action_type = 'reviewRating'

        <where>
            <if test="repairOrder.workOrderProjectNo != null and repairOrder.workOrderProjectNo != ''"> and work_order_project_no like concat('%', #{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 repairOrder.scoringStatus != ''"> and scoring_status in
                <foreach collection="repairOrder.scoringStatus.split(',')" item="status" open="(" close=")" separator=",">
                    #{status}
                </foreach>
            </if>
            <if test="repairOrder.orderType != null"> and order_type = #{repairOrder.orderType}</if>
            <if test="repairOrder.beginOccurTime != null and repairOrder.beginOccurTime != ''"> and date_format(occur_time,'%y%m%d') &gt;= date_format(#{repairOrder.beginOccurTime},'%y%m%d')</if>
            <if test="repairOrder.endOccurTime != null and repairOrder.endOccurTime != ''"> and date_format(occur_time,'%y%m%d') &lt;= date_format(#{repairOrder.endOccurTime},'%y%m%d')</if>
            <if test="repairOrder.repairMethod != null  and repairOrder.repairMethod != ''"> and repair_method = #{repairOrder.repairMethod}</if>
            <if test="repairOrder.faultCode != null  and repairOrder.faultCode != ''"> and fault_code like concat('%', #{repairOrder.faultCode}, '%')</if>

<!--            <if test="repairOrder.orderType != null and repairOrder.orderType != ''"> and 1 = #{repairOrder.orderType}</if>-->
            AND work_order_status != 'invalid'
            <!-- 业务特定数据权限过滤条件 -->
            <if test="repairOrder.params.businessDataScope != null and repairOrder.params.businessDataScope != ''">
                ${repairOrder.params.businessDataScope}
            </if>
        </where>

        UNION ALL

        SELECT
        2 as order_type,
        work_order_project_no as work_order_project_no,
        (SELECT dict.dict_label FROM sys_dict_data dict
        WHERE dict.dict_type = 'gxt_work_order_status' AND dict.dict_value = t.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,
        t.score as score,
        (SELECT GROUP_CONCAT(CONCAT(p.nick_name, ':',
            CASE 
                WHEN p.score = ROUND(p.score, 0) THEN CAST(ROUND(p.score, 0) AS CHAR)
                ELSE TRIM(TRAILING '.0' FROM CAST(ROUND(p.score, 1) AS CHAR))
            END
        ) SEPARATOR ',')
         FROM gxt_work_order_person p
         WHERE p.order_id = t.id AND p.is_leader = 0 AND p.score IS NOT NULL) as workGroupMemberScore,
        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,
        (SELECT dict.dict_label FROM sys_dict_data dict
        WHERE dict.dict_type = 'gxt_inspection_type' AND dict.dict_value = t.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,
        t.feedback_reason as feedback_reason,
        t.confirm_status as confirm_status,
        t.create_time as create_time,
        t.update_time as update_time,
        null as appeal_reason,
        t.score_return_reason as score_return_reason,
        t.wwry_num as wwry_num,
        t.wlry_num as wlry_num,
        t.work_permit_num as work_permit_num,
        t.mis_no as misOrderNo,
        null as faultBarcode,
        null as realFailureReason,
        lost_power as lostPower,
        null as resetMethod,
        null as extraWork,
        null as workArea,
        item_completion_factor_sum as itemCompletionFactorSum,
        item_completion_factor as itemCompletionFactor,
        f1.action_time as selfRatingTime,
        f2.action_time as reviewRatingTime,
        (SELECT dict.dict_label FROM sys_dict_data dict
        WHERE dict.dict_type = 'gxt_finalize_method' AND dict.dict_value = t.finalize_method) as finalizeMethod,
        p.score as teamLeaderScore,
        ROUND(TIMESTAMPDIFF( SECOND, t.create_time, t.assign_time ) / 3600,2) AS issueHour,
        ROUND(TIMESTAMPDIFF( SECOND, t.assign_time, t.accept_time ) / 3600,2) AS acceptHour,
        ROUND((TIMESTAMPDIFF( SECOND, t.accept_time, t.real_start_time ) - IFNULL(gq.total_before_start_suspend_seconds, 0)) / 3600,2) AS prepareHour,
        ROUND(TIMESTAMPDIFF( SECOND, COALESCE ( t.pause_time, t.real_start_time ), t.restart_time ) / 3600,2) AS downtimeHour,
        ROUND(TIMESTAMPDIFF( SECOND, t.real_end_time, t.restart_time ) / 3600,2) AS restartHour,
        ROUND(( TIMESTAMPDIFF( SECOND, t.real_start_time, t.real_end_time ) - IFNULL(gq.total_in_work_suspend_seconds, 0)) / 3600,2) AS workHour,
        ROUND(CASE WHEN gq.order_id IS NOT NULL THEN (gq.total_before_start_suspend_seconds + gq.total_in_work_suspend_seconds + gq.total_night_auto_suspend_seconds)/3600 ELSE 0 END,2) AS suspendHour
        FROM gxt_work_order t
        LEFT JOIN gxt_work_order_flow_records_next gq ON gq.order_id = t.id
        LEFT JOIN gxt_work_order_person p ON p.order_id = t.id and p.is_leader = 1
        LEFT JOIN gxt_work_order_flow f1 ON f1.order_id = t.id and f1.action_type = 'selfRating'
        LEFT JOIN gxt_work_order_flow f2 ON f2.order_id = t.id and f2.action_type = 'reviewRating'
        <where>
            <if test="workOrder.workOrderProjectNo != null and workOrder.workOrderProjectNo != ''"> and work_order_project_no like concat('%', #{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 workOrder.scoringStatus != ''"> and scoring_status in
                <foreach collection="workOrder.scoringStatus.split(',')" item="status" open="(" close=")" separator=",">
                    #{status}
                </foreach>
            </if>
            <if test="workOrder.orderType != null"> and order_type = #{workOrder.orderType}</if>
            <if test="workOrder.beginCreateTime != null and workOrder.beginCreateTime != ''"> and date_format(t.create_time,'%y%m%d') &gt;= date_format(#{workOrder.beginCreateTime},'%y%m%d')</if>
            <if test="workOrder.endCreateTime != null and workOrder.endCreateTime != ''"> and date_format(t.create_time,'%y%m%d') &lt;= date_format(#{workOrder.endCreateTime},'%y%m%d')</if>
<!--            <if test="workOrder.orderType != null and workOrder.orderType != ''"> and 2 = #{workOrder.orderType}</if>-->
            AND work_order_status != 'invalid'
            <!-- 业务特定数据权限过滤条件 -->
            <if test="workOrder.params.businessDataScope != null and workOrder.params.businessDataScope != ''">
                ${workOrder.params.businessDataScope}
            </if>
        </where>
        ) t
        ORDER BY COALESCE(update_time, create_time) DESC, create_time DESC
    </select>
</mapper>
