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


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.DeptGetBase;
import com.njcn.harmonic.pojo.po.dim.RDimStationBaseD;
import com.njcn.harmonic.pojo.po.dim.RDimStationTargetD;
import com.njcn.prepare.harmonic.mapper.mysql.dim.RDimStationTargetDMapper;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.service.mysql.dim.IRDimStationBaseDService;
import com.njcn.prepare.harmonic.service.mysql.dim.IRDimStationTargetDService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cdf
 * @since 2023-11-21
 */
@Service
@RequiredArgsConstructor
public class RDimStationTargetDServiceImpl extends MppServiceImpl<RDimStationTargetDMapper, RDimStationTargetD> implements IRDimStationTargetDService {


    private final IRDimStationBaseDService irDimStationBaseDService;

    @Override
    public void calDimStationTargetDay(CalculatedParam<DeptGetBase> calculatedParam) {
        List<RDimStationTargetD> poList = new ArrayList<>();
        LocalDate date = LocalDate.parse(calculatedParam.getDataDate());

        List<DeptGetBase> deptList = calculatedParam.getIdList();

        for(DeptGetBase deptGetBase : deptList){
           List<String> deptCodes = deptGetBase.getUnitChildrenList();

            LambdaQueryWrapper<RDimStationBaseD> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.in(RDimStationBaseD::getOrgId,deptCodes)
                    .eq(RDimStationBaseD::getStatisDate,date).eq(RDimStationBaseD::getLevel,0);

            List<RDimStationBaseD> rDimStationBaseDList = irDimStationBaseDService.list(lambdaQueryWrapper);
            if(CollectionUtil.isEmpty(rDimStationBaseDList)){
                continue;
            }


            Map<String,List<RDimStationBaseD>> voltageMap = rDimStationBaseDList.stream().collect(Collectors.groupingBy(RDimStationBaseD::getVoltageLevel));
            voltageMap.forEach((voltageKey,voltageVal)->{
                long stationNum = voltageVal.stream().map(RDimStationBaseD::getStationId).distinct().count();

                Map<String,List<RDimStationBaseD>> targetMap = voltageVal.stream().collect(Collectors.groupingBy(RDimStationBaseD::getTargetType));
                targetMap.forEach((targetKey,targetVal)->{
                    RDimStationTargetD rDimStationTargetD = new RDimStationTargetD();
                    rDimStationTargetD.setOrgId(deptGetBase.getUnitId());
                    rDimStationTargetD.setStatisDate(LocalDate.parse(calculatedParam.getDataDate()));
                    rDimStationTargetD.setVoltageLevel(voltageKey);
                    rDimStationTargetD.setTargetType(targetKey);

                    List<String> limitList = targetVal.stream().filter(targetItem->targetItem.getLimitCount()>0).map(RDimStationBaseD::getStationId).distinct().collect(Collectors.toList());
                    rDimStationTargetD.setLimitRate((double) (limitList.size() / stationNum));
                    rDimStationTargetD.setLimitAvgRate((double) (limitList.size() / stationNum));
                    //**kV变电站**指标合格率（%）
                    int limit = targetVal.stream().mapToInt(RDimStationBaseD::getLimitCount).sum();
                    int all = targetVal.stream().mapToInt(RDimStationBaseD::getStatisCount).sum();
                    rDimStationTargetD.setPassRate((double)(all-limit)/all);
                    rDimStationTargetD.setPassAvgRate((double)(all-limit)/all);
                    rDimStationTargetD.setState(DataStateEnum.ENABLE.getCode());
                    poList.add(rDimStationTargetD);
                });
            });
            this.saveOrUpdateBatch(poList,100);
        }
    }
}
