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

import cn.hutool.core.collection.CollUtil;
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.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.device.biz.pojo.dto.DeptGetChildrenMoreDTO;
import com.njcn.harmonic.pojo.po.dim.RDimBusStationEventStartisYPO;
import com.njcn.harmonic.pojo.po.dim.RDimBusbarHarmnicDPO;
import com.njcn.harmonic.pojo.po.dim.RDimStationHarmnicDPO;
import com.njcn.prepare.harmonic.mapper.mysql.dim.RDimBusStationEventStartisYPOMapper;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.service.mysql.dim.RDimBusStationEventStartisYPOService;
import com.njcn.prepare.harmonic.service.mysql.dim.RDimBusbarHarmnicDPOService;
import com.njcn.prepare.harmonic.service.mysql.dim.RDimStationHarmnicDPOService;
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.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 
 * Description:
 * Date: 2023/11/27  15:10【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class RDimBusStationEventStartisYPOServiceImpl extends MppServiceImpl<RDimBusStationEventStartisYPOMapper, RDimBusStationEventStartisYPO> implements RDimBusStationEventStartisYPOService{


    private final RDimBusbarHarmnicDPOService rDimBusbarHarmnicDPOService;
    private final RDimStationHarmnicDPOService rDimStationHarmnicDPOService;
    private final DicDataFeignClient dicDataFeignClient;

    @Override
    public void handleYear(CalculatedParam<DeptGetChildrenMoreDTO> calculatedParam) {
        List<RDimBusStationEventStartisYPO> poList = new ArrayList<>();
        //获取日期
        DateTime begin = DateUtil.beginOfMonth(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        DateTime end = DateUtil.endOfMonth(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        List<DictData> eventStatis = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.EVENT_STATIS.getCode()).getData();
        LocalDate localDate = LocalDateTimeUtil.parseDate( DateUtil.format(begin, DatePattern.NORM_DATE_PATTERN), DatePattern.NORM_DATE_PATTERN);


        for (DeptGetChildrenMoreDTO deptGetChildrenMoreDTO : calculatedParam.getIdList()) {
            LambdaQueryWrapper<RDimBusbarHarmnicDPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(RDimBusbarHarmnicDPO::getOrgId,deptGetChildrenMoreDTO.getUnitId())
                    .between(RDimBusbarHarmnicDPO::getStatisDate,begin,end)
                    .eq(RDimBusbarHarmnicDPO::getLevel,1);
            List<RDimBusbarHarmnicDPO> rDimBusbarHarmnicDPOList = rDimBusbarHarmnicDPOService.list(lambdaQueryWrapper);
            List<RDimStationHarmnicDPO> rDimStationHarmnicDPOList = rDimStationHarmnicDPOService.lambdaQuery().eq(RDimStationHarmnicDPO::getOrgId, deptGetChildrenMoreDTO.getUnitId())
                    .between(RDimStationHarmnicDPO::getStatisDate,begin,end)
                    .eq(RDimStationHarmnicDPO::getLevel, 1).list();

            //暂态
            eventStatis.forEach(dictData -> {
                if (Objects.equals(dictData.getCode(), DicDataEnum.DISTURBANCE.getCode())
                        || Objects.equals(dictData.getCode(), DicDataEnum.OTHER.getCode())
                        || Objects.equals(dictData.getCode(), DicDataEnum.RECORDING_WAVE.getCode())
                        || Objects.equals(dictData.getCode(), DicDataEnum.TOTAL_INDICATORS.getCode())) {
                    return;
                }
                RDimBusStationEventStartisYPO rDimBusStationEventStartisYPO = new RDimBusStationEventStartisYPO();
                rDimBusStationEventStartisYPO.setOrgId(deptGetChildrenMoreDTO.getUnitId());
                rDimBusStationEventStartisYPO.setStatisDate(localDate);
                rDimBusStationEventStartisYPO.setStartisType(dictData.getId());


                List<RDimBusbarHarmnicDPO> rDimBusbarHarmnicDPOListBytag = rDimBusbarHarmnicDPOList.stream().filter(temp -> Objects.equals(temp.getTargetType(), dictData.getId())).collect(Collectors.toList());
                List<RDimStationHarmnicDPO> rDimStationHarmnicDPOListBytag = rDimStationHarmnicDPOList.stream().filter(temp -> Objects.equals(temp.getTargetType(), dictData.getId())).collect(Collectors.toList());
                if(CollUtil.isEmpty(rDimBusbarHarmnicDPOListBytag)){
                    rDimBusStationEventStartisYPO.setLimitRate(0.00);
                    rDimBusStationEventStartisYPO.setLimitFreq(0.00);
                }else {
                    long allBus = rDimBusbarHarmnicDPOListBytag.stream().map(RDimBusbarHarmnicDPO::getBusbarId).distinct().count();
                    List<RDimBusbarHarmnicDPO> limitBus = rDimBusbarHarmnicDPOListBytag.stream().filter(item->item.getLimitCount()>0).collect(Collectors.toList());

                    long limit = rDimBusbarHarmnicDPOListBytag.stream().map(RDimBusbarHarmnicDPO::getBusbarId).distinct().count();
                    rDimBusStationEventStartisYPO.setLimitRate(Double.valueOf((float)limit/allBus));

                    int sum = limitBus.stream().mapToInt(RDimBusbarHarmnicDPO::getLimitCount).sum();
                    rDimBusStationEventStartisYPO.setLimitFreq(Double.valueOf((float)sum/allBus));

                }
                if(CollUtil.isEmpty(rDimStationHarmnicDPOListBytag)){
                    rDimBusStationEventStartisYPO.setStationFreq(0.00);
                    rDimBusStationEventStartisYPO.setStationLimitRate(0.00);
                }else {
                    long allBus = rDimStationHarmnicDPOListBytag.stream().map(RDimStationHarmnicDPO::getStationId).distinct().count();
                    List<RDimStationHarmnicDPO> limitstation = rDimStationHarmnicDPOListBytag.stream().filter(item->item.getLimitCount()>0).collect(Collectors.toList());

                    long limit = rDimStationHarmnicDPOListBytag.stream().map(RDimStationHarmnicDPO::getStationId).distinct().count();
                    rDimBusStationEventStartisYPO.setLimitRate(Double.valueOf((float)limit/allBus));

                    int sum = limitstation.stream().mapToInt(RDimStationHarmnicDPO::getLimitCount).sum();
                    rDimBusStationEventStartisYPO.setLimitFreq(Double.valueOf((float)sum/allBus));

                }

                poList.add(rDimBusStationEventStartisYPO);

            });


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