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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hdkj.lt.bf.entity.dto.VolLimitViolationSchemeDetailDTO;
import com.hdkj.lt.bf.entity.vo.LimitTransferSchemeDetailVO;
import com.hdkj.lt.bf.entity.vo.VolSchemeCheckNumVO;
import com.hdkj.lt.bf.mapper.FhzgInfoLimitTransferSchemeMapper;
import com.hdkj.lt.bf.mapper.FhzgLimitTransferSchemePathDetailMapper;
import com.hdkj.lt.bf.mapper.FhzgVoltageLimitViolationTransferSchemeDetailMapper;
import com.hdkj.lt.bf.service.voltageLimitViolation.FhzgVoltageLimitViolationTransferSchemeDetailService;
import com.hdkj.lt.core.bizms.modle.po.voltageLimitViolation.FhzgInfoLimitTransferScheme;
import com.hdkj.lt.core.bizms.modle.po.voltageLimitViolation.FhzgLimitTransferSchemePathDetail;
import com.hdkj.lt.core.bizms.modle.po.voltageLimitViolation.FhzgVoltageLimitViolationTransferSchemeDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * @author userlrk
 * @description 针对表【fhzg_voltage_limit_violation_transfer_scheme_detail(成片电压越限方案 电压越想详情信息)】的数据库操作Service实现
 * @createDate 2025-09-24 15:24:11
 */
