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

import cn.reghao.autodop.dmaster.machine.db.repository.MachineInfoRepository;
import cn.reghao.autodop.dmaster.machine.model.po.MachineInfo;
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-09-15 16:35:24
 */
@Service
public class MachineInfoQueryImpl implements MachineInfoQuery {
    private MongoTemplate mongoTemplate;
    private final MachineInfoRepository infoRepository;

    public MachineInfoQueryImpl(MongoTemplate mongoTemplate, MachineInfoRepository infoRepository) {
        this.mongoTemplate = mongoTemplate;
        this.infoRepository = infoRepository;
    }

    @Override
    public List<MachineInfo> findAll() {
        return infoRepository.findAll();
    }

    @Override
    public MachineInfo findByMachineId(String machineId) {
        return infoRepository.findByMachineId(machineId);
    }

    @Override
    public List<MachineInfo> findByMachineIds(List<String> machineIds) {
        Query query = new Query();
        query.addCriteria(Criteria.where("machineId").in(machineIds));
        return mongoTemplate.find(query, MachineInfo.class);
    }

    @Override
    public List<MachineInfo> findByMachineIpv4(String machineIpv4) {
        Query query = new Query();
        query.addCriteria(Criteria.where("networkInfos.ipv4").in(machineIpv4));
        return mongoTemplate.find(query, MachineInfo.class);
    }
}
