package com.cico.nest.lcdp.boot.core.qxsd.application.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cico.nest.lcdp.boot.core.qxsd.application.service.TunnelService;
import com.cico.nest.lcdp.boot.core.qxsd.domain.gateway.TunnelGateway;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.TrafficFlowMetricMapper;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.TrafficFlowMetricDO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author zws
 * @date 2024/8/8
 */
@Service
public class TunnelServiceImpl implements TunnelService {

    @Resource
    private TunnelGateway tunnelGateway;
    @Resource
    private TrafficFlowMetricMapper trafficFlowMetricMapper;


    @Override
    public void saveTrafficFlowMetricByTunnelId(String tunnelId) {
        TrafficFlowMetricDO trafficFlowMetric = tunnelGateway.getTrafficFlowMetric(tunnelId);
        if (trafficFlowMetric!=null){
            trafficFlowMetricMapper.insert(trafficFlowMetric);
        }
    }

    @Override
    public List<TrafficFlowMetricDO> getTrafficFlowMetricList(String tunnelId) {
        LambdaQueryWrapper<TrafficFlowMetricDO> query = Wrappers.lambdaQuery();
        query.eq(TrafficFlowMetricDO::getTunnelId,tunnelId);
        query.ge(TrafficFlowMetricDO::getCreateTime, DateUtil.beginOfDay(new Date()));
        query.orderByAsc(TrafficFlowMetricDO::getCreateTime);
        return trafficFlowMetricMapper.selectList(query);
    }
}
