package com.hdkj.lt.bf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdkj.lt.bf.aspect.RequiresCountryPermissions;
import com.hdkj.lt.bf.entity.dto.AbnormalContrastInfoDTO;
import com.hdkj.lt.bf.entity.dto.FhzgAbnormalityWithVolVO;
import com.hdkj.lt.bf.entity.dto.OperationAbnormalityQueryDTO;
import com.hdkj.lt.bf.entity.vo.BreakerAreaVO;
import com.hdkj.lt.bf.entity.vo.FhzgAwaitTransferPathDetailVO;
import com.hdkj.lt.bf.entity.vo.FhzgGridOperationAbnormalityVO;
import com.hdkj.lt.bf.service.FhzgOperationAbnormalityService;
import com.hdkj.lt.core.bizms.modle.dto.FhzgGridOperationAbnormalityDTO;
import com.hdkj.lt.core.bizms.modle.po.operationAbnormality.FhzgOperationAbnormalityLineLoadfactorLoss;
import com.hdkj.lt.core.bizms.modle.po.operationAbnormality.FhzgOperationAbnormalitySwitchingSequence;
import com.hdkj.lt.core.bizms.modle.po.operationAbnormality.FhzgOperationAbnormalityVoltageViolation;
import com.hdkj.lt.core.mvc.BaseController;
import com.hdkj.lt.modle.dto.abnormal.SwitchDetailDTO;
import com.hdkj.hussar.ApiResponse;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 运行异常转供
 *
 * @author lihao
 * @since 2025-07-05
 */
@RestController
@RequestMapping("/fhzgOperationAbnormality")
@RequiredArgsConstructor
public class FhzgOperationAbnormalityController extends BaseController {

    private final FhzgOperationAbnormalityService fhzgOperationAbnormalityService;

    /**
     * 获取运行异常转供列表
     *
     * @param queryDTO
     * @return
     */
    @PostMapping("/getOperationAbnormalityList")
    @ApiOperation(value = "获取运行异常转供列表")
    public ApiResponse<Page<FhzgGridOperationAbnormalityVO>> getOperationAbnormalityList(@ApiParam("查看运行异常转供dto") @RequestBody FhzgGridOperationAbnormalityDTO queryDTO) {
        return ApiResponse.success(fhzgOperationAbnormalityService.getOperationAbnormalityList(queryDTO));
    }

    @PostMapping("/getAllOperationAbnormalityList")
    @ApiOperation(value = "获取运行异常转供列表(含成片电压越限)")
    public ApiResponse<Page<FhzgAbnormalityWithVolVO>> getOperationAbnormalityListNew(@ApiParam("查看运行异常转供dto") @RequestBody FhzgGridOperationAbnormalityDTO queryDTO) {
        return ApiResponse.success(fhzgOperationAbnormalityService.getOperationAbnormalityListNew(queryDTO));
    }

    /**
     * 获取转供路径明细
     *
     * @param oaId
     * @return
     */
    @GetMapping("/getAwaitTransferPathDetail")
    @ApiOperation(value = "通过事件ID查询获取转供路径明细")
    public ApiResponse<List<FhzgAwaitTransferPathDetailVO>> getAwaitTransferPathDetail(String oaId) {
        if (StringUtils.isEmpty(oaId)) {
            return fail("参数不能为空");
        }
        return ApiResponse.success(fhzgOperationAbnormalityService.getAwaitTransferPathDetail(oaId));
    }

    /**
     * 获取开关动作顺序
     *
     * @param queryDTO
     * @return
     */
    @PostMapping("/getSwitchingSequence")
    @ApiOperation(value = "通过事件ID查询开关动作顺序")
    public ApiResponse<Page<FhzgOperationAbnormalitySwitchingSequence>> getSwitchingSequence(@RequestBody FhzgGridOperationAbnormalityDTO queryDTO) {
        if (ObjectUtils.isEmpty(queryDTO)) {
            return fail("参数不能为空");
        }
        return ApiResponse.success(fhzgOperationAbnormalityService.getSwitchingSequence(queryDTO));
    }

