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.common.pojo.enums.common.DataStateEnum;
import com.njcn.device.biz.pojo.dto.DeptGetChildrenDTO;
import com.njcn.harmonic.pojo.po.dim.RDimBusGlobalD;
import com.njcn.harmonic.pojo.po.dim.RDimBusGlobalM;
import com.njcn.harmonic.pojo.po.dim.RDimBusbarHarmnicDPO;
import com.njcn.harmonic.pojo.po.dim.RDimStationBaseD;
import com.njcn.prepare.harmonic.mapper.mysql.dim.RDimBusGlobalMMapper;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.service.mysql.dim.IRDimBusGlobalMService;
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.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hongawen
 * @since 2023-11-14
 */
@Service
@RequiredArgsConstructor
public class RDimBusGlobalMServiceImpl extends MppServiceImpl<RDimBusGlobalMMapper, RDimBusGlobalM> implements IRDimBusGlobalMService {

    private final RDimBusbarHarmnicDPOService rDimBusbarHarmnicDPOService;

    private final IRDimStationBaseDService irDimStationBaseDService;

    @Override
    public void globalCalBusBarMonth(CalculatedParam<DeptGetChildrenDTO> calculatedParam) {
        List<RDimBusGlobalM> 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) {
            RDimBusGlobalM rDimBusGlobalM = new RDimBusGlobalM();

            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)){
                    rDimBusGlobalM.setLimitRate(0f);
                    rDimBusGlobalM.setLimitAvgRate(0f);
                }else {
                    long good =rDimBusbarHarmnicDPOList.stream().map(RDimBusbarHarmnicDPO::getBusbarId).distinct().count();
                    long limit =limtList.stream().map(RDimBusbarHarmnicDPO::getBusbarId).distinct().count();
                    rDimBusGlobalM.setLimitRate((float)limit/good);
                    rDimBusGlobalM.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)){
                    rDimBusGlobalM.setStationLimitRate(0f);
                    rDimBusGlobalM.setStationLimitAvgRate(0f);
                }else {

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

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

            this.saveOrUpdateBatchByMultiId(poList,100);


        }


    }

}
