ConcurrencyUtils.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package jnpf.base.util;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import jnpf.constant.MsgCode;
  4. import jnpf.database.model.dbfield.DbFieldModel;
  5. import jnpf.database.model.dbfield.base.DbFieldModelBase;
  6. import jnpf.database.model.dbtable.DbTableFieldModel;
  7. import jnpf.database.source.DbBase;
  8. import jnpf.exception.WorkFlowException;
  9. import jnpf.model.visualJson.TableModel;
  10. import jnpf.util.TableFeildsEnum;
  11. import org.apache.commons.collections4.CollectionUtils;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.stereotype.Component;
  14. import java.util.List;
  15. @Component
  16. public class ConcurrencyUtils {
  17. @Autowired
  18. private ServiceBaseUtil serviceUtil;
  19. /**
  20. * 根据枚举获取字段对象
  21. *
  22. * @param
  23. * @return
  24. * @copyright 引迈信息技术有限公司
  25. * @date 2023/1/14
  26. */
  27. public static DbFieldModel getDbFieldModel(TableFeildsEnum tableFeildsEnum, boolean isUpperCase, boolean isLowerCase) {
  28. DbFieldModel dbFieldModel = new DbFieldModel();
  29. BeanUtil.copyProperties(tableFeildsEnum, dbFieldModel);
  30. String field = dbFieldModel.getField();
  31. field = isUpperCase ? field.toUpperCase() : isLowerCase ? field.toLowerCase() : field;
  32. dbFieldModel.setField(field);
  33. dbFieldModel.setIsPrimaryKey(tableFeildsEnum.getPrimaryKey());
  34. //设置租户字段默认值
  35. if (TableFeildsEnum.TENANTID.equals(tableFeildsEnum) || TableFeildsEnum.FLOWSTATE.equals(tableFeildsEnum)) {
  36. dbFieldModel.setDefaultValue("0");
  37. }
  38. return dbFieldModel;
  39. }
  40. /**
  41. * 创建锁字段
  42. *
  43. * @throws Exception
  44. */
  45. public void createVersion(List<DbFieldModelBase> fieldList, String type, List<DbFieldModel> addList) {
  46. addFeild(TableFeildsEnum.VERSION, fieldList, type, addList);
  47. }
  48. /**
  49. * 创建flowTaskId
  50. *
  51. * @throws Exception
  52. */
  53. public void createFlowTaskId(List<DbFieldModelBase> fieldList, String type, List<DbFieldModel> addList) {
  54. addFeild(TableFeildsEnum.FLOWTASKID, fieldList, type, addList);
  55. }
  56. /**
  57. * 创建createFlowState
  58. *
  59. * @throws Exception
  60. */
  61. public void createFlowState(List<DbFieldModelBase> fieldList, String type, List<DbFieldModel> addList) {
  62. addFeild(TableFeildsEnum.FLOWSTATE, fieldList, type, addList);
  63. }
  64. /**
  65. * 创建租户id
  66. *
  67. * @throws Exception
  68. */
  69. public void createTenantId(List<DbFieldModelBase> fieldList, String type, List<DbFieldModel> addList) {
  70. addFeild(TableFeildsEnum.TENANTID, fieldList, type, addList);
  71. }
  72. /**
  73. * 创建删除字段
  74. *
  75. * @copyright 引迈信息技术有限公司
  76. * @date 2023/1/14
  77. */
  78. public void creDeleteMark(List<DbFieldModelBase> fieldList, String type, List<DbFieldModel> addList) {
  79. addFeild(TableFeildsEnum.DELETEMARK, fieldList, type, addList);
  80. addFeild(TableFeildsEnum.DELETETIME, fieldList, type, addList);
  81. addFeild(TableFeildsEnum.DELETEUSERID, fieldList, type, addList);
  82. }
  83. /**
  84. * 创建流程引擎id字段
  85. *
  86. * @param
  87. * @return
  88. * @copyright 引迈信息技术有限公司
  89. * @date 2023/1/7
  90. */
  91. public void createFlowEngine(List<DbFieldModelBase> fieldList, String type, List<DbFieldModel> addList) {
  92. addFeild(TableFeildsEnum.FLOWID, fieldList, type, addList);
  93. }
  94. /**
  95. * 新增字段通用方法
  96. *
  97. * @param tableFeildsEnum
  98. * @throws Exception
  99. */
  100. private void addFeild(TableFeildsEnum tableFeildsEnum, List<DbFieldModelBase> fieldList, String type, List<DbFieldModel> fieldOneList) {
  101. boolean isUpperCase = (DbBase.DM.equals(type) || DbBase.ORACLE.equals(type));
  102. boolean isLowerCase = (DbBase.POSTGRE_SQL.equals(type) || DbBase.KINGBASE_ES.equals(type));
  103. DbFieldModelBase dbFieldModel = fieldList.stream().filter(f -> f.getField().equalsIgnoreCase(tableFeildsEnum.getField())).findFirst().orElse(null);
  104. boolean hasVersion = dbFieldModel != null;
  105. if (!hasVersion) {
  106. DbFieldModel dbTableModel1 = this.getDbFieldModel(tableFeildsEnum, isUpperCase, isLowerCase);
  107. fieldOneList.add(dbTableModel1);
  108. }
  109. }
  110. /**
  111. * 判断表是否是自增id
  112. *
  113. * @param primaryKeyPolicy
  114. * @param dbLinkId
  115. * @param tableList
  116. * @return
  117. * @throws Exception
  118. */
  119. public boolean checkAutoIncrement(int primaryKeyPolicy, String dbLinkId, List<TableModel> tableList) throws Exception {
  120. boolean isIncre = primaryKeyPolicy == 2;
  121. String strategy = primaryKeyPolicy == 1 ? "[雪花ID]" : "[自增长id]";
  122. for (TableModel tableModel : tableList) {
  123. List<DbFieldModel> data = serviceUtil.getFieldList(dbLinkId, tableModel.getTable());
  124. DbFieldModel dbFieldModel = data.stream().filter(DbFieldModel::getIsPrimaryKey).findFirst().orElse(null);
  125. if (dbFieldModel == null) {
  126. throw new WorkFlowException(MsgCode.FM011.get(tableModel.getTable()));
  127. }
  128. if (!isIncre == (dbFieldModel.getIsAutoIncrement() != null && dbFieldModel.getIsAutoIncrement())) {
  129. throw new WorkFlowException(MsgCode.FM012.get(strategy, tableModel.getTable()));
  130. }
  131. }
  132. return true;
  133. }
  134. /**
  135. * 执行字段添加
  136. * @param table
  137. * @param linkId
  138. * @param addList
  139. * @throws Exception
  140. */
  141. public void addFileds(String table, String linkId, List<DbFieldModel> addList) throws Exception {
  142. if (CollectionUtils.isNotEmpty(addList)) {
  143. DbTableFieldModel dbTableFieldModel = new DbTableFieldModel();
  144. dbTableFieldModel.setDbFieldModelList(addList);
  145. dbTableFieldModel.setUpdateNewTable(table);
  146. dbTableFieldModel.setUpdateOldTable(table);
  147. dbTableFieldModel.setDbLinkId(linkId);
  148. serviceUtil.addField(dbTableFieldModel);
  149. }
  150. }
  151. }