|
@@ -0,0 +1,113 @@
|
|
|
+/*
|
|
|
+ * 爱组搭 http://aizuda.com 低代码组件化开发平台
|
|
|
+ * ------------------------------------------
|
|
|
+ * 受知识产权保护,请勿删除版权申明
|
|
|
+ */
|
|
|
+package com.aizuda.service.service;
|
|
|
+
|
|
|
+import com.aizuda.core.api.ApiAssert;
|
|
|
+import com.aizuda.core.exception.ApiException;
|
|
|
+import com.aizuda.core.script.GroovyScriptEngine;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+
|
|
|
+import javax.script.ScriptException;
|
|
|
+import java.util.Collection;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.function.Function;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 爱组搭 http://aizuda.com
|
|
|
+ * ----------------------------------------
|
|
|
+ * Groovy 脚本执行服务处理类
|
|
|
+ *
|
|
|
+ * @author 青苗
|
|
|
+ * @since 2023-04-23
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+public class GroovyScriptService<T> {
|
|
|
+ private Function<T, String> groovyScriptFunction;
|
|
|
+
|
|
|
+ public GroovyScriptService(Function<T, String> groovyScriptFunction) {
|
|
|
+ this.groovyScriptFunction = groovyScriptFunction;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 执行脚本返回分页列表
|
|
|
+ *
|
|
|
+ * @param id 主键ID
|
|
|
+ * @param page 分页对象
|
|
|
+ * @param data 参数对象
|
|
|
+ * @return Page
|
|
|
+ */
|
|
|
+ public Page<Object> pageById(T id, Page<Object> page, Object data) {
|
|
|
+ Object obj = this.evaluateGroovyScript(id, page, data);
|
|
|
+ if (null != obj) {
|
|
|
+ ApiAssert.fail(!(obj instanceof Page), "执行脚本返回结果非分页类型");
|
|
|
+ return (Page<Object>) obj;
|
|
|
+ }
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Object evaluateGroovyScript(T id, Page<Object> page, Object data) {
|
|
|
+ String script = groovyScriptFunction.apply(id);
|
|
|
+ ApiAssert.fail(StringUtils.isBlank(script), "执行脚本不存在");
|
|
|
+ try {
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ if (null != page) {
|
|
|
+ params.put("_page_current_", page.getCurrent());
|
|
|
+ params.put("_page_size_", page.getSize());
|
|
|
+ }
|
|
|
+ params.put("_data_", data);
|
|
|
+ return GroovyScriptEngine.evaluate(script, params);
|
|
|
+ } catch (ScriptException e) {
|
|
|
+ log.error("Groovy 脚本执行异常 id={}, {}", id, e.getMessage());
|
|
|
+ throw new ApiException("脚本执行异常: " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 执行脚本返回数据列表
|
|
|
+ *
|
|
|
+ * @param id 主键ID
|
|
|
+ * @param data 参数对象
|
|
|
+ * @return Collection
|
|
|
+ */
|
|
|
+ public Collection<Object> listById(T id, Object data) {
|
|
|
+ Object obj = this.evaluateGroovyScript(id, null, data);
|
|
|
+ if (null != obj) {
|
|
|
+ ApiAssert.fail(!(obj instanceof Collection), "执行脚本返回结果非集合类型");
|
|
|
+ return (Collection<Object>) obj;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 执行脚本返回数据对象
|
|
|
+ *
|
|
|
+ * @param id 主键ID
|
|
|
+ * @param data 参数对象
|
|
|
+ * @return Object
|
|
|
+ */
|
|
|
+ public Object getOneById(T id, Object data) {
|
|
|
+ return this.evaluateGroovyScript(id, null, data);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 执行脚本
|
|
|
+ *
|
|
|
+ * @param id 主键ID
|
|
|
+ * @param data 参数对象
|
|
|
+ * @return boolean
|
|
|
+ */
|
|
|
+ public boolean executeById(T id, Object data) {
|
|
|
+ Object obj = this.evaluateGroovyScript(id, null, data);
|
|
|
+ if (null != obj) {
|
|
|
+ ApiAssert.fail(!(obj instanceof Boolean), "执行脚本返回结果非布尔类型");
|
|
|
+ return (Boolean) obj;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|