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

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdkj.lt.base.exception.BusinessException;
import com.hdkj.lt.bf.constants.TransferSchemeConstants;
import com.hdkj.lt.bf.entity.dto.FhzgFaultTransferSchemeSwitchingSequenceDTO;
import com.hdkj.lt.core.bizms.modle.po.plan.FhzgPlanTransferScheme;
import com.hdkj.lt.bf.entity.dto.FhzgPlanTransferSchemeSwitchingSafeCheckCountDTO;
import com.hdkj.lt.bf.entity.dto.FhzgPlanTransferSchemeSwitchingSequenceDTO;
import com.hdkj.lt.bf.mapper.FhzgPlanTransferSchemeSwitchingSequenceMapper;
import com.hdkj.lt.bf.service.FhzgPlanTransferSchemeSwitchingSequenceService;
import lombok.extern.slf4j.Slf4j;
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;

/**
 * @author luorui
 * @date 2025/5/16 16:00
 */
@Service
@Slf4j
public class FhzgPlanTransferSchemeSwitchingSequenceServiceImpl implements FhzgPlanTransferSchemeSwitchingSequenceService {

    @Autowired
    FhzgPlanTransferSchemeSwitchingSequenceMapper fhzgPlanTransferSchemeSwitchingSequenceMapper;

    /**
     * @author luorui
     * @date 2025/5/16 16:01
     * @discription 开关动作顺序分页查询
     **/
    @Override
    public Page<FhzgPlanTransferSchemeSwitchingSequenceDTO> getTransferSchemeSwitchingSequenceBySchemeIdPage(FhzgPlanTransferScheme fhzgPlanTransferScheme) {
        if (Objects.isNull(fhzgPlanTransferScheme.getId())) {
            throw new BusinessException("组合方案id为空");
        }
        if (Objects.isNull(fhzgPlanTransferScheme.getPageNum())) {
            throw new BusinessException("分页页码为空");
        }
        if (Objects.isNull(fhzgPlanTransferScheme.getPageSize())) {
            throw new BusinessException("分页长度为空");
        }
        Page<FhzgPlanTransferScheme> page = new Page(fhzgPlanTransferScheme.getPageNum(), fhzgPlanTransferScheme.getPageSize());
        Page<FhzgPlanTransferSchemeSwitchingSequenceDTO> fhzgPlanTransferSchemePathDetailDTOList = fhzgPlanTransferSchemeSwitchingSequenceMapper.getTransferSchemeSwitchingSequenceBySchemeIdPage(page, fhzgPlanTransferScheme);
        List<FhzgPlanTransferSchemeSwitchingSequenceDTO> list = fhzgPlanTransferSchemePathDetailDTOList.getRecords();
        //对开关动作顺序值重新赋值，从1开始
        if (!list.isEmpty()) {
            list.sort(Comparator.comparing(FhzgPlanTransferSchemeSwitchingSequenceDTO::getSwitchingSeq));
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setSwitchingSeq(i + 1);
            }
        }
        fhzgPlanTransferSchemePathDetailDTOList.setRecords(list);
        return fhzgPlanTransferSchemePathDetailDTOList;
    }

    /**
     * @author luorui
     * @date 2025/5/17 17:45
     * @discription 查询方案下开关校验通过/不通过数量
     **/
    @Override
    public List<FhzgPlanTransferSchemeSwitchingSafeCheckCountDTO> countSwitchPass(FhzgPlanTransferScheme fhzgPlanTransferScheme) {
        if (Objects.isNull(fhzgPlanTransferScheme.getId())) {
            throw new BusinessException("组合方案id为空");
        }
        List<FhzgPlanTransferSchemeSwitchingSequenceDTO> fhzgPlanTransferSchemeSwitchingSequenceDTOList = fhzgPlanTransferSchemeSwitchingSequenceMapper.getTransferSchemeSwitchingSequenceBySchemeId(fhzgPlanTransferScheme.getId());
        FhzgPlanTransferSchemeSwitchingSafeCheckCountDTO fhzgPlanTransferSchemeSwitchingSafeCheckCountDTOPass = new FhzgPlanTransferSchemeSwitchingSafeCheckCountDTO();
        FhzgPlanTransferSchemeSwitchingSafeCheckCountDTO fhzgPlanTransferSchemeSwitchingSafeCheckCountDTOUnPass = new FhzgPlanTransferSchemeSwitchingSafeCheckCountDTO();
        fhzgPlanTransferSchemeSwitchingSafeCheckCountDTOPass.setPassType(TransferSchemeConstants.SafeCheckResult.PASS);
        fhzgPlanTransferSchemeSwitchingSafeCheckCountDTOUnPass.setPassType(TransferSchemeConstants.SafeCheckResult.UN_PASS);
        int passCount = 0;
        int unPassCount = 0;
        for (FhzgPlanTransferSchemeSwitchingSequenceDTO fhzgPlanTransferSchemeSwitchingSequenceDTO : fhzgPlanTransferSchemeSwitchingSequenceDTOList) {
            if (TransferSchemeConstants.SafeCheckResult.PASS.equals(fhzgPlanTransferSchemeSwitchingSequenceDTO.getLoopCloseCheck())) {
                passCount++;
            }
            if (TransferSchemeConstants.SafeCheckResult.PASS.equals(fhzgPlanTransferSchemeSwitchingSequenceDTO.getFinalStateCheck())) {
                passCount++;
            }
            if (TransferSchemeConstants.SafeCheckResult.UN_PASS.equals(fhzgPlanTransferSchemeSwitchingSequenceDTO.getLoopCloseCheck())) {
                unPassCount++;
            }
            if (TransferSchemeConstants.SafeCheckResult.UN_PASS.equals(fhzgPlanTransferSchemeSwitchingSequenceDTO.getFinalStateCheck())) {
                unPassCount++;
            }
        }
        fhzgPlanTransferSchemeSwitchingSafeCheckCountDTOPass.setCount(passCount);
        fhzgPlanTransferSchemeSwitchingSafeCheckCountDTOUnPass.setCount(unPassCount);
        List<FhzgPlanTransferSchemeSwitchingSafeCheckCountDTO> result = new ArrayList<>();
        result.add(fhzgPlanTransferSchemeSwitchingSafeCheckCountDTOPass);
        result.add(fhzgPlanTransferSchemeSwitchingSafeCheckCountDTOUnPass);
        return result;
    }

}
