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

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdkj.lt.bf.entity.dto.OperationAbnormalityQueryDTO;
import com.hdkj.lt.bf.mapper.FhzgInfoLimitTransferSchemeMapper;
import com.hdkj.lt.bf.mapper.FhzgInfoLimitTransferSchemeResultMapper;
import com.hdkj.lt.bf.service.voltageLimitViolation.FhzgInfoLimitTransferSchemeResultService;
import com.hdkj.lt.core.bizms.modle.po.voltageLimitViolation.FhzgInfoLimitTransferScheme;
import com.hdkj.lt.core.bizms.modle.po.voltageLimitViolation.FhzgInfoLimitTransferSchemeResult;
import com.hdkj.lt.modle.dto.abnormal.SwitchDetailDTO;
import com.hdkj.lt.modle.dto.voltageLimitViolation.AllSwitchInfoListDTO;
import com.hdkj.lt.modle.dto.voltageLimitViolation.SwitchWithMeasurementDTO;
import com.hdkj.lt.modle.dto.voltageLimitViolation.VoltageTransferDataDTO;
import com.hdkj.lt.modle.dto.voltageLimitViolation.XferPlanDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * @author userlrk
 * @description 针对表【fhzg_info_limit_transfer_scheme_result(成片电压越限转供方案计划推演平台结果)】的数据库操作Service实现
 * @createDate 2025-09-24 15:24:10
 */
@Service
public class FhzgInfoLimitTransferSchemeResultServiceImpl extends ServiceImpl<FhzgInfoLimitTransferSchemeResultMapper, FhzgInfoLimitTransferSchemeResult>
        implements FhzgInfoLimitTransferSchemeResultService {
    @Autowired
    private FhzgInfoLimitTransferSchemeResultMapper fhzgInfoLimitTransferSchemeResultMapper;
    @Autowired
    private FhzgInfoLimitTransferSchemeMapper fhzgInfoLimitTransferSchemeMapper;


    @Override
    public List<SwitchDetailDTO> getSvgPquiDataLimit(OperationAbnormalityQueryDTO request) {
        List<SwitchDetailDTO> result = new ArrayList<>();
        LambdaQueryWrapper<FhzgInfoLimitTransferSchemeResult> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FhzgInfoLimitTransferSchemeResult::getLimitId, request.getOaId());
        FhzgInfoLimitTransferSchemeResult areaResult = fhzgInfoLimitTransferSchemeResultMapper.selectOne(lambdaQueryWrapper);
        FhzgInfoLimitTransferScheme fhzgInfoLimitTransferScheme = fhzgInfoLimitTransferSchemeMapper.selectById(request.getSchemeId());
        String schemeJson = fhzgInfoLimitTransferScheme.getSchemeJson();

        Optional.ofNullable(areaResult.getSchemeResultJson())
                .map(e -> JSON.parseObject(e, VoltageTransferDataDTO.class))
                .ifPresent(data -> {
                    String feederId = data.getFeederId();
                    LocalDateTime startTime = data.getStartTime();
                    //计算离当天0点的分钟数
                    long until = startTime.with(LocalTime.MIN).until(startTime, ChronoUnit.MINUTES);
                    int num = (int) until / 15;
                    AllSwitchInfoListDTO before = data.getAllSwitchInfoList().stream().filter(i -> i.getFeederId().equals(feederId)).findFirst().get();

                    Optional.ofNullable(schemeJson).map(i -> JSON.parseObject(schemeJson, XferPlanDTO.class))
                            .ifPresent(plan -> {
                                AllSwitchInfoListDTO after = plan.getAllSwitchInfoList().stream().filter(i -> i.getFeederId().equals(feederId)).findFirst().get();
                                Map<String, SwitchWithMeasurementDTO> measurementDTOAfterMap = new HashMap<>();
                                after.getAllSwitchList().forEach(i -> {
                                    measurementDTOAfterMap.put(i.getPsrID(), i);
                                });
                                before.getAllSwitchList().forEach(i -> {
                                            SwitchWithMeasurementDTO switchWithMeasurementDTO = measurementDTOAfterMap.get(i.getPsrID());
                                            SwitchDetailDTO switchDetailDTO = new SwitchDetailDTO();
                                            switchDetailDTO.setPsrID(i.getPsrID());
                                            switchDetailDTO.setBeforeI(Double.valueOf(i.getI().get(num)));
                                            switchDetailDTO.setBeforeU(Double.valueOf(i.getU().get(num)));
                                            switchDetailDTO.setAfterI(Double.valueOf(switchWithMeasurementDTO.getU().get(num)));
                                            switchDetailDTO.setAfterU(Double.valueOf(switchWithMeasurementDTO.getU().get(num)));
                                            switchDetailDTO.setPsrType(i.getPsrType());
                                            result.add(switchDetailDTO);
                                        }
                                );


                            });

                });

        return result;
    }

}




