package com.usky.ems.controller.web;

import com.usky.common.core.bean.ApiResult;
import com.usky.ems.service.EmsAnalysisService;
import com.usky.ems.service.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 能耗分析接口
 * 趋势、指标、分类占比、区域分析、对比分析
 * 区域与面积相关统计在 {@link com.usky.ems.service.impl.EmsAnalysisServiceImpl} 中基于
 * base_space、base_area、base_space_area、base_space_build 等表查询。
 * 基础路径前缀：/analysis（网关再加 /prod-api/service-ems）
 */
@RestController
@RequestMapping("/analysis")
public class EmsAnalysisController {

    @Autowired
    private EmsAnalysisService emsAnalysisService;

    /**
     * 获取当前日期的默认值
     */
    private String getCurrentDefaultTimeDimension() {
        return "D"; // 按日统计
    }

    /**
     * 获取当前日期的默认值（根据时间维度返回对应格式）
     */
    private String getCurrentDefaultTimeValue(String timeDimension) {
        java.time.LocalDate now = java.time.LocalDate.now();
        int year = now.getYear();
        int month = now.getMonthValue();
        int day = now.getDayOfMonth();
        if ("D".equals(timeDimension)) {
            // 按日：返回年月日格式，如 2026-03-09
            return year + "-" + (month < 10 ? "0" : "") + month + "-" + (day < 10 ? "0" : "") + day;
        } else if ("M".equals(timeDimension)) {
            // 按月：返回年月格式，如 2026-03
            return year + "-" + (month < 10 ? "0" : "") + month;
        } else {
            // 按年：返回年格式，如 2026
            return String.valueOf(year);
        }
    }

    @GetMapping("/trend")
    public ApiResult<EmsTrendResponse> getTrend(
            @RequestParam(required = false) Long projectId,
            @RequestParam(required = false) String timeDimension,
            @RequestParam(required = false) String timeValue,
            @RequestParam(required = false) Long energyTypeId) {
        if (timeDimension == null) timeDimension = getCurrentDefaultTimeDimension();
        if (timeValue == null) timeValue = getCurrentDefaultTimeValue(timeDimension);
        return ApiResult.success(emsAnalysisService.getTrend(projectId, timeDimension, timeValue, energyTypeId));
    }

    @GetMapping("/trend/indicators")
    public ApiResult<EmsTrendIndicatorsResponse> getTrendIndicators(
            @RequestParam(required = false) Long projectId,
            @RequestParam(required = false) String timeDimension,
            @RequestParam(required = false) String timeValue,
            @RequestParam(required = false) Long energyTypeId) {
        if (timeDimension == null) timeDimension = getCurrentDefaultTimeDimension();
        if (timeValue == null) timeValue = getCurrentDefaultTimeValue(timeDimension);
        return ApiResult.success(emsAnalysisService.getTrendIndicators(projectId, timeDimension, timeValue, energyTypeId));
    }

    @GetMapping("/trend/category")
    public ApiResult<EmsTrendCategoryResponse> getTrendCategory(
            @RequestParam(required = false) Long projectId,
            @RequestParam(required = false) String timeDimension,
            @RequestParam(required = false) String timeValue,
            @RequestParam(required = false) Long energyTypeId) {
        if (timeDimension == null) timeDimension = getCurrentDefaultTimeDimension();
        if (timeValue == null) timeValue = getCurrentDefaultTimeValue(timeDimension);
        return ApiResult.success(emsAnalysisService.getTrendCategory(projectId, timeDimension, timeValue, energyTypeId));
    }

    /**
     * 区域能耗时序分析
     * 按分项编码与区域列表，查询各区域在时间轴上的能耗数据
     */
    @PostMapping("/region")
    public ApiResult<EmsAverageResponseVO> getRegionAnalysis(@RequestBody EmsAverageRequest request) {
        return ApiResult.success(emsAnalysisService.getRegionalAnalysis(request));
    }

    @PostMapping("/compare")
    public ApiResult<EmsCompareResponse> getCompare(@RequestBody EmsCompareRequest request) {
        return ApiResult.success(emsAnalysisService.getCompare(request));
    }

    @GetMapping("/average")
    public ApiResult<EmsAverageResponse> getAverage(
            @RequestParam(required = false) Long projectId,
            @RequestParam(required = false) String timeDimension,
            @RequestParam(required = false) String timeValue,
            @RequestParam(required = false) Long energyTypeId) {
        // 设置默认值：如果没有提供参数，使用默认的项目和能源类型
        if (projectId == null) projectId = 1L;  // 默认项目ID
        if (energyTypeId == null) energyTypeId = 1L;  // 默认电能源类型
        if (timeDimension == null) timeDimension = getCurrentDefaultTimeDimension();
        if (timeValue == null) timeValue = getCurrentDefaultTimeValue(timeDimension);
        return ApiResult.success(emsAnalysisService.getAverage(projectId, timeDimension, timeValue, energyTypeId));
    }
}