<?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.GxtOrderPersonMapper">
    
    <resultMap type="com.ygtx.gxt.domain.GxtOrderPerson" id="GxtOrderPersonResult">
        <result property="userId" column="user_id"/>
        <result property="nickName" column="nick_name"/>
        <result property="orderCode" column="order_code"/>
        <result property="center" column="center"/>
        <result property="parent" column="parent"/>
        <result property="pcs" column="pcs"/>
        <result property="orderType" column="order_type"/>
        <result property="scoreType" column="score_type"/>
        <result property="finishDate" column="finish_date"/>
        <result property="score" column="score"/>
        <result property="status" column="status"/>
    </resultMap>
    
    <select id="selectGxtOrderPersonList" parameterType="GxtOrderPerson" resultMap="GxtOrderPersonResult">
        SELECT po.*,DATE( po.real_end_time ) AS finish_date FROM (
            SELECT
                o.work_order_project_no AS order_code,
                o.gxt_center AS center,
                d.dept_name AS parent,
                o.pcs_station_name AS pcs,
                '维修工单' AS order_type,
                o.real_end_time,
                p.score,p.user_id
            FROM
                gxt_repair_order_person p
                LEFT JOIN gxt_repair_order o ON p.order_id = o.id
                LEFT JOIN sys_dept d ON o.pcs_station_pid = d.dept_id 
            WHERE
                o.occur_time >= DATE_FORMAT( CURDATE(), '%Y-%m-01 00:00:00' )
                AND o.real_end_time <![CDATA[<]]> DATE_FORMAT( DATE_ADD( CURDATE(), INTERVAL 1 MONTH ), '%Y-%m-01 00:00:00' )
                AND p.score IS NOT NULL AND o.scoring_status IN ('to_archive','archived')
            UNION ALL
            SELECT
                o.work_order_project_no AS order_code,
                o.gxt_center AS center,
                d.dept_name AS parent,
                o.pcs_station_name AS pcs,
                '维保工单' AS order_type,
                o.real_end_time,
                p.score,p.user_id
            FROM
                gxt_work_order_person p
                LEFT JOIN gxt_work_order o ON p.order_id = o.id
                LEFT JOIN sys_dept d ON o.pcs_station_pid = d.dept_id 
            WHERE
                o.create_time >= DATE_FORMAT( CURDATE(), '%Y-%m-01 00:00:00' )
                AND o.real_end_time <![CDATA[<]]> DATE_FORMAT( DATE_ADD( CURDATE(), INTERVAL 1 MONTH ), '%Y-%m-01 00:00:00' )
                AND p.score IS NOT NULL AND o.scoring_status IN ('to_archive','archived')
        ) po
        WHERE 1=1
        <if test="userId != null">
            AND po.user_id = #{userId}
        </if>
        <if test="orderCode != null and orderCode != ''">
            AND po.order_code LIKE CONCAT( '%', #{orderCode}, '%' )
        </if>
        ORDER BY po.real_end_time DESC
    </select>

    <select id="selectGxtScoreDetailList" parameterType="GxtOrderPerson" resultMap="GxtOrderPersonResult">
        SELECT
            u.nick_name,
            CASE WHEN t.order_type=1 THEN ro.work_order_project_no ELSE wo.work_order_project_no END AS order_code,
            CASE WHEN t.order_type=1 THEN ro.gxt_center	ELSE wo.gxt_center END AS center,
            d.dept_name AS parent,DATE( t.create_time ) AS finish_date,t.user_id,
            CASE WHEN t.order_type=1 THEN ro.pcs_station_name ELSE wo.pcs_station_name END AS pcs,
            CASE WHEN t.order_type=1 THEN '维修工单' ELSE '维保工单' END AS order_type,
            CASE WHEN t.score_type=1 THEN '工单得分' WHEN t.score_type=2 THEN '运行得分' ELSE '停机扣分' END AS score_type,t.score
        FROM gxt_order_score_detail t
            LEFT JOIN sys_user u ON t.user_id=u.user_id
            LEFT JOIN gxt_repair_order ro ON t.order_id=ro.id
            LEFT JOIN gxt_work_order wo ON wo.id=t.order_id
            LEFT JOIN sys_dept d ON
                d.dept_id=CASE WHEN t.order_type=1 THEN ro.pcs_station_pid	ELSE wo.pcs_station_pid END
        WHERE DATE_FORMAT(t.create_time, '%Y-%m') = DATE_FORMAT(CURDATE(), '%Y-%m')
        <if test="userId != null">
            AND t.user_id = #{userId}
        </if>
        <if test="status != null">
            AND t.status = #{status}
        </if>
        <if test="orderCode != null and orderCode != ''">
            AND t.order_code LIKE CONCAT( '%', #{orderCode}, '%' )
        </if>
        ORDER BY t.create_time DESC
    </select>

    <delete id="delRepairOrderFlowRecords">
        DELETE FROM gxt_repair_order_flow_records
    </delete>

    <delete id="delRepairOrderFlowRecordsNext">
        DELETE FROM gxt_repair_order_flow_records_next
    </delete>

    <delete id="delWorkOrderFlowRecords">
        DELETE FROM gxt_work_order_flow_records
    </delete>

    <delete id="delWorkOrderFlowRecordsNext">
        DELETE FROM gxt_work_order_flow_records_next
    </delete>

    <select id="selectRepairOrderFlowRecordsSize" resultType="Integer">
        SELECT count(*) FROM gxt_repair_order_flow_records
    </select>

    <select id="repairOrderFlowRecordsSize" resultType="Integer">
        SELECT COUNT(*) FROM (
            SELECT
            f.order_id,
            f.action_type,
            f.action_time,
            ro.real_start_time,
            (SELECT COUNT(*)
             FROM gxt_repair_order_flow f2
             WHERE f2.order_id = f.order_id
               AND f2.action_type IN ('approved', 'resume', 'auto_suspend', 'auto_resume')
               AND f.action_time >= f2.action_time) AS order_seq
        FROM gxt_repair_order_flow f
                 LEFT JOIN gxt_repair_order ro
                           ON ro.id = f.order_id
        WHERE ro.work_order_status IN ('completed','archived')
          AND ro.real_start_time IS NOT NULL AND ro.real_end_time IS NOT NULL
          AND f.action_type IN ('approved', 'resume', 'auto_suspend', 'auto_resume')
        ) t
    </select>

    <insert id="addRepairOrderFlowRecords">
        INSERT INTO gxt_repair_order_flow_records (
            order_id,
            action_type,
            action_time,
            real_start_time,
            order_seq
        )
        SELECT
            f.order_id,
            f.action_type,
            f.action_time,
            ro.real_start_time,
            (SELECT COUNT(*)
             FROM gxt_repair_order_flow f2
             WHERE f2.order_id = f.order_id
               AND f2.action_type IN ('approved', 'resume', 'auto_suspend', 'auto_resume')
               AND f.action_time >= f2.action_time) AS order_seq
        FROM gxt_repair_order_flow f
                 LEFT JOIN gxt_repair_order ro
                           ON ro.id = f.order_id
        WHERE ro.work_order_status IN ('completed','archived')
          AND ro.real_start_time IS NOT NULL AND ro.real_end_time IS NOT NULL
          AND f.action_type IN ('approved', 'resume', 'auto_suspend', 'auto_resume')
        ORDER BY f.order_id, f.action_time
    </insert>

    <insert id="addRepairOrderFlowRecordsNext">
        INSERT INTO gxt_repair_order_flow_records_next (
            order_id,
            total_before_start_suspend_seconds,
            total_in_work_suspend_seconds,
            total_night_auto_suspend_seconds,
            before_start_suspend_format,
            in_work_suspend_format,
            night_auto_suspend_format
        )
        SELECT
            t0.order_id,
            COALESCE(SUM(CASE
                             WHEN wo0.real_start_time>t2.action_time AND wo0.real_end_time>wo0.real_start_time
                                 THEN TIMESTAMPDIFF(SECOND, t1.action_time, t2.action_time)
                             WHEN wo0.real_start_time>t1.action_time AND t2.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time
                                 THEN TIMESTAMPDIFF(SECOND, t1.action_time, wo0.real_start_time)
                             ELSE 0
                END), 0) AS total_before_start_suspend_seconds,

            COALESCE(SUM(CASE
                             WHEN t1.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND wo0.real_end_time>=t2.action_time
                                 THEN TIMESTAMPDIFF(SECOND, t1.action_time, t2.action_time)
                             WHEN wo0.real_start_time>t1.action_time AND t2.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND wo0.real_end_time>=t2.action_time
                                 THEN TIMESTAMPDIFF(SECOND, wo0.real_start_time, t2.action_time)
                             WHEN t1.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND t2.action_time>wo0.real_end_time AND wo0.real_end_time>=t1.action_time
                                 THEN TIMESTAMPDIFF(SECOND, t1.action_time, wo0.real_end_time)
                             WHEN wo0.real_start_time>t1.action_time AND t2.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND t2.action_time>wo0.real_end_time
                                 THEN TIMESTAMPDIFF(SECOND, wo0.real_start_time, wo0.real_end_time)
                             ELSE 0
                END), 0) AS total_in_work_suspend_seconds,
            COALESCE(SUM(CASE
                             WHEN t4.action_time IS NOT NULL
                                 THEN TIMESTAMPDIFF(SECOND, t3.action_time, t4.action_time)
                             ELSE 0
                END), 0) AS total_night_auto_suspend_seconds,

            CONCAT(
                    FLOOR(COALESCE(SUM(CASE
                                           WHEN wo0.real_start_time>t2.action_time AND wo0.real_end_time>wo0.real_start_time
                                               THEN TIMESTAMPDIFF(SECOND, t1.action_time, t2.action_time)
                                           WHEN wo0.real_start_time>t1.action_time AND t2.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time
                                               THEN TIMESTAMPDIFF(SECOND, t1.action_time, wo0.real_start_time)
                                           ELSE 0
                        END), 0) / 3600), '小时',
                    FLOOR(MOD(COALESCE(SUM(CASE
                                               WHEN wo0.real_start_time>t2.action_time AND wo0.real_end_time>wo0.real_start_time
                                                   THEN TIMESTAMPDIFF(SECOND, t1.action_time, t2.action_time)
                                               WHEN wo0.real_start_time>t1.action_time AND t2.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time
                                                   THEN TIMESTAMPDIFF(SECOND, t1.action_time, wo0.real_start_time)
                                               ELSE 0
                        END), 0), 3600) / 60), '分钟',
                    MOD(COALESCE(SUM(CASE
                                         WHEN wo0.real_start_time>t2.action_time AND wo0.real_end_time>wo0.real_start_time
                                             THEN TIMESTAMPDIFF(SECOND, t1.action_time, t2.action_time)
                                         WHEN wo0.real_start_time>t1.action_time AND t2.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time
                                             THEN TIMESTAMPDIFF(SECOND, t1.action_time, wo0.real_start_time)
                                         ELSE 0
                        END), 0), 60), '秒'
                ) AS before_start_suspend_format,

            CONCAT(
                    FLOOR(COALESCE(SUM(CASE
                                           WHEN t1.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND wo0.real_end_time>=t2.action_time
                                               THEN TIMESTAMPDIFF(SECOND, t1.action_time, t2.action_time)
                                           WHEN wo0.real_start_time>t1.action_time AND t2.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND wo0.real_end_time>=t2.action_time
                                               THEN TIMESTAMPDIFF(SECOND, wo0.real_start_time, t2.action_time)
                                           WHEN t1.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND t2.action_time>wo0.real_end_time AND wo0.real_end_time>=t1.action_time
                                               THEN TIMESTAMPDIFF(SECOND, t1.action_time, wo0.real_end_time)
                                           WHEN wo0.real_start_time>t1.action_time AND t2.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND t2.action_time>wo0.real_end_time
                                               THEN TIMESTAMPDIFF(SECOND, wo0.real_start_time, wo0.real_end_time)
                                           ELSE 0
                        END), 0) / 3600), '小时',
                    FLOOR(MOD(COALESCE(SUM(CASE
                                               WHEN t1.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND wo0.real_end_time>=t2.action_time
                                                   THEN TIMESTAMPDIFF(SECOND, t1.action_time, t2.action_time)
                                               WHEN wo0.real_start_time>t1.action_time AND t2.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND wo0.real_end_time>=t2.action_time
                                                   THEN TIMESTAMPDIFF(SECOND, wo0.real_start_time, t2.action_time)
                                               WHEN t1.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND t2.action_time>wo0.real_end_time AND wo0.real_end_time>=t1.action_time
                                                   THEN TIMESTAMPDIFF(SECOND, t1.action_time, wo0.real_end_time)
                                               WHEN wo0.real_start_time>t1.action_time AND t2.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND t2.action_time>wo0.real_end_time
                                                   THEN TIMESTAMPDIFF(SECOND, wo0.real_start_time, wo0.real_end_time)
                                               ELSE 0
                        END), 0), 3600) / 60), '分钟',
                    MOD(COALESCE(SUM(CASE
                                         WHEN t1.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND wo0.real_end_time>=t2.action_time
                                             THEN TIMESTAMPDIFF(SECOND, t1.action_time, t2.action_time)
                                         WHEN wo0.real_start_time>t1.action_time AND t2.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND wo0.real_end_time>=t2.action_time
                                             THEN TIMESTAMPDIFF(SECOND, wo0.real_start_time, t2.action_time)
                                         WHEN t1.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND t2.action_time>wo0.real_end_time AND wo0.real_end_time>=t1.action_time
                                             THEN TIMESTAMPDIFF(SECOND, t1.action_time, wo0.real_end_time)
                                         WHEN wo0.real_start_time>t1.action_time AND t2.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND t2.action_time>wo0.real_end_time
                                             THEN TIMESTAMPDIFF(SECOND, wo0.real_start_time, wo0.real_end_time)
                                         ELSE 0
                        END), 0), 60), '秒'
                ) AS in_work_suspend_format,

            CONCAT(
                    FLOOR(COALESCE(SUM(CASE
                                           WHEN t4.action_time IS NOT NULL
                                               THEN TIMESTAMPDIFF(SECOND, t3.action_time, t4.action_time)
                                           ELSE 0
                        END), 0) / 3600), '小时',
                    FLOOR(MOD(COALESCE(SUM(CASE
                                               WHEN t4.action_time IS NOT NULL
                                                   THEN TIMESTAMPDIFF(SECOND, t3.action_time, t4.action_time)
                                               ELSE 0
                        END), 0), 3600) / 60), '分钟',
                    MOD(COALESCE(SUM(CASE
                                         WHEN t4.action_time IS NOT NULL
                                             THEN TIMESTAMPDIFF(SECOND, t3.action_time, t4.action_time)
                                         ELSE 0
                        END), 0), 60), '秒'
                ) AS night_auto_suspend_format

        FROM (SELECT order_id FROM gxt_repair_order_flow_records GROUP BY order_id) t0
                 LEFT JOIN gxt_repair_order_flow_records t1
                           ON t0.order_id = t1.order_id
                               AND t1.action_type = 'approved'
                 LEFT JOIN gxt_repair_order_flow_records t2
                           ON t1.order_id = t2.order_id
                               AND t2.order_seq = t1.order_seq + 1
                               AND t1.action_type = 'approved'
                               AND t2.action_type IN ('resume','auto_resume')
                 LEFT JOIN gxt_repair_order_flow_records t3
                           ON t0.order_id = t3.order_id
                               AND t3.action_type = 'auto_suspend'
                 LEFT JOIN gxt_repair_order_flow_records t4
                           ON t3.order_id = t4.order_id
                               AND t4.order_seq = t3.order_seq + 1
                               AND t3.action_type = 'auto_suspend'
                               AND t4.action_type IN ('resume','auto_resume')
                 LEFT JOIN gxt_repair_order wo0
                           ON t0.order_id = wo0.id
        GROUP BY t0.order_id
        ORDER BY t0.order_id
    </insert>

    <select id="selectWorkOrderFlowRecordsSize" resultType="Integer">
        SELECT count(*) FROM gxt_work_order_flow_records
    </select>

    <select id="workOrderFlowRecordsSize" resultType="Integer">
        SELECT COUNT(*) FROM (
        SELECT
            f.order_id,
            f.action_type,
            f.action_time,
            ro.real_start_time,
            (SELECT COUNT(*)
             FROM gxt_work_order_flow f2
             WHERE f2.order_id = f.order_id
               AND f2.action_type IN ('approved', 'resume', 'auto_suspend', 'auto_resume')
               AND f.action_time >= f2.action_time) AS order_seq
        FROM gxt_work_order_flow f
                 LEFT JOIN gxt_work_order ro
                           ON ro.id = f.order_id
        WHERE ro.work_order_status IN ('completed','archived')
          AND ro.real_start_time IS NOT NULL AND ro.real_end_time IS NOT NULL
          AND f.action_type IN ('approved', 'resume', 'auto_suspend', 'auto_resume')) t
    </select>

    <insert id="addWorkOrderFlowRecords">
        INSERT INTO gxt_work_order_flow_records (
            order_id,
            action_type,
            action_time,
            real_start_time,
            order_seq
        )
        SELECT
            f.order_id,
            f.action_type,
            f.action_time,
            ro.real_start_time,
            (SELECT COUNT(*)
             FROM gxt_work_order_flow f2
             WHERE f2.order_id = f.order_id
               AND f2.action_type IN ('approved', 'resume', 'auto_suspend', 'auto_resume')
               AND f.action_time >= f2.action_time) AS order_seq
        FROM gxt_work_order_flow f
                 LEFT JOIN gxt_work_order ro
                           ON ro.id = f.order_id
        WHERE ro.work_order_status IN ('completed','archived')
          AND ro.real_start_time IS NOT NULL AND ro.real_end_time IS NOT NULL
          AND f.action_type IN ('approved', 'resume', 'auto_suspend', 'auto_resume')
        ORDER BY f.order_id, f.action_time
    </insert>

    <insert id="addWorkOrderFlowRecordsNext">
        INSERT INTO gxt_work_order_flow_records_next (
            order_id,
            total_before_start_suspend_seconds,
            total_in_work_suspend_seconds,
            total_night_auto_suspend_seconds,
            before_start_suspend_format,
            in_work_suspend_format,
            night_auto_suspend_format
        )
        SELECT
            t0.order_id,
            COALESCE(SUM(CASE
                             WHEN wo0.real_start_time>t2.action_time AND wo0.real_end_time>wo0.real_start_time
                                 THEN TIMESTAMPDIFF(SECOND, t1.action_time, t2.action_time)
                             WHEN wo0.real_start_time>t1.action_time AND t2.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time
                                 THEN TIMESTAMPDIFF(SECOND, t1.action_time, wo0.real_start_time)
                             ELSE 0
                END), 0) AS total_before_start_suspend_seconds,

            COALESCE(SUM(CASE
                             WHEN t1.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND wo0.real_end_time>=t2.action_time
                                 THEN TIMESTAMPDIFF(SECOND, t1.action_time, t2.action_time)
                             WHEN wo0.real_start_time>t1.action_time AND t2.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND wo0.real_end_time>=t2.action_time
                                 THEN TIMESTAMPDIFF(SECOND, wo0.real_start_time, t2.action_time)
                             WHEN t1.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND t2.action_time>wo0.real_end_time AND wo0.real_end_time>=t1.action_time
                                 THEN TIMESTAMPDIFF(SECOND, t1.action_time, wo0.real_end_time)
                             WHEN wo0.real_start_time>t1.action_time AND t2.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND t2.action_time>wo0.real_end_time
                                 THEN TIMESTAMPDIFF(SECOND, wo0.real_start_time, wo0.real_end_time)
                             ELSE 0
                END), 0) AS total_in_work_suspend_seconds,

            COALESCE(SUM(CASE
                             WHEN t4.action_time IS NOT NULL
                                 THEN TIMESTAMPDIFF(SECOND, t3.action_time, t4.action_time)
                             ELSE 0
                END), 0) AS total_night_auto_suspend_seconds,

            CONCAT(
                    FLOOR(COALESCE(SUM(CASE
                                           WHEN wo0.real_start_time>t2.action_time AND wo0.real_end_time>wo0.real_start_time
                                               THEN TIMESTAMPDIFF(SECOND, t1.action_time, t2.action_time)
                                           WHEN wo0.real_start_time>t1.action_time AND t2.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time
                                               THEN TIMESTAMPDIFF(SECOND, t1.action_time, wo0.real_start_time)
                                           ELSE 0
                        END), 0) / 3600), '小时',
                    FLOOR(MOD(COALESCE(SUM(CASE
                                               WHEN wo0.real_start_time>t2.action_time AND wo0.real_end_time>wo0.real_start_time
                                                   THEN TIMESTAMPDIFF(SECOND, t1.action_time, t2.action_time)
                                               WHEN wo0.real_start_time>t1.action_time AND t2.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time
                                                   THEN TIMESTAMPDIFF(SECOND, t1.action_time, wo0.real_start_time)
                                               ELSE 0
                        END), 0), 3600) / 60), '分钟',
                    MOD(COALESCE(SUM(CASE
                                         WHEN wo0.real_start_time>t2.action_time AND wo0.real_end_time>wo0.real_start_time
                                             THEN TIMESTAMPDIFF(SECOND, t1.action_time, t2.action_time)
                                         WHEN wo0.real_start_time>t1.action_time AND t2.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time
                                             THEN TIMESTAMPDIFF(SECOND, t1.action_time, wo0.real_start_time)
                                         ELSE 0
                        END), 0), 60), '秒'
                ) AS before_start_suspend_format,

            CONCAT(
                    FLOOR(COALESCE(SUM(CASE
                                           WHEN t1.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND wo0.real_end_time>=t2.action_time
                                               THEN TIMESTAMPDIFF(SECOND, t1.action_time, t2.action_time)
                                           WHEN wo0.real_start_time>t1.action_time AND t2.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND wo0.real_end_time>=t2.action_time
                                               THEN TIMESTAMPDIFF(SECOND, wo0.real_start_time, t2.action_time)
                                           WHEN t1.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND t2.action_time>wo0.real_end_time AND wo0.real_end_time>=t1.action_time
                                               THEN TIMESTAMPDIFF(SECOND, t1.action_time, wo0.real_end_time)
                                           WHEN wo0.real_start_time>t1.action_time AND t2.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND t2.action_time>wo0.real_end_time
                                               THEN TIMESTAMPDIFF(SECOND, wo0.real_start_time, wo0.real_end_time)
                                           ELSE 0
                        END), 0) / 3600), '小时',
                    FLOOR(MOD(COALESCE(SUM(CASE
                                               WHEN t1.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND wo0.real_end_time>=t2.action_time
                                                   THEN TIMESTAMPDIFF(SECOND, t1.action_time, t2.action_time)
                                               WHEN wo0.real_start_time>t1.action_time AND t2.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND wo0.real_end_time>=t2.action_time
                                                   THEN TIMESTAMPDIFF(SECOND, wo0.real_start_time, t2.action_time)
                                               WHEN t1.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND t2.action_time>wo0.real_end_time AND wo0.real_end_time>=t1.action_time
                                                   THEN TIMESTAMPDIFF(SECOND, t1.action_time, wo0.real_end_time)
                                               WHEN wo0.real_start_time>t1.action_time AND t2.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND t2.action_time>wo0.real_end_time
                                                   THEN TIMESTAMPDIFF(SECOND, wo0.real_start_time, wo0.real_end_time)
                                               ELSE 0
                        END), 0), 3600) / 60), '分钟',
                    MOD(COALESCE(SUM(CASE
                                         WHEN t1.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND wo0.real_end_time>=t2.action_time
                                             THEN TIMESTAMPDIFF(SECOND, t1.action_time, t2.action_time)
                                         WHEN wo0.real_start_time>t1.action_time AND t2.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND wo0.real_end_time>=t2.action_time
                                             THEN TIMESTAMPDIFF(SECOND, wo0.real_start_time, t2.action_time)
                                         WHEN t1.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND t2.action_time>wo0.real_end_time AND wo0.real_end_time>=t1.action_time
                                             THEN TIMESTAMPDIFF(SECOND, t1.action_time, wo0.real_end_time)
                                         WHEN wo0.real_start_time>t1.action_time AND t2.action_time>wo0.real_start_time AND wo0.real_end_time>wo0.real_start_time AND t2.action_time>wo0.real_end_time
                                             THEN TIMESTAMPDIFF(SECOND, wo0.real_start_time, wo0.real_end_time)
                                         ELSE 0
                        END), 0), 60), '秒'
                ) AS in_work_suspend_format,

            CONCAT(
                    FLOOR(COALESCE(SUM(CASE
                                           WHEN t4.action_time IS NOT NULL
                                               THEN TIMESTAMPDIFF(SECOND, t3.action_time, t4.action_time)
                                           ELSE 0
                        END), 0) / 3600), '小时',
                    FLOOR(MOD(COALESCE(SUM(CASE
                                               WHEN t4.action_time IS NOT NULL
                                                   THEN TIMESTAMPDIFF(SECOND, t3.action_time, t4.action_time)
                                               ELSE 0
                        END), 0), 3600) / 60), '分钟',
                    MOD(COALESCE(SUM(CASE
                                         WHEN t4.action_time IS NOT NULL
                                             THEN TIMESTAMPDIFF(SECOND, t3.action_time, t4.action_time)
                                         ELSE 0
                        END), 0), 60), '秒'
                ) AS night_auto_suspend_format
        FROM (SELECT order_id FROM gxt_work_order_flow_records GROUP BY order_id) t0
                 LEFT JOIN gxt_work_order_flow_records t1
                           ON t0.order_id = t1.order_id
                               AND t1.action_type = 'approved'
                 LEFT JOIN gxt_work_order_flow_records t2
                           ON t1.order_id = t2.order_id
                               AND t2.order_seq = t1.order_seq + 1
                               AND t1.action_type = 'approved'
                               AND t2.action_type IN ('resume','auto_resume')
                 LEFT JOIN gxt_work_order_flow_records t3
                           ON t0.order_id = t3.order_id
                               AND t3.action_type = 'auto_suspend'
                 LEFT JOIN gxt_work_order_flow_records t4
                           ON t3.order_id = t4.order_id
                               AND t4.order_seq = t3.order_seq + 1
                               AND t3.action_type = 'auto_suspend'
                               AND t4.action_type IN ('resume','auto_resume')
                 LEFT JOIN gxt_work_order wo0
                           ON t0.order_id = wo0.id
        GROUP BY t0.order_id
        ORDER BY t0.order_id
    </insert>
    
</mapper>