Run_1_Script2DataBase.groovy 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import Ignore_ReadUTF8File as ReadUTF8File
  2. import com.yinjie.heating.common.api.BusinessExecutor
  3. import com.yinjie.heating.common.api.NoSqlKeysService
  4. import com.yinjie.heating.common.api.SupplierInitService
  5. import com.yinjie.heating.common.datas.RedisKeys
  6. import com.yinjie.heating.common.entity.doc.BusinessScript
  7. import com.yinjie.heating.common.tool.ERPUtils
  8. import com.dySweetFishPlugin.redis.RedisService
  9. import groovy.io.FileType
  10. import org.apache.commons.io.FilenameUtils
  11. import org.apache.logging.log4j.Logger
  12. import org.codehaus.groovy.control.CompilationFailedException
  13. import java.lang.reflect.InvocationTargetException
  14. static def createRunnable(String appHome, SupplierInitService supplierService, RedisService redisService, NoSqlKeysService keysService, Logger logger, String dataSourceId, long supplierCode) {
  15. String directoryName = appHome + File.separator +
  16. "conf" + File.separator +
  17. "script" + File.separator +
  18. supplierCode + File.separator
  19. File directoryScript = new File(directoryName)
  20. if (!directoryScript.exists()) {
  21. directoryScript.mkdirs()
  22. }
  23. List.metaClass.findElement = { Map<String, String> conditions ->
  24. delegate.find { element ->
  25. conditions.every { condition, value -> element[condition] == value }
  26. }
  27. }
  28. directoryScript.eachFileRecurse(FileType.FILES, { x ->
  29. String code = FilenameUtils.getBaseName(x.getName())
  30. if (code.startsWith("BE_")) {
  31. code = code.substring(3)
  32. if (!redisService.hexists(keysService.getRedisKey(RedisKeys.KEY_BUSINESSSCRIPT, supplierCode), code)) {
  33. //读取脚本信息
  34. try (GroovyClassLoader groovyCl = new GroovyClassLoader(supplierService.getClass().getClassLoader(), ERPUtils.buildERPGroovyConfig())) {
  35. //添加公共commons目录
  36. groovyCl.addClasspath(directoryName + "commons")
  37. groovyCl.addClasspath(x.getParent().toString())
  38. String fileStr = ReadUTF8File.execute(x.toPath(), false, logger)
  39. Class groovyClass = groovyCl.parseClass(fileStr, "BE_" + code + "_" + supplierCode + ".groovy")
  40. BusinessExecutor p = (BusinessExecutor) groovyClass.getDeclaredConstructor().newInstance()
  41. BusinessScript businessScript = BusinessScript.newBuilder()
  42. .businessName(p.scriptName())
  43. .businessCode(code)
  44. .businessModule(p.module().getValue())
  45. .systemFlag(1)
  46. .build()
  47. supplierService.addBusinessScriptLocal(businessScript, dataSourceId, supplierCode)
  48. groovyCl.clearCache()
  49. } catch (IOException | CompilationFailedException | ClassCastException | InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
  50. logger.error(e.getMessage(), e)
  51. }
  52. }
  53. }
  54. })
  55. }
  56. //groovy最后一个表达式的值为返回
  57. createRunnable(appHome, supplierService, redisService, keysService, logger, dataSourceId, supplierCode)