package com.datacenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.datacenter.entity.StatDailyAggregate;
import com.datacenter.mapper.StatDailyAggregateMapper;
import com.datacenter.service.StatDailyAggregateService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StatDailyAggregateServiceImpl extends ServiceImpl<StatDailyAggregateMapper, StatDailyAggregate>
        implements StatDailyAggregateService {

    @Override
    public List<StatDailyAggregate> listRecentByMetricKey(String metricKey, int limitDays) {
        int limit = Math.min(Math.max(limitDays, 1), 366);
        return list(new LambdaQueryWrapper<StatDailyAggregate>()
                .eq(StatDailyAggregate::getMetricKey, metricKey)
                .orderByDesc(StatDailyAggregate::getBizDate)
                .last("LIMIT " + limit));
    }
}
