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

    <resultMap type="GxtWorkOrder" id="GxtWorkOrderResult">
        <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="inspectionType"    column="inspection_type"    />
        <result property="misNo"    column="mis_no"    />
        <result property="planStartTime"    column="plan_start_time"    />
        <result property="planEndTime"    column="plan_end_time"    />
        <result property="orderSource"    column="order_source"    />
        <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="suspendReason"    column="suspend_reason"    />
        <result property="suspendTime"    column="suspend_time"    />
        <result property="statusBeforeSuspend"    column="status_before_suspend"    />
        <result property="rejectionReason"    column="rejection_reason"    />
        <result property="modifyReason"    column="modify_reason"    />
        <result property="pauseReason"    column="pause_reason"    />
        <result property="pauseTime"    column="pause_time"    />
        <result property="restartTime"    column="restart_time"    />
        <result property="completeTime"    column="complete_time"    />
        <result property="planHour"    column="plan_hour"    />
        <result property="content"    column="content"    />
        <result property="realContent"    column="real_content"    />
        <result property="faultCode"    column="fault_code"    />
        <result property="faultDesc"    column="fault_desc"    />
        <result property="orderType"    column="order_type"    />
        <result property="orderEntryType"    column="order_entry_type"    />
        <result property="priorityType"    column="priority_type"    />
        <result property="score"    column="score"    />
        <result property="reviewContent"    column="review_content"    />
        <result property="issuerName"    column="issuer_name"    />
        <result property="permitterName"    column="permitter_name"    />
        <result property="scoringStatus"    column="scoring_status"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="attachmentUrls"    column="attachment_urls"    />
        <result property="itemCompletionFactor"    column="item_completion_factor"    />
        <result property="itemCompletionFactorSum"    column="item_completion_factor_sum"    />
        <result property="reviewScoreNum"    column="review_score_num"    />
        <result property="finalCoefficient"    column="final_coefficient"    />
        <result property="isDeferred"    column="is_deferred"    />
        <result property="invalidReason"    column="invalid_reason"    />
        <result property="feedbackReason"    column="feedback_reason"    />
        <result property="confirmStatus"    column="confirm_status"    />
        <result property="wwryNum"    column="wwry_num"    />
        <result property="wlryNum"    column="wlry_num"    />
        <result property="infoEntry"    column="info_entry"    />
        <result property="lostPower"    column="lost_power"    />
        <result property="workPermitNum"    column="work_permit_num"    />
        <result property="finalizeMethod"    column="finalize_method"    />
        <result property="appealUserId"    column="appeal_user_id"    />
        <result property="appealUserName"    column="appeal_user_name"    />
        <result property="appealTime"    column="appeal_time"    />
        <result property="appealReason"    column="appeal_reason"    />
        <result property="suspendExplain"    column="suspend_explain"    />
        <result property="scoreReturnReason"    column="score_return_reason"    />
        <result property="leader"    column="leader"    />
        <result property="runningDays"    column="running_days" />
        <result property="stopDays"    column="stop_days" />
        <result property="deptName"    column="dept_name" />
    </resultMap>

    <resultMap type="GxtOrderData" id="GxtOrderDataResult">
        <result property="startTime"    column="startTime" />
        <result property="endTime"    column="endTime" />
        <result property="code"    column="code" />
        <result property="brand"    column="brand" />
        <result property="model"    column="model" />
        <result property="system"    column="system" />
        <result property="pcs"    column="pcs" />
        <result property="center"    column="center" />
        <result property="orderType"    column="orderType" />
        <result property="repairPerson"    column="repairPerson" />
        <result property="chargePerson"    column="chargePerson" />
        <result property="score"    column="score" />
        <result property="xfsc"    column="xfsc" />
        <result property="xysc"    column="xysc" />
        <result property="zbsc"    column="zbsc" />
        <result property="tjsc"    column="tjsc" />
        <result property="qjbwsc"    column="qjbwsc" />
        <result property="clsc"    column="clsc" />
        <result property="gqsc"    column="gqsc" />
        <result property="workOrderNum"    column="workOrderNum" />
        <result property="repairOrderNum"    column="repairOrderNum" />
        <result property="orderCode"    column="order_code" />
        <result property="restartNum"    column="restart_num" />
        <result property="devices"    column="devices" />
    </resultMap>

    <sql id="selectGxtWorkOrderVo">
        select id, work_order_project_no, work_order_status, gxt_center_id, gxt_center, pcs_station_id, pcs_station_pid, pcs_station_name,
               pcs_device_id, pcs_device_name, e.brand, e.model, inspection_type, mis_no, plan_start_time, plan_end_time, order_source, assign_time,
               assign_user_id, assign_user_name, accept_time, accept_user_id, accept_user_name, real_start_time, real_end_time,
               team_leader_id, team_leader_name, work_group_member_id, work_group_member_name, suspend_reason,
               suspend_time,status_before_suspend, modify_reason, pause_reason, pause_time, restart_time, complete_time, plan_hour,
               content, real_content, fault_code, fault_desc, order_type, priority_type, score, review_content, issuer_name,
               permitter_name, scoring_status, t.create_by, t.create_time, t.update_by, t.update_time, t.remark, t.attachment_urls,
               item_completion_factor, item_completion_factor_sum, review_score_num, final_coefficient, is_deferred, invalid_reason, feedback_reason, confirm_status,
               wwry_num, wlry_num, info_entry, lost_power, work_permit_num, finalize_method, appeal_user_id, appeal_user_name, appeal_time, appeal_reason, suspend_explain, order_entry_type, score_return_reason
        from gxt_work_order t
         left join sys_user u on u.user_name = t.create_by
         left join sys_dept d on u.dept_id = d.dept_id
         left join gxt_equipment e on t.pcs_device_name = e.equipment_code and t.pcs_station_name = e.station
    </sql>

    <select id="selectGxtWorkOrderList" parameterType="GxtWorkOrder" resultMap="GxtWorkOrderResult">
        <include refid="selectGxtWorkOrderVo"/>
        <where>
            <if test="pcsDeviceName != null  and pcsDeviceName != ''"> and pcs_device_name like concat('%', #{pcsDeviceName}, '%')</if>
            <if test="workOrderProjectNo != null  and workOrderProjectNo != ''"> and work_order_project_no like concat('%', #{workOrderProjectNo}, '%')</if>
            <if test="workOrderStatus != null  and workOrderStatus != ''"> and work_order_status = #{workOrderStatus}</if>
            <if test="gxtCenter != null  and gxtCenter != ''"> and gxt_center = #{gxtCenter}</if>
            <if test="pcsStationName != null  and pcsStationName != ''"> and pcs_station_name = #{pcsStationName}</if>
            <if test="orderSource != null  and orderSource != ''"> and order_source = #{orderSource}</if>
            <if test="teamLeaderId != null"> and team_leader_id = #{teamLeaderId}</if>
            <if test="workGroupMemberId != null"> and work_group_member_id = #{workGroupMemberId}</if>
            <if test="scoringStatus != null"> and scoring_status = #{scoringStatus}</if>
            <if test="itemCompletionFactor != null"> and item_completion_factor = #{itemCompletionFactor}</if>
            <if test="itemCompletionFactorSum != null"> and item_completion_factor_sum = #{itemCompletionFactorSum}</if>
            <if test="reviewScoreNum != null"> and review_score_num = #{reviewScoreNum}</if>
            <if test="finalCoefficient != null"> and final_coefficient = #{finalCoefficient}</if>
            <if test="misNo != null"> and mis_no = #{misNo}</if>
            <!-- 超时工单条件：realEndTime为空且当前时间超过planEndTime -->
            <if test="params.overdueCondition != null and params.overdueCondition == 'overdue'">
                and real_end_time is null and plan_end_time &lt; NOW()
            </if>
            <!-- 即将超时工单条件：根据配置判断 -->
            <if test="params.overdueCondition != null and params.overdueCondition == 'almostOverdue'">
                and real_end_time is null and plan_end_time &gt; NOW() 
                and TIMESTAMPDIFF(HOUR, NOW(), plan_end_time) &lt;= (
                    SELECT config_value FROM sys_config WHERE config_key = 'gxt.order.timeout' LIMIT 1
                )
            </if>
            <if test="params.keyword != null and params.keyword != ''">
                and (work_order_project_no like concat('%', #{params.keyword}, '%') or pcs_device_name like concat('%', #{params.keyword}, '%'))
            </if>
            <if test="wwryNum != null"> and wwry_num = #{wwryNum}</if>
            <if test="wlryNum != null"> and wlry_num = #{wlryNum}</if>
            <if test="infoEntry != null and infoEntry != ''"> and info_entry = #{infoEntry}</if>
            <if test="lostPower != null"> and lost_power = #{lostPower}</if>
            <if test="workPermitNum != null and workPermitNum != ''"> and work_permit_num = #{workPermitNum}</if>
            <!-- 业务特定数据权限过滤条件 -->
            <if test="params.businessDataScope != null and params.businessDataScope != ''">
                ${params.businessDataScope}
            </if>
            <!-- 限制待下发工单只能创建人本部门查看 -->
            <if test="params.currentUser != null and 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 = #{params.currentUser,jdbcType=VARCHAR} and del_flag='0')))
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="selectGxtWorkOrderListByMonth" parameterType="GxtWorkOrder" resultMap="GxtWorkOrderResult">
        SELECT
        t.*,
        p.nick_names AS leader
        FROM
        gxt_work_order t
        LEFT JOIN (
        SELECT
        order_id,
        GROUP_CONCAT(nick_name) AS nick_names
        FROM
        gxt_work_order_person
        WHERE
        is_leader = 1
        GROUP BY
        order_id
        ) p ON p.order_id = t.id
        <where>
            <if test="workOrderStatus != null  and workOrderStatus != ''"> and work_order_status = #{workOrderStatus}</if>
            <if test="model != null and model != ''"> and t.model = #{model}</if>
            <if test="inspectionType != null and inspectionType != ''"> and FIND_IN_SET(#{inspectionType}, inspection_type) > 0</if>
            <if test="pcsStationPid != null"> and pcs_station_pid = #{pcsStationPid}</if>
            <if test="monthPeriod != null"> and DATE_FORMAT(t.create_time, '%Y-%m') = #{monthPeriod}</if>
            <if test="params.businessDataScope != null and params.businessDataScope != ''">
                ${params.businessDataScope}
            </if>
        </where>
        order by t.create_time desc
    </select>

    <select id="selectGxtWorkOrderById" parameterType="Long" resultMap="GxtWorkOrderResult">
        <include refid="selectGxtWorkOrderVo"/>
        where id = #{id}
    </select>

    <select id="selectGxtWorkOrderListForAutoFinalize" parameterType="GxtWorkOrder" resultMap="GxtWorkOrderResult">
        <include refid="selectGxtWorkOrderVo"/>
        <where>
            <if test="workOrderStatus != null and workOrderStatus != ''"> and work_order_status in
                <foreach collection="workOrderStatus.split(',')" item="status" open="(" close=")" separator=",">
                    #{status}
                </foreach>
            </if>
            and accept_time is not null
            and pause_time is not null
            and restart_time is not null
        </where>
        order by create_time desc
    </select>

    <select id="selectGxtWorkOrderByCode" parameterType="String" resultMap="GxtWorkOrderResult">
        <include refid="selectGxtWorkOrderVo"/>
        where work_order_project_no = #{workOrderProjectNo}
        limit 1
    </select>

    <insert id="insertGxtWorkOrder" parameterType="GxtWorkOrder" useGeneratedKeys="true" keyProperty="id">
        insert into gxt_work_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="workOrderProjectNo != null and workOrderProjectNo != ''">work_order_project_no,</if>
            <if test="workOrderStatus != null and workOrderStatus != ''">work_order_status,</if>
            <if test="gxtCenterId != null">gxt_center_id,</if>
            <if test="gxtCenter != null">gxt_center,</if>
            <if test="pcsStationId != null">pcs_station_id,</if>
            <if test="pcsStationPid != null">pcs_station_pid,</if>
            <if test="pcsStationName != null">pcs_station_name,</if>
            <if test="pcsDeviceId != null">pcs_device_id,</if>
            <if test="pcsDeviceName != null">pcs_device_name,</if>
            <if test="brand != null">brand,</if>
            <if test="model != null">model,</if>
            <if test="inspectionType != null">inspection_type,</if>
            <if test="misNo != null">mis_no,</if>
            <if test="planStartTime != null">plan_start_time,</if>
            <if test="planEndTime != null">plan_end_time,</if>
            <if test="orderSource != null">order_source,</if>
            <if test="assignTime != null">assign_time,</if>
            <if test="assignUserId != null">assign_user_id,</if>
            <if test="assignUserName != null">assign_user_name,</if>
            <if test="acceptTime != null">accept_time,</if>
            <if test="acceptUserId != null">accept_user_id,</if>
            <if test="acceptUserName != null">accept_user_name,</if>
            <if test="realStartTime != null">real_start_time,</if>
            <if test="realEndTime != null">real_end_time,</if>
            <if test="teamLeaderId != null">team_leader_id,</if>
            <if test="teamLeaderName != null">team_leader_name,</if>
            <if test="workGroupMemberId != null">work_group_member_id,</if>
            <if test="workGroupMemberName != null">work_group_member_name,</if>
            <if test="suspendReason != null">suspend_reason,</if>
            <if test="suspendTime != null">suspend_time,</if>
            <if test="statusBeforeSuspend != null">status_before_suspend,</if>
            <if test="modifyReason != null">modify_reason,</if>
            <if test="pauseReason != null">pause_reason,</if>
            <if test="pauseTime != null">pause_time,</if>
            <if test="restartTime != null">restart_time,</if>
            <if test="completeTime != null">complete_time,</if>
            <if test="planHour != null">plan_hour,</if>
            <if test="content != null">content,</if>
            <if test="realContent != null">real_content,</if>
            <if test="faultCode != null">fault_code,</if>
            <if test="faultDesc != null">fault_desc,</if>
            <if test="orderType != null">order_type,</if>
            <if test="priorityType != null">priority_type,</if>
            <if test="score != null">score,</if>
            <if test="reviewContent != null">review_content,</if>
            <if test="issuerName != null">issuer_name,</if>
            <if test="permitterName != null">permitter_name,</if>
            <if test="scoringStatus != null">scoring_status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="attachmentUrls != null">attachment_urls,</if>
            <if test="itemCompletionFactor != null">item_completion_factor,</if>
            <if test="itemCompletionFactorSum != null">item_completion_factor_sum,</if>
            <if test="reviewScoreNum != null">review_score_num,</if>
            <if test="finalCoefficient != null">final_coefficient,</if>
            <if test="invalidReason != null and invalidReason != ''">invalid_reason,</if>
            <if test="feedbackReason != null and feedbackReason != ''">feedback_reason,</if>
            <if test="confirmStatus != null">confirm_status,</if>
            <if test="wwryNum != null">wwry_num,</if>
            <if test="wlryNum != null">wlry_num,</if>
            <if test="infoEntry != null and infoEntry != ''">info_entry,</if>
            <if test="lostPower != null">lost_power,</if>
            <if test="workPermitNum != null and workPermitNum != ''">work_permit_num,</if>
            <if test="appealUserId != null">appeal_user_id,</if>
            <if test="appealUserName != null and appealUserName != ''">appeal_user_name,</if>
            <if test="appealTime != null">appeal_time,</if>
            <if test="appealReason != null and appealReason != ''">appeal_reason,</if>
            <if test="suspendExplain != null and suspendExplain != ''">suspend_explain,</if>
            <if test="orderEntryType != null and orderEntryType != ''">order_entry_type,</if>
            <if test="scoreReturnReason != null and scoreReturnReason != ''">score_return_reason,</if>
            <if test="finalizeMethod != null and finalizeMethod != ''">finalize_method,</if>
            </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="workOrderProjectNo != null and workOrderProjectNo != ''">#{workOrderProjectNo},</if>
            <if test="workOrderStatus != null and workOrderStatus != ''">#{workOrderStatus},</if>
            <if test="gxtCenterId != null">#{gxtCenterId},</if>
            <if test="gxtCenter != null">#{gxtCenter},</if>
            <if test="pcsStationId != null">#{pcsStationId},</if>
            <if test="pcsStationPid != null">#{pcsStationPid},</if>
            <if test="pcsStationName != null">#{pcsStationName},</if>
            <if test="pcsDeviceId != null">#{pcsDeviceId},</if>
            <if test="pcsDeviceName != null">#{pcsDeviceName},</if>
            <if test="brand != null">#{brand},</if>
            <if test="model != null">#{model},</if>
            <if test="inspectionType != null">#{inspectionType},</if>
            <if test="misNo != null">#{misNo},</if>
            <if test="planStartTime != null">#{planStartTime},</if>
            <if test="planEndTime != null">#{planEndTime},</if>
            <if test="orderSource != null">#{orderSource},</if>
            <if test="assignTime != null">#{assignTime},</if>
            <if test="assignUserId != null">#{assignUserId},</if>
            <if test="assignUserName != null">#{assignUserName},</if>
            <if test="acceptTime != null">#{acceptTime},</if>
            <if test="acceptUserId != null">#{acceptUserId},</if>
            <if test="acceptUserName != null">#{acceptUserName},</if>
            <if test="realStartTime != null">#{realStartTime},</if>
            <if test="realEndTime != null">#{realEndTime},</if>
            <if test="teamLeaderId != null">#{teamLeaderId},</if>
            <if test="teamLeaderName != null">#{teamLeaderName},</if>
            <if test="workGroupMemberId != null">#{workGroupMemberId},</if>
            <if test="workGroupMemberName != null">#{workGroupMemberName},</if>
            <if test="suspendReason != null">#{suspendReason},</if>
            <if test="suspendTime != null">#{suspendTime},</if>
            <if test="statusBeforeSuspend != null">#{statusBeforeSuspend},</if>
            <if test="modifyReason != null">#{modifyReason},</if>
            <if test="pauseReason != null">#{pauseReason},</if>
            <if test="pauseTime != null">#{pauseTime},</if>
            <if test="restartTime != null">#{restartTime},</if>
            <if test="completeTime != null">#{completeTime},</if>
            <if test="planHour != null">#{planHour},</if>
            <if test="content != null">#{content},</if>
            <if test="realContent != null">#{realContent},</if>
            <if test="faultCode != null">#{faultCode},</if>
            <if test="faultDesc != null">#{faultDesc},</if>
            <if test="orderType != null">#{orderType},</if>
            <if test="priorityType != null">#{priorityType},</if>
            <if test="score != null">#{score},</if>
            <if test="reviewContent != null">#{reviewContent},</if>
            <if test="issuerName != null">#{issuerName},</if>
            <if test="permitterName != null">#{permitterName},</if>
            <if test="scoringStatus != null">#{scoringStatus},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="attachmentUrls != null">#{attachmentUrls},</if>
            <if test="itemCompletionFactor != null">#{itemCompletionFactor},</if>
            <if test="itemCompletionFactorSum != null">#{itemCompletionFactorSum},</if>
            <if test="reviewScoreNum != null">#{reviewScoreNum},</if>
            <if test="finalCoefficient != null">#{finalCoefficient},</if>
            <if test="invalidReason != null and invalidReason != ''">#{invalidReason},</if>
            <if test="feedbackReason != null and feedbackReason != ''">#{feedbackReason},</if>
            <if test="confirmStatus != null">#{confirmStatus},</if>
            <if test="wwryNum != null">#{wwryNum},</if>
            <if test="wlryNum != null">#{wlryNum},</if>
            <if test="infoEntry != null and infoEntry != ''">#{infoEntry},</if>
            <if test="lostPower != null">#{lostPower},</if>
            <if test="workPermitNum != null and workPermitNum != ''">#{workPermitNum},</if>
            <if test="appealUserId != null">#{appealUserId},</if>
            <if test="appealUserName != null and appealUserName != ''">#{appealUserName},</if>
            <if test="appealTime != null">#{appealTime},</if>
            <if test="appealReason != null and appealReason != ''">#{appealReason},</if>
            <if test="suspendExplain != null and suspendExplain != ''">#{suspendExplain},</if>
            <if test="orderEntryType != null and orderEntryType != ''">#{orderEntryType},</if>
            <if test="scoreReturnReason != null and scoreReturnReason != ''">#{scoreReturnReason},</if>
            <if test="finalizeMethod != null and finalizeMethod != ''">#{finalizeMethod},</if>
        </trim>
    </insert>

    <update id="updateGxtWorkOrderByStatus" parameterType="GxtWorkOrder">
        update gxt_work_order
        SET is_deferred = #{isDeferred}
        <where>
            <if test="itemCompletionFactorSum != null"> AND #{itemCompletionFactorSum} > item_completion_factor_sum</if>
            <if test="workOrderStatus != null  and workOrderStatus != ''"> AND work_order_status = #{workOrderStatus}</if>
            <if test="realEndTime != null"> AND #{realEndTime} > create_time</if>
        </where>
    </update>

    <update id="updateGxtWorkOrderByDeferred" parameterType="GxtWorkOrder">
        update gxt_work_order
        SET is_deferred = null
        <where>
            <if test="itemCompletionFactorSum != null"> AND item_completion_factor_sum = #{itemCompletionFactorSum}</if>
            <if test="workOrderStatus != null  and workOrderStatus != ''"> AND work_order_status = #{workOrderStatus}</if>
            <if test="realEndTime != null"> AND #{realEndTime} > create_time</if>
        </where>
    </update>

    <update id="updateGxtWorkOrder" parameterType="GxtWorkOrder">
        update gxt_work_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="workOrderProjectNo != null and workOrderProjectNo != ''">work_order_project_no = #{workOrderProjectNo},</if>
            <if test="workOrderStatus != null and workOrderStatus != ''">work_order_status = #{workOrderStatus},</if>
            <if test="gxtCenterId != null">gxt_center_id = #{gxtCenterId},</if>
            <if test="gxtCenter != null">gxt_center = #{gxtCenter},</if>
            <if test="pcsStationId != null">pcs_station_id = #{pcsStationId},</if>
            <if test="pcsStationPid != null">pcs_station_pid = #{pcsStationPid},</if>
            <if test="pcsStationName != null">pcs_station_name = #{pcsStationName},</if>
            <if test="pcsDeviceId != null">pcs_device_id = #{pcsDeviceId},</if>
            <if test="pcsDeviceName != null">pcs_device_name = #{pcsDeviceName},</if>
            <if test="brand != null">brand = #{brand},</if>
            <if test="model != null">model = #{model},</if>
            <if test="inspectionType != null">inspection_type = #{inspectionType},</if>
            <if test="misNo != null">mis_no = #{misNo},</if>
            <if test="planStartTime != null">plan_start_time = #{planStartTime},</if>
            <if test="planEndTime != null">plan_end_time = #{planEndTime},</if>
            <if test="orderSource != null">order_source = #{orderSource},</if>
            <if test="assignTime != null">assign_time = #{assignTime},</if>
            <if test="assignUserId != null">assign_user_id = #{assignUserId},</if>
            <if test="assignUserName != null">assign_user_name = #{assignUserName},</if>
            <if test="acceptTime != null">accept_time = #{acceptTime},</if>
            <if test="acceptUserId != null">accept_user_id = #{acceptUserId},</if>
            <if test="acceptUserName != null">accept_user_name = #{acceptUserName},</if>
            <if test="realStartTime != null">real_start_time = #{realStartTime},</if>
            <if test="realEndTime != null">real_end_time = #{realEndTime},</if>
            <if test="teamLeaderId != null">team_leader_id = #{teamLeaderId},</if>
            <if test="teamLeaderName != null">team_leader_name = #{teamLeaderName},</if>
            <if test="workGroupMemberId != null">work_group_member_id = #{workGroupMemberId},</if>
            <if test="workGroupMemberName != null">work_group_member_name = #{workGroupMemberName},</if>
            <if test="suspendReason != null">suspend_reason = #{suspendReason},</if>
            <if test="suspendTime != null">suspend_time = #{suspendTime},</if>
            <if test="statusBeforeSuspend != null">status_before_suspend = #{statusBeforeSuspend},</if>
            <if test="modifyReason != null">modify_reason = #{modifyReason},</if>
            <if test="pauseReason != null">pause_reason = #{pauseReason},</if>
            <if test="pauseTime != null">pause_time = #{pauseTime},</if>
            <if test="restartTime != null">restart_time = #{restartTime},</if>
            <if test="completeTime != null">complete_time = #{completeTime},</if>
            <if test="planHour != null">plan_hour = #{planHour},</if>
            <if test="content != null">content = #{content},</if>
            <if test="realContent != null">real_content = #{realContent},</if>
            <if test="faultCode != null">fault_code = #{faultCode},</if>
            <if test="faultDesc != null">fault_desc = #{faultDesc},</if>
            <if test="orderType != null">order_type = #{orderType},</if>
            <if test="priorityType != null">priority_type = #{priorityType},</if>
            <if test="score != null">score = #{score},</if>
            <if test="reviewContent != null">review_content = #{reviewContent},</if>
            <if test="issuerName != null">issuer_name = #{issuerName},</if>
            <if test="permitterName != null">permitter_name = #{permitterName},</if>
            <if test="scoringStatus != null">scoring_status = #{scoringStatus},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="attachmentUrls != null">attachment_urls = #{attachmentUrls},</if>
            <if test="itemCompletionFactor != null">item_completion_factor = #{itemCompletionFactor},</if>
            <if test="itemCompletionFactorSum != null">item_completion_factor_sum = #{itemCompletionFactorSum},</if>
            <if test="reviewScoreNum != null">review_score_num = #{reviewScoreNum},</if>
            <if test="finalCoefficient != null">final_coefficient = #{finalCoefficient},</if>
            <if test="invalidReason != null and invalidReason != ''">invalid_reason = #{invalidReason},</if>
            <if test="feedbackReason != null and feedbackReason != ''">feedback_reason = #{feedbackReason},</if>
            <if test="confirmStatus != null">confirm_status = #{confirmStatus},</if>
            <if test="wwryNum != null">wwry_num = #{wwryNum},</if>
            <if test="wlryNum != null">wlry_num = #{wlryNum},</if>
            <if test="infoEntry != null and infoEntry != ''">info_entry = #{infoEntry},</if>
            <if test="lostPower != null">lost_power = #{lostPower},</if>
            <if test="workPermitNum != null and workPermitNum != ''">work_permit_num = #{workPermitNum},</if>
            <if test="appealUserId != null">appeal_user_id = #{appealUserId},</if>
            <if test="appealUserName != null and appealUserName != ''">appeal_user_name = #{appealUserName},</if>
            <if test="appealTime != null">appeal_time = #{appealTime},</if>
            <if test="appealReason != null and appealReason != ''">appeal_reason = #{appealReason},</if>
            <if test="suspendExplain != null and suspendExplain != ''">suspend_explain = #{suspendExplain},</if>
            <if test="orderEntryType != null and orderEntryType != ''">order_entry_type = #{orderEntryType},</if>
            <if test="scoreReturnReason != null and scoreReturnReason != ''">score_return_reason = #{scoreReturnReason},</if>
            <if test="finalizeMethod != null and finalizeMethod != ''">finalize_method = #{finalizeMethod},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteGxtWorkOrderById" parameterType="Long">
        delete from gxt_work_order where id = #{id}
    </delete>

    <delete id="deleteGxtWorkOrderByIds" parameterType="String">
        delete from gxt_work_order where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- 查询会超时的工单（待归档且超时3天） -->
    <select id="selectExpiredWorkOrders" parameterType="GxtWorkOrder" resultMap="GxtWorkOrderResult">
        <include refid="selectGxtWorkOrderVo"/>
        where work_order_status = #{status}
        and t.update_time &lt; #{thresholdDate}
        order by t.update_time asc
    </select>

    <!-- 根据MIS系统编号查询维保工单 -->
    <select id="selectGxtWorkOrderByMisNo" parameterType="String" resultMap="GxtWorkOrderResult">
        <include refid="selectGxtWorkOrderVo"/>
        where mis_no = #{misNo}
        limit 1
    </select>

    <resultMap type="GxtWorkOrder" id="GxtWorkOrderWithPersonResult" extends="GxtWorkOrderResult">
        <collection property="workOrderPersonList" ofType="com.ygtx.gxt.domain.GxtWorkOrderPerson">
            <result property="id" column="person_id"/>
            <result property="userId" column="person_user_id"/>
            <result property="nickName" column="person_nick_name"/>
            <result property="orderId" column="person_order_id"/>
            <result property="orderCode" column="person_order_code"/>
            <result property="status" column="person_status"/>
            <result property="misNo" column="person_mis_no"/>
            <result property="userName" column="person_user_name"/>
            <result property="selfScore" column="person_self_score"/>
            <result property="reviewScore" column="person_review_score"/>
            <result property="finalScore" column="person_final_score"/>
            <result property="isLeader" column="person_is_leader"/>
        </collection>
    </resultMap>

    <select id="selectWorkOrderWithPersonById" parameterType="Long" resultMap="GxtWorkOrderWithPersonResult">
        SELECT
            t1.id,
            t1.work_order_project_no,
            t1.work_order_status,
            t1.gxt_center_id,
            t1.gxt_center,
            t1.pcs_station_id,
            t1.pcs_station_name,
            t1.pcs_device_id,
            t1.pcs_device_name,
            t1.brand,
            t1.model,
            t1.inspection_type,
            t1.mis_no,
            t1.plan_start_time,
            t1.plan_end_time,
            t1.order_source,
            t1.assign_time,
            t1.assign_user_id,
            t1.assign_user_name,
            t1.accept_time,
            t1.accept_user_id,
            t1.accept_user_name,
            t1.real_start_time,
            t1.real_end_time,
            t1.team_leader_id,
            t1.team_leader_name,
            t1.work_group_member_id,
            t1.work_group_member_name,
            t1.suspend_reason,
            t1.suspend_time,
            t1.status_before_suspend,
            t1.modify_reason,
            t1.pause_reason,
            t1.pause_time,
            t1.restart_time,
            t1.complete_time,
            t1.plan_hour,
            t1.content,
            t1.real_content,
            t1.fault_code,
            t1.fault_desc,
            t1.order_type,
            t1.priority_type,
            t1.score,
            t1.review_content,
            t1.issuer_name,
            t1.permitter_name,
            t1.scoring_status,
            t1.attachment_urls,
            t1.attachment_file_names,
            t1.attachment_original_names,
            t1.review_score_num,
            t1.final_coefficient,
            t1.create_by,
            t1.create_time,
            t1.update_by,
            t1.update_time,
            t1.remark,
            t1.work_permit_num,
            t1.appeal_user_id,
            t1.appeal_user_name,
            t1.appeal_time,
            t1.appeal_reason,
            t2.id as person_id,
            t2.user_id as person_user_id,
            t2.nick_name as person_nick_name,
            t2.order_id as person_order_id,
            t2.order_code as person_order_code,
            t2.status as person_status,
            t2.mis_no as person_mis_no,
            t2.user_name as person_user_name,
            t2.self_score as person_self_score,
            t2.review_score as person_review_score,
            t2.final_score as person_final_score,
            t2.is_leader as person_is_leader
        FROM gxt_work_order t1
                 LEFT JOIN gxt_work_order_person t2 ON t1.id = t2.order_id
        WHERE t1.id = #{id}
    </select>

    <select id="selectHomePageWorkOrderList" parameterType="GxtOrderData" resultMap="GxtOrderDataResult">
        <trim prefix="SELECT" suffixOverrides=",">
            <choose>
                <when test="(xfsc != null and xfsc == 'true') or (xysc != null and xysc == 'true') or (zbsc != null and zbsc == 'true') or (tjsc != null and tjsc == 'true') or (qjbwsc != null and qjbwsc == 'true') or (clsc != null and clsc == 'true') or (gqsc != null and gqsc == 'true') or (score != null and score == 1)">
                    o.work_order_project_no AS order_code,
                    <if test="workOrderNum != null and workOrderNum == 1">
                        CASE
                        WHEN o.orderType='维保工单' THEN 1
                        ELSE NULL
                        END AS workOrderNum,
                    </if>
                    <if test="repairOrderNum != null and repairOrderNum == 1">
                        CASE
                        WHEN o.orderType='维修工单' THEN 1
                        ELSE NULL
                        END AS repairOrderNum,
                    </if>
                    <if test="restartNum != null and restartNum == 1">
                        o.restart_flag AS restart_num,
                    </if>
                </when>
                <otherwise>
                    <if test="workOrderNum != null and workOrderNum == 1">
                        CASE
                        WHEN o.orderType='维保工单' THEN COUNT(*)
                        ELSE NULL
                        END AS workOrderNum,
                    </if>
                    <if test="repairOrderNum != null and repairOrderNum == 1">
                        CASE
                        WHEN o.orderType='维修工单' THEN COUNT(*)
                        ELSE NULL
                        END AS repairOrderNum,
                    </if>
                    <if test="restartNum != null and restartNum == 1">
                        SUM(o.restart_flag) AS restart_num,
                    </if>
                </otherwise>
            </choose>
            <if test="(workOrder != null and workOrder == true) or (repairOrder != null and repairOrder==true)">o.orderType,</if>
            <if test="code != null and code == 'true'">o.`code`,</if>
            <if test="brand != null and brand == 'true'">o.brand,</if>
            <if test="model != null and model == 'true'">o.model,</if>
            <if test="system != null and system == 'true'">o.`system`,</if>
            <if test="pcs != null and pcs == 'true'">o.pcs,</if>
            <if test="center != null and center == 'true'">IFNULL(o.center,'未知中心') AS center,</if>
            <if test="repairPerson != null and repairPerson == 'true'">o.repairPerson,</if>
            <if test="chargePerson != null and chargePerson == 'true'">o.chargePerson,</if>
            <if test="score != null and score == 1">o.score,</if>
            <if test="xfsc != null and xfsc == 'true'">o.xfsc,</if>
            <if test="xysc != null and xysc == 'true'">o.xysc,</if>
            <if test="zbsc != null and zbsc == 'true'">
                CASE
                WHEN o.zbsc IS NULL THEN NULL
                WHEN o.zbsc <![CDATA[<]]> 0 THEN 0
                ELSE o.zbsc
                END AS zbsc,</if>
            <if test="tjsc != null and tjsc == 'true'">o.tjsc,</if>
            <if test="qjbwsc != null and qjbwsc == 'true'">o.qjbwsc,</if>
            <if test="clsc != null and clsc == 'true'">o.clsc,</if>
            <if test="gqsc != null and gqsc == 'true'">o.gqsc,</if>
        </trim>
        FROM (
            SELECT
                wo.work_order_project_no,
                wo.create_time,
                wo.pcs_device_name AS `code`,
                wo.pcs_station_name AS pcs,
                wo.brand,
                wo.model,
                IF(wo.repair_method='2', 1, 0) AS restart_flag,
                wo.mis_no AS `system`,
--                 d.dept_name AS pcs,
                p.dept_name AS center,
                '维保工单' AS orderType,
                m.nick_name AS chargePerson,
                <if test="repairPerson != null and repairPerson == 'true'">u.nick_name AS repairPerson,ROUND(wop.score,2) AS score,</if>
                <if test="repairPerson == null or repairPerson == '' or repairPerson == 'false'">ROUND(wo.score,2) AS score,</if>
                ROUND(TIMESTAMPDIFF( MINUTE, wo.create_time, wo.assign_time ) / 60,2) AS xfsc,
                ROUND(TIMESTAMPDIFF( MINUTE, wo.assign_time, wo.accept_time ) / 60,2) AS xysc,
                ROUND((TIMESTAMPDIFF( MINUTE, wo.accept_time, wo.real_start_time ) / 60) - (IFNULL(gq.total_before_start_suspend_seconds, 0) / 3600),2) AS zbsc,
                ROUND(TIMESTAMPDIFF( MINUTE, wo.real_start_time, wo.restart_time ) / 60,2) AS tjsc,
                ROUND(TIMESTAMPDIFF( MINUTE, wo.real_end_time, wo.restart_time ) / 60,2) AS qjbwsc,
                ROUND(( TIMESTAMPDIFF( MINUTE, wo.create_time, wo.restart_time ) / 60 ) - (IFNULL(gq.total_before_start_suspend_seconds, 0) / 3600) - (IFNULL(gq.total_in_work_suspend_seconds, 0) / 3600),2) AS clsc,
                ROUND(TIMESTAMPDIFF( MINUTE, approved.approved_time, resume.resume_time ) / 60,2) AS gqsc
            FROM
                gxt_work_order wo
                LEFT JOIN sys_dept d ON wo.pcs_station_pid = d.dept_id
                LEFT JOIN sys_dept p ON d.parent_id = p.dept_id
                <if test="repairPerson != null and repairPerson == 'true'">
                    LEFT JOIN gxt_work_order_person wop ON wo.id = wop.order_id
                    LEFT JOIN sys_user u ON wop.user_id = u.user_id
                </if>
                LEFT JOIN sys_user m ON wo.team_leader_id = m.user_id
                LEFT JOIN ( SELECT order_id, COUNT(*) AS order_num FROM gxt_work_order_person GROUP BY order_id ) t ON t.order_id = wo.id
                LEFT JOIN ( SELECT MIN( action_time ) AS assign_time, order_id FROM gxt_work_order_flow WHERE action_type = 'assign' AND order_id IS NOT NULL GROUP BY order_id ) assign ON assign.order_id = wo.id
                LEFT JOIN ( SELECT MIN( action_time ) AS accept_time, order_id FROM gxt_work_order_flow WHERE action_type = 'accept' AND order_id IS NOT NULL GROUP BY order_id ) accept ON accept.order_id = wo.id
                LEFT JOIN ( SELECT MIN( action_time ) AS process_time, order_id FROM gxt_work_order_flow WHERE action_type = 'process' AND order_id IS NOT NULL GROUP BY order_id ) process ON process.order_id = wo.id
                LEFT JOIN ( SELECT MAX( action_time ) AS complete_time, order_id FROM gxt_work_order_flow WHERE action_type = 'complete' AND order_id IS NOT NULL GROUP BY order_id ) complete ON complete.order_id = wo.id
                LEFT JOIN ( SELECT MAX( action_time ) AS restart_time, order_id FROM gxt_work_order_flow WHERE action_type = 'restart' AND order_id IS NOT NULL GROUP BY order_id ) restart ON restart.order_id = wo.id
                LEFT JOIN ( SELECT MIN( action_time ) AS approved_time, order_id FROM gxt_work_order_flow WHERE action_type = 'approved' AND order_id IS NOT NULL GROUP BY order_id ) approved ON approved.order_id = wo.id
                LEFT JOIN ( SELECT MAX( action_time ) AS resume_time, order_id FROM gxt_work_order_flow WHERE action_type = 'resume' AND order_id IS NOT NULL GROUP BY order_id ) resume ON resume.order_id = wo.id
                LEFT JOIN gxt_work_order_flow_records_next gq ON gq.order_id = wo.id
            <where>
                <if test="startTime != null "> AND wo.create_time > #{startTime}</if>
                <if test="endTime != null "> AND wo.create_time <![CDATA[<]]> #{endTime}</if>
                <if test="pcsId != null "> AND wo.pcs_station_pid = #{pcsId}</if>
                <if test="centerId != null "> AND d.parent_id = #{centerId}</if>
                <if test="remark != null and remark != '' ">
                    AND (
                    wo.pcs_device_name LIKE concat('%', #{remark}, '%')
                    OR wo.brand LIKE concat('%', #{remark}, '%')
                    OR wo.model LIKE concat('%', #{remark}, '%')
                    OR m.nick_name LIKE concat('%', #{remark}, '%')
                    OR d.dept_name LIKE concat('%', #{remark}, '%')
                    OR p.dept_name LIKE concat('%', #{remark}, '%')
                    )
                </if>
                <if test="devices != null and devices != ''">
                    AND wo.pcs_device_id IN
                    <foreach item="item" collection="devices.split(',')" open="(" separator="," close=")">
                        #{item, jdbcType=BIGINT}  <!-- 指定 Long 类型 -->
                    </foreach>
                </if>
                <if test="dataPermission != null and dataPermission != ''">${dataPermission}</if>
                AND p.dept_name IS NOT NULL AND wo.work_order_status IS NOT NULL AND wo.work_order_status!='invalid'
            </where>

            UNION ALL

            SELECT
                wo.work_order_project_no,
                wo.occur_time AS create_time,
                wo.pcs_device_name AS `code`,
                wo.pcs_station_name AS pcs,
                wo.brand,
                wo.model,
                IF(wo.repair_method='2', 1, 0) AS restart_flag,
                NULL AS `system`,
--                 d.dept_name AS pcs,
                p.dept_name AS center,
                '维修工单' AS orderType,
                m.nick_name AS chargePerson,
                <if test="repairPerson != null and repairPerson == 'true'">u.nick_name AS repairPerson,ROUND(wop.score,2) AS score,</if>
                <if test="repairPerson == null or repairPerson == '' or repairPerson == 'false'">ROUND(wo.score,2) AS score,</if>
                ROUND(TIMESTAMPDIFF( MINUTE, wo.occur_time, wo.assign_time ) / 60,2) AS xfsc,
                ROUND(TIMESTAMPDIFF( MINUTE, wo.assign_time, wo.accept_time ) / 60,2) AS xysc,
                ROUND((TIMESTAMPDIFF( MINUTE, wo.accept_time, wo.real_start_time ) / 60)-(IFNULL(gq.total_before_start_suspend_seconds, 0) / 3600),2) AS zbsc,
                ROUND(TIMESTAMPDIFF( MINUTE, wo.real_start_time, wo.restart_time ) / 60,2) AS tjsc,
                ROUND(TIMESTAMPDIFF( MINUTE, wo.real_end_time, wo.restart_time ) / 60,2) AS qjbwsc,
                ROUND((TIMESTAMPDIFF( MINUTE, wo.occur_time, wo.restart_time ) / 60 ) - (IFNULL(gq.total_before_start_suspend_seconds, 0) / 3600) - (IFNULL(gq.total_in_work_suspend_seconds, 0) / 3600),2) AS clsc,
                ROUND(TIMESTAMPDIFF( MINUTE, approved.approved_time, resume.resume_time ) / 60,2) AS gqsc
            FROM
                gxt_repair_order wo
                LEFT JOIN sys_dept d ON wo.pcs_station_pid = d.dept_id
                LEFT JOIN sys_dept p ON d.parent_id = p.dept_id
                <if test="repairPerson != null and repairPerson == 'true'">
                    LEFT JOIN gxt_repair_order_person wop ON wo.id = wop.order_id
                    LEFT JOIN sys_user u ON wop.user_id = u.user_id
                </if>
                LEFT JOIN sys_user m ON wo.team_leader_id = m.user_id
                LEFT JOIN ( SELECT order_id, COUNT(*) AS order_num FROM gxt_repair_order_person GROUP BY order_id ) t ON t.order_id = wo.id
                LEFT JOIN ( SELECT MIN( action_time ) AS assign_time, order_id FROM gxt_repair_order_flow WHERE action_type = 'assign' AND order_id IS NOT NULL GROUP BY order_id ) assign ON assign.order_id = wo.id
                LEFT JOIN ( SELECT MIN( action_time ) AS accept_time, order_id FROM gxt_repair_order_flow WHERE action_type = 'accept' AND order_id IS NOT NULL GROUP BY order_id ) accept ON accept.order_id = wo.id
                LEFT JOIN ( SELECT MIN( action_time ) AS process_time, order_id FROM gxt_repair_order_flow WHERE action_type = 'process' AND order_id IS NOT NULL GROUP BY order_id ) process ON process.order_id = wo.id
                LEFT JOIN ( SELECT MAX( action_time ) AS complete_time, order_id FROM gxt_repair_order_flow WHERE action_type = 'complete' AND order_id IS NOT NULL GROUP BY order_id ) complete ON complete.order_id = wo.id
                LEFT JOIN ( SELECT MAX( action_time ) AS restart_time, order_id FROM gxt_repair_order_flow WHERE action_type = 'restart' AND order_id IS NOT NULL GROUP BY order_id ) restart ON restart.order_id = wo.id
                LEFT JOIN ( SELECT MIN( action_time ) AS approved_time, order_id FROM gxt_repair_order_flow WHERE action_type = 'approved' AND order_id IS NOT NULL GROUP BY order_id ) approved ON approved.order_id = wo.id
                LEFT JOIN ( SELECT MAX( action_time ) AS resume_time, order_id FROM gxt_repair_order_flow WHERE action_type = 'resume' AND order_id IS NOT NULL GROUP BY order_id ) resume ON resume.order_id = wo.id
                LEFT JOIN gxt_repair_order_flow_records_next gq ON gq.order_id = wo.id
            <where>
                <if test="startTime != null "> AND wo.occur_time > #{startTime}</if>
                <if test="endTime != null "> AND wo.occur_time <![CDATA[<]]> #{endTime}</if>
                <if test="pcsId != null "> AND wo.pcs_station_pid = #{pcsId}</if>
                <if test="centerId != null "> AND d.parent_id = #{centerId}</if>
                <if test="remark != null and remark != '' ">
                    AND (
                    wo.pcs_device_name LIKE concat('%', #{remark}, '%')
                    OR wo.brand LIKE concat('%', #{remark}, '%')
                    OR wo.model LIKE concat('%', #{remark}, '%')
                    OR m.nick_name LIKE concat('%', #{remark}, '%')
                    OR d.dept_name LIKE concat('%', #{remark}, '%')
                    OR p.dept_name LIKE concat('%', #{remark}, '%')
                    )
                </if>
                <if test="devices != null and devices != ''">
                    AND wo.pcs_device_id IN
                    <foreach item="item" collection="devices.split(',')" open="(" separator="," close=")">
                        #{item, jdbcType=BIGINT}  <!-- 指定 Long 类型 -->
                    </foreach>
                </if>
                <if test="dataPermission != null and dataPermission != ''">${dataPermission}</if>
                AND p.dept_name IS NOT NULL AND wo.work_order_status IS NOT NULL AND wo.work_order_status!='invalid'
            </where>
            ) o
        <where>
            <if test="orderType != null and orderType != '' "> AND o.orderType = #{orderType}</if>
        </where>
        <choose>
            <when test="(xfsc != null and xfsc == 'true') or (xysc != null and xysc == 'true') or (zbsc != null and zbsc == 'true') or (tjsc != null and tjsc == 'true') or (qjbwsc != null and qjbwsc == 'true') or (clsc != null and clsc == 'true') or (gqsc != null and gqsc == 'true') or (score != null and score == 1)">
                <trim prefix="ORDER BY" suffixOverrides=",">
                    <if test="center != null and center == 'true'">o.center,</if>
                    <if test="pcs != null and pcs == 'true'">o.pcs,</if>
                    <if test="(workOrder != null and workOrder == true) or (repairOrder != null and repairOrder==true)">o.orderType,</if>
                    o.work_order_project_no,
                    <if test="chargePerson != null and chargePerson == 'true'">o.chargePerson,</if>
                    <if test="repairPerson != null and repairPerson == 'true'">o.repairPerson,</if>
                    <if test="brand != null and brand == 'true'">o.brand,</if>
                    <if test="model != null and model == 'true'">o.model,</if>
                    <if test="system != null and system == 'true'">o.`system`,</if>
                    <if test="code != null and code == 'true'">o.`code`,</if>
                </trim>
            </when>
            <otherwise>
                <trim prefix="GROUP BY" suffixOverrides=",">
                    <if test="(workOrder != null and workOrder == true) or (repairOrder != null and repairOrder==true)">o.orderType,</if>
                    <if test="code != null and code == 'true'">o.`code`,</if>
                    <if test="brand != null and brand == 'true'">o.brand,</if>
                    <if test="model != null and model == 'true'">o.model,</if>
                    <if test="system != null and system == 'true'">o.`system`,</if>
                    <if test="pcs != null and pcs == 'true'">o.pcs,</if>
                    <if test="center != null and center == 'true'">o.center,</if>
                    <if test="repairPerson != null and repairPerson == 'true'">o.repairPerson,</if>
                    <if test="chargePerson != null and chargePerson == 'true'">o.chargePerson,</if>
                    <if test="score != null and score == 1">o.score,</if>
                    <if test="xfsc != null and xfsc == 'true'">o.xfsc,</if>
                    <if test="xysc != null and xysc == 'true'">o.xysc,</if>
                    <if test="zbsc != null and zbsc == 'true'">o.zbsc,</if>
                    <if test="tjsc != null and tjsc == 'true'">o.tjsc,</if>
                    <if test="qjbwsc != null and qjbwsc == 'true'">o.qjbwsc,</if>
                    <if test="clsc != null and clsc == 'true'">o.clsc,</if>
                    <if test="gqsc != null and gqsc == 'true'">o.gqsc,</if>
                </trim>
                <trim prefix="ORDER BY" suffixOverrides=",">
                    <if test="center != null and center == 'true'">o.center,</if>
                    <if test="pcs != null and pcs == 'true'">o.pcs,</if>
                    <if test="chargePerson != null and chargePerson == 'true'">o.chargePerson,</if>
                    <if test="(workOrder != null and workOrder == true) or (repairOrder != null and repairOrder==true)">o.orderType,</if>
                    <if test="repairPerson != null and repairPerson == 'true'">o.repairPerson,</if>
                    <if test="brand != null and brand == 'true'">o.brand,</if>
                    <if test="model != null and model == 'true'">o.model,</if>
                    <if test="system != null and system == 'true'">o.`system`,</if>
                    <if test="code != null and code == 'true'">o.`code`,</if>
                    <if test="score != null and score == 1">o.score,</if>
                    <if test="xfsc != null and xfsc == 'true'">o.xfsc,</if>
                    <if test="xysc != null and xysc == 'true'">o.xysc,</if>
                    <if test="zbsc != null and zbsc == 'true'">o.zbsc,</if>
                    <if test="tjsc != null and tjsc == 'true'">o.tjsc,</if>
                    <if test="qjbwsc != null and qjbwsc == 'true'">o.qjbwsc,</if>
                    <if test="clsc != null and clsc == 'true'">o.clsc,</if>
                    <if test="gqsc != null and gqsc == 'true'">o.gqsc,</if>
                </trim>
            </otherwise>
        </choose>
    </select>

    <select id="selectLatestGxtWorkOrderByDeviceId" parameterType="map" resultMap="GxtWorkOrderResult">
        <include refid="selectGxtWorkOrderVo"/>
        <where>
            <if test="pcsDeviceId != null"> and pcs_device_id = #{pcsDeviceId}</if>
            <if test="limit != null and limit > 0"> 
                ORDER BY create_time DESC LIMIT #{limit}
            </if>
        </where>
    </select>
    
    <select id="selectGxtWorkOrderByDeviceIdWithCondition" parameterType="map" resultMap="GxtWorkOrderResult">
        <include refid="selectGxtWorkOrderVo"/>
        <where>
            <if test="pcsDeviceId != null"> and pcs_device_id = #{pcsDeviceId}</if>
            <if test="workOrderStatus != null and workOrderStatus != ''">
                <if test="excludeStatus">
                    and work_order_status != #{workOrderStatus}
                </if>
                <if test="!excludeStatus">
                    and work_order_status = #{workOrderStatus}
                </if>
            </if>
        </where>
        <choose>
            <when test="orderByField != null and orderByField == 'pauseTime'">
                ORDER BY pause_time DESC
            </when>
            <when test="orderByField != null and orderByField == 'restartTime'">
                ORDER BY restart_time DESC
            </when>
            <otherwise>
                ORDER BY create_time DESC
            </otherwise>
        </choose>
        <if test="limit != null and limit > 0"> 
            LIMIT #{limit}
        </if>
    </select>
    
    <select id="selectGxtWorkOrderRunningDaysInfo" parameterType="GxtWorkOrder" resultMap="GxtWorkOrderResult">
        SELECT
            t.pcs_device_name,
            t.model,
            t.work_order_project_no,
            t.restart_time,
            t.pause_time,
            t.pcs_station_pid,
            1 AS order_type,
            t.pcs_device_id,t.gxt_center,t.pcs_station_name,d.dept_name,
            DATEDIFF( t.restart_time, t.pause_time ) AS stop_days,
            DATEDIFF( NOW(), t.restart_time ) AS running_days
        FROM
            gxt_work_order t
            INNER JOIN ( 
                SELECT 
                    pcs_device_id, 
                    MAX( pause_time ) AS latest_pause_time 
                FROM 
                    gxt_work_order 
                WHERE 
                    pause_time IS NOT NULL AND restart_time IS NOT NULL 
                GROUP BY 
                    pcs_device_id 
            ) latest ON t.pcs_device_id = latest.pcs_device_id
            AND t.pause_time = latest.latest_pause_time
            LEFT JOIN sys_dept d ON d.dept_id=t.pcs_station_pid
        WHERE
            t.pause_time IS NOT NULL
            AND t.restart_time IS NOT NULL
        <if test="deptName != null  and deptName != ''"> and d.dept_name like concat('%', #{deptName}, '%')</if>
        <if test="pcsStationPid != null"> and t.pcs_station_pid = #{pcsStationPid}</if>
        <if test="params.businessDataScope != null and params.businessDataScope != ''">
            ${params.businessDataScope}
        </if>
    </select>

    <select id="selectGxtRepairOrderRunningDaysInfo" parameterType="GxtWorkOrder" resultMap="GxtWorkOrderResult">
        SELECT
        t.pcs_device_name,
        t.model,
        t.work_order_project_no,
        t.restart_time,
        t.pcs_station_pid,
        t.occur_time AS pause_time,
        2 AS order_type,
        t.pcs_device_id,t.gxt_center,t.pcs_station_name,d.dept_name,
        DATEDIFF( t.restart_time, t.occur_time ) AS stop_days,
        DATEDIFF( NOW(), t.restart_time ) AS running_days
        FROM
        gxt_repair_order t
        INNER JOIN (
        SELECT
        pcs_device_id,
        MAX( occur_time ) AS latest_pause_time
        FROM
        gxt_repair_order
        WHERE
        occur_time IS NOT NULL AND restart_time IS NOT NULL
        GROUP BY
        pcs_device_id
        ) latest ON t.pcs_device_id = latest.pcs_device_id
        AND t.occur_time = latest.latest_pause_time
        LEFT JOIN sys_dept d ON d.dept_id=t.pcs_station_pid
        WHERE
        t.occur_time IS NOT NULL
        AND t.restart_time IS NOT NULL
        <if test="deptName != null  and deptName != ''"> and d.dept_name like concat('%', #{deptName}, '%')</if>
        <if test="pcsStationPid != null"> and t.pcs_station_pid = #{pcsStationPid}</if>
        <if test="params.businessDataScope != null and params.businessDataScope != ''">
            ${params.businessDataScope}
        </if>
    </select>
</mapper>