Browse Source

feat(online): 添加从表关联字段同步功能和表单类型查询接口

- 在OnlCgreportItem模型中新增isSelectShow字段用于控制下拉显示
- 在OnlineFormController中实现主表/从表已有字段的refPropert同步更新逻辑
- 修改saveField方法以支持refPropert参数传递
- 新增mapFieldNameToWidget方法构建字段名到组件的映射关系
- 添加queryFormByType接口按表单类型查询表单列表
- 实现OnlineFormService的queryListByFormType方法和OnlinePageService的queryByEnableList方法
- 添加Guava Lists依赖并优化相关工具类导入
online-form-direct-creation
chenchuchuan 2 weeks ago
parent
commit
d26d2b14a5
  1. 5
      common/common-generator/src/main/java/apelet/common/generator/model/OnlCgreportItem.java
  2. 110
      common/common-online/src/main/java/apelet/common/online/controller/OnlineFormController.java
  3. 8
      common/common-online/src/main/java/apelet/common/online/service/OnlineFormService.java
  4. 7
      common/common-online/src/main/java/apelet/common/online/service/OnlinePageService.java
  5. 20
      common/common-online/src/main/java/apelet/common/online/service/impl/OnlineFormServiceImpl.java
  6. 5
      common/common-online/src/main/java/apelet/common/online/service/impl/OnlinePageServiceImpl.java

5
common/common-generator/src/main/java/apelet/common/generator/model/OnlCgreportItem.java

