package cn.reghao.autodop.dmaster.machine.db.query;

import cn.reghao.autodop.dmaster.machine.db.repository.MachineStatRepository;
import cn.reghao.autodop.dmaster.machine.model.po.MachineStat;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author reghao
 * @date 2021-06-17 22:52:32
 */
@Service
public class MachineStatQueryImpl implements MachineStatQuery {
    private final MongoTemplate mongoTemplate;
    private MachineStatRepository statRepository;

    public MachineStatQueryImpl(MongoTemplate mongoTemplate, MachineStatRepository statRepository) {
        this.mongoTemplate = mongoTemplate;
        this.statRepository = statRepository;
    }

    @Override
    public List<MachineStat> queryByMachineId(String machineId, int limit) {
        Query query = new Query();
        query.addCriteria(Criteria.where("machineId").is(machineId));
        query.with(Sort.by(Sort.Order.desc("createTime"))).limit(limit);
        return mongoTemplate.find(query, MachineStat.class);
    }
}
