package com.aegis.web.controller.resource.dashboard;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.aegis.common.annotation.Log;
import com.aegis.common.core.controller.BaseController;
import com.aegis.common.core.domain.AjaxResult;
import com.aegis.common.enums.BusinessType;
import com.aegis.resource.dashboard.service.IResourceDashboardService;
import com.aegis.resource.dashboard.vo.InventoryAlertVO;
import com.aegis.resource.dashboard.vo.ResourceStatsVO;
import com.aegis.resource.dashboard.vo.UsageTrendVO;

/**
 * 资源看板Controller
 * 
 * @author aegis
 */
@RestController
@RequestMapping("/resources/dashboard")
public class ResourceDashboardController extends BaseController {
    
    @Autowired
    private IResourceDashboardService dashboardService;
    
    /**
     * 获取资源总览统计
     */
    @PreAuthorize("@ss.hasPermi('resource:dashboard:view')")
    @GetMapping("/stats")
    public AjaxResult getResourceStats() {
        ResourceStatsVO stats = dashboardService.getResourceStats();
        return success(stats);
    }
    
    /**
     * 获取库存预警列表
     */
    @PreAuthorize("@ss.hasPermi('resource:dashboard:view')")
    @GetMapping("/inventory-alerts")
    public AjaxResult getInventoryAlerts() {
        List<InventoryAlertVO> alerts = dashboardService.getInventoryAlerts();
        Map<String, Object> result = new java.util.HashMap<>();
        result.put("total", alerts != null ? alerts.size() : 0);
        result.put("list", alerts != null ? alerts : new java.util.ArrayList<>());
        return success(result);
    }
    
    /**
     * 获取采购状态统计
     */
    @PreAuthorize("@ss.hasPermi('resource:dashboard:view')")
    @GetMapping("/procurement-status")
    public AjaxResult getProcurementStatus() {
        Map<String, Integer> status = dashboardService.getProcurementStatus();
        return success(status);
    }
    
    /**
     * 获取征用状态统计
     */
    @PreAuthorize("@ss.hasPermi('resource:dashboard:view')")
    @GetMapping("/requisition-status")
    public AjaxResult getRequisitionStatus() {
        Map<String, Integer> status = dashboardService.getRequisitionStatus();
        return success(status);
    }
    
    /**
     * 获取资源状态分布
     */
    @PreAuthorize("@ss.hasPermi('resource:dashboard:view')")
    @GetMapping("/resource-distribution")
    public AjaxResult getResourceDistribution() {
        Map<String, Map<String, Double>> distribution = dashboardService.getResourceDistribution();
        return success(distribution);
    }
    
    /**
     * 获取资源使用趋势
     */
    @PreAuthorize("@ss.hasPermi('resource:dashboard:view')")
    @GetMapping("/usage-trends")
    public AjaxResult getUsageTrends(@RequestParam(value = "days", required = false, defaultValue = "7") Integer days) {
        UsageTrendVO trends = dashboardService.getUsageTrends(days);
        return success(trends);
    }
    
    /**
     * 手动刷新看板缓存
     */
    @PreAuthorize("@ss.hasPermi('resource:dashboard:refresh')")
    @Log(title = "资源看板", businessType = BusinessType.CLEAN)
    @PostMapping("/refresh")
    public AjaxResult refreshDashboard() {
        dashboardService.refreshDashboardCache();
        return success("看板缓存刷新成功");
    }
}