package com.muyu.rule.server;

import com.muyu.etl.domain.DataValue;

import com.muyu.etl.enums.DataType;
import com.muyu.rule.common.utils.OssUtil;
import com.muyu.rule.common.basic.BasicEngine;
import com.muyu.rule.server.complie.SourceCodeComplier;
import com.muyu.rule.common.load.JavaBinaryClassLoader;

import java.io.*;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class OSSClassLoaderExample {

    static Map<String , BasicEngine<DataValue>> engineMap = new ConcurrentHashMap<>();
    public static void main(String[] args) throws Exception {

        String filePath = "cloud-rule-server/src/main/java/com/muyu/rule/server/basic/engine/";

        String pack = "com.muyu.rule.server.basic.engine.value.";

        File file = new File(filePath+"ENGINE_phone_zzzzz_V9.java");

        String ossFilePath = "ENGINE_phone_zzzzz_V9.java";

        try {
            boolean newFile = file.createNewFile();

            String string = OssUtil.readFileContentFromOSS("zcz-vfd-1000",ossFilePath);
            System.out.println(string);
            try (FileWriter writer = new FileWriter(file)) {
                writer.write(string);
            }catch (IOException e) {
                // 处理异常，例如记录日志或者通知用户
                e.printStackTrace();
            }

            //扫描原码进行编译+-+-
            SourceCodeComplier.javaCompilerPath(filePath);
//对class文件进行自定义类加载规则引擎
            Map<String ,Class<?>> stringClassMap  = JavaBinaryClassLoader.loadClassesByLocation(pack,filePath);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        test1();
    }


    public static void test1(){

                Class<?> aClass = null;
        try {
            aClass = Class.forName("com.muyu.rule.common.engine.ENGINE_VALUE_VFD1000_V1");

            BasicEngine<DataValue> object =(BasicEngine<DataValue>)aClass.newInstance();

            DataValue dataValue = DataValue.builder()
                    .type(DataType.valueOf("varchar"))
                    .label("姓名")
                    .key("name")
                    .value(null)
                    .build();
            object.set(dataValue);

            object.execution();

        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }




}
