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 rows; /** * 本次请求存放的临时数据 * !!慎用,最好不要在各个对象间传递 */ private Map 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 validators; /** * 附加的过滤器 */ private Collection 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 fixTable = fixColumn.keySet(); // Iterator it = fixTable.iterator(); // while (it.hasNext()) { // tableName = (String) it.next(); // ArrayList 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 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 getRows() { return rows; } public void setRows(List rows) { this.rows = rows; } public Map getValues() { return values; } public void setValues(Map 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 getValidators() { return validators; } public void setValidators(Collection validators) { this.validators = validators; } public Collection getFilters() { return filters; } public void setFilters(Collection filters) { this.filters = filters; } }