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


import cn.hutool.core.collection.CollUtil;
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.device.biz.pojo.dto.DeptGetChildrenDTO;
import com.njcn.device.pms.pojo.po.Monitor;
import com.njcn.harmonic.pojo.po.dim.RDimMonitorRateD;
import com.njcn.harmonic.pojo.po.dim.RDimObjRateD;
import com.njcn.prepare.harmonic.mapper.mysql.area.PmsMonitorPOMapper;
import com.njcn.prepare.harmonic.mapper.mysql.dim.RDimObjRateDMapper;
import com.njcn.prepare.harmonic.mapper.mysql.line.ROperatingMonitorDMapper;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.pojo.mysql.ROperatingMonitorDPO;
import com.njcn.prepare.harmonic.service.mysql.dim.IRDimObjRateDService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.enums.DicDataTypeEnum;
import com.njcn.system.pojo.po.DictData;
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-23
 */
@Service
@RequiredArgsConstructor
public class RDimObjRateDServiceImpl extends MppServiceImpl<RDimObjRateDMapper, RDimObjRateD> implements IRDimObjRateDService {

    private final DicDataFeignClient dicDataFeignClient;

    private final ROperatingMonitorDMapper rOperatingMonitorDMapper;

    private final PmsMonitorPOMapper pmsMonitorPOMapper;


    @Override
    public void objMonitorRateDay(CalculatedParam<DeptGetChildrenDTO> calculatedParam) {
        List<RDimObjRateD> poList = new ArrayList<>();
        LocalDate date = LocalDate.parse(calculatedParam.getDataDate());

        List<DictData> dictDataList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.MONITORING_LABELS.getCode()).getData();
        dictDataList = dictDataList.stream().filter(item -> item.getCode().equals(DicDataEnum.ONSHORE_WIND.getCode()) || item.getCode().equals(DicDataEnum.POWER_STATION.getCode()) || item.getCode().equals(DicDataEnum.POWER_STATION.getCode()) || item.getCode().equals(DicDataEnum.ONSHORE_WIND.getCode())).collect(Collectors.toList());

        List<DeptGetChildrenDTO> deptList = calculatedParam.getIdList();
        for (DeptGetChildrenDTO dto : deptList) {
            List<String> monitorIds = dto.getLineIds();

            if (CollUtil.isEmpty(monitorIds)) {
                for (DictData dictData : dictDataList) {
                    RDimObjRateD rDimObjRateD = new RDimObjRateD();
                    rDimObjRateD.setOrgId(dto.getDeptId());
                    rDimObjRateD.setStatisDate(date);
                    rDimObjRateD.setObjType(dictData.getId());
                    rDimObjRateD.setObjMonitorRate(0f);
                    poList.add(rDimObjRateD);
                }
                continue;
            }

            LambdaQueryWrapper<ROperatingMonitorDPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(ROperatingMonitorDPO::getDataDate, date)
                    .in(ROperatingMonitorDPO::getMeasurementPointId, monitorIds)
                    .eq(ROperatingMonitorDPO::getIsUnusual, 1).ge(ROperatingMonitorDPO::getIndexIntegrityRate, 0.95);
            List<ROperatingMonitorDPO> rOperatingMonitorDPOList = rOperatingMonitorDMapper.selectList(lambdaQueryWrapper);

            if (CollUtil.isNotEmpty(rOperatingMonitorDPOList)) {
                List<String> ids = rOperatingMonitorDPOList.stream().map(ROperatingMonitorDPO::getMeasurementPointId).collect(Collectors.toList());

                LambdaQueryWrapper<Monitor> monitorLambdaQueryWrapper = new LambdaQueryWrapper<>();
                monitorLambdaQueryWrapper.in(Monitor::getId, ids);
                List<Monitor> monitorList = pmsMonitorPOMapper.selectList(monitorLambdaQueryWrapper);
                Map<String,List<Monitor>> map = monitorList.stream().collect(Collectors.groupingBy(Monitor::getMonitorTag));

                for (DictData dictData : dictDataList) {
                    RDimObjRateD rDimObjRateD = new RDimObjRateD();
                    rDimObjRateD.setOrgId(dto.getDeptId());
                    rDimObjRateD.setStatisDate(date);
                    rDimObjRateD.setObjType(dictData.getId());
                    if(map.containsKey(dictData.getId())){
                        List<Monitor> temList = map.get(dictData.getId());
                        rDimObjRateD.setObjMonitorRate((float)temList.size()/100);
                    }else {
                        rDimObjRateD.setObjMonitorRate(0f);
                    }
                    poList.add(rDimObjRateD);
                }
            }else {
                for (DictData dictData : dictDataList) {
                    RDimObjRateD rDimObjRateD = new RDimObjRateD();
                    rDimObjRateD.setOrgId(dto.getDeptId());
                    rDimObjRateD.setStatisDate(date);
                    rDimObjRateD.setObjType(dictData.getId());
                    rDimObjRateD.setObjMonitorRate(0f);
                    poList.add(rDimObjRateD);
                }
            }

        }
        this.saveOrUpdateBatchByMultiId(poList,100);
    }
}
