package com.ruoyi.fire.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.fire.domain.FireFacilityReport;
import com.ruoyi.fire.service.IFireFacilityReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;
import java.util.HashMap;

@RestController
@RequestMapping("/fire/facility-report")
public class FireFacilityReportController extends BaseController {

    @Autowired
    private IFireFacilityReportService fireFacilityReportService;

    /**
     * 按设施类型筛选统计数据
     */
    @GetMapping("/report")
    public AjaxResult getReport(@RequestParam(required = false) String facilityType) {
        return AjaxResult.success(fireFacilityReportService.getReportData(facilityType));
    }

    /**
     * 获取指定状态+设施类型的详情数据
     */
    @GetMapping("/detail")
    public AjaxResult getDetail(
            @RequestParam Integer status,
            @RequestParam(required = false) String facilityType,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {

        FireFacilityReport query = new FireFacilityReport();
        query.setFacilityType(facilityType);

        // 1. 查询所有数据
        List<FireFacilityReport> facilityList = fireFacilityReportService.selectFireFacilityReportList(query);

        // 2. 筛选出所有符合状态条件的数据
        List<FireFacilityReport> filteredList = facilityList.stream()
                .map(facility -> {
                    Integer dynamicStatus = fireFacilityReportService.calculateDynamicStatus(
                            facility.getExpiryDate(),
                            facility.getStatus()
                    );
                    facility.setDynamicStatus(dynamicStatus);
                    facility.setStatusText(fireFacilityReportService.getStatusText(dynamicStatus));
                    return facility;
                })
                .filter(facility -> Objects.equals(facility.getDynamicStatus(), status))
                .collect(Collectors.toList());

        // 3. 计算总条数（关键：用于前端分页控件）
        int total = filteredList.size();

        // 4. 执行分页逻辑
        int start = (pageNum - 1) * pageSize;
        // 处理边界情况：当start超过列表长度时，返回空列表
        if (start >= filteredList.size()) {
            Map<String, Object> result = new HashMap<>();
            result.put("list", Collections.emptyList());
            result.put("total", total);
            return AjaxResult.success(result);
        }

        int end = Math.min(start + pageSize, filteredList.size());
        List<FireFacilityReport> pagedList = filteredList.subList(start, end);

        // 5. 返回分页数据和总条数
        Map<String, Object> result = new HashMap<>();
        result.put("list", pagedList);
        result.put("total", total);
        return AjaxResult.success(result);
    }

    /**
     * 获取所有状态的设施数据（支持按类型筛选）
     */
    @GetMapping("/all")
    public AjaxResult getAllData(@RequestParam(required = false) String facilityType) {
        FireFacilityReport query = new FireFacilityReport();
        query.setFacilityType(facilityType);

        List<FireFacilityReport> list = fireFacilityReportService.selectFireFacilityReportList(query);
        list.forEach(facility -> {
            Integer dynamicStatus = fireFacilityReportService.calculateDynamicStatus(
                    facility.getExpiryDate(),
                    facility.getStatus()
            );
            facility.setDynamicStatus(dynamicStatus);
            facility.setStatusText(fireFacilityReportService.getStatusText(dynamicStatus));
        });
        return AjaxResult.success(list);
    }
}
