TableServiceContext.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package com.boman.system.common;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.boman.common.core.utils.SpringUtils;
  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.google.common.base.Strings;
  8. import com.google.common.collect.Lists;
  9. import com.google.common.collect.Maps;
  10. import org.springframework.stereotype.Service;
  11. import org.springframework.util.StringUtils;
  12. import javax.annotation.Resource;
  13. import java.sql.Timestamp;
  14. import java.util.*;
  15. /**
  16. * @author shiqian
  17. * @description
  18. * @date 2021年03月22日 09:58
  19. **/
  20. @Service
  21. public class TableServiceContext {
  22. private final static String PREFIX = "Controller:/event/do";
  23. /**
  24. * 当前表
  25. */
  26. private GenTable table;
  27. /**
  28. * 真实表名
  29. */
  30. private String realTableName;
  31. /**
  32. * 动作名(ADD,SAVE,SUBMIT,VOID,UNSUBMIT)
  33. */
  34. private String actionName; //Controller:/event/doAdd
  35. /**
  36. * 业务发生的时间戳
  37. */
  38. private Timestamp currentTime;
  39. /**
  40. * 上行的数据
  41. * 每个row对象对应到一条单据(如果存在事务,应该在这个级别上体现)
  42. */
  43. private List<MainTableRecord> rows;
  44. /**
  45. * 本次请求存放的临时数据
  46. * !!慎用,最好不要在各个对象间传递
  47. */
  48. private Map<String, Object> values;
  49. private boolean isDelMtable;
  50. /**
  51. * 是否批量新增
  52. */
  53. private boolean isInsertBacth = false;
  54. private TableServiceContext() {
  55. values = Maps.newHashMap();
  56. }
  57. // /**
  58. // * 表名
  59. // * 与realTableName的含义有差异
  60. // */
  61. // public String getTableName() {
  62. // return table.getName();
  63. // }
  64. private User user;
  65. /**
  66. * 附加的校验器
  67. */
  68. private Collection<Validator> validators;
  69. /**
  70. * 附加的过滤器
  71. */
  72. private Collection<Filter> filters;
  73. @Resource
  74. private RedisService redisService;
  75. public static TableServiceContext createFrom(BaseTableDTO baseTableDTO) {
  76. TableServiceContext result = new TableServiceContext();
  77. result.user = RequestUtil.getUser();
  78. // result.actionName = actionName.replace(PREFIX, "").toUpperCase();
  79. // if (StringUtils.isEmpty(result.actionName)) {
  80. // throw new IllegalArgumentException("actionName不能为空");
  81. // }
  82. String tableName = baseTableDTO.getTable();
  83. if (StringUtils.isEmpty(tableName)) {
  84. throw new IllegalArgumentException("表名参数不能为空");
  85. }
  86. //从redis中获取表信息
  87. RedisService redisService = SpringUtils.getBean(RedisService.class);
  88. result.table = redisService.getCacheObject(RedisKey.TABLE_INFO + tableName);
  89. if (result.table == null) {
  90. throw new IllegalArgumentException(tableName + "表信息不存在");
  91. }
  92. result.realTableName = result.table.getTableName();
  93. if (Strings.isNullOrEmpty(result.realTableName)) {
  94. throw new IllegalArgumentException(tableName + "表名不存在");
  95. }
  96. // 前台传过来的数据
  97. JSONObject fixedData = baseTableDTO.getFixedData();
  98. //获取FK
  99. JSONObject fkColumn = /*getFKColumn(fixColumn, tableName)*/null;
  100. //删除
  101. Boolean isdelmtable = baseTableDTO.getDelMTable();
  102. JSONObject tabItem = baseTableDTO.getTabItem();
  103. if (isdelmtable != null) {
  104. result.isDelMtable = isdelmtable;
  105. }
  106. result.currentTime = new Timestamp(System.currentTimeMillis());
  107. // 获取objid判断 新增或更新
  108. Long objid = baseTableDTO.getObjId();
  109. MainTableRecord mainRowData = new MainTableRecord(result, result.table, objid, fkColumn, fixedData, tabItem);
  110. result.rows = Lists.newArrayList();
  111. result.rows.add(mainRowData);
  112. return result;
  113. }
  114. public static Table getTableByName(String tableName) {
  115. // return TableManagerCache.getTableManager().getTable(tableName);
  116. return null;
  117. }
  118. //
  119. // /**
  120. // * 获取子表外健
  121. // * @param fixColumn
  122. // * @param tableName
  123. // * @return
  124. // */
  125. // private static JSONObject getFKColumn(JSONObject fixColumn, String tableName) {
  126. //
  127. // Table table = getTableByName(tableName);
  128. // if (fixColumn == null || table == null) {
  129. // return null;
  130. // }
  131. // //获取对应外键
  132. // JSONObject fkcolumn = new JSONObject();
  133. // Set<String> fixTable = fixColumn.keySet();
  134. // Iterator it = fixTable.iterator();
  135. // while (it.hasNext()) {
  136. // tableName = (String) it.next();
  137. // ArrayList<RefByTable> list = table.getRefByTables();
  138. // for (int j = 0; j < list.size(); j++) {
  139. // if (list.get(j).getReftable().getName().equals(tableName)) {
  140. // fkcolumn.put(tableName, TableManagerCache.getTableManager().getColumn(list.get(j).getRefByColumnId()).getName());
  141. // }
  142. // }
  143. // }
  144. //
  145. // return fkcolumn;
  146. // }
  147. // /**
  148. // * 获取主表的明细表和外健
  149. // *
  150. // * @param tableName
  151. // * @return
  152. // */
  153. // private static JSONObject getMainTableFKColumn(String tableName) {
  154. //
  155. // Table table = getTableByName(tableName);
  156. // if (table == null) {
  157. // return null;
  158. // }
  159. // JSONObject fkcolumn = new JSONObject();
  160. // //获取对应外键
  161. // ArrayList<RefByTable> list = table.getRefByTables();
  162. // for (int j = 0; j < list.size(); j++) {
  163. // fkcolumn.put(list.get(j).getReftable().getName(), TableManagerCache.getTableManager().getColumn(list.get(j).getRefByColumnId()).getName());
  164. // }
  165. //
  166. // return fkcolumn;
  167. // }
  168. public GenTable getTable() {
  169. return table;
  170. }
  171. public void setTable(GenTable table) {
  172. this.table = table;
  173. }
  174. public String getRealTableName() {
  175. return realTableName;
  176. }
  177. public void setRealTableName(String realTableName) {
  178. this.realTableName = realTableName;
  179. }
  180. public String getActionName() {
  181. return actionName;
  182. }
  183. public void setActionName(String actionName) {
  184. this.actionName = actionName;
  185. }
  186. public Timestamp getCurrentTime() {
  187. return currentTime;
  188. }
  189. public void setCurrentTime(Timestamp currentTime) {
  190. this.currentTime = currentTime;
  191. }
  192. public List<MainTableRecord> getRows() {
  193. return rows;
  194. }
  195. public void setRows(List<MainTableRecord> rows) {
  196. this.rows = rows;
  197. }
  198. public Map<String, Object> getValues() {
  199. return values;
  200. }
  201. public void setValues(Map<String, Object> values) {
  202. this.values = values;
  203. }
  204. public boolean isDelMtable() {
  205. return isDelMtable;
  206. }
  207. public void setDelMtable(boolean delMtable) {
  208. isDelMtable = delMtable;
  209. }
  210. public boolean isInsertBacth() {
  211. return isInsertBacth;
  212. }
  213. public void setInsertBacth(boolean insertBacth) {
  214. isInsertBacth = insertBacth;
  215. }
  216. public User getUser() {
  217. return user;
  218. }
  219. public void setUser(User user) {
  220. this.user = user;
  221. }
  222. public Collection<Validator> getValidators() {
  223. return validators;
  224. }
  225. public void setValidators(Collection<Validator> validators) {
  226. this.validators = validators;
  227. }
  228. public Collection<Filter> getFilters() {
  229. return filters;
  230. }
  231. public void setFilters(Collection<Filter> filters) {
  232. this.filters = filters;
  233. }
  234. }