package cn.fateverse.query.entity;

import cn.fateverse.common.core.annotaion.EnableAutoField;
import cn.fateverse.common.core.entity.BaseEntity;
import cn.fateverse.common.core.exception.CustomException;
import cn.fateverse.query.enums.DataAdapterSource;
import cn.fateverse.query.enums.DataAdapterType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;


/**
 * 数据源适配器对象 data_adapter
 *
 * @author clay
 * @date 2023-10-29
 */
@Data
@Builder
@EnableAutoField
@AllArgsConstructor
@NoArgsConstructor
public class DataAdapter extends BaseEntity {

    /**
     * 数据适配器id
     */
    private Long adapterId;

    /**
     * 适配器名称
     */
    private String adapterName;

    /**
     * 数据适配器代码类型 Java or Js
     */
    private DataAdapterType type;

    /**
     * 是否通用
     */
    private Boolean common;

    /**
     * 适配器代码
     */
    private String code;

    /**
     * 可以执行的代码块
     */
    private String executeCode;

    /**
     * 模拟数据 strng类型的json字符串
     */
    private String mockData;


    public void init() {
        if (type == null) {
            throw new CustomException("数据适配器类型为空");
        }
        if (DataAdapterType.JAVA.equals(type)) {
            //Java代码初始化
            this.code = "\n" +
                    "import java.util.*;\n" +
                    "import java.util.stream.*;\n" +
                    "\n" +
                    "public class DataAdapter {\n" +
                    "\n" +
                    "    public static Object execute(List<Map<String, Object>> data) {\n" +
                    "        return data;\n" +
                    "    }\n" +
                    "}\n";
        } else if (DataAdapterType.JAVA_SCRIPT.equals(type)) {
            //JavaScript代码初始化
            this.code = "function execute(data) {\n" +
                    "\n" +
                    "}";
        }
    }

}