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 com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.device.biz.pojo.dto.DeptGetChildrenDTO;
import com.njcn.harmonic.pojo.po.dim.RDimBusStationEventM;
import com.njcn.harmonic.pojo.po.dim.RDimBusStationEventY;
import com.njcn.harmonic.pojo.po.dim.RDimBusbarHarmnicDPO;
import com.njcn.harmonic.pojo.po.dim.RDimStationBaseD;
import com.njcn.prepare.harmonic.mapper.mysql.dim.RDimBusStationEventYMapper;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.service.mysql.dim.IRDimBusStationEventYService;
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 RDimBusStationEventYServiceImpl extends MppServiceImpl<RDimBusStationEventYMapper, RDimBusStationEventY> implements IRDimBusStationEventYService {

    private final RDimBusbarHarmnicDPOService rDimBusbarHarmnicDPOService;

    private final IRDimStationBaseDService irDimStationBaseDService;

    @Override
    public void calEventYear(CalculatedParam<DeptGetChildrenDTO> calculatedParam) {
        List<RDimBusStationEventY> poList = new ArrayList<>();

        DateTime start = DateUtil.beginOfYear(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_FORMAT));
        DateTime end = DateUtil.endOfYear(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_FORMAT));


        List<DeptGetChildrenDTO> deptList = calculatedParam.getIdList();
        for(DeptGetChildrenDTO dto : deptList){
            List<String> deptIds = dto.getDeptChildren();
            RDimBusStationEventY rDimBusStationEventY = new RDimBusStationEventY();
            rDimBusStationEventY.setOrgId(dto.getDeptId());
            rDimBusStationEventY.setStatisDate(LocalDate.parse(DateUtil.format(start,DatePattern.NORM_DATE_PATTERN)));

            QueryWrapper<RDimBusbarHarmnicDPO> lambdaQueryWrapper = new QueryWrapper<>();
            lambdaQueryWrapper.select("BusBar_Id,Statis_Date,sum(limit_count) as limit_count");
            lambdaQueryWrapper.lambda().in(RDimBusbarHarmnicDPO::getOrgId,deptIds).between(RDimBusbarHarmnicDPO::getStatisDate,start,end).eq(RDimBusbarHarmnicDPO::getLevel,1);
            lambdaQueryWrapper.groupBy("Statis_Date,BusBar_Id");




            List<RDimBusbarHarmnicDPO> rDimBusbarHarmnicDPOList = rDimBusbarHarmnicDPOService.list(lambdaQueryWrapper);
            if(CollUtil.isNotEmpty(rDimBusbarHarmnicDPOList)){
                List<RDimBusbarHarmnicDPO> limitBus = rDimBusbarHarmnicDPOList.stream().filter(item->item.getLimitCount()>0).collect(Collectors.toList());
                if(CollUtil.isEmpty(limitBus)){
                    rDimBusStationEventY.setLimitRate(0f);
                    rDimBusStationEventY.setLimitFreq(0f);
                }else {
                    long allBus = rDimBusbarHarmnicDPOList.stream().map(RDimBusbarHarmnicDPO::getBusbarId).distinct().count();
                    long limit = limitBus.stream().map(RDimBusbarHarmnicDPO::getBusbarId).distinct().count();
                    rDimBusStationEventY.setLimitRate((float)limit/allBus);

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

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

            QueryWrapper<RDimStationBaseD> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("Station_Id,Statis_Date,sum(limit_count) as limit_count");
            queryWrapper.lambda().in(RDimStationBaseD::getOrgId,deptIds).between(RDimStationBaseD::getStatisDate,start,end);
            queryWrapper.groupBy("Statis_Date,Station_Id");
            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)){
                    rDimBusStationEventY.setStationLimitRate(0f);
                    rDimBusStationEventY.setStationFreq(0f);
                }else {
                    long limitNum = limitStation.stream().map(RDimStationBaseD::getStationId).distinct().count();
                    rDimBusStationEventY.setStationLimitRate((float)limitNum/allStation);
                    int sum = limitStation.stream().mapToInt(RDimStationBaseD::getLimitCount).sum();
                    rDimBusStationEventY.setStationFreq((float)sum/allStation);
                }
            }


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

        }

        this.saveOrUpdateBatchByMultiId(poList,100);

    }
}
