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.LambdaQueryWrapper;
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.RDimBusStationEventD;
import com.njcn.harmonic.pojo.po.dim.RDimBusStationEventM;
import com.njcn.harmonic.pojo.po.dim.RDimBusbarHarmnicDPO;
import com.njcn.harmonic.pojo.po.dim.RDimStationBaseD;
import com.njcn.prepare.harmonic.mapper.mysql.dim.RDimBusStationEventMMapper;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.service.mysql.dim.IRDimBusStationEventMService;
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.time.format.DateTimeFormatter;
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 RDimBusStationEventMServiceImpl extends MppServiceImpl<RDimBusStationEventMMapper, RDimBusStationEventM> implements IRDimBusStationEventMService {

    private final RDimBusbarHarmnicDPOService rDimBusbarHarmnicDPOService;

    private final IRDimStationBaseDService irDimStationBaseDService;

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

        DateTime start = DateUtil.beginOfMonth(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_FORMAT));
        DateTime end = DateUtil.endOfMonth(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_FORMAT));


        List<DeptGetChildrenDTO> deptList = calculatedParam.getIdList();
        for(DeptGetChildrenDTO dto : deptList){
            List<String> deptIds = dto.getDeptChildren();
            RDimBusStationEventM rDimBusStationEventM = new RDimBusStationEventM();
            rDimBusStationEventM.setOrgId(dto.getDeptId());
            rDimBusStationEventM.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)){
                    rDimBusStationEventM.setLimitRate(0f);
                    rDimBusStationEventM.setLimitFreq(0f);
                }else {
                    long allBus = rDimBusbarHarmnicDPOList.stream().map(RDimBusbarHarmnicDPO::getBusbarId).distinct().count();
                    long limit = limitBus.stream().map(RDimBusbarHarmnicDPO::getBusbarId).distinct().count();
                    rDimBusStationEventM.setLimitRate((float)limit/allBus);

                    int sum = limitBus.stream().mapToInt(RDimBusbarHarmnicDPO::getLimitCount).sum();
                    rDimBusStationEventM.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)){
                    rDimBusStationEventM.setStationLimitRate(0f);
                    rDimBusStationEventM.setStationFreq(0f);
                }else {
                    long limitNum = limitStation.stream().map(RDimStationBaseD::getStationId).distinct().count();
                    rDimBusStationEventM.setStationLimitRate((float)limitNum/allStation);
                    int sum = limitStation.stream().mapToInt(RDimStationBaseD::getLimitCount).sum();
                    rDimBusStationEventM.setStationFreq((float)sum/allStation);
                }
            }


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

        }

        this.saveOrUpdateBatchByMultiId(poList,100);


    }
}
