BE_System_Init_ERPClient.groovy 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import com.yinjie.printerAuthorize.common.api.BusinessExecutor
  2. import com.yinjie.printerAuthorize.common.api.ERPService
  3. import com.yinjie.printerAuthorize.common.api.NoSqlKeysService
  4. import com.yinjie.printerAuthorize.common.api.SupplierInitService
  5. import com.yinjie.printerAuthorize.common.datas.ERPModule
  6. import com.yinjie.printerAuthorize.common.datas.RedisKeys
  7. import com.yinjie.printerAuthorize.common.entity.base.DataBaseMultiItemEx
  8. import com.yinjie.printerAuthorize.common.entity.base.ProcessERPClientItem
  9. import com.yinjie.printerAuthorize.common.entity.base.ProcessStringItem
  10. import com.yinjie.printerAuthorize.common.tool.CodeGenerate
  11. import com.yinjie.printerAuthorize.common.tool.ERPUtils
  12. import com.dySweetFishPlugin.redis.RedisService
  13. import com.dySweetFishPlugin.sql.DBService
  14. import com.dySweetFishPlugin.sql.TableIdService
  15. import com.sweetfish.service.RetResult
  16. import org.apache.commons.io.FileUtils
  17. import org.apache.logging.log4j.LogManager
  18. import org.apache.logging.log4j.Logger
  19. import org.rex.DB
  20. import org.rex.RMap
  21. import org.rex.db.exception.DBException
  22. import javax.annotation.Resource
  23. @SuppressWarnings("unused")
  24. class BE_System_Init_ERPClient implements BusinessExecutor<ProcessERPClientItem, ProcessStringItem> {
  25. protected final Logger logger = LogManager.getLogger(this.getClass().getSimpleName())
  26. @Resource
  27. private SupplierInitService supplierService
  28. @Resource
  29. private ERPService erpService
  30. @Resource
  31. private DBService dbService
  32. @Resource
  33. private RedisService redisService
  34. @Resource
  35. private NoSqlKeysService keysService
  36. @Resource
  37. private TableIdService tableIdService
  38. @Resource
  39. private CodeGenerate codeGenerate
  40. @Resource(name = "APP_HOME")
  41. private String appHome
  42. @Resource(name = "property.sysRunMode")
  43. private String sysRunMode
  44. @Override
  45. String scriptName() {
  46. return "初始化SAAS商户"
  47. }
  48. @Override
  49. ERPModule module() {
  50. return ERPModule.SYSTEM
  51. }
  52. void start(long supplierCode) {
  53. }
  54. RetResult<ProcessStringItem> execute(ProcessERPClientItem source) {
  55. DataBaseMultiItemEx dataItem = source.dataItem
  56. DataBaseMultiItemEx srcItem = erpService.getERPSupplierInfoByCode(dataItem.getItemCode())
  57. if (srcItem != null) {
  58. return RetResult.<ProcessStringItem> errorT().retinfo("重复的itemCode")
  59. }
  60. srcItem = erpService.getERPSupplierInfoBySharding(Long.parseLong(dataItem.getShardingKey()))
  61. if (srcItem != null) {
  62. return RetResult.<ProcessStringItem> errorT().retinfo("重复的shardingKey")
  63. }
  64. // dataItem.setMd5ParamDigest(codeGenerate.generateMD5Key());
  65. dataItem.itemType = 1
  66. dataItem.voidFlag = 0
  67. dataItem.dataSchema = dataItem.itemCode + "_" + dataItem.shardingKey
  68. //1、创建数据库
  69. dbService.update("create dataBase IF NOT EXISTS " + dataItem.dataSchema + " CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;")
  70. //2、sys_DataBaseMultiItem表中插入记录
  71. RMap dataParams = new RMap()
  72. dataParams.put("itemName", dataItem.itemName)
  73. dataParams.put("dataBaseAlias", dataItem.dataBaseAlias)
  74. dataParams.put("shardingKey", dataItem.shardingKey)
  75. dataParams.put("itemType", dataItem.itemType)
  76. dataParams.put("contactUrl", dataItem.contactUrl)
  77. dataParams.put("voidFlag", dataItem.voidFlag)
  78. dataParams.put("itemCode", dataItem.itemCode)
  79. dataParams.put("contactPerson", dataItem.contactPerson)
  80. dataParams.put("contactPhone", dataItem.contactPhone)
  81. dataParams.put("md5ParamDigest", dataItem.md5ParamDigest)
  82. //先不执行,免得启动的时候找脚本
  83. DB.update("insert into sys_DataBaseMultiItem " +
  84. "(itemName, dataBaseAlias, shardingKey, itemType, contactUrl, voidFlag, itemCode, contactPerson, contactPhone, md5ParamDigest) " +
  85. "values(" +
  86. "#{itemName}, #{dataBaseAlias}, #{shardingKey}, #{itemType}, #{contactUrl}, #{voidFlag}, #{itemCode}, #{contactPerson}, #{contactPhone}, #{md5ParamDigest})",
  87. dataParams)
  88. //需要上面的id,
  89. long idItem = dbService.getLong("select id from sys_DataBaseMultiItem where shardingKey = " + dataItem.shardingKey)
  90. //3、增加数据源,这个必须执行,否则下面执行sql无法带上数据源
  91. dbService.addDataSource(dataItem.dataBaseAlias,
  92. "com.mysql.jdbc.Driver",
  93. "jdbc:mysql://192.168.1.22:3306/" + dataItem.dataSchema + "?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&rewriteBatchedStatements=true",
  94. "dbadmin", "Admin@123")
  95. //4、创建表和视图
  96. String initSqlRoot = appHome + File.separator +
  97. "conf" + File.separator +
  98. "sqlInit" + File.separator +
  99. (("ERP".equalsIgnoreCase(sysRunMode)) ? "erp" : "mall")
  100. //获取脚本目录下的所有文件,并按照文件名排序
  101. File dir = new File(initSqlRoot)
  102. File[] scriptFiles = dir.listFiles().sort { file ->
  103. return Integer.parseInt(file.name.split("_")[1])
  104. }
  105. scriptFiles.each { file ->
  106. //如果有存储过程的语句,则需要单独处理,最好每一个存储过程一个sql,因为存储过程的sql包含有 ; 号,整个文件一起执行最好
  107. def fileContent = file.text
  108. // 替换${supplyCode}为1000
  109. def replacedContent = fileContent.replaceAll('\\$\\{supplyCode}', dataItem.shardingKey)
  110. try {
  111. def content = ERPUtils.replaceEscape(replacedContent)
  112. def sqlArray = content.split(";")
  113. sqlArray.each {sql ->
  114. try {
  115. dbService.update(dataItem.dataBaseAlias, sql)
  116. } catch (DBException e1) {
  117. logger.error(sql)
  118. logger.error("执行初始化sql失败: " + e1.getMessage(), e1)
  119. }
  120. }
  121. logger.info("初始化" + dataItem.itemName + "完成")
  122. //好像文件太大了直接批量执行不行:(
  123. //dbService.batchUpdate(dataItem.dataBaseAlias, content.split(";"))
  124. } catch (DBException e) {
  125. logger.error("执行初始化sql失败: " + e.getMessage(), e)
  126. }
  127. }
  128. //用于产品中心对接,此处将上面生成的itemCode与Md5ParamDigest更新进数据库
  129. String sql = "UPDATE tbconfigvalue" + dataItem.shardingKey + " SET configValue1 = '" + dataItem.itemCode + "', " +
  130. "configValue2 = '" + dataItem.md5ParamDigest + "' WHERE configKey = 'productCenterCallClientInfo';"
  131. dbService.update(dataItem.dataBaseAlias, sql)
  132. //5、拷贝脚本目录,1000是保留的,不论是mall还erp,都保留一个1000的目录,不然此脚本也不方便调用,其它的都从1000拷贝出去
  133. FileUtils.copyDirectory(
  134. new File(appHome + File.separator +
  135. "conf" + File.separator +
  136. "script" + File.separator +
  137. "1000"),
  138. new File(appHome + File.separator +
  139. "conf" + File.separator +
  140. "script" + File.separator +
  141. dataItem.shardingKey)
  142. )
  143. //6、分表分库写入到redis
  144. dataItem.id = idItem
  145. redisService.hset(keysService.getRedisKey(RedisKeys.KEY_SUPPLIER_PLATFORM), String.valueOf(idItem), dataItem)
  146. redisService.hset(keysService.getRedisKey(RedisKeys.KEY_SUPPLIER_SHARDING_KEY_PLATFORM), dataItem.shardingKey, dataItem)
  147. redisService.hset(keysService.getRedisKey(RedisKeys.KEY_SUPPLIER_ITEM_CODE_PLATFORM), dataItem.itemCode, dataItem)
  148. //7、id、code初始化
  149. tableIdService.initSupplierId(dataItem)
  150. tableIdService.initSupplierCode(dataItem)
  151. //8、调用初始化
  152. supplierService.addSupplier(dataItem)
  153. return RetResult.<ProcessStringItem> successT()
  154. }
  155. }