@ -56,6 +56,11 @@ public class OnlCgreportItem extends BaseModel implements Serializable {
@Schema(description = "是否显示 0否,1显示") @Schema(description = "是否显示 0否,1显示")
private Integer isShow; private Integer isShow;
@Schema(description = "是否下拉显示 0否,1显示")
private Integer isSelectShow;
@Schema(description = "排序") @Schema(description = "排序")
private Integer orderNum; private Integer orderNum;

110
common/common-online/src/main/java/apelet/common/online/controller/OnlineFormController.java

@ -40,6 +40,7 @@ import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.page.PageMethod; import com.github.pagehelper.page.PageMethod;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.google.common.collect.Lists;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -735,7 +736,23 @@ public class OnlineFormController {
.map(OnlFormField::getFieldName).collect(Collectors.toSet()); .map(OnlFormField::getFieldName).collect(Collectors.toSet());
Set<String> newColumnNames = new HashSet<>(widgetColumnNames); Set<String> newColumnNames = new HashSet<>(widgetColumnNames);
newColumnNames.removeAll(existingFieldNames); newColumnNames.removeAll(existingFieldNames);
// 主表/单表已有字段:如果绑定了从表(slaveTableId),同步更新 refPropert
Map<String, JSONObject> fieldNameToWidget = mapFieldNameToWidget(widgetJson);
for (OnlFormField existingField : existingFields) {
JSONObject widget = fieldNameToWidget.get(existingField.getFieldName());
if (widget == null) {
continue;
}
JSONObject bindData = widget.getJSONObject("bindData");
if (bindData == null) {
continue;
}
String slaveTableId = bindData.getString("slaveTableId");
if (StrUtil.isNotEmpty(slaveTableId)) {
existingField.setRefPropert(Long.valueOf(slaveTableId));
onlFormFieldService.updateById(existingField);
}
}
if (!newColumnNames.isEmpty()) { if (!newColumnNames.isEmpty()) {
Map<String, JSONObject> nameToWidget = mapColumnNameToWidget(widgetJson); Map<String, JSONObject> nameToWidget = mapColumnNameToWidget(widgetJson);
OnlineDblink dblink = onlineDblinkService.getById(datasource.getDblinkId()); OnlineDblink dblink = onlineDblinkService.getById(datasource.getDblinkId());
@ -758,7 +775,8 @@ public class OnlineFormController {
JSONObject widget = nameToWidget.get(fieldName); JSONObject widget = nameToWidget.get(fieldName);
// 从 widget 的 bindData.columnComment 设置字段备注 // 从 widget 的 bindData.columnComment 设置字段备注
JSONObject bindData = widget.getJSONObject("bindData"); JSONObject bindData = widget.getJSONObject("bindData");
this.saveField(widget.getInteger("widgetType"), bindData.getString("columnComment"), widget.getString("showName"), onlFormHead, fieldName); String ref = bindData.containsKey("slaveTableId") ? bindData.getString("slaveTableId") : null;
this.saveField(widget.getInteger("widgetType"), bindData.getString("columnComment"), widget.getString("showName"), onlFormHead, fieldName, ref);
} }
// ALTER TABLE ADD COLUMN // ALTER TABLE ADD COLUMN
@ -841,8 +859,9 @@ public class OnlineFormController {
JSONObject columnInfoJson = nameToWidget.get(newSubColumnName); JSONObject columnInfoJson = nameToWidget.get(newSubColumnName);
//step 1 : 先保存 field 表 //step 1 : 先保存 field 表
String showName = columnInfoJson.getString("showName"); String showName = columnInfoJson.getString("showName");
String ref = columnInfoJson.containsKey("slaveTableId") ? columnInfoJson.getString("slaveTableId") : null;
// 默认是字符串类型 // 默认是字符串类型
this.saveField(750, showName, null, entryHead, newSubColumnName); this.saveField(750, showName, null, entryHead, newSubColumnName, ref);
} }
onlFormHeadService.syncDB(entryHead, 0L); onlFormHeadService.syncDB(entryHead, 0L);
//step 2 :在保存 OnlineColumn 表 //step 2 :在保存 OnlineColumn 表
@ -873,6 +892,49 @@ public class OnlineFormController {
metaCacheService.removeData(tableName); metaCacheService.removeData(tableName);
changed = true; changed = true;
} }
// 附表已有字段:检查 tableColumnList 中 comType=402 的列是否有 slaveTableId,同步更新 refPropert
for (Long tableId : existingSubTableIdSet) {
String tableName = tableIdAndNameMap.get(tableId);
if (tableName == null) {
continue;
}
OnlFormHead subHead = tableNameAndHeadIdMap.get(tableName);
if (subHead == null) {
continue;
}
List<OnlFormField> subFields = onlFormFieldService.list(
new LambdaQueryWrapper<OnlFormField>().eq(OnlFormField::getHeadId, subHead.getId()));
JSONObject props = tableIdAndPropMap.get(String.valueOf(tableId));
JSONArray tableColumnList = props != null ? props.getJSONArray("tableColumnList") : null;
if (tableColumnList == null) {
continue;
}
for (OnlFormField field : subFields) {
for (int i = 0; i < tableColumnList.size(); i++) {
JSONObject col = tableColumnList.getJSONObject(i);
// 匹配 showFieldName,兼容 showName
String showFieldName = col.getString("showFieldName");
if (StrUtil.isEmpty(showFieldName)) {
showFieldName = col.getString("showName");
}
if (!field.getFieldName().equals(showFieldName)) {
continue;
}
// 只有 comType=402(关联选择器)才有 slaveTableId
Integer comType = col.getInteger("comType");
if (comType != null && comType == 402) {
String slaveTableId = col.getString("slaveTableId");
if (StrUtil.isNotEmpty(slaveTableId)) {
field.setRefPropert(Long.valueOf(slaveTableId));
onlFormFieldService.updateById(field);
changed = true;
}
}
break;
}
}
}
} }
//step2 : 不存在的附表,新增 //step2 : 不存在的附表,新增
if (newSubTableNames.isEmpty()) { if (newSubTableNames.isEmpty()) {
@ -906,7 +968,7 @@ public class OnlineFormController {
} }
private void saveField(Integer widgetType, String columnComment, String showName, OnlFormHead onlFormHead, String fieldName) { private void saveField(Integer widgetType, String columnComment, String showName, OnlFormHead onlFormHead, String fieldName, String refPropert) {
OnlFormField field = new OnlFormField(); OnlFormField field = new OnlFormField();
field.setHeadId(onlFormHead.getId()); field.setHeadId(onlFormHead.getId());
field.setFieldName(fieldName); field.setFieldName(fieldName);
@ -915,6 +977,9 @@ public class OnlineFormController {
if (length != null) { if (length != null) {
field.setFieldLength(length); field.setFieldLength(length);
} }
if (StringUtils.isNotBlank(refPropert)) {
field.setRefPropert(Long.valueOf(refPropert));
}
field.setIsEmpty(1); field.setIsEmpty(1);
field.setIsDefault(0); field.setIsDefault(0);
field.setRowDisabled(0); field.setRowDisabled(0);
@ -1025,6 +1090,28 @@ public class OnlineFormController {
return map; return map;
} }
/**
* 构建 fieldName widget 的映射用于主表已有字段匹配
* key 优先取 bindData.columnFieldName兼容 bindData.columnName
*/
private Map<String, JSONObject> mapFieldNameToWidget(JSONObject widgetJson) {
Map<String, JSONObject> map = new HashMap<>();
walkAllModes(widgetJson, widget -> {
JSONObject bindData = widget.getJSONObject("bindData");
if (bindData == null) {
return;
}
String fieldName = bindData.getString("columnFieldName");
if (StrUtil.isEmpty(fieldName)) {
fieldName = bindData.getString("columnName");
}
if (StrUtil.isNotEmpty(fieldName)) {
map.put(fieldName, widget);
}
});
return map;
}
private Map<String, JSONObject> mapColumnIdAndWidget(JSONObject widgetJson) { private Map<String, JSONObject> mapColumnIdAndWidget(JSONObject widgetJson) {
Map<String, JSONObject> resultMap = new HashMap<>(); Map<String, JSONObject> resultMap = new HashMap<>();
walkAllModes(widgetJson, widget -> { walkAllModes(widgetJson, widget -> {
@ -1104,4 +1191,19 @@ public class OnlineFormController {
} }
} }
} }
/**
* 根据表单类型查询表单列表
*
* @param formType 表单类型
* @return 表单列表
* @see apelet.common.online.model.constant.FormType
*/
@PostMapping(value = "/queryFormByType")
public ResponseResult<List<OnlineForm>> queryFormByType(@MyRequestBody Integer formType) {
if (formType == null) {
return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST);
}
return ResponseResult.success(onlineFormService.queryListByFormType(Lists.newArrayList(formType)));
}
} }

8
common/common-online/src/main/java/apelet/common/online/service/OnlineFormService.java

@ -139,4 +139,12 @@ public interface OnlineFormService extends IBaseService<OnlineForm, Long> {
* @return * @return
*/ */
ObjectValue getObjectValue(Map<String, Object> masterData, Map<String, Object> savedata, String tableName); ObjectValue getObjectValue(Map<String, Object> masterData, Map<String, Object> savedata, String tableName);
/**
* 根据表单类型查询表单列表
*
* @param formType 表单类型
* @return 表单列表
*/
List<OnlineForm> queryListByFormType(List<Integer> formType);
} }

