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

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.device.biz.pojo.dto.DeptGetChildrenMoreDTO;
import com.njcn.harmonic.pojo.po.dim.RDimBusTargetMPO;
import com.njcn.harmonic.pojo.po.dim.RDimBusbarHarmnicDPO;
import com.njcn.prepare.harmonic.mapper.mysql.dim.RDimBusTargetMPOMapper;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.service.mysql.dim.RDimBusTargetMPOService;
import com.njcn.prepare.harmonic.service.mysql.dim.RDimBusbarHarmnicDPOService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.enums.DicDataTypeEnum;
import com.njcn.system.pojo.po.DictData;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * 
 * Description:
 * Date: 2023/11/20  10:29【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class RDimBusTargetMPOServiceImpl extends MppServiceImpl<RDimBusTargetMPOMapper, RDimBusTargetMPO> implements RDimBusTargetMPOService {

    private final DicDataFeignClient dicDataFeignClient;
    private final RDimBusbarHarmnicDPOService rDimBusbarHarmnicDPOService;

    @Override
    public void handleMonth(CalculatedParam<DeptGetChildrenMoreDTO> calculatedParam) {
        List<RDimBusTargetMPO> rDimBusTargetMPOList = new ArrayList<>();
        //指标对象类型
        List<DictData> indicatorTypeDate = dicDataFeignClient.getDicDataByTypeCode(
                DicDataTypeEnum.STEADY_STATIS.getCode()).getData();
        //获取日期
        DateTime begin = DateUtil.beginOfMonth(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        DateTime end = DateUtil.endOfMonth(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        LocalDate localDate = LocalDateTimeUtil.parseDate( DateUtil.format(begin, DatePattern.NORM_DATE_PATTERN), DatePattern.NORM_DATE_PATTERN);

        List<String> collect = indicatorTypeDate.stream().filter(temp -> Objects.equals(temp.getCode(), DicDataEnum.VOLTAGE_DEV.getCode()) ||
                Objects.equals(temp.getCode(), DicDataEnum.FLICKER.getCode()) ||
                Objects.equals(temp.getCode(), DicDataEnum.PHASE_VOLTAGE.getCode()) ||
                Objects.equals(temp.getCode(), DicDataEnum.THD_V.getCode())).map(DictData::getId).collect(Collectors.toList());
        List<RDimBusbarHarmnicDPO> rdibBusbarHarms = rDimBusbarHarmnicDPOService.lambdaQuery().between(RDimBusbarHarmnicDPO::getStatisDate, begin,end).in(RDimBusbarHarmnicDPO::getTargetType, collect).list();
        if (CollectionUtils.isEmpty(rdibBusbarHarms)){
            return;
        }
        Map<String, Map<String, List<RDimBusbarHarmnicDPO>>> collect1 = rdibBusbarHarms.stream().collect(Collectors.groupingBy(RDimBusbarHarmnicDPO::getOrgId,
                Collectors.groupingBy(RDimBusbarHarmnicDPO::getTargetType)));
        collect1.forEach((orgId,v)->{
            v.forEach((tag,rdibBusbarHarmList)->{
                RDimBusTargetMPO rDimBusTargetMPO = new RDimBusTargetMPO();
                rDimBusTargetMPO.setOrgId(orgId);
                rDimBusTargetMPO.setTargetType(tag);
                rDimBusTargetMPO.setStatisDate(localDate);
                //有效接入母线数量
                Double effectiveBuses = Double.valueOf(rdibBusbarHarmList.stream().map(RDimBusbarHarmnicDPO::getBusbarId).distinct().count()
                );
                long count = rdibBusbarHarmList.stream().filter(temp -> temp.getLimitCount() > 0).map(RDimBusbarHarmnicDPO::getBusbarId).distinct().count();
                double v1 = count / effectiveBuses;
                double limitrate = Math.round(v1 * 100.0) / 100.0;
                rDimBusTargetMPO.setLimitRate(limitrate);
                OptionalDouble average = rdibBusbarHarmList.stream().filter(temp -> temp.getStatisCount() > 0).mapToDouble(temp -> {
                    return Double.valueOf(temp.getLimitCount()) / temp.getStatisCount();
                }).average();
                Double passRate=0.00;
                if(average.isPresent()) {
                    passRate = average.getAsDouble();
                    rDimBusTargetMPO.setPassAvgRate(1 - passRate);
                    passRate = Math.round(passRate * 100.0) / 100.0;
                    rDimBusTargetMPO.setPassRate(1 - passRate);
                }else {
                    rDimBusTargetMPO.setPassAvgRate(3.14159);

                    rDimBusTargetMPO.setPassRate(3.14159);

                }
                rDimBusTargetMPO.setLimitAvgRate(limitrate);
                rDimBusTargetMPOList.add(rDimBusTargetMPO);
            });
        });
        if (!CollectionUtils.isEmpty(rDimBusTargetMPOList)) {
            this.saveOrUpdateBatchByMultiId(rDimBusTargetMPOList, 500);
        }


    }
}
