|
@@ -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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|