package com.muyu.rule.server;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.OSSObjectSummary;
import com.muyu.rule.server.service.RuleEngineVersionService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @Author：张承志
 * @Package：com.muyu.rule.server
 * @Project：cloud-etl-rule
 * @name：DownloadOss
 * @Date：2024/9/3 16:51
 */

/**
 * 实现项目启动时,把规则引擎class文件下载到服务器里
 */
@Log4j2
@Component
public class DownloadOssSynchronization implements ApplicationRunner {





    private static final String endPoint = "oss-cn-beijing.aliyuncs.com";
    private static final String accessKeyId = "LTAI5tRRrrYqiSXddVq7RvqW";
    private static final String accessKeySecret = "GhEg1LlHTOx4q0rxs1S3pCaSQayCVL";
    private static final String accessPre = "https://zcz-vfd-1000.oss-cn-beijing.aliyuncs.com/";


    /**
     * 服务器项目路径放文件
     */
    private static String serverClassPath = "home/lib/com/muyu/rule/common/engine/";

    private static String bucketName = "zcz-vfd-1000";
    @Autowired
    private RuleEngineVersionService ruleEngineVersionService;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("开始从Oss下载规则引擎");
        OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);

        // 获取存储桶信息
        ObjectListing objectListing = ossClient.listObjects(bucketName);
        // 循环获取Oss存储桶中的所有对象
        for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            String key = objectSummary.getKey();
            downloadFile(ossClient, bucketName, key, serverClassPath);

        }
        // 关闭OSSClient
        ossClient.shutdown();
        //下载完成实现批量批量类加载

//        for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
//            String key = objectSummary.getKey();
//            ruleEngineVersionService.loadRuleEngineClass(key);
//        }



    }

    private void downloadFile(OSS ossClient, String bucketName, String key, String serverPath) {
          log.info("开始下载文件[]{}:"+key);
        //创建服务器项目容器的文件目录路径
        File serverFile = new File(serverPath, key);
        //如果目录不存在,则创建目录
        if (!serverFile.getParentFile().exists()) {
            serverFile.getParentFile().mkdirs();
        }
        //下载文件到项目中
        OSSObject ossObject = ossClient.getObject(new GetObjectRequest(bucketName, key));

        try (FileOutputStream fos = new FileOutputStream(serverFile);) {
            byte[] buffer = new byte[1024];
            int byteRead;
            while ((byteRead = ossObject.getObjectContent().read(buffer)) != -1) {
                fos.write(buffer, 0, byteRead);
            }
            log.info("文件 " + key + "已经下载到" + serverFile.getAbsolutePath());
        } catch (IOException e) {
            log.error("下载文件失败:" + key);
            e.printStackTrace();
        }
    }

}