7
common/common-online/src/main/java/apelet/common/online/service/OnlinePageService.java

@ -135,4 +135,11 @@ public interface OnlinePageService extends IBaseService<OnlinePage, Long> {
* @return 在线表单页面列表 * @return 在线表单页面列表
*/ */
List<OnlinePage> getInListWithNonTenant(List<Long> pageIds, String orderBy); List<OnlinePage> getInListWithNonTenant(List<Long> pageIds, String orderBy);
/**
* 查询启用的在线表单页面列表
*
* @return 在线表单页面列表
*/
List<OnlinePage> queryByEnableList();
} }

20
common/common-online/src/main/java/apelet/common/online/service/impl/OnlineFormServiceImpl.java

@ -26,6 +26,7 @@ import apelet.common.orm.impl.LinkEntityColumn;
import apelet.common.redis.util.CommonRedisUtil; import apelet.common.redis.util.CommonRedisUtil;
import apelet.common.sequence.wrapper.IdGeneratorWrapper; import apelet.common.sequence.wrapper.IdGeneratorWrapper;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
@ -35,7 +36,6 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.Page; import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.redisson.api.RBucket; import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient; import org.redisson.api.RedissonClient;
@ -460,6 +460,24 @@ public class OnlineFormServiceImpl extends BaseService<OnlineForm, Long> impleme
return objectValue; return objectValue;
} }
@Override
public List<OnlineForm> queryListByFormType(List<Integer> formType) {
// 查询已发布的 page
List<OnlinePage> pageList = onlinePageService.queryByEnableList();
if (CollectionUtil.isEmpty(pageList)) {
return Collections.emptyList();
}
Set<Long> pageIdSet = pageList.stream().map(OnlinePage::getPageId).collect(Collectors.toSet());
List<OnlineForm> formList = onlineFormMapper.selectList(new LambdaQueryWrapper<OnlineForm>().in(OnlineForm::getFormType, formType).in(OnlineForm::getPageId, pageIdSet));
if (CollectionUtil.isNotEmpty(formList)) {
formList.forEach(item -> {
item.setWidgetJson(null);
});
}
return formList;
}
/** /**
* 设置关联表字段转换为对象 * 设置关联表字段转换为对象
* *

5
common/common-online/src/main/java/apelet/common/online/service/impl/OnlinePageServiceImpl.java

@ -296,4 +296,9 @@ public class OnlinePageServiceImpl extends BaseService<OnlinePage, Long> impleme
} }
return onlinePageMapper.selectList(queryWrapper); return onlinePageMapper.selectList(queryWrapper);
} }
@Override
public List<OnlinePage> queryByEnableList() {
return onlinePageMapper.selectList(new LambdaQueryWrapper<OnlinePage>().eq(OnlinePage::getPublished, true));
}
} }

Loading…
Cancel
Save