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

    <!-- 分组统计工单数据 -->
    <select id="selectGroupStatistics" resultType="java.util.HashMap">
        SELECT
            <choose>
                <when test="params.groupFields != null and params.groupFields != ''">
                    <foreach collection="params.groupFields.split(',')" item="field" separator=",">
                        t.${field}
                    </foreach>,
                </when>
                <otherwise>
                    t.gxt_center,
                </otherwise>
            </choose>
            SUM(t.total_count) as total_count,
            SUM(t.work_order_count) as work_order_count,
            SUM(t.repair_order_count) as repair_order_count,
            SUM(t.annual_inspection_count) as annual_inspection_count,
            SUM(t.semi_annual_inspection_count) as semi_annual_inspection_count,
            SUM(t.bolt_inspection_count) as bolt_inspection_count,
            SUM(t.annual_inspection_score) as annual_inspection_score,
            SUM(t.semi_annual_inspection_score) as semi_annual_inspection_score,
            SUM(t.bolt_inspection_score) as bolt_inspection_score,
            SUM(t.total_score) as total_score,
            SUM(t.total_accept_duration) as total_accept_duration,
            SUM(t.total_prepare_duration) as total_prepare_duration,
            SUM(t.total_operation_duration) as total_operation_duration,
            SUM(t.total_duration) as total_duration
        FROM (
            -- 维保工单统计
            SELECT
                <choose>
                    <when test="params.groupFields != null and params.groupFields != ''">
                        <foreach collection="params.groupFields.split(',')" item="field" separator=",">
                            ${field}
                        </foreach>,
                    </when>
                    <otherwise>
                        gxt_center,
                    </otherwise>
                </choose>
                COUNT(*) as total_count,
                COUNT(*) as work_order_count,
                0 as repair_order_count,
                SUM(CASE WHEN inspection_type = 1 THEN 1 ELSE 0 END) as annual_inspection_count,
                SUM(CASE WHEN inspection_type = 2 THEN 1 ELSE 0 END) as semi_annual_inspection_count,
                SUM(CASE WHEN inspection_type = 3 THEN 1 ELSE 0 END) as bolt_inspection_count,
                COALESCE(SUM(CASE WHEN inspection_type = 1 THEN score ELSE 0 END), 0) as annual_inspection_score,
                COALESCE(SUM(CASE WHEN inspection_type = 2 THEN score ELSE 0 END), 0) as semi_annual_inspection_score,
                COALESCE(SUM(CASE WHEN inspection_type = 3 THEN score ELSE 0 END), 0) as bolt_inspection_score,
                COALESCE(SUM(score), 0) as total_score,
                COALESCE(SUM(TIMESTAMPDIFF(HOUR, assign_time, accept_time)), 0) as total_accept_duration,
                COALESCE(SUM(TIMESTAMPDIFF(HOUR, accept_time, real_start_time)), 0) as total_prepare_duration,
                COALESCE(SUM(TIMESTAMPDIFF(HOUR, real_start_time, real_end_time)), 0) as total_operation_duration,
                COALESCE(SUM(TIMESTAMPDIFF(HOUR, assign_time, accept_time)), 0) +
                COALESCE(SUM(TIMESTAMPDIFF(HOUR, accept_time, real_start_time)), 0) +
                COALESCE(SUM(TIMESTAMPDIFF(HOUR, real_start_time, real_end_time)), 0) as total_duration
            FROM gxt_work_order
            WHERE 1=1
                <if test="params.gxtCenter != null and params.gxtCenter != ''">
                    AND gxt_center = #{params.gxtCenter}
                </if>
                <if test="params.pcsStationName != null and params.pcsStationName != ''">
                    AND pcs_station_name = #{params.pcsStationName}
                </if>
                <if test="params.brand != null and params.brand != ''">
                    AND brand = #{params.brand}
                </if>
                <if test="params.model != null and params.model != ''">
                    AND model = #{params.model}
                </if>
                <if test="params.startTime != null and params.startTime != ''">
                    AND create_time &gt;= #{params.startTime}
                </if>
                <if test="params.endTime != null and params.endTime != ''">
                    AND create_time &lt;= #{params.endTime}
                </if>
            GROUP BY
                <choose>
                    <when test="params.groupFields != null and params.groupFields != ''">
                        <foreach collection="params.groupFields.split(',')" item="field" separator=",">
                            ${field}
                        </foreach>
                    </when>
                    <otherwise>
                        gxt_center
                    </otherwise>
                </choose>

            UNION ALL

            -- 维修工单统计
            SELECT
                <choose>
                    <when test="params.groupFields != null and params.groupFields != ''">
                        <foreach collection="params.groupFields.split(',')" item="field" separator=",">
                            ${field}
                        </foreach>,
                    </when>
                    <otherwise>
                        gxt_center,
                    </otherwise>
                </choose>
                COUNT(*) as total_count,
                0 as work_order_count,
                COUNT(*) as repair_order_count,
                0 as annual_inspection_count,
                0 as semi_annual_inspection_count,
                0 as bolt_inspection_count,
                0 as annual_inspection_score,
                0 as semi_annual_inspection_score,
                0 as bolt_inspection_score,
                COALESCE(SUM(score), 0) as total_score,
                COALESCE(SUM(TIMESTAMPDIFF(HOUR, assign_time, accept_time)), 0) as total_accept_duration,
                COALESCE(SUM(TIMESTAMPDIFF(HOUR, accept_time, real_start_time)), 0) as total_prepare_duration,
                COALESCE(SUM(TIMESTAMPDIFF(HOUR, real_start_time, real_end_time)), 0) as total_operation_duration,
                COALESCE(SUM(TIMESTAMPDIFF(HOUR, assign_time, accept_time)), 0) +
                COALESCE(SUM(TIMESTAMPDIFF(HOUR, accept_time, real_start_time)), 0) +
                COALESCE(SUM(TIMESTAMPDIFF(HOUR, real_start_time, real_end_time)), 0) as total_duration
            FROM gxt_repair_order
            WHERE 1=1
                <if test="params.gxtCenter != null and params.gxtCenter != ''">
                    AND gxt_center = #{params.gxtCenter}
                </if>
                <if test="params.pcsStationName != null and params.pcsStationName != ''">
                    AND pcs_station_name = #{params.pcsStationName}
                </if>
                <if test="params.brand != null and params.brand != ''">
                    AND brand = #{params.brand}
                </if>
                <if test="params.model != null and params.model != ''">
                    AND model = #{params.model}
                </if>
                <if test="params.startTime != null and params.startTime != ''">
                    AND create_time &gt;= #{params.startTime}
                </if>
                <if test="params.endTime != null and params.endTime != ''">
                    AND create_time &lt;= #{params.endTime}
                </if>
            GROUP BY
                <choose>
                    <when test="params.groupFields != null and params.groupFields != ''">
                        <foreach collection="params.groupFields.split(',')" item="field" separator=",">
                            ${field}
                        </foreach>
                    </when>
                    <otherwise>
                        gxt_center
                    </otherwise>
                </choose>
        ) t
        GROUP BY
            <choose>
                <when test="params.groupFields != null and params.groupFields != ''">
                    <foreach collection="params.groupFields.split(',')" item="field" separator=",">
                        t.${field}
                    </foreach>
                </when>
                <otherwise>
                    t.gxt_center
                </otherwise>
            </choose>
        ORDER BY total_count DESC
    </select>

    <!-- 获取所有维保中心 -->
    <select id="selectAllGxtCenters" resultType="String">
        SELECT DISTINCT gxt_center
        FROM (
            SELECT gxt_center FROM gxt_work_order WHERE gxt_center IS NOT NULL
            UNION
            SELECT gxt_center FROM gxt_repair_order WHERE gxt_center IS NOT NULL
        ) t
        ORDER BY gxt_center
    </select>

    <!-- 获取所有场站 -->
    <select id="selectAllStations" resultType="String">
        SELECT DISTINCT pcs_station_name
        FROM (
            SELECT pcs_station_name FROM gxt_work_order WHERE pcs_station_name IS NOT NULL
            UNION
            SELECT pcs_station_name FROM gxt_repair_order WHERE pcs_station_name IS NOT NULL
        ) t
        ORDER BY pcs_station_name
    </select>

    <!-- 获取所有品牌 -->
    <select id="selectAllBrands" resultType="String">
        SELECT DISTINCT brand
        FROM (
            SELECT brand FROM gxt_work_order WHERE brand IS NOT NULL
            UNION
            SELECT brand FROM gxt_repair_order WHERE brand IS NOT NULL
        ) t
        ORDER BY brand
    </select>

    <!-- 获取所有型号 -->
    <select id="selectAllModels" resultType="String">
        SELECT DISTINCT model
        FROM (
            SELECT model FROM gxt_work_order WHERE model IS NOT NULL
            UNION
            SELECT model FROM gxt_repair_order WHERE model IS NOT NULL
        ) t
        ORDER BY model
    </select>

    <!-- 获取所有维修类型 -->
    <select id="selectAllMaintenanceTypes" resultType="String">
        SELECT DISTINCT maintenance_type
        FROM gxt_repair_order
        WHERE maintenance_type IS NOT NULL
        ORDER BY maintenance_type
    </select>

    <!-- 分组统计维修工单数据（按maintenanceType统计分数） -->
    <select id="selectRepairOrderStatistics" resultType="java.util.HashMap">
        SELECT
            <choose>
                <when test="params.groupFields != null and params.groupFields != ''">
                    <foreach collection="params.groupFields.split(',')" item="field" separator=",">
                        ${field}
                    </foreach>,
                </when>
                <otherwise>
                    gxt_center,
                </otherwise>
            </choose>
            COUNT(*) as repair_order_count,
            maintenance_type,
            COUNT(*) as type_count,
            SUM(COALESCE(score, 0)) as type_score,
            COALESCE(SUM(TIMESTAMPDIFF(HOUR, assign_time, accept_time)), 0) as total_accept_duration,
            COALESCE(SUM(TIMESTAMPDIFF(HOUR, accept_time, real_start_time)), 0) as total_prepare_duration,
            COALESCE(SUM(TIMESTAMPDIFF(HOUR, real_start_time, real_end_time)), 0) as total_operation_duration,
            COALESCE(SUM(TIMESTAMPDIFF(HOUR, assign_time, accept_time)), 0) +
            COALESCE(SUM(TIMESTAMPDIFF(HOUR, accept_time, real_start_time)), 0) +
            COALESCE(SUM(TIMESTAMPDIFF(HOUR, real_start_time, real_end_time)), 0) as total_duration
        FROM gxt_repair_order
        WHERE 1=1
            <if test="params.gxtCenter != null and params.gxtCenter != ''">
                AND gxt_center = #{params.gxtCenter}
            </if>
            <if test="params.pcsStationName != null and params.pcsStationName != ''">
                AND pcs_station_name = #{params.pcsStationName}
            </if>
            <if test="params.brand != null and params.brand != ''">
                AND brand = #{params.brand}
            </if>
            <if test="params.model != null and params.model != ''">
                AND model = #{params.model}
            </if>
            <if test="params.startTime != null and params.startTime != ''">
                AND create_time &gt;= #{params.startTime}
            </if>
            <if test="params.endTime != null and params.endTime != ''">
                AND create_time &lt;= #{params.endTime}
            </if>
        GROUP BY
            <choose>
                <when test="params.groupFields != null and params.groupFields != ''">
                    <foreach collection="params.groupFields.split(',')" item="field" separator=",">
                        ${field}
                    </foreach>,
                </when>
                <otherwise>
                    gxt_center,
                </otherwise>
            </choose>
            maintenance_type
        ORDER BY
            <choose>
                <when test="params.groupFields != null and params.groupFields != ''">
                    <foreach collection="params.groupFields.split(',')" item="field" separator=",">
                        ${field}
                    </foreach>,
                </when>
                <otherwise>
                    gxt_center,
                </otherwise>
            </choose>
            maintenance_type
    </select>

    <!-- 分组统计维保运行时间分数 -->
    <select id="selectRuntimeScoreStatistics" resultType="java.util.HashMap">
        SELECT
            <choose>
                <when test="params.groupFields != null and params.groupFields != ''">
                    <foreach collection="params.groupFields.split(',')" item="field" separator=",">
                        t.${field}
                    </foreach>,
                </when>
                <otherwise>
                    t.gxt_center,
                </otherwise>
            </choose>
            SUM(t.under_10_hours_score) as under_10_hours_score,
            SUM(t.range_10_240_score) as range_10_240_score,
            SUM(t.range_240_270_score) as range_240_270_score,
            SUM(t.range_270_300_score) as range_270_300_score,
            SUM(t.over_300_score) as over_300_score,
            SUM(t.total_score) as total_score,
            COUNT(t.device_id) as device_count
        FROM (
            SELECT
                <choose>
                    <when test="params.groupFields != null and params.groupFields != ''">
                        <foreach collection="params.groupFields.split(',')" item="field" separator=",">
                            w.${field}
                        </foreach>,
                    </when>
                    <otherwise>
                        w.gxt_center,
                    </otherwise>
                </choose>
                w.pcs_device_id as device_id,
                w.pcs_device_name as device_name,
                MAX(r.restart_time) as last_restart_time,
                TIMESTAMPDIFF(HOUR, MAX(r.restart_time), NOW()) as downtime_hours,
                CASE
                    WHEN TIMESTAMPDIFF(HOUR, MAX(r.restart_time), NOW()) &lt; 10 THEN COALESCE(SUM(w.score), 0)
                    ELSE 0
                END as under_10_hours_score,
                CASE
                    WHEN TIMESTAMPDIFF(HOUR, MAX(r.restart_time), NOW()) &gt;= 10 AND TIMESTAMPDIFF(HOUR, MAX(r.restart_time), NOW()) &lt; 240 THEN COALESCE(SUM(w.score), 0)
                    ELSE 0
                END as range_10_240_score,
                CASE
                    WHEN TIMESTAMPDIFF(HOUR, MAX(r.restart_time), NOW()) &gt;= 240 AND TIMESTAMPDIFF(HOUR, MAX(r.restart_time), NOW()) &lt; 270 THEN COALESCE(SUM(w.score), 0)
                    ELSE 0
                END as range_240_270_score,
                CASE
                    WHEN TIMESTAMPDIFF(HOUR, MAX(r.restart_time), NOW()) &gt;= 270 AND TIMESTAMPDIFF(HOUR, MAX(r.restart_time), NOW()) &lt; 300 THEN COALESCE(SUM(w.score), 0)
                    ELSE 0
                END as range_270_300_score,
                CASE
                    WHEN TIMESTAMPDIFF(HOUR, MAX(r.restart_time), NOW()) &gt;= 300 THEN COALESCE(SUM(w.score), 0)
                    ELSE 0
                END as over_300_score,
                COALESCE(SUM(w.score), 0) as total_score
            FROM gxt_work_order w
            LEFT JOIN gxt_repair_order r ON w.pcs_device_id = r.pcs_device_id AND r.restart_time IS NOT NULL
            WHERE w.create_time &gt;= DATE_SUB(NOW(), INTERVAL 1 MONTH)
                AND w.pcs_device_id IS NOT NULL
                <if test="params.gxtCenter != null and params.gxtCenter != ''">
                    AND w.gxt_center = #{params.gxtCenter}
                </if>
                <if test="params.pcsStationName != null and params.pcsStationName != ''">
                    AND w.pcs_station_name = #{params.pcsStationName}
                </if>
                <if test="params.brand != null and params.brand != ''">
                    AND w.brand = #{params.brand}
                </if>
                <if test="params.model != null and params.model != ''">
                    AND w.model = #{params.model}
                </if>
            GROUP BY
                <choose>
                    <when test="params.groupFields != null and params.groupFields != ''">
                        <foreach collection="params.groupFields.split(',')" item="field" separator=",">
                            w.${field}
                        </foreach>,
                    </when>
                    <otherwise>
                        w.gxt_center,
                    </otherwise>
                </choose>
                w.pcs_device_id, w.pcs_device_name
        ) t
        GROUP BY
            <choose>
                <when test="params.groupFields != null and params.groupFields != ''">
                    <foreach collection="params.groupFields.split(',')" item="field" separator=",">
                        t.${field}
                    </foreach>
                </when>
                <otherwise>
                    t.gxt_center
                </otherwise>
            </choose>
        ORDER BY total_score DESC
    </select>

</mapper>
