<?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.aegis.resource.dashboard.mapper.ResourceDashboardMapper">

    <!-- 队伍统计 -->
    <select id="countTeamByStatus" resultType="java.util.Map">
        SELECT 
            COUNT(*) as total,
            COALESCE(SUM(CASE WHEN status = 'available' THEN 1 ELSE 0 END), 0) as available,
            COALESCE(SUM(CASE WHEN status = 'busy' THEN 1 ELSE 0 END), 0) as busy,
            COALESCE(SUM(CASE WHEN status = 'offline' THEN 1 ELSE 0 END), 0) as offline
        FROM resource_team
        WHERE del_flag = '0'
    </select>

    <!-- 车辆统计 -->
    <select id="countVehicleByStatus" resultType="java.util.Map">
        SELECT 
            COUNT(*) as total,
            COALESCE(SUM(CASE WHEN status = 'available' THEN 1 ELSE 0 END), 0) as available,
            COALESCE(SUM(CASE WHEN status = 'busy' THEN 1 ELSE 0 END), 0) as busy,
            COALESCE(SUM(CASE WHEN status = 'repair' THEN 1 ELSE 0 END), 0) as repair
        FROM resource_vehicle
        WHERE del_flag = '0'
    </select>

    <!-- 专家统计 -->
    <select id="countExpertByStatus" resultType="java.util.Map">
        SELECT 
            COUNT(*) as total,
            COALESCE(SUM(CASE WHEN status = 'active' THEN 1 ELSE 0 END), 0) as active,
            COALESCE(SUM(CASE WHEN status = 'inactive' THEN 1 ELSE 0 END), 0) as inactive,
            COALESCE(SUM(CASE WHEN status = 'blacklisted' THEN 1 ELSE 0 END), 0) as blacklisted
        FROM resource_expert
        WHERE del_flag = '0'
    </select>

    <!-- 装备统计 -->
    <select id="countEquipmentByStatus" resultType="java.util.Map">
        SELECT 
            COUNT(*) as total,
            COALESCE(SUM(CASE WHEN status = 'available' THEN 1 ELSE 0 END), 0) as available,
            COALESCE(SUM(CASE WHEN status = 'busy' THEN 1 ELSE 0 END), 0) as busy,
            COALESCE(SUM(CASE WHEN status = 'repair' THEN 1 ELSE 0 END), 0) as repair,
            COALESCE(SUM(CASE WHEN status = 'scrap' THEN 1 ELSE 0 END), 0) as scrap
        FROM resource_equipment
        WHERE del_flag = '0'
    </select>

    <!-- 物资分类统计 -->
    <select id="countMaterialCategory" resultType="java.lang.Integer">
        SELECT COUNT(*) 
        FROM resource_material_category
        WHERE del_flag = '0'
    </select>

    <!-- 物资统计 -->
    <select id="countMaterial" resultType="java.lang.Integer">
        SELECT COUNT(*) 
        FROM resource_material
        WHERE del_flag = '0'
    </select>

    <!-- 采购统计 -->
    <select id="countProcurementByStatus" resultType="java.util.Map">
        SELECT 
            COUNT(*) as total,
            COALESCE(SUM(CASE WHEN status = 'draft' THEN 1 ELSE 0 END), 0) as draft,
            COALESCE(SUM(CASE WHEN status = 'submitted' THEN 1 ELSE 0 END), 0) as submitted,
            COALESCE(SUM(CASE WHEN status = 'approved' THEN 1 ELSE 0 END), 0) as approved,
            COALESCE(SUM(CASE WHEN status = 'rejected' THEN 1 ELSE 0 END), 0) as rejected,
            COALESCE(SUM(CASE WHEN status = 'in_progress' THEN 1 ELSE 0 END), 0) as inProgress,
            COALESCE(SUM(CASE WHEN status = 'received' THEN 1 ELSE 0 END), 0) as received
        FROM resource_procurement
        WHERE del_flag = '0'
    </select>

    <!-- 征用统计 -->
    <select id="countRequisitionByStatus" resultType="java.util.Map">
        SELECT 
            COUNT(*) as total,
            COALESCE(SUM(CASE WHEN status = 'draft' THEN 1 ELSE 0 END), 0) as draft,
            COALESCE(SUM(CASE WHEN status = 'approved' THEN 1 ELSE 0 END), 0) as approved,
            COALESCE(SUM(CASE WHEN status = 'rejected' THEN 1 ELSE 0 END), 0) as rejected,
            COALESCE(SUM(CASE WHEN status = 'active' THEN 1 ELSE 0 END), 0) as active,
            COALESCE(SUM(CASE WHEN status = 'ended' THEN 1 ELSE 0 END), 0) as ended
        FROM resource_requisition
        WHERE del_flag = '0'
    </select>

    <!-- 库存预警列表 -->
    <resultMap type="com.aegis.resource.dashboard.vo.InventoryAlertVO" id="InventoryAlertResult">
        <result property="materialId" column="material_id" />
        <result property="materialName" column="material_name" />
        <result property="materialCode" column="material_code" />
        <result property="warehouseId" column="warehouse_id" />
        <result property="warehouseName" column="warehouse_name" />
        <result property="currentQty" column="current_qty" />
        <result property="threshold" column="threshold" />
        <result property="alertLevel" column="alert_level" />
    </resultMap>

    <select id="selectInventoryAlerts" resultMap="InventoryAlertResult">
        SELECT 
            mi.material_id,
            m.material_name,
            m.material_code,
            mi.warehouse_id,
            w.warehouse_name,
            mi.available_qty as current_qty,
            m.threshold,
            CASE 
                WHEN mi.available_qty &lt;= m.threshold * 0.5 THEN 'critical'
                WHEN mi.available_qty &lt; m.threshold THEN 'warning'
                ELSE 'normal'
            END as alert_level
        FROM resource_material_inventory mi
        INNER JOIN resource_material m ON mi.material_id = m.id
        INNER JOIN resource_warehouse w ON mi.warehouse_id = w.id
        WHERE mi.available_qty &lt; m.threshold
          AND m.del_flag = '0'
          AND w.del_flag = '0'
          AND m.threshold IS NOT NULL
          AND m.threshold > 0
        ORDER BY mi.available_qty ASC, m.material_name ASC
    </select>

    <!-- 物资消耗趋势 -->
    <select id="selectMaterialConsumeTrend" resultType="java.util.Map">
        SELECT 
            DATE(created_at) as date,
            COALESCE(SUM(qty), 0) as count
        FROM resource_material_usage
        WHERE action = 'consume'
          AND created_at >= DATE_SUB(CURDATE(), INTERVAL #{days} DAY)
        GROUP BY DATE(created_at)
        ORDER BY date ASC
    </select>

    <!-- 物资归还趋势 -->
    <select id="selectMaterialReturnTrend" resultType="java.util.Map">
        SELECT 
            DATE(created_at) as date,
            COALESCE(SUM(qty), 0) as count
        FROM resource_material_usage
        WHERE action = 'return'
          AND created_at >= DATE_SUB(CURDATE(), INTERVAL #{days} DAY)
        GROUP BY DATE(created_at)
        ORDER BY date ASC
    </select>

    <!-- 装备分配趋势 -->
    <select id="selectEquipmentAssignTrend" resultType="java.util.Map">
        SELECT 
            DATE(created_at) as date,
            COUNT(*) as count
        FROM resource_equipment_log
        WHERE action = 'assign'
          AND created_at >= DATE_SUB(CURDATE(), INTERVAL #{days} DAY)
        GROUP BY DATE(created_at)
        ORDER BY date ASC
    </select>

    <!-- 装备释放趋势 -->
    <select id="selectEquipmentReleaseTrend" resultType="java.util.Map">
        SELECT 
            DATE(created_at) as date,
            COUNT(*) as count
        FROM resource_equipment_log
        WHERE action = 'release'
          AND created_at >= DATE_SUB(CURDATE(), INTERVAL #{days} DAY)
        GROUP BY DATE(created_at)
        ORDER BY date ASC
    </select>

</mapper>