123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- package com.boman.system.common;
- import com.alibaba.fastjson.JSONObject;
- import com.boman.common.core.utils.SpringUtils;
- import com.boman.common.redis.RedisKey;
- import com.boman.common.redis.service.RedisService;
- import com.boman.gen.domain.GenTable;
- import com.google.common.base.Strings;
- import com.google.common.collect.Lists;
- import com.google.common.collect.Maps;
- import org.springframework.stereotype.Service;
- import org.springframework.util.StringUtils;
- import javax.annotation.Resource;
- import java.sql.Timestamp;
- import java.util.*;
- /**
- * @author shiqian
- * @description
- * @date 2021年03月22日 09:58
- **/
- @Service
- public class TableServiceContext {
- private final static String PREFIX = "Controller:/event/do";
- /**
- * 当前表
- */
- private GenTable table;
- /**
- * 真实表名
- */
- private String realTableName;
- /**
- * 动作名(ADD,SAVE,SUBMIT,VOID,UNSUBMIT)
- */
- private String actionName; //Controller:/event/doAdd
- /**
- * 业务发生的时间戳
- */
- private Timestamp currentTime;
- /**
- * 上行的数据
- * 每个row对象对应到一条单据(如果存在事务,应该在这个级别上体现)
- */
- private List<MainTableRecord> rows;
- /**
- * 本次请求存放的临时数据
- * !!慎用,最好不要在各个对象间传递
- */
- private Map<String, Object> values;
- private boolean isDelMtable;
- /**
- * 是否批量新增
- */
- private boolean isInsertBacth = false;
- private TableServiceContext() {
- values = Maps.newHashMap();
- }
- // /**
- // * 表名
- // * 与realTableName的含义有差异
- // */
- // public String getTableName() {
- // return table.getName();
- // }
- private User user;
- /**
- * 附加的校验器
- */
- private Collection<Validator> validators;
- /**
- * 附加的过滤器
- */
- private Collection<Filter> filters;
- @Resource
- private RedisService redisService;
- public static TableServiceContext createFrom(BaseTableDTO baseTableDTO) {
- TableServiceContext result = new TableServiceContext();
- result.user = RequestUtil.getUser();
- // result.actionName = actionName.replace(PREFIX, "").toUpperCase();
- // if (StringUtils.isEmpty(result.actionName)) {
- // throw new IllegalArgumentException("actionName不能为空");
- // }
- String tableName = baseTableDTO.getTable();
- if (StringUtils.isEmpty(tableName)) {
- throw new IllegalArgumentException("表名参数不能为空");
- }
- //从redis中获取表信息
- RedisService redisService = SpringUtils.getBean(RedisService.class);
- result.table = redisService.getCacheObject(RedisKey.TABLE_INFO + tableName);
- if (result.table == null) {
- throw new IllegalArgumentException(tableName + "表信息不存在");
- }
- result.realTableName = result.table.getTableName();
- if (Strings.isNullOrEmpty(result.realTableName)) {
- throw new IllegalArgumentException(tableName + "表名不存在");
- }
- // 前台传过来的数据
- JSONObject fixedData = baseTableDTO.getFixedData();
- //获取FK
- JSONObject fkColumn = /*getFKColumn(fixColumn, tableName)*/null;
- //删除
- Boolean isdelmtable = baseTableDTO.getDelMTable();
- JSONObject tabItem = baseTableDTO.getTabItem();
- if (isdelmtable != null) {
- result.isDelMtable = isdelmtable;
- }
- result.currentTime = new Timestamp(System.currentTimeMillis());
- // 获取objid判断 新增或更新
- Long objid = baseTableDTO.getObjId();
- MainTableRecord mainRowData = new MainTableRecord(result, result.table, objid, fkColumn, fixedData, tabItem);
- result.rows = Lists.newArrayList();
- result.rows.add(mainRowData);
- return result;
- }
- public static Table getTableByName(String tableName) {
- // return TableManagerCache.getTableManager().getTable(tableName);
- return null;
- }
- //
- // /**
- // * 获取子表外健
- // * @param fixColumn
- // * @param tableName
- // * @return
- // */
- // private static JSONObject getFKColumn(JSONObject fixColumn, String tableName) {
- //
- // Table table = getTableByName(tableName);
- // if (fixColumn == null || table == null) {
- // return null;
- // }
- // //获取对应外键
- // JSONObject fkcolumn = new JSONObject();
- // Set<String> fixTable = fixColumn.keySet();
- // Iterator it = fixTable.iterator();
- // while (it.hasNext()) {
- // tableName = (String) it.next();
- // ArrayList<RefByTable> list = table.getRefByTables();
- // for (int j = 0; j < list.size(); j++) {
- // if (list.get(j).getReftable().getName().equals(tableName)) {
- // fkcolumn.put(tableName, TableManagerCache.getTableManager().getColumn(list.get(j).getRefByColumnId()).getName());
- // }
- // }
- // }
- //
- // return fkcolumn;
- // }
- // /**
- // * 获取主表的明细表和外健
- // *
- // * @param tableName
- // * @return
- // */
- // private static JSONObject getMainTableFKColumn(String tableName) {
- //
- // Table table = getTableByName(tableName);
- // if (table == null) {
- // return null;
- // }
- // JSONObject fkcolumn = new JSONObject();
- // //获取对应外键
- // ArrayList<RefByTable> list = table.getRefByTables();
- // for (int j = 0; j < list.size(); j++) {
- // fkcolumn.put(list.get(j).getReftable().getName(), TableManagerCache.getTableManager().getColumn(list.get(j).getRefByColumnId()).getName());
- // }
- //
- // return fkcolumn;
- // }
- public GenTable getTable() {
- return table;
- }
- public void setTable(GenTable table) {
- this.table = table;
- }
- public String getRealTableName() {
- return realTableName;
- }
- public void setRealTableName(String realTableName) {
- this.realTableName = realTableName;
- }
- public String getActionName() {
- return actionName;
- }
- public void setActionName(String actionName) {
- this.actionName = actionName;
- }
- public Timestamp getCurrentTime() {
- return currentTime;
- }
- public void setCurrentTime(Timestamp currentTime) {
- this.currentTime = currentTime;
- }
- public List<MainTableRecord> getRows() {
- return rows;
- }
- public void setRows(List<MainTableRecord> rows) {
- this.rows = rows;
- }
- public Map<String, Object> getValues() {
- return values;
- }
- public void setValues(Map<String, Object> values) {
- this.values = values;
- }
- public boolean isDelMtable() {
- return isDelMtable;
- }
- public void setDelMtable(boolean delMtable) {
- isDelMtable = delMtable;
- }
- public boolean isInsertBacth() {
- return isInsertBacth;
- }
- public void setInsertBacth(boolean insertBacth) {
- isInsertBacth = insertBacth;
- }
- public User getUser() {
- return user;
- }
- public void setUser(User user) {
- this.user = user;
- }
- public Collection<Validator> getValidators() {
- return validators;
- }
- public void setValidators(Collection<Validator> validators) {
- this.validators = validators;
- }
- public Collection<Filter> getFilters() {
- return filters;
- }
- public void setFilters(Collection<Filter> filters) {
- this.filters = filters;
- }
- }
|