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

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.device.biz.pojo.dto.DeptGetSubStationDTO;
import com.njcn.device.biz.pojo.dto.SubGetBase;
import com.njcn.event.pojo.po.RMpEventDetailM;
import com.njcn.harmonic.pojo.po.RMpPartHarmonicDetailM;
import com.njcn.prepare.harmonic.mapper.mysql.line.RMpPartHarmonicDetailMMapper;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.service.mysql.line.RMpEventDetailMService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.njcn.prepare.harmonic.mapper.mysql.newalgorithm.RStatSubstationVoltageMPOMapper;
import com.njcn.prepare.harmonic.pojo.mysql.po.RStatSubstationVoltageMPO;
import com.njcn.prepare.harmonic.service.mysql.newalgorithm.RStatSubstationVoltageMPOService;
import org.springframework.util.CollectionUtils;

/**
 * 
 * Description:
 * Date: 2023/8/4  14:57【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class RStatSubstationVoltageMPOServiceImpl extends MppServiceImpl<RStatSubstationVoltageMPOMapper, RStatSubstationVoltageMPO> implements RStatSubstationVoltageMPOService{

    private final RMpPartHarmonicDetailMMapper rMpPartHarmonicDetailMMapper;
    private final RMpEventDetailMService rMpEventDetailMService;

    @Override
    public void handler(CalculatedParam calculatedParam) {
        DateTime begin = DateUtil.beginOfMonth(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        DateTime end = DateUtil.endOfMonth(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        //变电站id集合
        List<DeptGetSubStationDTO.Info> idList = calculatedParam.getIdList();
        List<RStatSubstationVoltageMPO> result=new ArrayList<>();
        RStatSubstationVoltageMPO po;
        QueryWrapper<RMpPartHarmonicDetailM> queryWrapper = new QueryWrapper<>();
        QueryWrapper<RMpEventDetailM> queryWrapper2 = new QueryWrapper<>();
        //单位
        for (DeptGetSubStationDTO.Info info : idList) {
            List<SubGetBase> stationIds = info.getStationIds();
            //根据电压等级进行分组
            Map<String, List<SubGetBase>> subMap = stationIds.stream().collect(Collectors.groupingBy(SubGetBase::getVoltageLevel));
            for (Map.Entry<String, List<SubGetBase>> stringListEntry : subMap.entrySet()) {
                po=new RStatSubstationVoltageMPO();
                po.setOrgNo(info.getUnitId());
                po.setDataDate(begin);
                po.setSubstationVoltage(stringListEntry.getKey());
                po.setSubstationCount(stringListEntry.getValue().size());
                Integer harmonicCount=0;
                Integer eventCount=0;
                for (SubGetBase subGetBase : stringListEntry.getValue()) {
                    queryWrapper.clear();
                    queryWrapper.select("measurement_over_day")
                            .in("measurement_point_id",subGetBase.getUnitChildrenList())
                            .between("data_date",begin,end);
                    List<RMpPartHarmonicDetailM> rMpPartHarmonicDetailMS = rMpPartHarmonicDetailMMapper.selectList(queryWrapper);
                    long count = rMpPartHarmonicDetailMS.stream().filter(x -> x.getMeasurementOverDay() > 0).count();
                    if(count>0){
                        harmonicCount=harmonicCount+1;
                    }
                    queryWrapper2.clear();
                    queryWrapper2.in("measurement_point_id",subGetBase.getUnitChildrenList())
                            .between("data_date",begin,end);
                    List<RMpEventDetailM> list = rMpEventDetailMService.list(queryWrapper2);
                    long count1 = list.stream().filter(x -> x.getSagTimes() > 0 || x.getSwellTimes() > 0 || x.getInterruptTimes() > 0).count();
                    if(count1>0){
                        eventCount=eventCount+1;
                    }
                }
                po.setHarmonicCount(harmonicCount);
                po.setEventCount(eventCount);
                result.add(po);
            }

        }
        if (!CollectionUtils.isEmpty(result)) {
            this.saveOrUpdateBatchByMultiId(result, 500);
        }
    }
}