@Service
public class FhzgVoltageLimitViolationTransferSchemeDetailServiceImpl extends ServiceImpl<FhzgVoltageLimitViolationTransferSchemeDetailMapper, FhzgVoltageLimitViolationTransferSchemeDetail>
        implements FhzgVoltageLimitViolationTransferSchemeDetailService {

    @Autowired
    private FhzgVoltageLimitViolationTransferSchemeDetailMapper detailMapper;
    @Autowired
    private FhzgInfoLimitTransferSchemeMapper transferSchemeMapper;
    @Autowired
    private FhzgLimitTransferSchemePathDetailMapper pathDetailMapper;

    /**
     * 查询方案对应电压越限对比详情数据
     *
     * @param dto
     * @return
     */
    @Override
    public List<FhzgVoltageLimitViolationTransferSchemeDetail> getVolLimitDetail(VolLimitViolationSchemeDetailDTO dto) {
        LambdaQueryWrapper<FhzgVoltageLimitViolationTransferSchemeDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FhzgVoltageLimitViolationTransferSchemeDetail::getSchemeId, dto.getSchemeId());
        List<FhzgVoltageLimitViolationTransferSchemeDetail> detailList = detailMapper.selectList(lambdaQueryWrapper);
        // Long limitId = detailList.get(0).getLimitId();
//        LambdaQueryWrapper<FhzgVoltageLimitViolationTransferSchemeDetail> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(FhzgVoltageLimitViolationTransferSchemeDetail::getSchemeId, null)
//                .eq(FhzgVoltageLimitViolationTransferSchemeDetail::getLimitId, limitId);
//        List<FhzgVoltageLimitViolationTransferSchemeDetail> detailListBefore = detailMapper.selectList(lambdaQueryWrapper);
//        detailList.addAll(detailListBefore);
        List<FhzgVoltageLimitViolationTransferSchemeDetail> result = new ArrayList<>();
        detailList.forEach(e -> {
            if (dto.getType() == 0 && e.getLineTag() == 0) {
                result.add(e);
            }
            if (dto.getType() == 1 && e.getLineTag() == 1) {
                result.add(e);
            }
            if (dto.getType() == 2 && e.getLineTag() == 2) {
                result.add(e);
            }
        });
        List<LimitTransferSchemeDetailVO> list = new ArrayList<>();
        return result;
    }

    /**
     * 对比结果转换成表格
     *
     * @param param
     * @return
     */
    public List<LimitTransferSchemeDetailVO> transToForm(List<FhzgVoltageLimitViolationTransferSchemeDetail> param) {
        List<LimitTransferSchemeDetailVO> list = new ArrayList<>();
        return null;
    }


    //查询全口径低电压事件对应方案列表
    @Override
    public List<FhzgInfoLimitTransferScheme> getVolLimitSchemeList(VolLimitViolationSchemeDetailDTO dto) {
        LambdaQueryWrapper<FhzgInfoLimitTransferScheme> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(FhzgInfoLimitTransferScheme::getId,
                        FhzgInfoLimitTransferScheme::getSchemeNo,
                        FhzgInfoLimitTransferScheme::getCheckState,
                        FhzgInfoLimitTransferScheme::getSchemeName,
                        FhzgInfoLimitTransferScheme::getEvaluationScore,
                        FhzgInfoLimitTransferScheme::getFileId,
                        FhzgInfoLimitTransferScheme::getLimitId,
                        FhzgInfoLimitTransferScheme::getSchemeState,
                        FhzgInfoLimitTransferScheme::getSwitchNum,
                        FhzgInfoLimitTransferScheme::getNonAutoSwitchNum)
                .eq(FhzgInfoLimitTransferScheme::getLimitId, dto.getLimitId());
        //.eq(FhzgInfoLimitTransferScheme::getCheckState, dto.getType());
        List<FhzgInfoLimitTransferScheme> list = transferSchemeMapper.selectList(queryWrapper);
        list.forEach(e -> {
            if (e.getEvaluationScore() != null) {
                e.setEvaluationScore(e.getEvaluationScore().setScale(0, RoundingMode.DOWN));
            }
            LambdaQueryWrapper<FhzgVoltageLimitViolationTransferSchemeDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(FhzgVoltageLimitViolationTransferSchemeDetail::getSchemeId, e.getId());
            List<FhzgVoltageLimitViolationTransferSchemeDetail> detailList = detailMapper.selectList(lambdaQueryWrapper);
            detailList.forEach(a -> {
                if (a.getLineTag() == 0) {
                    e.setLineName(a.getLineName());
                    e.setLineId(a.getLineId());
                }
                if (a.getLineTag() == 1) {
                    e.setOppLineName(a.getLineName());
                    e.setOppLineId(a.getLineId());
                }
            });
        });
        return list;
    }

    /**
     * 获取全口径低电压方案校验结果
     *
     * @param dto
     * @return
     */
    @Override
    public VolSchemeCheckNumVO getVolLimitSchemeLCheck(VolLimitViolationSchemeDetailDTO dto) {
        LambdaQueryWrapper<FhzgInfoLimitTransferScheme> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(FhzgInfoLimitTransferScheme::getId,
                FhzgInfoLimitTransferScheme::getCheckState).eq(FhzgInfoLimitTransferScheme::getLimitId, dto.getLimitId());
        List<FhzgInfoLimitTransferScheme> list = transferSchemeMapper.selectList(queryWrapper);
        VolSchemeCheckNumVO result = new VolSchemeCheckNumVO();
        int successNum = (int) list.stream().filter(v -> v.getCheckState() == 1).count();
        result.setSuccessNum(successNum);
        result.setFalseNum(list.size() - successNum);
        return result;
    }

    /**
     * 查询全口径低电压方案转供路径细明
     *
     * @param dto
     * @return
     */
    @Override
    public Page<FhzgLimitTransferSchemePathDetail> getVolLimitSchemeTransferPathList(VolLimitViolationSchemeDetailDTO dto) {
        LambdaQueryWrapper<FhzgLimitTransferSchemePathDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FhzgLimitTransferSchemePathDetail::getSchemeId, dto.getSchemeId());
        List<FhzgLimitTransferSchemePathDetail> list = pathDetailMapper.selectList(lambdaQueryWrapper);
        Page<FhzgLimitTransferSchemePathDetail> page = new Page<>();
        page.setTotal(list.size());
        page.setRecords(list);
        return page;
    }
}




