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


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.device.biz.pojo.dto.DeptGetChildrenDTO;
import com.njcn.harmonic.pojo.po.dim.RDimBusStationEventD;
import com.njcn.harmonic.pojo.po.dim.RDimBusbarHarmnicDPO;
import com.njcn.harmonic.pojo.po.dim.RDimStationBaseD;
import com.njcn.prepare.harmonic.mapper.mysql.dim.RDimBusStationEventDMapper;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.service.mysql.dim.IRDimBusStationEventDService;
import com.njcn.prepare.harmonic.service.mysql.dim.IRDimStationBaseDService;
import com.njcn.prepare.harmonic.service.mysql.dim.RDimBusbarHarmnicDPOService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cdf
 * @since 2023-11-24
 */
@Service
@RequiredArgsConstructor
public class RDimBusStationEventDServiceImpl extends MppServiceImpl<RDimBusStationEventDMapper, RDimBusStationEventD> implements IRDimBusStationEventDService {

    private final RDimBusbarHarmnicDPOService rDimBusbarHarmnicDPOService;

    private final IRDimStationBaseDService irDimStationBaseDService;

    @Override
    public void calEventDay(CalculatedParam<DeptGetChildrenDTO> calculatedParam) {
        List<RDimBusStationEventD> poList = new ArrayList<>();
        LocalDate date = LocalDate.parse(calculatedParam.getDataDate());

        List<DeptGetChildrenDTO> deptList = calculatedParam.getIdList();
        for(DeptGetChildrenDTO dto : deptList){
            List<String> deptIds = dto.getDeptChildren();
            RDimBusStationEventD rDimBusStationEventD = new RDimBusStationEventD();
            rDimBusStationEventD.setOrgId(dto.getDeptId());
            rDimBusStationEventD.setStatisDate(date);

            LambdaQueryWrapper<RDimBusbarHarmnicDPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.in(RDimBusbarHarmnicDPO::getOrgId,deptIds)
                    .eq(RDimBusbarHarmnicDPO::getStatisDate,date)
                    .eq(RDimBusbarHarmnicDPO::getLevel,1);

            List<RDimBusbarHarmnicDPO> rDimBusbarHarmnicDPOList = rDimBusbarHarmnicDPOService.list(lambdaQueryWrapper);
            if(CollUtil.isNotEmpty(rDimBusbarHarmnicDPOList)){
                long allBus = rDimBusbarHarmnicDPOList.stream().map(RDimBusbarHarmnicDPO::getBusbarId).distinct().count();
                List<RDimBusbarHarmnicDPO> limitBus = rDimBusbarHarmnicDPOList.stream().filter(item->item.getLimitCount()>0).collect(Collectors.toList());

                if(CollUtil.isEmpty(limitBus)){
                    rDimBusStationEventD.setLimitRate(0f);
                    rDimBusStationEventD.setLimitFreq(0f);
                }else {
                    long limit = limitBus.stream().map(RDimBusbarHarmnicDPO::getBusbarId).distinct().count();
                    rDimBusStationEventD.setLimitRate((float)limit/allBus);

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

                    //TODO 后续加入暂态指标的代码
                }
            }

            LambdaQueryWrapper<RDimStationBaseD> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(RDimStationBaseD::getOrgId,deptIds)
                    .eq(RDimStationBaseD::getStatisDate,date)
                    .eq(RDimStationBaseD::getLevel,1);

            List<RDimStationBaseD> rDimStationBaseDList = irDimStationBaseDService.list(queryWrapper);
            if(CollUtil.isNotEmpty(rDimStationBaseDList)){
                long allStation = rDimStationBaseDList.stream().map(RDimStationBaseD::getStationId).distinct().count();
                List<RDimStationBaseD> limitStation = rDimStationBaseDList.stream().filter(item->item.getLimitCount()>0).collect(Collectors.toList());

                if(CollUtil.isEmpty(limitStation)){
                    rDimBusStationEventD.setStationLimitRate(0f);
                    rDimBusStationEventD.setStationFreq(0f);
                }else {
                    long limitNum = limitStation.stream().map(RDimStationBaseD::getStationId).distinct().count();
                    rDimBusStationEventD.setLimitRate((float)limitNum/allStation);

                    int sum = limitStation.stream().mapToInt(RDimStationBaseD::getLimitCount).sum();
                    rDimBusStationEventD.setLimitFreq((float)sum/allStation);
                }
            }

            if(CollUtil.isNotEmpty(rDimBusbarHarmnicDPOList) && CollUtil.isEmpty(rDimStationBaseDList)){
                rDimBusStationEventD.setStationLimitRate(3.14159f);
                rDimBusStationEventD.setStationFreq(3.14159f);
            }
            if(CollUtil.isEmpty(rDimBusbarHarmnicDPOList) && CollUtil.isNotEmpty(rDimStationBaseDList)){
                rDimBusStationEventD.setLimitRate(3.14159f);
                rDimBusStationEventD.setLimitFreq(3.14159f);
            }
            poList.add(rDimBusStationEventD);

        }
        this.saveOrUpdateBatchByMultiId(poList,100);

    }
}
