package com.hdkj.lt.mq.controller;

import com.hdkj.hussar.ResultCode;
import com.hdkj.lt.core.mvc.BaseController;
import com.hdkj.lt.mq.entity.vo.FaultPowerCutVO;
import com.hdkj.lt.mq.entity.vo.PlanPowerCutVO;
import com.hdkj.lt.mq.service.base.MessageRetryLogService;
import com.hdkj.lt.mq.service.fault.IAnalysisFaultTransferResultService;
import com.hdkj.lt.mq.service.plan.IAnalysisTransferResultService;
import com.hdkj.hussar.ApiResponse;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * @Description 解析负荷转供结果
 * @Author wcx
 * @Date 2025/5/24 11:20
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("dealData")
@RequiredArgsConstructor
@Validated
public class AnalysisTransferResultController extends BaseController {

    private final IAnalysisTransferResultService analysisTransferResultService;
    private final IAnalysisFaultTransferResultService analysisFaultTransferResultService;
    private final MessageRetryLogService messageRetryLogService;

    @GetMapping("getTransferResultInfo")
    public ApiResponse<PlanPowerCutVO> getTransferAreaResultByPlanId(@NotNull(message = "计划ID不能为空") Long planId) {
        return data(analysisTransferResultService.getTransferAreaResultByPlanId(planId));
    }

    @PostMapping("saveTransferResult")
    public ApiResponse<String> saveTransferAreaResult(@NotNull(message = "计划ID不能为空") Long planId,
                                                      @NotNull(message = "计划ID不能为空") String transferAreaResult) {
        analysisTransferResultService.saveTransferAreaResult(planId, transferAreaResult);
        return data(ResultCode.SUCCESS.getMessage());
    }

    /**
     * 解析推演平台返回计划停电推演结果
     *
     * @param planId
     * @return
     */
    @ApiOperation(value = "解析推演平台返回计划停电推演结果")
    @GetMapping("analysisTransferResult")
    public ApiResponse<String> analysisTransferResult(@NotNull(message = "计划ID不能为空") Long planId) {
        return data(analysisTransferResultService.analysisTransferResult(planId));
    }


    @GetMapping("getFaultTransferResultInfo")
    public ApiResponse<FaultPowerCutVO> getFaultTransferAreaResultByPlanId(@NotNull(message = "故障ID不能为空") Long faultId) {
        return data(analysisFaultTransferResultService.getFaultTransferAreaResultByFaultId(faultId));
    }

    @ApiOperation(value = "解析推演平台返回故障停电推演结果")
    @GetMapping("analysisFaultTransferResult")
    public ApiResponse<String> analysisFaultTransferResult(@NotNull(message = "计划ID不能为空") Long faultId) {
        return data(analysisFaultTransferResultService.analysisFaultTransferResult(faultId));
    }

    /**
     * MQ重试
     */
    @PostMapping("/retry")
    public ApiResponse<String> saveTransferAreaResult(@RequestBody String messageId) {
        try {
            messageRetryLogService.sendByMsgId(messageId);
            return ApiResponse.success(ResultCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.fail("重试失败");
        }
    }


}
