package com.ruoyi.dk.oss.ali;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.auth.sts.AssumeRoleRequest;
import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.ruoyi.component.core.exception.ServiceException;
import com.ruoyi.component.satoken.utils.LoginHelper;
import com.ruoyi.system.domain.vo.SysTenantVo;
import com.ruoyi.system.service.ISysTenantService;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Component
@Slf4j
public class StsOssKit {

    @Autowired
    @Setter
    @Getter
    private OssConfig ossConfig;
    @Getter
    private IAcsClient acsClient = null;
    @Autowired
    private ISysTenantService tenantService;

    @PostConstruct
    public void init(){
        if(!ossConfig.isEnable()){
            log.error("未开启阿里云OSS配置");
            return;
        }
        DefaultProfile profile = DefaultProfile.getProfile(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
        acsClient = new DefaultAcsClient(profile);
    }


    public StsResult getStsToken(){
        SysTenantVo tenant = tenantService.queryByTenantId(LoginHelper.getTenantId());
        if(tenant == null){
            throw new ServiceException("平台错误");
        }
        // 创建API请求并设置参数
        AssumeRoleRequest request = new AssumeRoleRequest();
        request.setDurationSeconds(ossConfig.getDurationSeconds());
        request.setRoleArn(ossConfig.getRoleArn());
        request.setRoleSessionName(ossConfig.getRoleSessionName());
        try {
            AssumeRoleResponse response = acsClient.getAcsResponse(request);
            log.info(JSON.toJSONString(response));
            // 打印您需要的返回值，此处打印的是此次请求的 RequestId
            log.info(response.getRequestId());
            StsResult  stsResult = new StsResult();
            stsResult.setToken(response.getCredentials().getSecurityToken());
            stsResult.setKeyId(response.getCredentials().getAccessKeyId());
            stsResult.setKeySecret(response.getCredentials().getAccessKeySecret());
            stsResult.setRegion("oss-"+ossConfig.getEndpoint());
            stsResult.setBucket(ossConfig.getBucketName());
            String nowStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
            stsResult.setKey(ossConfig.getKey()+tenant.getPrefix()+"/"+nowStr+"/");
            stsResult.setCdnDomain(ossConfig.getCdnDomain());
            return stsResult;
        } catch (ServerException e) {
            log.error("上传文件系统异常",e);
        } catch (ClientException e) {
            // 打印错误码
            log.error("ErrCode:" + e.getErrCode());
            log.error("ErrMsg:" + e.getErrMsg());
            log.error("RequestId:" + e.getRequestId());
        }
        return null;
    }
}

