import Ignore_ReadUTF8File as ReadUTF8File import com.yinjie.heating.common.api.BusinessExecutor import com.yinjie.heating.common.api.NoSqlKeysService import com.yinjie.heating.common.api.SupplierInitService import com.yinjie.heating.common.datas.RedisKeys import com.yinjie.heating.common.entity.doc.BusinessScript import com.yinjie.heating.common.tool.ERPUtils import com.dySweetFishPlugin.redis.RedisService import groovy.io.FileType import org.apache.commons.io.FilenameUtils import org.apache.logging.log4j.Logger import org.codehaus.groovy.control.CompilationFailedException import java.lang.reflect.InvocationTargetException static def createRunnable(String appHome, SupplierInitService supplierService, RedisService redisService, NoSqlKeysService keysService, Logger logger, String dataSourceId, long supplierCode) { String directoryName = appHome + File.separator + "conf" + File.separator + "script" + File.separator + supplierCode + File.separator File directoryScript = new File(directoryName) if (!directoryScript.exists()) { directoryScript.mkdirs() } List.metaClass.findElement = { Map conditions -> delegate.find { element -> conditions.every { condition, value -> element[condition] == value } } } directoryScript.eachFileRecurse(FileType.FILES, { x -> String code = FilenameUtils.getBaseName(x.getName()) if (code.startsWith("BE_")) { code = code.substring(3) if (!redisService.hexists(keysService.getRedisKey(RedisKeys.KEY_BUSINESSSCRIPT, supplierCode), code)) { //读取脚本信息 try (GroovyClassLoader groovyCl = new GroovyClassLoader(supplierService.getClass().getClassLoader(), ERPUtils.buildERPGroovyConfig())) { groovyCl.addClasspath(x.getParent().toString()) String fileStr = ReadUTF8File.execute(x.toPath(), false, logger) Class groovyClass = groovyCl.parseClass(fileStr, "BE_" + code + "_" + supplierCode + ".groovy") BusinessExecutor p = (BusinessExecutor) groovyClass.getDeclaredConstructor().newInstance() BusinessScript businessScript = BusinessScript.newBuilder() .businessName(p.scriptName()) .businessCode(code) .businessModule(p.module().getValue()) .systemFlag(1) .build() supplierService.addBusinessScriptLocal(businessScript, dataSourceId, supplierCode) groovyCl.clearCache() } catch (IOException | CompilationFailedException | ClassCastException | InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { logger.error(e.getMessage(), e) } } } }) } //groovy最后一个表达式的值为返回 createRunnable(appHome, supplierService, redisService, keysService, logger, dataSourceId, supplierCode)