    /**
     * 获取线路负载率和线路损耗
     *
     * @param queryDTO
     * @return
     */
    @PostMapping("/getLineLoadfactorLoss")
    @ApiOperation(value = "通过事件ID查询线路负载率和线路损耗")
    public ApiResponse<Page<FhzgOperationAbnormalityLineLoadfactorLoss>> getLineLoadfactorLoss(@RequestBody FhzgGridOperationAbnormalityDTO queryDTO) {
        if (ObjectUtils.isEmpty(queryDTO)) {
            return fail("参数不能为空");
        }
        return ApiResponse.success(fhzgOperationAbnormalityService.getLineLoadfactorLoss(queryDTO));
    }

    /**
     * 获取配电电压越限
     *
     * @param queryDTO
     * @return
     */
    @PostMapping("/getVoltageViolation")
    @ApiOperation(value = "通过事件ID查询配电电压越限")
    public ApiResponse<Page<FhzgOperationAbnormalityVoltageViolation>> getVoltageViolation(@RequestBody FhzgGridOperationAbnormalityDTO queryDTO) {
        if (ObjectUtils.isEmpty(queryDTO)) {
            return fail("参数不能为空");
        }
        return ApiResponse.success(fhzgOperationAbnormalityService.getVoltageViolation(queryDTO));
    }


    /**
     * 转供路径查询开关段
     */
    @PostMapping("/getPathOverviewSwitch")
    @ApiOperation(value = "通过事件ID查询开关段")
    public ApiResponse<List<BreakerAreaVO>> getPathOverviewSwitch(@RequestBody OperationAbnormalityQueryDTO request) {
        Boolean assertFlag = ObjectUtils.isNotEmpty(request) &&
                (ObjectUtils.isNotEmpty(request.getOaId()) || ObjectUtils.isNotEmpty(request.getSchemeId())) &&
                StringUtils.isNotEmpty(request.getFeederId());
        Assert.isTrue(assertFlag, "线路ID和运行异常场景ID不允许为空!");
        return ApiResponse.success(fhzgOperationAbnormalityService.getPathOverviewSwitch(request));
    }

    @PostMapping("/getDefaultOverviewSwitch")
    @ApiOperation(value = "通过事件ID查询默认态开关段")
    @RequiresCountryPermissions(lineFields = "feederId")
    public ApiResponse<List<BreakerAreaVO>> getDefaultOverviewSwitch(@RequestBody OperationAbnormalityQueryDTO request) {
        Boolean assertFlag = ObjectUtils.isNotEmpty(request) && StringUtils.isNotEmpty(request.getFeederId());
        Assert.isTrue(assertFlag, "线路ID不允许为空!");
        return ApiResponse.success(fhzgOperationAbnormalityService.getDefaultOverviewSwitch(request));
    }


    /**
     * 获取运行异常转供前后对比数据
     *
     * @param request
     * @return
     */
    @PostMapping("/getContrastInfo")
    @ApiOperation(value = "获取运行异常转供前后对比数据")
    public ApiResponse<AbnormalContrastInfoDTO> getContrastInfo(@RequestBody OperationAbnormalityQueryDTO request) {
        Boolean assertFlag = ObjectUtils.isNotEmpty(request) && StringUtils.isNotEmpty(request.getFeederId());
        Assert.isTrue(assertFlag, "线路ID不允许为空!");
        return ApiResponse.success(fhzgOperationAbnormalityService.getContrastInfo(request));
    }

    /**
     * 获取运行调优pqui前后数据对比
     *
     * @param request
     * @return
     */
    @PostMapping("/getSvgPquiDataAbnormal")
    @ApiOperation(value = "获取运行调优pqui前后数据对比")
    public ApiResponse<List<SwitchDetailDTO>> getSvgPquiDataAbnormal(@RequestBody OperationAbnormalityQueryDTO request) {
        Boolean assertFlag = ObjectUtils.isNotEmpty(request) && request.getOaId() != null;
        Assert.isTrue(assertFlag, "运行调优ID不允许为空!");
        return ApiResponse.success(fhzgOperationAbnormalityService.getSvgPquiDataAbnormal(request));
    }

}
