package com.bocloud.ims.service.resource.azure.checkname;

import com.bocloud.cmp.driver.azure.checkname.ResourceCheckNameDriver;
import com.bocloud.cmp.params.azure.ResourceCheckNameParams;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.RequestContext;
import com.bocloud.ims.entity.resource.CloudVendor;
import com.bocloud.ims.interfaces.resource.azure.checkname.ResourceCheckNameService;
import com.bocloud.ims.repository.resource.CloudVendorRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: ResourceCheckNameServiceImpl
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2021/3/10 11:21
 */
@Service
public class ResourceCheckNameServiceImpl implements ResourceCheckNameService {

    private static Logger logger = LoggerFactory.getLogger(ResourceCheckNameServiceImpl.class);

    @Autowired
    private CloudVendorRepository vendorRepository;
    @Autowired
    private ResourceCheckNameDriver resourceCheckNameDriver;

    @Override
    public BocloudResult checkName(ResourceCheckNameParams params, RequestContext context) {
        String name = params.getName();
        String type = params.getType();
        Long vendorId = params.getVendorId();
        try {
            CloudVendor vendor = vendorRepository.query(vendorId);
            if (ObjectUtils.isEmpty(vendor)) {
                logger.error("get cloudVendor failure");
                return new BocloudResult(false, "平台验证失败");
            }
            return resourceCheckNameDriver.checkName(vendor.getUuid(), name, type);
        } catch (Exception e) {
            logger.error("resource checkName error!", e);
            return new BocloudResult(false, "名称校验失败:" + e.getMessage());
        }
    }
}