<?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="org.jeecg.modules.stm.metric.mapper.MetricAppSystemReportMapper">

    <!-- 优化说明：
         1. 统一了SQL格式和缩进
         2. 修正了方法名拼写错误(caluculat->calculate)
         3. 优化了SQL语句的可读性
         4. 移除了不必要的子查询
         5. 保持了原有的功能逻辑
         6. 添加了注释说明
    -->

    <!-- 计算应用系统平均运行率 -->
    <select id="calculateAppSystems" resultType="java.util.Map">
        SELECT
            (SUM((B.total_timelen - B.plan_stop_timelen - B.stop_timelen) / B.total_timelen) / COUNT(*)) * 100 AS avgval,
            B.fill_year,
            B.fill_quarter
        FROM metric_app_system A
                 INNER JOIN metric_app_system_report B ON A.id = B.app_sys_id
        GROUP BY B.fill_year, B.fill_quarter
    </select>

    <!-- 计算应用事件总数 -->
    <select id="calculateAppEventCount" resultType="java.util.Map">
        SELECT
            fill_year,
            fill_quarter,
            SUM(event_count) AS cnt
        FROM metric_app_system_report
        GROUP BY fill_year, fill_quarter
        ORDER BY fill_year, fill_quarter
    </select>

    <!-- 计算紧急事件总数 -->
    <select id="calculateAppUrgentCount" resultType="java.util.Map">
        SELECT
            fill_year,
            fill_quarter,
            SUM(urgent_count) AS cnt
        FROM metric_app_system_report
        GROUP BY fill_year, fill_quarter
        ORDER BY fill_year, fill_quarter
    </select>

    <!-- 计算故障事件总数 -->
    <select id="calculateAppFaultCount" resultType="java.util.Map">
        SELECT
            fill_year,
            fill_quarter,
            SUM(fault_count) AS cnt
        FROM metric_app_system_report
        GROUP BY fill_year, fill_quarter
        ORDER BY fill_year, fill_quarter
    </select>

</mapper>