package com.hdkj.lt.bf.controller.optimization;

import com.hdkj.hussar.ApiResponse;
import com.hdkj.lt.bf.entity.vo.IndicatorDashboardVO;
import com.hdkj.lt.bf.entity.vo.PageResult;
import com.hdkj.lt.bf.entity.vo.SeCapacityDashboardVO;
import com.hdkj.lt.bf.entity.vo.FeederSelectVO;
import com.hdkj.lt.bf.entity.vo.SeAlarmDetailVO;
import com.hdkj.lt.bf.entity.vo.SeAlarmListVO;
import com.hdkj.lt.bf.entity.vo.SeOutageDashboardVO;
import com.hdkj.lt.bf.entity.vo.SeVoltageDashboardVO;
import com.hdkj.lt.bf.service.IndicatorDashboardService;
import com.hdkj.lt.bf.service.SeAlarmService;
import com.hdkj.lt.bf.service.SeIndicatorService;
import com.hdkj.lt.core.mvc.BaseController;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 运行态指标 仪表盘 Controller
 * <p>
 * POST /indicator/dashboard — 返回综合态势仪表盘数据
 *
 * @author lsl
 * @since 2026-07-21
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/indicator")
public class IndicatorController extends BaseController {

    private final IndicatorDashboardService indicatorDashboardService;
    private final SeIndicatorService seIndicatorService;
    private final SeAlarmService seAlarmService;

    /**
     * 查询运行态仪表盘
     *
     * @param params {id: 节点ID, type: 节点类型(2/3/4)}
     */
//    @PostMapping("/dashboard")
//    public ApiResponse<IndicatorDashboardVO> dashboard(@RequestBody Map<String, String> params) {
//        String id = params.get("id");
//        String type = params.get("type");
//        IndicatorDashboardVO vo = indicatorDashboardService.queryDashboard(id, type);
//        if (vo == null) {
//            return ApiResponse.fail("参数不合法或无数据");
//        }
//        return ApiResponse.success(vo);
//    }

    /**
     * 停电时户数（无时间维度切换）
     *
     * @param params {id, type(2/3/4)}
     */
    @PostMapping("/outage")
    public ApiResponse<SeOutageDashboardVO> outage(@RequestBody Map<String, String> params) {
        String id = params.get("id");
        String type = params.get("type");
        if (id == null || type == null) {
            return ApiResponse.fail("参数缺失");
        }
        return ApiResponse.success(seIndicatorService.queryOutage(id, Integer.parseInt(type)));
    }

    /**
     * 电压质量（today/month/year）
     *
     * @param params {id, type(2/3/4), timeScope(today/month/year)}
     */
    @PostMapping("/voltage")
    public ApiResponse<SeVoltageDashboardVO> voltage(@RequestBody Map<String, String> params) {
        String id = params.get("id");
        String type = params.get("type");
        String timeScope = params.get("timeScope");
        if (id == null || type == null || timeScope == null) {
            return ApiResponse.fail("参数缺失");
        }
        return ApiResponse.success(seIndicatorService.queryVoltage(id, Integer.parseInt(type), timeScope));
    }

    /**
     * 供电能力（today/month/year）
     *
     * @param params {id, type(2/3/4), timeScope(today/month/year)}
     */
    @PostMapping("/capacity")
    public ApiResponse<SeCapacityDashboardVO> capacity(@RequestBody Map<String, String> params) {
        String id = params.get("id");
        String type = params.get("type");
        String timeScope = params.get("timeScope");
        if (id == null || type == null || timeScope == null) {
            return ApiResponse.fail("参数缺失");
        }
        return ApiResponse.success(seIndicatorService.queryCapacity(id, Integer.parseInt(type), timeScope));
    }

    /**
     * 异常告警-线路重过载列表
     *
     * @param params {id, type(2/3/4), feederName(可选), date(可选), orderBy(asc/desc,可选)}
     */
    @PostMapping("/alarm/overload")
    public ApiResponse<PageResult<SeAlarmListVO>> alarmOverload(@RequestBody Map<String, String> params) {
        String id = params.get("id");
        String type = params.get("type");
        String feederName = params.get("feederName");
        String date = params.get("date");
        String orderBy = params.get("orderBy");
        String pageStr = params.get("page");
        String pageSizeStr = params.get("pageSize");
        if (id == null || type == null) {
            return ApiResponse.fail("参数缺失");
        }
        Integer page = pageStr != null ? Integer.parseInt(pageStr) : 1;
        Integer pageSize = pageSizeStr != null ? Integer.parseInt(pageSizeStr) : 20;
        return ApiResponse.success(seAlarmService.queryOverloadAlarmList(id, Integer.parseInt(type), feederName, date, orderBy, page, pageSize));
    }

    /**
     * 异常告警-电压越限列表
     *
     * @param params {id, type(2/3/4), feederName(可选), date(可选), orderBy(asc/desc,可选)}
     */
    @PostMapping("/alarm/voltage")
    public ApiResponse<PageResult<SeAlarmListVO>> alarmVoltage(@RequestBody Map<String, String> params) {
        String id = params.get("id");
        String type = params.get("type");
        String feederName = params.get("feederName");
        String date = params.get("date");
        String orderBy = params.get("orderBy");
        String pageStr = params.get("page");
        String pageSizeStr = params.get("pageSize");
        if (id == null || type == null) {
            return ApiResponse.fail("参数缺失");
        }
        Integer page = pageStr != null ? Integer.parseInt(pageStr) : 1;
        Integer pageSize = pageSizeStr != null ? Integer.parseInt(pageSizeStr) : 20;
        return ApiResponse.success(seAlarmService.queryVoltageAlarmList(id, Integer.parseInt(type), feederName, date, orderBy, page, pageSize));
    }

    /**
     * 运行异常信息详情
     *
     * @param params {alarmEventId}
     */
    @PostMapping("/alarm/detail")
    public ApiResponse<SeAlarmDetailVO> alarmDetail(@RequestBody Map<String, String> params) {
        String alarmEventId = params.get("alarmEventId");
        if (alarmEventId == null) {
            return ApiResponse.fail("参数缺失");
        }
        return ApiResponse.success(seAlarmService.queryAlarmDetail(Long.parseLong(alarmEventId)));
    }

    /**
     * 线路名称下拉列表
     *
     * @param params {id, type(2/3/4)}
     */
    @PostMapping("/alarm/feederList")
    public ApiResponse<java.util.List<FeederSelectVO>> feederList(@RequestBody Map<String, String> params) {
        String id = params.get("id");
        String type = params.get("type");
        if (id == null || type == null) {
            return ApiResponse.fail("参数缺失");
        }
        return ApiResponse.success(seAlarmService.queryFeederList(id, Integer.parseInt(type)));
    }
}
