package com.hdkj.lt.bf.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdkj.lt.base.exception.BusinessException;
import com.hdkj.lt.bf.constants.TransferSchemeConstants;
import com.hdkj.lt.bf.entity.dto.FhzgFaultTransferSchemeSwitchingSafeCheckCountDTO;
import com.hdkj.lt.bf.entity.dto.FhzgFaultTransferSchemeSwitchingSequenceDTO;
import com.hdkj.lt.bf.mapper.FaultTransferSchemeSwitchingSequenceMapper;
import com.hdkj.lt.bf.service.IFaultTransferSchemeSwitchingSequenceService;
import com.hdkj.lt.core.bizms.modle.po.fault.FaultTransferScheme;
import com.hdkj.lt.core.bizms.modle.po.fault.FaultTransferSchemeSwitchingSequence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 故障停电转供方案开关动作顺序信息表 服务实现类
 * </p>
 *
 * @author luorui
 * @since 2025-05-29
 */
@Service
public class FaultTransferSchemeSwitchingSequenceServiceImpl extends ServiceImpl<FaultTransferSchemeSwitchingSequenceMapper, FaultTransferSchemeSwitchingSequence> implements IFaultTransferSchemeSwitchingSequenceService {

    @Autowired
    FaultTransferSchemeSwitchingSequenceMapper faultTransferSchemeSwitchingSequenceMapper;


    @Override
    public List<FhzgFaultTransferSchemeSwitchingSafeCheckCountDTO> countSwitchPass(FaultTransferScheme faultTransferScheme) {
        if (Objects.isNull(faultTransferScheme.getId())) {
            throw new BusinessException("组合方案id为空");
        }
        List<FhzgFaultTransferSchemeSwitchingSequenceDTO> fhzgPlanTransferSchemeSwitchingSequenceDTOList = faultTransferSchemeSwitchingSequenceMapper.getTransferSchemeSwitchingSequenceBySchemeId(faultTransferScheme.getId());
        FhzgFaultTransferSchemeSwitchingSafeCheckCountDTO faultTransferSchemeSwitchingSafeCheckCountDTOPass = new FhzgFaultTransferSchemeSwitchingSafeCheckCountDTO();
        FhzgFaultTransferSchemeSwitchingSafeCheckCountDTO faultTransferSchemeSwitchingSafeCheckCountDTOUnPass = new FhzgFaultTransferSchemeSwitchingSafeCheckCountDTO();
        faultTransferSchemeSwitchingSafeCheckCountDTOPass.setPassType(TransferSchemeConstants.SafeCheckResult.PASS);
        faultTransferSchemeSwitchingSafeCheckCountDTOUnPass.setPassType(TransferSchemeConstants.SafeCheckResult.UN_PASS);
        int passCount = 0;
        int unPassCount = 0;
        for (FhzgFaultTransferSchemeSwitchingSequenceDTO faultTransferSchemeSwitchingSequenceDTO : fhzgPlanTransferSchemeSwitchingSequenceDTOList) {
            if (TransferSchemeConstants.SafeCheckResult.PASS.equals(faultTransferSchemeSwitchingSequenceDTO.getLoopCloseCheck())) {
                passCount++;
            }
            if (TransferSchemeConstants.SafeCheckResult.PASS.equals(faultTransferSchemeSwitchingSequenceDTO.getFinalStateCheck())) {
                passCount++;
            }
            if (TransferSchemeConstants.SafeCheckResult.UN_PASS.equals(faultTransferSchemeSwitchingSequenceDTO.getLoopCloseCheck())) {
                unPassCount++;
            }
            if (TransferSchemeConstants.SafeCheckResult.UN_PASS.equals(faultTransferSchemeSwitchingSequenceDTO.getFinalStateCheck())) {
                unPassCount++;
            }
        }
        faultTransferSchemeSwitchingSafeCheckCountDTOPass.setCount(passCount);
        faultTransferSchemeSwitchingSafeCheckCountDTOUnPass.setCount(unPassCount);
        List<FhzgFaultTransferSchemeSwitchingSafeCheckCountDTO> result = new ArrayList<>();
        result.add(faultTransferSchemeSwitchingSafeCheckCountDTOPass);
        result.add(faultTransferSchemeSwitchingSafeCheckCountDTOUnPass);
        return result;
    }

    @Override
    public Page<FhzgFaultTransferSchemeSwitchingSequenceDTO> getTransferSchemeSwitchingSequenceBySchemeIdPage(FaultTransferScheme faultTransferScheme) {
        if (Objects.isNull(faultTransferScheme.getId())) {
            throw new BusinessException("组合方案id为空");
        }
        if (Objects.isNull(faultTransferScheme.getPageNum())) {
            throw new BusinessException("分页页码为空");
        }
        if (Objects.isNull(faultTransferScheme.getPageSize())) {
            throw new BusinessException("分页长度为空");
        }
        Page<FaultTransferScheme> page = new Page(faultTransferScheme.getPageNum(), faultTransferScheme.getPageSize());
        Page<FhzgFaultTransferSchemeSwitchingSequenceDTO> faultTransferSchemeSwitchingSequenceDTOPage = faultTransferSchemeSwitchingSequenceMapper.getTransferSchemeSwitchingSequenceBySchemeIdPage(page, faultTransferScheme);
        List<FhzgFaultTransferSchemeSwitchingSequenceDTO> list = faultTransferSchemeSwitchingSequenceDTOPage.getRecords();
        //对开关动作顺序值重新赋值，从1开始
        if (!list.isEmpty()) {
            list.sort(Comparator.comparing(FhzgFaultTransferSchemeSwitchingSequenceDTO::getSwitchingSeq));
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setSwitchingSeq(i + 1);
            }
        }
        faultTransferSchemeSwitchingSequenceDTOPage.setRecords(list);
        return faultTransferSchemeSwitchingSequenceDTOPage;
    }
}
