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

import cn.hutool.core.date.DatePattern;
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.RDimBusbarHarmnicDPO;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.service.mysql.dim.RDimBusTargetDPOService;
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 com.njcn.harmonic.pojo.po.dim.RDimBusTargetDPO;
import com.njcn.prepare.harmonic.mapper.mysql.dim.RDimBusTargetDPOMapper;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

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


    private final DicDataFeignClient dicDataFeignClient;
    private final RDimBusbarHarmnicDPOService rDimBusbarHarmnicDPOService;

    @Override
    public void handleDay(CalculatedParam<DeptGetChildrenMoreDTO> calculatedParam) {
        List<RDimBusTargetDPO>  rDimBusTargetDPOList = new ArrayList<>();
        //指标对象类型
        List<DictData> indicatorTypeDate = dicDataFeignClient.getDicDataByTypeCode(
                DicDataTypeEnum.STEADY_STATIS.getCode()).getData();
        LocalDate localDate = LocalDateTimeUtil.parseDate(calculatedParam.getDataDate(), 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().eq(RDimBusbarHarmnicDPO::getStatisDate, localDate).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)->{
                RDimBusTargetDPO rDimBusTargetDPO = new RDimBusTargetDPO();
                rDimBusTargetDPO.setOrgId(orgId);
                rDimBusTargetDPO.setTargetType(tag);
                rDimBusTargetDPO.setStatisDate(localDate);
                //有效接入母线数量
                Double effectiveBuses = Double.valueOf(rdibBusbarHarmList.size());
                long count = rdibBusbarHarmList.stream().filter(temp -> temp.getLimitCount() > 0).count();
                double v1 = count / effectiveBuses;
                double limitrate = Math.round(v1 * 100.0) / 100.0;
                rDimBusTargetDPO.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();
                    rDimBusTargetDPO.setPassAvgRate(1 - passRate);
                    passRate = Math.round(passRate * 100.0) / 100.0;
                    rDimBusTargetDPO.setPassRate(1 - passRate);
                }else {
                    rDimBusTargetDPO.setPassAvgRate(3.14159);

                    rDimBusTargetDPO.setPassRate(3.14159);

                }

                rDimBusTargetDPO.setLimitAvgRate(limitrate);

                rDimBusTargetDPOList.add(rDimBusTargetDPO);
            });
        });
        if (!CollectionUtils.isEmpty(rDimBusTargetDPOList)) {
            this.saveOrUpdateBatchByMultiId(rDimBusTargetDPOList, 500);
        }


    }


}
