LoadTableServerImpl.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package com.boman.gen.service;
  2. import com.boman.common.core.utils.obj.ObjectUtils;
  3. import com.boman.common.core.web.domain.AjaxResult;
  4. import com.boman.common.redis.RedisKey;
  5. import com.boman.common.redis.service.RedisService;
  6. import com.boman.domain.GenTable;
  7. import com.boman.domain.GenTableColumn;
  8. import com.boman.domain.GenTableRelation;
  9. import com.boman.domain.constant.FormDataConstant;
  10. import com.google.common.collect.Lists;
  11. import com.google.common.collect.Maps;
  12. import org.apache.commons.collections4.CollectionUtils;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Service;
  15. import java.util.ArrayList;
  16. import java.util.Comparator;
  17. import java.util.List;
  18. import java.util.Map;
  19. import java.util.concurrent.TimeUnit;
  20. import java.util.stream.Collectors;
  21. import static com.boman.common.core.utils.obj.ObjectUtils.requireNonNull;
  22. /**
  23. * @author tjf
  24. * @Date: 2021/04/27/15:07
  25. */
  26. @Service
  27. public class LoadTableServerImpl implements ILoadTableServer{
  28. @Autowired
  29. private IGenTableService genTableService;
  30. @Autowired
  31. private IGenTableColumnService genTableColumnService;
  32. @Autowired
  33. private RedisService redisService;
  34. @Autowired
  35. private IGenTableRelationService genTableRelationService;
  36. @Override
  37. public AjaxResult loadTable(GenTable genTable) {
  38. List<GenTable> tableList = genTableService.selectGenTableList(genTable);
  39. requireNonNull(tableList);
  40. // load table and tableColumn
  41. List<Long> tableIdList = tableList.stream().map(GenTable::getId).collect(Collectors.toList());
  42. List<GenTableColumn> genTableColumns = genTableColumnService.listByTableIdList(tableIdList);
  43. requireNonNull(genTableColumns);
  44. packTableAndInsertToRedis(tableList, genTableColumns);
  45. List<GenTableRelation> relationList = genTableRelationService.selectGenTableRelationList(new GenTableRelation());
  46. if (relationList.size() > 0) {
  47. // load gen_table_relation表数据到redis
  48. redisService.setCacheObject(RedisKey.RELATION_INFO, relationList, 12L, TimeUnit.DAYS);
  49. // load tableRelation
  50. packRelationAndInsertToRedis(tableList, relationList, genTableColumns);
  51. }
  52. return AjaxResult.success(tableList);
  53. }
  54. /**
  55. * 手动刷新redis
  56. * @return
  57. */
  58. @Override
  59. public AjaxResult updateRedisTable() {
  60. //获取所有的表和表字段,更新到redis中
  61. List<GenTable> tableList = genTableService.selectGenTableList(new GenTable());
  62. if (null == tableList || tableList.isEmpty()) {
  63. return AjaxResult.error("当前没有可更新数据");
  64. }
  65. List<Long> tableIdList = tableList.stream().map(GenTable::getId).collect(Collectors.toList());
  66. List<GenTableColumn> genTableColumns = genTableColumnService.listByTableIdList(tableIdList);
  67. if (null == genTableColumns || genTableColumns.isEmpty()) {
  68. return AjaxResult.error("当前没有可更新数据");
  69. }
  70. packTableAndInsertToRedis(tableList, genTableColumns);
  71. return AjaxResult.success();
  72. }
  73. /**
  74. * 功能描述: 把主表关联的附表的信息和附表的字段信息存进redis
  75. *
  76. * @param tableList tableList 所有表对象信息
  77. * @param relationList 所有主表和附表的关联关系
  78. * @param tableColumns 所有表字段
  79. */
  80. private void packRelationAndInsertToRedis(List<GenTable> tableList, List<GenTableRelation> relationList
  81. , List<GenTableColumn> tableColumns) {
  82. for (GenTable table : tableList) {
  83. List<GenTable> tableListTemp = new ArrayList<>(16);
  84. //对关联关系表进行排序
  85. relationList = relationList.stream().sorted(Comparator.comparing(GenTableRelation::getRelationParentId).thenComparing(GenTableRelation::getSort)).collect(Collectors.toList());
  86. for (GenTableRelation relation : relationList) {
  87. if (relation.getRelationParentId().equals(table.getId())) {
  88. //关联主表PK gen_table_column的id
  89. Long childId = relation.getRelationChildId();
  90. //根据gen_table_column的id查询出表id
  91. GenTableColumn genTableColumn = genTableColumnService.selectGenTableColumnListByColumnId(childId);
  92. // TODO: 2021/4/8
  93. //从tableList中去取出对应表的对象
  94. if (genTableColumn != null) {
  95. List<GenTable> collect = tableList.stream()
  96. .filter(genTable -> genTable.getId().equals(genTableColumn.getTableId()))
  97. .collect(Collectors.toList());
  98. GenTable genTable = collect.get(0);
  99. List<GenTableColumn> collectColumn = tableColumns.stream()
  100. .filter(genTableColumns -> genTableColumns.getTableId().equals(genTableColumn.getTableId()))
  101. .collect(Collectors.toList());
  102. genTable.setColumns(collectColumn);
  103. tableListTemp.add(genTable);
  104. }
  105. }
  106. }
  107. table.setRelationList(tableListTemp);
  108. redisService.setCacheObject(RedisKey.RELATION + table.getTableName(), table, 12L, TimeUnit.DAYS);
  109. }
  110. }
  111. /**
  112. * 功能描述: 把table中塞入对应的column, 同时把所有的信息塞到redis中
  113. *
  114. * @param tableList tableList
  115. * @param genTableColumns 所有的列
  116. */
  117. private void packTableAndInsertToRedis(List<GenTable> tableList, List<GenTableColumn> genTableColumns) {
  118. for (GenTable table : tableList) {
  119. List<GenTableColumn> columnList = new ArrayList<>(16);
  120. for (GenTableColumn tableColumn : genTableColumns) {
  121. if (table.getId().equals(tableColumn.getTableId())) {
  122. // 存一个tableName留作备用
  123. tableColumn.setTableName(table.getTableName());
  124. // 把外键信息存进去,留着查询的时候用
  125. packFkInfo(tableColumn);
  126. columnList.add(tableColumn);
  127. }
  128. }
  129. table.setColumns(columnList);
  130. table.setIsContainsBlob(isContainsBlob(columnList));
  131. redisService.setCacheObject(RedisKey.TABLE_INFO + table.getTableName(), table, 12L, TimeUnit.DAYS);
  132. }
  133. }
  134. private void packFkInfo(GenTableColumn tableColumn) {
  135. Long fkColumnId = tableColumn.getForeignKey();
  136. if (ObjectUtils.isNotEmpty(fkColumnId)) {
  137. // 外键在table_column中的id
  138. GenTableColumn fkTableColumn = genTableColumnService.selectGenTableColumnListByColumnId(fkColumnId);
  139. String fkColumnName = fkTableColumn.getColumnName();
  140. Long fkTableId = fkTableColumn.getTableId();
  141. GenTable fkGenTable = genTableService.getByTableId(fkTableId);
  142. // 显示键 table_column 表的id
  143. Long dkColumnId = fkGenTable.getDkColumn();
  144. String dkColumnName = fkColumnName;
  145. if(dkColumnId != null){
  146. GenTableColumn dkTableColumn = genTableColumnService.selectGenTableColumnListByColumnId(dkColumnId);
  147. dkColumnName = dkTableColumn.getColumnName();
  148. }
  149. String fkTableName = fkGenTable.getTableName();
  150. Map<String, Object> fkInfo = Maps.newConcurrentMap();
  151. fkInfo.put(FormDataConstant.FK_TABLE_NAME, fkTableName);
  152. fkInfo.put(FormDataConstant.FK_COLUMN_NAME, fkColumnName);
  153. fkInfo.put(FormDataConstant.DK_COLUMN_NAME, dkColumnName);
  154. tableColumn.setFkInfo(fkInfo);
  155. }
  156. }
  157. public boolean isContainsBlob(List<GenTableColumn> columnList) {
  158. if (CollectionUtils.isEmpty(columnList)) {
  159. return false;
  160. }
  161. ArrayList<String> blob = Lists.newArrayList("blob", "tinyblob", "mediumblob", "longblob");
  162. for (GenTableColumn column : columnList) {
  163. if (blob.contains(column.getColumnType())) {
  164. return true;
  165. }
  166. }
  167. return false;
  168. }
  169. }