package com.bocloud.ims.service.resource.fusionsphere.utils;

import com.bocloud.cmp.entity.enums.CloudProvider;
import com.bocloud.ims.entity.resource.CloudVendor;
import com.bocloud.ims.entity.resource.DataStore;
import com.bocloud.ims.repository.resource.CloudVendorRepository;
import com.bocloud.ims.repository.resource.DataStoreRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: FusionSphereResourceUtils
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2021/11/26 10:02
 */
@Service
public class FusionSphereResourceUtils {

    @Autowired
    private DataStoreRepository dataStoreRepository;
    @Autowired
    private CloudVendorRepository vendorRepository;

    public void modifyDataStore(Long id, Integer size, String method) {
        if (ObjectUtils.isEmpty(id)) {
            return;
        }
        DataStore dataStore = dataStoreRepository.query(id);
        if (ObjectUtils.isEmpty(dataStore)) {
            return;
        }
        CloudVendor vendor = vendorRepository.query(dataStore.getVendorId());
        if (ObjectUtils.isEmpty(vendor)) {
            return;
        }
        if (!CloudProvider.FUSIONSPHERE.name().equals(vendor.getType())) {
            return;
        }
        Long num;
        switch (method) {
            case "subtraction":
                num = dataStore.getFreeSpace() + size;
                break;
            case "add":
                num = dataStore.getFreeSpace() - size;
                break;
            default:
                num = dataStore.getFreeSpace();
                break;
        }
        dataStore.setFreeSpace(num);
        dataStoreRepository.update(dataStore);
    }

}