package com.usky.ems.controller.web;

import com.usky.common.core.bean.ApiResult;
import com.usky.ems.service.EmsReportService;
import com.usky.ems.service.EmsModelService;
import com.usky.ems.service.vo.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 统计报表接口
 * 能源报表、区域报表、采集报表
 * 基础路径前缀：/report（网关再加 /prod-api/service-ems）
 */
@RestController
@RequestMapping("/report")
public class EmsReportController {

    @Autowired
    private EmsReportService emsReportService;

    @Autowired
    private EmsModelService emsModelService;

    // ---------- 能源报表 ----------
    @GetMapping("/energy/devices")
    public ApiResult<List<EnergyTypeWrapperProductVO>> getEnergyDevices(
            @RequestParam(required = false) Long energyTypeId,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Long projectId) {
        // TODO: 如需按 energyTypeId/keyword/projectId 过滤，可在 EmsModelService 中扩展方法
        return ApiResult.success(emsModelService.showAssociatedEnergyTypes());
    }

    @PostMapping("/energy/statistics")
    public ApiResult<EmsEnergyStatisticsResponse> getEnergyStatistics(@RequestBody EmsEnergyStatisticsRequest request) {
        return ApiResult.success(emsReportService.getEnergyStatistics(request));
    }

    @GetMapping("/energy/export")
    public void exportEnergy(
            @RequestParam String deviceIds,
            @RequestParam(required = false) String attributePointIds,
            @RequestParam String timeDimension,
            @RequestParam String timeValue,
            @RequestParam(required = false, defaultValue = "excel") String format,
            HttpServletResponse response) {
        emsReportService.exportEnergy(deviceIds, attributePointIds, timeDimension, timeValue, format, response);
    }

    // ---------- 区域报表 ----------
    @GetMapping("/region/devices")
    public ApiResult<EmsReportDevicesResponse> getRegionDevices(
            @RequestParam Long energyTypeId,
            @RequestParam(required = false) Long regionId,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Long projectId) {
        return ApiResult.success(emsReportService.getRegionDevices(energyTypeId, regionId, keyword, projectId));
    }

    @PostMapping("/region/statistics")
    public ApiResult<EmsEnergyStatisticsResponse> getRegionStatistics(
            @RequestBody EmsEnergyStatisticsRequest request,
            @RequestParam(required = false) List<Long> regionIds) {
        return ApiResult.success(emsReportService.getRegionStatistics(request, regionIds));
    }

    @GetMapping("/region/export")
    public void exportRegion(
            @RequestParam String deviceIds,
            @RequestParam(required = false) String attributePointIds,
            @RequestParam String timeDimension,
            @RequestParam String timeValue,
            @RequestParam(required = false) String regionIds,
            @RequestParam(required = false, defaultValue = "excel") String format,
            HttpServletResponse response) {
        emsReportService.exportRegion(deviceIds, attributePointIds, timeDimension, timeValue, regionIds, format, response);
    }

    // ---------- 采集报表 ----------
    @GetMapping("/collection/devices")
    public ApiResult<EmsReportDevicesResponse> getCollectionDevices(
            @RequestParam Long energyTypeId,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Long projectId) {
        return ApiResult.success(emsReportService.getCollectionDevices(energyTypeId, keyword, projectId));
    }

    @PostMapping("/collection/realtime")
    public ApiResult<EmsCollectionRealtimeResponse> getCollectionRealtime(@RequestBody EmsCollectionRealtimeRequest request) {
        return ApiResult.success(emsReportService.getCollectionRealtime(request));
    }

    @PostMapping("/collection/statistics")
    public ApiResult<EmsEnergyStatisticsResponse> getCollectionStatistics(@RequestBody EmsEnergyStatisticsRequest request) {
        return ApiResult.success(emsReportService.getCollectionStatistics(request));
    }

    @GetMapping("/collection/export")
    public void exportCollection(
            @RequestParam String deviceIds,
            @RequestParam(required = false) String attributePointIds,
            @RequestParam String timeDimension,
            @RequestParam String timeValue,
            @RequestParam(required = false, defaultValue = "excel") String format,
            HttpServletResponse response) {
        emsReportService.exportCollection(deviceIds, attributePointIds, timeDimension, timeValue, format, response);
    }

    /**
     * 1. 能源报表
     * 支持按设备列表、功能点、时间类型（时/日/月/年）聚合数据
     *
     * @param request 能源报表请求参数
     * @return 能源报表响应数据
     */
    @PostMapping("/energy")
    @ApiOperation("能源报表")
    public ApiResult<EnergyReportResponse> getEnergyReport(@RequestBody EnergyReportRequest request) {
        return ApiResult.success(emsReportService.getEnergyReport(request));
    }

    /**
     * 能源报表导出 Excel（请求体与 /report/energy 一致）
     */
    @PostMapping("/energy/export")
    @ApiOperation("能源报表导出")
    public void exportEnergyReport(@RequestBody EnergyReportRequest request, HttpServletResponse response) {
        emsReportService.exportEnergyReport(request, response);
    }

    /**
     * 2. 分项报表
     * 支持按空间ID、分项编码、日期类型统计能耗
     *
     * @param request 分项报表请求参数
     * @return 分项报表响应数据
     */
    @PostMapping("/item")
    @ApiOperation("分项报表")
    public ApiResult<ItemReportResponse> getItemReport(@RequestBody ItemReportRequest request) {
        return ApiResult.success(emsReportService.getItemReport(request));
    }

    /**
     * 3. 区域报表
     * 支持按区域、分项、日期类型统计能耗
     *
     * @param request 区域报表请求参数
     * @return 区域报表响应数据
     */
    @PostMapping("/space")
    @ApiOperation("区域报表")
    public ApiResult<SpaceReportResponse> getSpaceReport(@RequestBody SpaceReportRequest request) {
        return ApiResult.success(emsReportService.getSpaceReport(request));
    }

    /**
     * 区域报表导出 Excel（请求体与 /report/space 一致）
     */
    @PostMapping("/space/export")
    @ApiOperation("区域报表导出")
    public void exportSpaceReport(@RequestBody SpaceReportRequest request, HttpServletResponse response) {
        emsReportService.exportSpaceReport(request, response);
    }
}
