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.RDimBusGlobalM;
import com.njcn.harmonic.pojo.po.dim.RDimBusGlobalY;
import com.njcn.harmonic.pojo.po.dim.RDimBusbarHarmnicDPO;
import com.njcn.harmonic.pojo.po.dim.RDimStationBaseD;
import com.njcn.prepare.harmonic.mapper.mysql.dim.RDimBusGlobalYMapper;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.service.mysql.dim.IRDimBusGlobalYService;
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.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hongawen
 * @since 2023-11-14
 */
@Service
@RequiredArgsConstructor
public class RDimBusGlobalYServiceImpl extends MppServiceImpl<RDimBusGlobalYMapper, RDimBusGlobalY> implements IRDimBusGlobalYService {

    private final RDimBusbarHarmnicDPOService rDimBusbarHarmnicDPOService;

    private final IRDimStationBaseDService irDimStationBaseDService;

    @Override
    public void globalCalBusBarYear(CalculatedParam<DeptGetChildrenDTO> calculatedParam) {

        List<RDimBusGlobalY> 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) {
            RDimBusGlobalY rDimBusGlobalY = new RDimBusGlobalY();

            List<String> childDept = dto.getDeptChildren();
            QueryWrapper<RDimBusbarHarmnicDPO> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("BusBar_Id,Statis_Date,sum(limit_count) as limit_count");
            queryWrapper.lambda().in(RDimBusbarHarmnicDPO::getOrgId,childDept).between(RDimBusbarHarmnicDPO::getStatisDate,start,end);
            queryWrapper.groupBy("Statis_Date,BusBar_Id");

            List<RDimBusbarHarmnicDPO> rDimBusbarHarmnicDPOList = rDimBusbarHarmnicDPOService.list(queryWrapper);

            if(CollUtil.isNotEmpty(rDimBusbarHarmnicDPOList)){
                List<RDimBusbarHarmnicDPO> limtList =  rDimBusbarHarmnicDPOList.stream().filter(item->item.getLimitCount()>0).collect(Collectors.toList());
                if(CollUtil.isEmpty(limtList)){
                    rDimBusGlobalY.setLimitRate(0f);
                    rDimBusGlobalY.setLimitAvgRate(0f);
                }else {
                    long good =rDimBusbarHarmnicDPOList.stream().map(RDimBusbarHarmnicDPO::getBusbarId).distinct().count();
                    long limit =limtList.stream().map(RDimBusbarHarmnicDPO::getBusbarId).distinct().count();
                    rDimBusGlobalY.setLimitRate((float)limit/good);
                    rDimBusGlobalY.setLimitAvgRate((float)limtList.size()/rDimBusbarHarmnicDPOList.size());
                }
            }


            LambdaQueryWrapper<RDimStationBaseD> stationQueryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.select("Station_Id,Statis_Date,sum(limit_count) as limit_count");
            queryWrapper.lambda().in(RDimBusbarHarmnicDPO::getOrgId,childDept).between(RDimBusbarHarmnicDPO::getStatisDate,start,end);
            queryWrapper.groupBy("Statis_Date,Station_Id");
            List<RDimStationBaseD> rDimStationBaseDList = irDimStationBaseDService.list(stationQueryWrapper);

            if(CollUtil.isNotEmpty(rDimStationBaseDList)){
                List<RDimStationBaseD> limtList =  rDimStationBaseDList.stream().filter(item->item.getLimitCount()>0).collect(Collectors.toList());
                if(CollUtil.isEmpty(limtList)){
                    rDimBusGlobalY.setStationLimitRate(0f);
                    rDimBusGlobalY.setStationLimitAvgRate(0f);
                }else {

                    long good =rDimStationBaseDList.stream().map(RDimStationBaseD::getStationId).distinct().count();
                    long limit =limtList.stream().map(RDimStationBaseD::getStationId).distinct().count();
                    rDimBusGlobalY.setStationLimitRate((float)limit/good);
                    rDimBusGlobalY.setStationLimitAvgRate((float)limtList.size()/rDimStationBaseDList.size());
                }
            }

            if(CollUtil.isEmpty(rDimBusbarHarmnicDPOList) && CollUtil.isNotEmpty(rDimStationBaseDList)){
                rDimBusGlobalY.setLimitRate(3.14159f);
                rDimBusGlobalY.setLimitAvgRate(3.14159f);
            }
            if(CollUtil.isNotEmpty(rDimBusbarHarmnicDPOList) && CollUtil.isEmpty(rDimStationBaseDList)){
                rDimBusGlobalY.setStationLimitRate(3.14159f);
                rDimBusGlobalY.setStationLimitAvgRate(3.14159f);
            }
            poList.add(rDimBusGlobalY);

            this.saveOrUpdateBatchByMultiId(poList,100);


        }

    }
}
