package com.yd.oss.service.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.yd.oss.service.model.OssProvider;
import com.yd.oss.service.service.IOssProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class OssClientFactory {

    @Autowired
    private IOssProviderService ossProviderService;

    /**
     * 根据服务商业务ID创建临时OSS客户端
     * @param providerBizId 服务商业务ID
     * @return OSS客户端（使用后必须调用shutdown()）
     */
    public OSS createOssClient(String providerBizId) {
        OssProvider provider = ossProviderService.getProviderByBizId(providerBizId);
        return new OSSClientBuilder().build(
                provider.getEndpoint(),
                provider.getAccessKey(),
                provider.getSecretKey()
        );
    }

    /**
     * 根据项目ID创建临时OSS客户端
     * @param projectBizId 项目业务ID
     * @return OSS客户端（使用后必须调用shutdown()）
     */
    public OSS createOssClientByProject(String projectBizId) {
        OssProvider provider = ossProviderService.getProviderByProjectId(projectBizId);
        return new OSSClientBuilder().build(
                provider.getEndpoint(),
                provider.getAccessKey(),
                provider.getSecretKey()
        );
    }

    /**
     * 直接根据OssProvider对象创建客户端（如果调用者已持有provider）
     */
    public OSS createOssClient(OssProvider provider) {
        return new OSSClientBuilder().build(
                provider.getEndpoint(),
                provider.getAccessKey(),
                provider.getSecretKey()
        );
    }
}