package com.persagy.dmp.file.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.persagy.dmp.common.constant.ResponseCode;
import com.persagy.dmp.common.exception.BusinessException;
import com.persagy.dmp.common.helper.SpringHelper;
import com.persagy.dmp.file.constant.FileMigrateEnum;
import com.persagy.dmp.file.constant.FileStorageEnum;

/**
 * 文件存储管理工厂
 * @author Charlie Yu
 * @date 2021-05-15
 */
public class FileMigrateFactory {

    /**
     * 获取文件存储服务
     * @return
     */
    public static FileMigrateService getService(String migrateType) {
        if (StrUtil.isBlank(migrateType)){
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        FileMigrateEnum fileMigrateEnum = FileMigrateEnum.load(migrateType);
        if (null==fileMigrateEnum){
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        String beanName = fileMigrateEnum.getName();
        FileMigrateService fileMigrateService = SpringHelper.getBean(beanName, FileMigrateService.class);
        if (ObjectUtil.isEmpty(fileMigrateService)){
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        return fileMigrateService;
    }
}
