Run_1_Script2DataBase.groovy 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. groovyCl.addClasspath(x.getParent().toString())
  36. String fileStr = ReadUTF8File.execute(x.toPath(), false, logger)
  37. Class groovyClass = groovyCl.parseClass(fileStr, "BE_" + code + "_" + supplierCode + ".groovy")
  38. BusinessExecutor p = (BusinessExecutor) groovyClass.getDeclaredConstructor().newInstance()
  39. BusinessScript businessScript = BusinessScript.newBuilder()
  40. .businessName(p.scriptName())
  41. .businessCode(code)
  42. .businessModule(p.module().getValue())
  43. .systemFlag(1)
  44. .build()
  45. supplierService.addBusinessScriptLocal(businessScript, dataSourceId, supplierCode)
  46. groovyCl.clearCache()
  47. } catch (IOException | CompilationFailedException | ClassCastException | InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
  48. logger.error(e.getMessage(), e)
  49. }
  50. }
  51. }
  52. })
  53. }
  54. //groovy最后一个表达式的值为返回
  55. createRunnable(appHome, supplierService, redisService, keysService, logger, dataSourceId, supplierCode)