package com.hzya.frame.sysnew.grovy.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.sysnew.grovy.service.IGroovyIntegrationService;
import com.hzya.frame.util.GroovyUtil;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * Groovy 脚本执行实现类
 *
 * @author 👻👻👻👻👻👻👻👻👻👻 gjh
 * @version 1.0
 * @content
 * @date 2024-05-15 13:50
 */
@Service(value = "groovyIntegrationService")
public class GroovyIntegrationServiceImpl implements IGroovyIntegrationService {
    private static final Logger logger = LoggerFactory.getLogger(GroovyIntegrationServiceImpl.class);

//    public static void main(String[] args) {
//        HelloWorld helloWorld = new HelloWorld();
//        System.out.println(helloWorld.sayHello());
//    }

    public static void main(String[] args) throws IllegalAccessException, InstantiationException {
        GroovyClassLoader groovyClassLoader = new GroovyClassLoader();

        // 动态编译和加载Groovy脚本
        Class<?> groovyClass = groovyClassLoader.parseClass("println(\"1231231\")");

        // 创建Groovy类的实例
        GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
        // 执行Groovy脚本
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "张三");
        Object returnObj = groovyObject.invokeMethod("run", jsonObject.toJSONString());
    }

    @Override
    public JsonResultEntity groovyScriptExecution(JSONObject jsonObject) {
        Object object = GroovyUtil.execute(jsonObject);
        return BaseResult.getSuccessMessageEntity(object);
    }
}
