package com.hdkj.lt.mq.feign;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hdkj.hussar.ApiResponse;
import com.hdkj.lt.core.bizms.modle.po.GridPdOutageEventBase;
import com.hdkj.lt.feign.IFaultCreateClient;
import com.hdkj.lt.modle.dto.fault.RegenerateFaultRequest;
import com.hdkj.lt.mq.entity.dto.AcceptFaultPowerCutDTO;
import com.hdkj.lt.mq.entity.dto.BreakerInfo;
import com.hdkj.lt.mq.entity.dto.FeederInfo;
import com.hdkj.lt.mq.service.base.GridPdOutageEventBaseService;
import com.hdkj.lt.mq.service.fault.AcceptFaultPowerCutService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author rj
 * @date 2025/9/1 16:39
 * @description
 */
@RequiredArgsConstructor
@Slf4j
@RestController
public class FaultCreateClient implements IFaultCreateClient {

    private final AcceptFaultPowerCutService acceptFaultPowerCutService;
    private final GridPdOutageEventBaseService gridPdOutageEventBaseService;


    @Override
    public ApiResponse<Boolean> regenerateFault(RegenerateFaultRequest request) {
        //根据OutageNumber查询对应的停电事件
        GridPdOutageEventBase event = gridPdOutageEventBaseService.getOne(new LambdaQueryWrapper<GridPdOutageEventBase>()
                        .eq(GridPdOutageEventBase::getOutageNumber, request.getFaultNo())
//                todo:暂时屏蔽停复电标识
//                .eq(GridPdOutageEventBase::getOutageFlag, '0')
        );
        AcceptFaultPowerCutDTO acceptFaultPowerCutDTO = new AcceptFaultPowerCutDTO();
        acceptFaultPowerCutDTO.setFaultNo(event.getId().toString());
        acceptFaultPowerCutDTO.setSourceSystem("负荷转供");
        acceptFaultPowerCutDTO.setFaultDesc(event.getOutageDesc());
        acceptFaultPowerCutDTO.setFaultStatus(event.getId().toString());
        acceptFaultPowerCutDTO.setStartTime(event.getBeginTime());
        acceptFaultPowerCutDTO.setEndTime(event.getEndTime());
        acceptFaultPowerCutDTO.setFaultType(Integer.parseInt(event.getOutageType()) - 1);
        List<FeederInfo> feederInfos = getFeederInfos(event, request);
        acceptFaultPowerCutDTO.setFeederList(feederInfos);
        acceptFaultPowerCutService.acceptFaultPowerCut(acceptFaultPowerCutDTO);
        return ApiResponse.success(true);
    }

    private static List<FeederInfo> getFeederInfos(GridPdOutageEventBase event, RegenerateFaultRequest request) {
        List<FeederInfo> feederInfos = new ArrayList<>();
        FeederInfo feederInfo = new FeederInfo();
        feederInfo.setFeederPsrId(event.getRdtFeederId());
        feederInfo.setFeederName(event.getRdtFeederName());
        List<BreakerInfo> breakerInfos = new ArrayList<>();
        BreakerInfo breakerInfo = new BreakerInfo();
        breakerInfo.setBreakerPsrId(request.getStartSwitch());
        breakerInfo.setBreakerName(request.getStartSwitchName());
        breakerInfo.setBreakerPsrType(request.getStartSwitchType());
        breakerInfos.add(breakerInfo);
        feederInfo.setBreakerList(breakerInfos);
        feederInfo.setFeederPsrId(event.getRdtFeederId());
        feederInfos.add(feederInfo);
        return feederInfos;
    }
}

