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

import cn.hutool.core.collection.CollUtil;
import com.hdkj.lt.base.exception.BusinessException;
import com.hdkj.lt.bf.constants.TransferSchemeConstants;
import com.hdkj.lt.core.bizms.modle.po.plan.FhzgPlanTransferScheme;
import com.hdkj.lt.core.bizms.modle.po.plan.FhzgPlanTransferSchemeFinalStateCheck;
import com.hdkj.lt.bf.entity.dto.FhzgPlanTransferSchemeCheckResultDTO;
import com.hdkj.lt.bf.entity.dto.FhzgPlanTransferSchemeSafeCheckFinalResultDTO;
import com.hdkj.lt.bf.mapper.FhzgPlanTransferSchemeFinalStateCheckMapper;
import com.hdkj.lt.bf.service.FhzgPlanTransferSchemeFinalStateCheckService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author luorui
 * @date 2025/5/15 10:38
 * 方案计划安全校验结果终态查询服务类
 */
@Service
@Slf4j
public class FhzgPlanTransferSchemeFinalStateCheckServiceImpl implements FhzgPlanTransferSchemeFinalStateCheckService {

    @Autowired
    FhzgPlanTransferSchemeFinalStateCheckMapper fhzgPlanTransferSchemeFinalStateCheckMapper;

    /**
     * @author luorui
     * @date 2025/5/15 10:36
     * @discription 根据组合方案id 查询该方案下所有子方案终态校验结果。
     **/
    @Override
    public Boolean getSchemeSafeCheckFinalResultByGroupId(FhzgPlanTransferScheme fhzgPlanTransferScheme) {
        if (Objects.isNull(fhzgPlanTransferScheme.getId())) {
            throw new BusinessException("根据组合方案id");
        }
        List<FhzgPlanTransferSchemeCheckResultDTO> fhzgPlanTransferSchemeCheckResultDTOS = fhzgPlanTransferSchemeFinalStateCheckMapper.getSchemeSafeCheckFinalCountResultByGroupId(fhzgPlanTransferScheme);
        for (FhzgPlanTransferSchemeCheckResultDTO fhzgPlanTransferSchemeCheckResultDTO : fhzgPlanTransferSchemeCheckResultDTOS) {
            Integer countCheckFalse = fhzgPlanTransferSchemeCheckResultDTO.getCountCheckFalse();
            //所有子方案的所有开关中只有有一条校验不通过则结果为校验不通过
            if (countCheckFalse > 0) {
                return false;
            }
        }
        return true;
    }

    /**
     * @author luorui
     * @date 2025/5/15 10:36
     * @discription 查询查询组合方案的安全校验终态结果详情
     **/
    @Override
    public FhzgPlanTransferSchemeSafeCheckFinalResultDTO getSchemeSafeCheckFinalResultDetailByGroupId(FhzgPlanTransferScheme fhzgPlanTransferScheme) {
        if (Objects.isNull(fhzgPlanTransferScheme.getId())) {
            throw new BusinessException("根据组合方案id为空");
        }
        List<FhzgPlanTransferSchemeFinalStateCheck> fhzgPlanTransferSchemeFinalStateChecks = fhzgPlanTransferSchemeFinalStateCheckMapper.getSchemeSafeCheckFinalResultByGroupId(fhzgPlanTransferScheme);
        // 默认值true
        /** 整线安全电流校验结果*/
        Boolean segmentSafeCurrentCheck = true;
        /** 开关电流校验 */
        Boolean switchCurrentCheck = true;
        /** 导线电流校验*/
        Boolean segmentCurrentCheck = true;
        if (CollUtil.isNotEmpty(fhzgPlanTransferSchemeFinalStateChecks)) {
            for (FhzgPlanTransferSchemeFinalStateCheck fhzgPlanTransferSchemeFinalStateCheck : fhzgPlanTransferSchemeFinalStateChecks) {
                if (TransferSchemeConstants.SafeCheckResult.UN_PASS.equals(fhzgPlanTransferSchemeFinalStateCheck.getSegmentSafeCurrentCheck())) {
                    segmentSafeCurrentCheck = false;
                }
                if (TransferSchemeConstants.SafeCheckResult.UN_PASS.equals(fhzgPlanTransferSchemeFinalStateCheck.getSwitchCurrentCheck())) {
                    switchCurrentCheck = false;
                }
                if (TransferSchemeConstants.SafeCheckResult.UN_PASS.equals(fhzgPlanTransferSchemeFinalStateCheck.getSegmentCurrentCheck())) {
                    segmentCurrentCheck = false;
                }
            }
        }
        FhzgPlanTransferSchemeSafeCheckFinalResultDTO fhzgPlanTransferSchemeSafeCheckFinalResultDTO = new FhzgPlanTransferSchemeSafeCheckFinalResultDTO();
        fhzgPlanTransferSchemeSafeCheckFinalResultDTO.setSegmentSafeCurrentCheck(segmentSafeCurrentCheck);
        fhzgPlanTransferSchemeSafeCheckFinalResultDTO.setSwitchCurrentCheck(switchCurrentCheck);
        fhzgPlanTransferSchemeSafeCheckFinalResultDTO.setSegmentCurrentCheck(segmentCurrentCheck);
        return fhzgPlanTransferSchemeSafeCheckFinalResultDTO;
    }

}
