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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdkj.lt.bf.entity.vo.*;
import com.hdkj.lt.bf.mapper.FhzgFaultProcessStateCheckMapper;
import com.hdkj.lt.bf.service.FhzgFaultProcessStateCheckService;
import com.hdkj.lt.core.bizms.modle.dto.SchemeQueryDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @author zhuzhe
 * @date 2025/5/16 10:43
 */
@Service
@Slf4j
public class FhzgFaultProcessStateCheckServiceImpl implements FhzgFaultProcessStateCheckService {

    @Autowired
    FhzgFaultProcessStateCheckMapper fhzgFaultProcessStateCheckMapper;


    /**
     * 停电计划转供方案过程态详情分页查询
     * @param dto
     **/
    @Override
    public Page<ProcessStateVO> getProcessStateList(SchemeQueryDTO dto) {
        Page<ProcessStateVO> page = new Page<>();
        dto.setOffset((dto.getPageNum()-1) * dto.getPageSize());
        List<ProcessStateVO> list = fhzgFaultProcessStateCheckMapper.selectProcessStateCheckList(dto);
        page.setRecords(list);
        Integer total = fhzgFaultProcessStateCheckMapper.selectProcessStateCheckCount(dto);
        page.setTotal(total);
        return page;
    }

    /**
     * 停电计划转供方案过程态开关电流检查明细分页查询
     * @param dto
     * @return
     */
    @Override
    public Page<SwitchingSequenceVO> getSwitchingSequenceList(SchemeQueryDTO dto) {
        Page<SwitchingSequenceVO> page = new Page<>();
        dto.setOffset((dto.getPageNum()-1) * dto.getPageSize());
        List<SwitchingSequenceVO> list = fhzgFaultProcessStateCheckMapper.getSwitchingSequenceList(dto);
        page.setRecords(list);
        Integer total = fhzgFaultProcessStateCheckMapper.getSwitchingSequenceCount(dto);
        page.setTotal(total);
        return page;
    }

    @Override
    public SwitchingLoopCheckVO getSwitchCurrentDetail(SchemeQueryDTO dto) {
        List<SwitchingLoopCheckVO> list = fhzgFaultProcessStateCheckMapper.getSwitchCurrentDetailList(dto);
        return CollUtil.isNotEmpty(list) ? list.get(0) : null;
    }

    @Override
    public Page<SwitchingLoopSwVO> getSwitchingLoopSwCurrentList(SchemeQueryDTO dto) {
        Page<SwitchingLoopSwVO> page = new Page<>();
        dto.setOffset((dto.getPageNum()-1) * dto.getPageSize());
        List<SwitchingLoopSwVO> list = fhzgFaultProcessStateCheckMapper.getSwitchingLoopSwCurrentList(dto);
        page.setRecords(list);
        Integer total = fhzgFaultProcessStateCheckMapper.getSwitchingLoopSwCurrentCount(dto);
        page.setTotal(total);
        return page;
    }

    public Page<SwitchingLoopSegVO> getSwitchingLoopSegCurrentList(SchemeQueryDTO dto) {
        Page<SwitchingLoopSegVO> page = new Page<>();
        dto.setOffset((dto.getPageNum()-1) * dto.getPageSize());
        List<SwitchingLoopSegVO> list = fhzgFaultProcessStateCheckMapper.getSwitchingLoopSegCurrentList(dto);
        page.setRecords(list);
        Integer total = fhzgFaultProcessStateCheckMapper.getSwitchingLoopSegCurrentCount(dto);
        page.setTotal(total);
        return page;
    }

    /**
     * 停电计划转供方案终态安全校验结论分页查询
     * @param dto
     * @return
     */
    @Override
    public FinalStateCheckVO getFinalStateCheckDetail(SchemeQueryDTO dto) {
        List<FinalStateCheckVO> list = fhzgFaultProcessStateCheckMapper.getFinalStateCheckDetail(dto);
        FinalStateCheckVO finalStateCheckVO = CollUtil.isNotEmpty(list) ? list.get(0) : null;
        //设置校验不通过提示语
        if(finalStateCheckVO !=null){
            String checkFailMsg ;
            List<String> msgList = new ArrayList<>();
            if(StrUtil.equals(finalStateCheckVO.getLoadFactorCheck(),"0")){
                msgList.add("对侧线路最大负载率校验不通过");
            }
            if(StrUtil.equals(finalStateCheckVO.getSwitchCurrentCheck(),"0")){
                msgList.add("转供路径开关电流校验不通过");
            }
            if(StrUtil.equals(finalStateCheckVO.getSegmentCurrentCheck(),"0")){
                msgList.add("转供路径导线段电流校验不通过");
            }
            checkFailMsg = StrUtil.join("、",msgList);
            finalStateCheckVO.setCheckFailMsg(checkFailMsg);
        }
        return finalStateCheckVO;
    }

}
