MyController.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package com.boman.gen.controller;
  2. import com.boman.common.core.web.controller.BaseController;
  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.gen.domain.GenTable;
  7. import com.boman.gen.domain.GenTableColumn;
  8. import com.boman.gen.domain.GenTableRelation;
  9. import com.boman.gen.service.IGenTableColumnService;
  10. import com.boman.gen.service.IGenTableRelationService;
  11. import com.boman.gen.service.IGenTableService;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.web.bind.annotation.GetMapping;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. import org.springframework.web.bind.annotation.RestController;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import java.util.concurrent.TimeUnit;
  19. import java.util.stream.Collectors;
  20. import static com.boman.common.core.utils.obj.ObjectUtils.requireNonNull;
  21. import static com.boman.common.core.utils.obj.ObjectUtils.requiredNonNull;
  22. /**
  23. * @author shiqian
  24. * @description 获取gen_table
  25. * @date 2021年03月17日 16:44
  26. **/
  27. @RestController
  28. @RequestMapping("/init")
  29. public class MyController extends BaseController {
  30. @Autowired
  31. private IGenTableService genTableService;
  32. @Autowired
  33. private IGenTableColumnService genTableColumnService;
  34. @Autowired
  35. private RedisService redisService;
  36. @Autowired
  37. private IGenTableRelationService genTableRelationService;
  38. /**
  39. * 功能描述: 查询代码生成列表,table中封装columnList
  40. *
  41. * @param genTable 查询条件
  42. * @return AjaxResult
  43. */
  44. @GetMapping("/loadTable")
  45. public AjaxResult loadTable(GenTable genTable) {
  46. List<GenTable> tableList = genTableService.selectGenTableList(genTable);
  47. requireNonNull(tableList);
  48. // load table and tableColumn
  49. List<Long> tableIdList = tableList.stream().map(GenTable::getTableId).collect(Collectors.toList());
  50. List<GenTableColumn> genTableColumns = genTableColumnService.listByTableIdList(tableIdList);
  51. requiredNonNull(genTableColumns);
  52. packTableAndInsertToRedis(tableList, genTableColumns);
  53. List<GenTableRelation> relationList = genTableRelationService.selectGenTableRelationList(new GenTableRelation());
  54. requiredNonNull(relationList);
  55. // load gen_table_relation表数据到redis
  56. redisService.setCacheObject(RedisKey.RELATION_INFO, relationList, 12L, TimeUnit.DAYS);
  57. // load tableRelation
  58. packRelationAndInsertToRedis(tableList, relationList, genTableColumns);
  59. return AjaxResult.success(tableList);
  60. }
  61. /**
  62. * 功能描述: 把主表关联的附表的信息和附表的字段信息存进redis
  63. *
  64. * @param tableList tableList
  65. * @param relationList 主表和附表的关联信息
  66. */
  67. private void packRelationAndInsertToRedis(List<GenTable> tableList, List<GenTableRelation> relationList
  68. , List<GenTableColumn> tableColumns) {
  69. for (GenTable table : tableList) {
  70. List<GenTable> tableListTemp = new ArrayList<>(16);
  71. for (GenTableRelation relation : relationList) {
  72. if (relation.getRelationParentId().equals(table.getTableId())) {
  73. Long childId = relation.getRelationChildId();
  74. // 存了附表的建表信息和表的所有字段信息
  75. // todo
  76. List<GenTable> collect = tableList.stream()
  77. .filter(genTable -> genTable.getTableId().equals(childId))
  78. .collect(Collectors.toList());
  79. tableListTemp.add(requireNonNull(collect).get(0));
  80. }
  81. }
  82. table.setRelationList(tableListTemp);
  83. redisService.setCacheObject(RedisKey.RELATION + table.getTableName(), table, 12L, TimeUnit.DAYS);
  84. }
  85. }
  86. /**
  87. * 功能描述: 把table中塞入对应的column, 同时把所有的信息塞到redis中
  88. *
  89. * @param tableList tableList
  90. * @param genTableColumns 所有的列
  91. */
  92. private void packTableAndInsertToRedis(List<GenTable> tableList, List<GenTableColumn> genTableColumns) {
  93. for (GenTable table : tableList) {
  94. List<GenTableColumn> columnList = new ArrayList<>(16);
  95. for (GenTableColumn tableColumn : genTableColumns) {
  96. if (table.getTableId().equals(tableColumn.getTableId())) {
  97. columnList.add(tableColumn);
  98. }
  99. }
  100. table.setColumns(columnList);
  101. redisService.setCacheObject(RedisKey.TABLE_INFO + table.getTableName(), table, 12L, TimeUnit.DAYS);
  102. }
  103. }
  104. /**手动更新redis缓存表数据
  105. * @Description
  106. * @author tjf
  107. * @Date 2021/3/29
  108. */
  109. public AjaxResult updateRedisTable(){
  110. //获取所有的表和表字段,更新到redis中
  111. List<GenTable> tableList = genTableService.selectGenTableList(new GenTable());
  112. if (null == tableList || tableList.isEmpty()) {
  113. return AjaxResult.error("当前没有可更新数据");
  114. }
  115. List<Long> tableIdList = tableList.stream().map(GenTable::getTableId).collect(Collectors.toList());
  116. List<GenTableColumn> genTableColumns = genTableColumnService.listByTableIdList(tableIdList);
  117. if (null == genTableColumns || genTableColumns.isEmpty()) {
  118. return AjaxResult.error("当前没有可更新数据");
  119. }
  120. packTableAndInsertToRedis(tableList, genTableColumns);
  121. return AjaxResult.success();
  122. }
  123. }