package com.njcn.harmonic.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.njcn.harmonic.mapper.RStatDataHarmpowerPDMapper;
import com.njcn.harmonic.pojo.po.day.RStatDataHarmpowerPDPO;
import com.njcn.harmonic.pojo.vo.TraceVO;
import com.njcn.harmonic.service.IHarmonicTraceService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 类的介绍：
 *
 * @author xuyang
 * @version 1.0.0
 * @createTime 2024/1/5 14:43
 */
@Service
@AllArgsConstructor
public class HarmonicTraceServiceImpl implements IHarmonicTraceService {

    private final RStatDataHarmpowerPDMapper rStatDataHarmpowerPDMapper;

    @Override
    public List<TraceVO> getTraceData(String lineId, String time) {
        List<TraceVO> result = new ArrayList<>();
        LambdaQueryWrapper<RStatDataHarmpowerPDPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RStatDataHarmpowerPDPO::getTime, time)
                .eq(RStatDataHarmpowerPDPO::getLineId, lineId)
                .eq(RStatDataHarmpowerPDPO::getValueType, "AVG")
                .in(RStatDataHarmpowerPDPO::getPhaseType, Arrays.asList("A","B","C"));
        List<RStatDataHarmpowerPDPO> list = rStatDataHarmpowerPDMapper.selectList(lambdaQueryWrapper);
        if (CollectionUtil.isNotEmpty(list)){
            Map<String,List<RStatDataHarmpowerPDPO>> map = list.stream().collect(Collectors.groupingBy(RStatDataHarmpowerPDPO::getPhaseType));
            List<Double> aList = getDataByPhaseType(map,"A");
            List<Double> bList = getDataByPhaseType(map,"B");
            List<Double> cList = getDataByPhaseType(map,"C");
            for (int i = 0; i < 49; i++) {
                TraceVO traceVO = new TraceVO();
                traceVO.setHarmonicTimes(i+2);
                traceVO.setHarmonicA(aList.get(i));
                traceVO.setHarmonicB(bList.get(i));
                traceVO.setHarmonicC(cList.get(i));
                result.add(traceVO);
            }
        }
        return result;
    }

    public List<Double> getDataByPhaseType(Map<String,List<RStatDataHarmpowerPDPO>> map, String phaseType) {
        RStatDataHarmpowerPDPO po = map.get(phaseType).get(0);
        return Arrays.asList(po.getP2(),po.getP3(),po.getP4(),po.getP5(),po.getP6(),po.getP7(),po.getP8(),po.getP9()
                ,po.getP10(),po.getP11(),po.getP12(),po.getP13(),po.getP14(),po.getP15(),po.getP16(),po.getP17(),po.getP18(),po.getP19()
                ,po.getP20(),po.getP21(),po.getP22(),po.getP23(),po.getP24(),po.getP25(),po.getP26(),po.getP27(),po.getP28(),po.getP29()
                ,po.getP30(),po.getP31(),po.getP32(),po.getP33(),po.getP34(),po.getP35(),po.getP36(),po.getP37(),po.getP38(),po.getP39()
                ,po.getP40(),po.getP41(),po.getP42(),po.getP43(),po.getP44(),po.getP45(),po.getP46(),po.getP47(),po.getP48(),po.getP49()
                ,po.getP50());
    }


}
