package com.hdkj.lt.bf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdkj.lt.modle.dto.fault.RegenerateFaultRequest;
import com.hdkj.lt.bf.entity.FhzgDistCustomer;
import com.hdkj.lt.bf.entity.dto.FhzgDistCustomerCountDTO;
import com.hdkj.lt.bf.entity.dto.HasDataTagDTO;
import com.hdkj.lt.bf.entity.vo.FhzgFaultOppositeLineCarryCapacityVO;
import com.hdkj.lt.bf.entity.vo.FhzgFaultPowerCutVO;
import com.hdkj.lt.bf.service.*;
import com.hdkj.lt.core.bizms.modle.dto.FhzgFaultDistCustomerDTO;
import com.hdkj.lt.core.bizms.modle.po.fault.FaultAwaitTransferSwitchSection;
import com.hdkj.lt.core.bizms.modle.po.fault.FaultPowerCut;
import com.hdkj.lt.core.bizms.modle.vo.FhzgFaultAwaitTransferUsersVO;
import com.hdkj.lt.core.mvc.BaseController;
import com.hdkj.hussar.ApiResponse;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 故障停电转供
 *
 * @author lisonglin
 * @since 2025-05-07
 */
@RestController
@RequestMapping("/fhzgFaultPowerCut")
@RequiredArgsConstructor
public class FhzgFaultPowerCutController extends BaseController {

    private final IFaultPowerCutService faultPowerCutService;

    private final FhzgDistCustomerService distCustomerService;

    private final IFaultAwaitTransferSwitchSectionService awaitTransferSwitchSectionServices;

    private final IFaultAwaitTransferUsersService awaitTransferUsersService;

    private final IFaultOppositeLineCarryCapacityService oppositeLineCarryCapacityService;

    /**
     * 获取故障停电方案列表
     *
     * @param faultPowerCut
     * @return
     */
    @PostMapping("/getSchemeList")
    @ApiOperation(value = "获取故障停电方案列表")
    public ApiResponse<Page<FhzgFaultPowerCutVO>> getSchemeList(@RequestBody FaultPowerCut faultPowerCut) {
        return ApiResponse.success(faultPowerCutService.getSchemeList(faultPowerCut));
    }

    /**
     * 获取停电区域光伏列表
     *
     * @param dto
     * @return
     */
    @PostMapping("/getDistCustomerList")
    @ApiOperation(value = "获取停电区域光伏列表")
    public ApiResponse<Page<FhzgDistCustomer>> getDistCustomerList(@RequestBody FhzgFaultDistCustomerDTO dto) {
        if (dto == null) {
            return fail("参数不能为空");
        }
        if (dto.getFaultId() == null) {
            return fail("故障停电ID不能为空");
        }
        return ApiResponse.success(distCustomerService.getCustomerList(dto));
    }

    /**
     * 统计停电区域光伏用户数量
     *
     * @param dto
     * @return
     */
    @PostMapping("/getDistCustomerCount")
    @ApiOperation(value = "统计停电区域光伏用户数量")
    public ApiResponse<FhzgDistCustomerCountDTO> getDistCustomerCount(@RequestBody FhzgFaultDistCustomerDTO dto) {
        if (dto == null) {
            return fail("参数不能为空");
        }
        if (dto.getFaultId() == null) {
            return fail("故障停电ID不能为空");
        }
        return ApiResponse.success(distCustomerService.getDistCustomerCount(dto));
    }

    /**
     * 获取待转供开关段列表
     *
     * @param dto
     * @return
     */
    @PostMapping("/getAwaitTransferSwitchSectionList")
    @ApiOperation(value = "获取待转供开关段列表")
    public ApiResponse<Page<FaultAwaitTransferSwitchSection>> getAwaitTransferSwitchSectionList(@RequestBody FhzgFaultDistCustomerDTO dto) {
        if (dto == null) {
            return fail("参数不能为空");
        }
        if (dto.getFaultId() == null) {
            return fail("故障停电ID不能为空");
        }
        return ApiResponse.success(awaitTransferSwitchSectionServices.getSwitchSectionList(dto));
    }

    /**
     * 获取待转供用户列表
     *
     * @param dto
     * @return
     */
    @PostMapping("/getAwaitTransferUsersList")
    @ApiOperation(value = "获取待转供用户列表")
    public ApiResponse<Page<FhzgFaultAwaitTransferUsersVO>> getAwaitTransferUsersList(@RequestBody FhzgFaultDistCustomerDTO dto) {
        if (dto == null) {
            return fail("参数不能为空");
        }
        if (dto.getFaultId() == null) {
            return fail("故障停电ID不能为空");
        }
        return ApiResponse.success(awaitTransferUsersService.getUserList(dto));
    }

    /**
     * 获取对侧联络线路承载力分析列表
     *
     * @param dto
     * @return
     */
    @PostMapping("/getOppositeLineCarryCapacityList")
    @ApiOperation(value = "获取对侧联络线路承载力分析列表")
    public ApiResponse<Page<FhzgFaultOppositeLineCarryCapacityVO>> getOppositeLineCarryCapacityList(@RequestBody FhzgFaultDistCustomerDTO dto) {
        if (dto == null) {
            return fail("参数不能为空");
        }
        if (dto.getFaultId() == null) {
            return fail("故障停电ID不能为空");
        }
        return ApiResponse.success(oppositeLineCarryCapacityService.getOppositeLineCarryCapacityList(dto));
    }

    /**
     *
     * 查询计划下，转供范围，转供计划，转供评分是否有数据，返回给前端禁用按钮
     * @author luorui
     * @date 2025/5/27 10:36
     *
     **/
    @PostMapping("/getHasDataTag")
    @ApiOperation(value = "查询计划下，转供范围，转供计划，转供评分是否有数据，返回给前端禁用按钮")
    public ApiResponse<HasDataTagDTO> getHasDataTag(@RequestBody FhzgFaultDistCustomerDTO dto) {
        if (dto.getFaultId() == null) {
            return fail("故障停电ID不能为空");
        }
        HasDataTagDTO hasDataTagDTO = faultPowerCutService.getHasDataTag( dto);
        return ApiResponse.success(hasDataTagDTO);
    }

    /**
     * 更新故障区段确认状态
     * @param faultId 故障id
     * @return
     * @param <T>
     */
    @GetMapping("/updateSectionStatus")
    @ApiOperation(value = "更新故障区段确认状态")
    public <T> ApiResponse<T> updateSectionStatus(@Valid @NotNull(message = "ID不能为空") Long faultId) {
        try {
            faultPowerCutService.updateSectionStatus(faultId);
        }catch (Exception e) {
            return ApiResponse.fail("更新失败！");
        }
        return ApiResponse.success();
    }
    /**
     * 删除故障区段
     * @param faultId 故障id
     * @return
     * @param <T>
     */
    @GetMapping("/deleteSection")
    @ApiOperation(value = "删除故障区段")
    public <T> ApiResponse<T> deleteSection(@Valid @NotNull(message = "ID不能为空") Long faultId) {
        try {
            faultPowerCutService.deleteSection(faultId);
        }catch (Exception e) {
            return ApiResponse.fail("删除失败！");
        }
        return ApiResponse.success();
    }


    /**
     * 重新选定故障范围，生成故障事件
     */
    @PostMapping("/regenerateFault")
    @ApiOperation(value = "重新选定故障范围，生成故障事件")
    public ApiResponse<String> regenerateFault(@RequestBody RegenerateFaultRequest request) {
        faultPowerCutService.regenerateFault(request);
        return ApiResponse.success();
    }

}
