zh vor 3 Jahren
Ursprung
Commit
083faed0d6

+ 3 - 0
boman-api/boman-domain/src/main/java/com.boman.domain/constant/FormDataConstant.java

@@ -11,6 +11,9 @@ import java.util.stream.Stream;
  **/
 public class FormDataConstant {
 
+
+    public static final String TABLE_NAME = "tableName";
+    public static final String TABLE = "table";
     /**
      * 删除数据时,需要删除的业务表的主键
      */

+ 74 - 2
boman-web-core/src/main/java/com/boman/web/core/service/TableServiceCmdService.java

@@ -31,11 +31,14 @@ import com.boman.web.core.service.submit.IBaseSubmitService;
 import com.boman.web.core.service.update.IBaseUpdateService;
 import com.boman.web.core.utils.ColumnUtils;
 import com.boman.web.core.utils.IdUtils;
+import com.boman.web.core.utils.JSONObjectUtils;
 import com.google.common.base.Strings;
 import com.google.common.collect.Lists;
 import org.apache.commons.lang3.BooleanUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.aop.framework.AopContext;
+import org.springframework.aop.support.AopUtils;
 import org.springframework.stereotype.Component;
 import org.springframework.transaction.annotation.Isolation;
 import org.springframework.transaction.annotation.Transactional;
@@ -159,6 +162,8 @@ public class TableServiceCmdService {
     public AjaxResult complexSave(FormDataDto dto) {
         // 取出所有的孩子
         List<FormDataDto> result = Lists.newArrayList(dto);
+        // 主表是修改
+        boolean primaryTableUpdate = !ltZero(dto.getObjId());
         recursionAllChildrenFromDto(dto, result);
 
         List<AjaxResult> resultList = new ArrayList<>(result.size());
@@ -166,8 +171,10 @@ public class TableServiceCmdService {
         AjaxResult ajaxResult;
         for (int i = 0; i < result.size(); i++) {
             FormDataDto formDataDto = result.get(i);
-            putFkValue(tableNameIdMap, formDataDto, i == 0);
-
+            boolean optPrimaryTable = i == 0;
+            // 子表关联主表的列名
+            TableServiceCmdService proxy = (TableServiceCmdService) AopContext.currentProxy();
+            proxy.putFkValue(tableNameIdMap, formDataDto, optPrimaryTable, primaryTableUpdate);
             String tableName = formDataDto.getTable();
             ajaxResult = objectSave(formDataDto);
             if (AjaxResult.checkFail(ajaxResult)) {
@@ -1184,5 +1191,70 @@ public class TableServiceCmdService {
         return isNotEmpty(dto.getChildren());
     }
 
+    /**
+     * 功能描述: 复杂对象保存时,设置子表关联主表的外键值, ps: 子表关联主表的fkName的value=fk.primaryTableName
+     *
+     * @param tableNameIdMap     主表的map key:tableName value:id
+     * @param dto                需要保存的对象
+     * @param first              复杂对象保存,是否是最外层的
+     * @param primaryTableUpdate 主表是修改
+     */
+    @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
+    public void putFkValue(Map<String, Long> tableNameIdMap, FormDataDto dto, boolean first, boolean primaryTableUpdate) {
+        // 第一次是主表
+        if (BooleanUtils.isTrue(first)) {
+            // 主表是新增操作
+            if (BooleanUtils.isFalse(primaryTableUpdate)) {
+                return;
+            }
+
+            // 主表是修改操作
+            JSONObject fixedData = dto.getFixedData();
+            for (Map.Entry<String, Object> entry : fixedData.entrySet()) {
+                Object value = entry.getValue();
+                if (value instanceof String) {
+                    String strValue = (String) value;
+                    if (!strValue.contains(FK_POINT)) {
+                        continue;
+                    }
+                    String[] split = strValue.split(POINT);
+                    // 默认关联的都是主表的id,fk.primaryTableName
+                    String lastTableName = split[split.length - 1];
+                    Long maxId = tableNameIdMap.get(lastTableName);
+                    if (maxId == null) {
+                        throw new RuntimeException(String.format("保存失败, 主表id: null, 表名为: %s ", lastTableName));
+                    }
+                    fixedData.put(entry.getKey(), maxId);
+
+                    deleteService.delete(dto.getTable(), JSONObjectUtils.putValue(entry.getKey(), maxId));
+                    dto.setFixedData(fixedData);
+                    break;
+                }
+            }
+        } else {
+            // 新增子表会走到这里
+            JSONObject fixedData = dto.getFixedData();
+            for (Map.Entry<String, Object> entry : fixedData.entrySet()) {
+                Object value = entry.getValue();
+                if (value instanceof String) {
+                    String strValue = (String) value;
+                    if (!strValue.contains(FK_POINT)) {
+                        continue;
+                    }
+                    String[] split = strValue.split(POINT);
+                    // 默认关联的都是主表的id,fk.primaryTableName
+                    String lastTableName = split[split.length - 1];
+                    Long maxId = tableNameIdMap.get(lastTableName);
+                    if (maxId == null) {
+                        throw new RuntimeException(String.format("保存失败, 主表id: null, 表名为: %s ", lastTableName));
+                    }
+                    fixedData.put(entry.getKey(), maxId);
+                    dto.setFixedData(fixedData);
+                    break;
+                }
+            }
+        }
+    }
+
 }
 

+ 13 - 0
boman-web-core/src/main/java/com/boman/web/core/utils/JSONObjectUtils.java

@@ -16,5 +16,18 @@ public class JSONObjectUtils {
         return json.containsKey(key.toLowerCase()) || json.containsKey(key.toUpperCase());
     }
 
+    public static JSONObject putValue(String key, Object value) {
+        JSONObject jsonObject = new JSONObject();
+        jsonObject.put(key, value);
+        return jsonObject;
+    }
+
+    public static JSONObject putValue(String key1, Object value1, String key2, Object value2) {
+        JSONObject jsonObject = new JSONObject();
+        jsonObject.put(key1, value1);
+        jsonObject.put(key2, value2);
+        return jsonObject;
+    }
+
 
 }

+ 1 - 1
ruoyi-ui/src/settings.js

@@ -2,7 +2,7 @@ module.exports = {
   title: 'OA管理系统',
   // urls: `http://118.31.23.65:7090`,
   // urls: `http://192.168.101.11:8090`,
-   urls: `http://192.168.101.110:8090`,
+   urls: `http://192.168.101.49:8090`,