package com.njcn.prepare.harmonic.service.mysql.newalgorithm.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.harmonic.pojo.po.RMpBenchmarkLevelD;
import com.njcn.harmonic.pojo.po.day.RStatDataPltDPO;
import com.njcn.harmonic.pojo.po.day.RStatDataVDPO;
import com.njcn.prepare.harmonic.mapper.mysql.day.RStatDataPltDMapper;
import com.njcn.prepare.harmonic.mapper.mysql.day.RStatDataVDMapper;
import com.njcn.prepare.harmonic.mapper.mysql.newalgorithm.RMpBenchmarkLevelDMapper;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.service.mysql.newalgorithm.IRMpBenchmarkLevelDService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 类的介绍：监测点基准水平评估_月
 *
 * @author xuyang
 * @version 1.0.0
 * @createTime 2023/11/21 14:50
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class RMpBenchmarkLevelDServiceImpl extends MppServiceImpl<RMpBenchmarkLevelDMapper, RMpBenchmarkLevelD> implements IRMpBenchmarkLevelDService {

    private final RStatDataVDMapper rStatDataVDMapper;

    private final RStatDataPltDMapper rStatDataPltDMapper;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void benchmarkLevelD(CalculatedParam calculatedParam) {
        log.info(LocalDateTime.now()+"===>监测点基准水平评估_日统计算法开始");
        List<RMpBenchmarkLevelD> result = new ArrayList<>();
        List<RStatDataVDPO> vList = new ArrayList<>();
        List<RStatDataPltDPO> pltList = new ArrayList<>();
        Map<String, List<RStatDataVDPO>> map1 = new HashMap<>();
        Map<String, List<RStatDataPltDPO>> map2 = new HashMap<>();
        List<String> lineIds = calculatedParam.getIdList();
        DateTime beginDay = DateUtil.beginOfDay(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        DateTime endDay = DateUtil.endOfDay(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        //以尺寸100分片,查询数据
        List<List<String>> pendingIds =  ListUtils.partition(lineIds,5);
        for (List<String> pendingId : pendingIds) {
            //查询电压表
            vList.addAll(rStatDataVDMapper.getBenchmarkLevel(beginDay,endDay,pendingId));
            //查询长闪表
            pltList.addAll(rStatDataPltDMapper.getBenchmarkLevel(beginDay,endDay,pendingId));
        }
        if (CollectionUtil.isNotEmpty(vList)){
            map1 = vList.stream().collect(Collectors.groupingBy(RStatDataVDPO::getLineId));
        }
        if (CollectionUtil.isNotEmpty(pltList)){
            map2 = pltList.stream().collect(Collectors.groupingBy(RStatDataPltDPO::getLineId));
        }
        for (String item : lineIds) {
            RMpBenchmarkLevelD rMpBenchmarkLevel = new RMpBenchmarkLevelD();
            rMpBenchmarkLevel.setMeasurementPointId(item);
            rMpBenchmarkLevel.setDataDate(beginDay);
            //初始化数据
            rMpBenchmarkLevel.setVoltageAvg(0.0);
            rMpBenchmarkLevel.setUnbalanceAvg(0.0);
            rMpBenchmarkLevel.setVthdAvg(0.0);
            rMpBenchmarkLevel.setFlickerAvg(0.0);
            //稳态-有数据处理
            List<RStatDataVDPO> l1 = map1.get(item);
            if (CollectionUtil.isNotEmpty(l1)){
                double vAvg = l1.stream().mapToDouble(RStatDataVDPO::getRms).average().getAsDouble();
                double vUnbalanceAvg = l1.stream().mapToDouble(RStatDataVDPO::getVUnbalance).average().getAsDouble();
                double vThdAvg = l1.stream().mapToDouble(RStatDataVDPO::getVThd).average().getAsDouble();
                rMpBenchmarkLevel.setVoltageAvg(vAvg);
                rMpBenchmarkLevel.setUnbalanceAvg(vUnbalanceAvg);
                rMpBenchmarkLevel.setVthdAvg(vThdAvg);
            }
            List<RStatDataPltDPO> l2 = map2.get(item);
            if (CollectionUtil.isNotEmpty(l2)){
                double pltAvg = l2.stream().mapToDouble(RStatDataPltDPO::getPlt).average().getAsDouble();
                rMpBenchmarkLevel.setFlickerAvg(pltAvg);
            }
            result.add(rMpBenchmarkLevel);
        }
        if (CollectionUtil.isNotEmpty(result)){
            this.saveOrUpdateBatchByMultiId(result,100);
        }
    }

}
