package com.jsc.oscm.service;

import com.jsc.oscm.dao.CloudServerRepository;
import com.jsc.oscm.entity.CloudServer;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CloudServerService {

    @Resource
    CloudServerRepository cloudServerRepository;

    public CloudServer addHost(CloudServer cloudHost) {
        return cloudServerRepository.save(cloudHost);
    }

    public List<CloudServer> queryAllHostPageable(int page, int size) {
        List<CloudServer> hosts = new ArrayList<>();
        Pageable pageable = PageRequest.of(page, size);
        Page<CloudServer> hostPage = cloudServerRepository.findAll(pageable);
        if (hostPage.hasContent()) {
            hosts = hostPage.getContent();
        }
        return hosts;
    }

    public long queryAllHostCount() {
        return cloudServerRepository.count();
    }

    public List<CloudServer> queryAllHosts() {
        return cloudServerRepository.findAll();
    }

    public boolean delHosts(List<Integer> ids) {
        for (int id : ids) {
            cloudServerRepository.deleteById(id);
        }
        return true;
    }

    public boolean updateHost(CloudServer cloudHost) {
        cloudServerRepository.save(cloudHost);
        return true;
    }

}
