package com.bocloud.ims.service.resource;

import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.Pager;
import com.bocloud.ims.entity.resource.CloudServer;
import com.bocloud.ims.entity.resource.Volume;
import com.bocloud.ims.interfaces.resource.ServerVolumeService;
import com.bocloud.ims.repository.resource.CloudServerRepository;
import com.bocloud.ims.repository.resource.ServerVolumeRepository;
import com.bocloud.ims.repository.resource.VolumeRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author luyanchao
 * @since 2019/3/5 9:52
 */
@Service("serverVolumeService")
public class ServerVolumeServiceImpl implements ServerVolumeService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final ServerVolumeRepository serverVolumeRepository;
    private final VolumeRepository volumeRepository;
    private final CloudServerRepository serverRepository;

    @Autowired
    public ServerVolumeServiceImpl(ServerVolumeRepository serverVolumeRepository, VolumeRepository volumeRepository, CloudServerRepository serverRepository) {
        this.serverVolumeRepository = serverVolumeRepository;
        this.volumeRepository = volumeRepository;
        this.serverRepository = serverRepository;
    }

    @Override
    public BocloudResult listVolumeByServer(long serverId, Boolean canDelete) {
        try {
            List<Volume> volumes = volumeRepository.listVolumeByServerId(serverId, canDelete);
            return new BocloudResult<>(true, volumes, "查询成功");
        } catch (Exception e) {
            logger.error("查询失败", e);
            return new BocloudResult<>(false, "查询失败：" + e.getMessage());
        }
    }

    @Override
    public BocloudResult<List<CloudServer>> listServerByVolume(long volumeId) {
        try {
            Pager pager = new Pager(1, Integer.MAX_VALUE, new ArrayList<>(), new HashMap<>());
            List<CloudServer> servers = serverRepository.listServerByVolumeId(volumeId, pager);
            return new BocloudResult<>(true, servers, "查询成功");
        } catch (Exception e) {
            logger.error("查询失败", e);
            return new BocloudResult<>(false, "查询失败：" + e.getMessage());
        }
    }
}
