package com.njcn.influx.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.njcn.influx.bo.po.PqsOnlinerateMysql;
import com.njcn.influx.config.RedisUtil;
import com.njcn.influx.service.PqsOnlinerateMysqlService;
import com.njcn.oracle.bo.param.DataAsynParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.influx.bo.po.PqsOnlineratePO;
import com.njcn.influx.mapper.PqsOnlineratePOMapper;
import com.njcn.influx.service.PqsOnlineratePOService;

/**
 * 
 * Description:
 * Date: 2024/5/9  10:42【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class PqsOnlineratePOServiceImpl extends ServiceImpl<PqsOnlineratePOMapper, PqsOnlineratePO> implements PqsOnlineratePOService{
    private final PqsOnlinerateMysqlService pqsOnlinerateMysqlService;
    private final RedisUtil redisUtil;
    @Override
    public void minutesDataBacthSysc(DataAsynParam dataAsynParam) {
        Map<String, String> DevIdMapping = (Map<String, String>) redisUtil.getObjectByKey("DevIdMapping");
        List<PqsOnlineratePO> list = this.lambdaQuery().between(PqsOnlineratePO::getTimeid, LocalDateTimeUtil.beginOfDay(dataAsynParam.getStartDateTime()), LocalDateTimeUtil.endOfDay(dataAsynParam.getStartDateTime()).minusSeconds(1)).list();
        log.info(dataAsynParam.getStartDateTime()+"-----数据量："+list.size());
        if (CollectionUtil.isNotEmpty(list)) {
            List<PqsOnlinerateMysql> collect = new ArrayList<>();
            list.stream().forEach(temp -> {
                PqsOnlinerateMysql pqsOnlinerateMysql = new PqsOnlinerateMysql();

                if (!DevIdMapping.containsKey(temp.getDevIndex()+"")){
                   return;
                }else {
                    pqsOnlinerateMysql.setDevIndex(DevIdMapping.get(temp.getDevIndex()+""));
                }
                pqsOnlinerateMysql.setTimeId(temp.getTimeid());
                pqsOnlinerateMysql.setOnlineMin(temp.getOnlinemin());
                pqsOnlinerateMysql.setOfflineMin(temp.getOfflinemin());

                collect.add(pqsOnlinerateMysql);
            });
            QueryWrapper<PqsOnlinerateMysql> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().between(PqsOnlinerateMysql::getTimeId, LocalDateTimeUtil.beginOfDay(dataAsynParam.getStartDateTime()), LocalDateTimeUtil.endOfDay(dataAsynParam.getStartDateTime()).minusSeconds(1));
            pqsOnlinerateMysqlService.remove(queryWrapper);
            pqsOnlinerateMysqlService.saveBatch(collect);
        }
    }

    @Override
    @Async
    public void AsyncData(DataAsynParam dataAsynParam) {
        LocalDateTime startDateTime1 = dataAsynParam.getStartDateTime();
        LocalDateTime endDateTime1 = dataAsynParam.getEndDateTime();
        while (endDateTime1.isBefore(startDateTime1)) {
            log.info("执行"+startDateTime1+"时刻数据");
            DataAsynParam dataAsynParam1 = new DataAsynParam();
            dataAsynParam1.setStartDateTime(startDateTime1);
            this.minutesDataBacthSysc(dataAsynParam1);
            startDateTime1 = startDateTime1.minusDays(1);

        }
    }
}
