fix framework p2 custom views

This commit is contained in:
2026-06-18 15:35:17 +08:00
parent 55b9c201d6
commit b6e6964913
11 changed files with 653 additions and 47 deletions

View File

@@ -7,6 +7,7 @@ import java.util.Set;
import java.util.regex.Pattern;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@@ -64,10 +65,16 @@ public class FieldEditorController {
@PostMapping("/api/field-editor/save")
public Map<String, Object> save(@RequestBody FieldEditRequest req) {
Identifiers ids = validate(req);
int updated = jdbcTemplate.update(
"UPDATE " + ids.tableName + " SET " + ids.fieldName + " = ? WHERE "
+ ids.idField + " = ?",
req.value == null ? "" : req.value, req.id);
String valueExpr = "XML".equals(ids.fieldName) ? "CAST(? AS xml)" : "?";
int updated;
try {
updated = jdbcTemplate.update(
"UPDATE " + ids.tableName + " SET " + ids.fieldName + " = " + valueExpr + " WHERE "
+ ids.idField + " = ?",
req.value == null ? "" : req.value, req.id);
} catch (DataAccessException ex) {
return fail("保存失败:" + mostSpecificMessage(ex));
}
if (updated <= 0) {
return fail("记录不存在或已被删除");
}
@@ -114,6 +121,12 @@ public class FieldEditorController {
return resp;
}
private static String mostSpecificMessage(DataAccessException ex) {
Throwable cause = ex.getMostSpecificCause();
String msg = cause == null ? ex.getMessage() : cause.getMessage();
return msg == null || msg.isBlank() ? "数据库更新失败" : msg;
}
public static class FieldEditRequest {
public String tableName;
public String idField;

View File

@@ -50,6 +50,10 @@ public class TemplateTreeController {
private static final Pattern IDENT = Pattern.compile("^[A-Za-z0-9_]+$");
private static final int MAX_NODES = 5000;
private static final String[] NAME_FALLBACKS = {"NAME", "SORT_NAME", "FULLNAME", "TITLE", "SUBJECT", "DISPLAYNAME"};
private static final String[] ORDER_FALLBACKS = {
"SEQ", "ORDERASC", "ORDERID", "ORDERNO", "ORDER_NO", "SORT", "SORTNO", "SORT_NO",
"SORTID", "SORTNUMBER", "TYPESORT", "SEQU", "SEQUENCE", "SEQUENCE_NUMBER", "DISPLAYSEQ"
};
private static final Map<String, EntityListJoin> ENTITYS_LIST_JOINS = Map.of(
"VIEW_TEMPLATE", new EntityListJoin("VIEW_TEMPLATE_ID", "VIEW_TEMPLATEENTITY", "TEMPLATEID", "ENTITYID"),
"VIEW_PAGE", new EntityListJoin("VIEW_PAGE_ID", "VIEW_PAGEENTITY", "PAGEID", "ENTITYID"),
@@ -376,6 +380,10 @@ public class TemplateTreeController {
sql.append(", ").append(parentCols.get(i)).append(" AS pref").append(i == 0 ? "" : i);
}
sql.append(" FROM eip3.").append(table);
String orderCol = orderColumn(table);
if (orderCol != null) {
sql.append(" ORDER BY ").append(orderCol).append(" NULLS LAST, lbl NULLS LAST, k");
}
try {
List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql.toString());
for (Map<String, Object> r : rows) {
@@ -451,6 +459,17 @@ public class TemplateTreeController {
return keyCol;
}
/** 树节点排序列:优先使用实体表里的显式排序字段;没有则保持数据库原顺序,避免误改无序树。 */
private String orderColumn(String table) {
Set<String> phys = physicalColumns(table);
for (String cand : ORDER_FALLBACKS) {
if (physExists(phys, cand) && IDENT.matcher(cand).matches()) {
return cand;
}
}
return null;
}
private Set<String> physicalColumns(String table) {
try {
return new LinkedHashSet<>(jdbcTemplate.queryForList(

View File

@@ -56,6 +56,22 @@ class FieldEditorControllerTest {
assertThat(ex.getMessage()).contains("只允许脚本/XML/配置字段");
}
@Test
void save_XML字段使用数据库xml类型转换() {
FieldEditorController.FieldEditRequest req = request("VIEW_TEMPLATE", "VIEW_TEMPLATE_ID",
"TPL-1", "XML", "<crud/>");
when(jdbcTemplate.update(
eq("UPDATE VIEW_TEMPLATE SET XML = CAST(? AS xml) WHERE VIEW_TEMPLATE_ID = ?"),
eq("<crud/>"), eq("TPL-1"))).thenReturn(1);
Map<String, Object> save = controller.save(req);
assertThat(save).containsEntry("status", 0).containsEntry("msg", "保存成功");
verify(whitelist).requireLegalTable("VIEW_TEMPLATE");
verify(whitelist).requireLegalColumn("VIEW_TEMPLATE", "VIEW_TEMPLATE_ID", "主键列");
verify(whitelist).requireLegalColumn("VIEW_TEMPLATE", "XML", "编辑字段");
}
private static FieldEditorController.FieldEditRequest request(String tableName, String idField, String id,
String fieldName, String value) {
FieldEditorController.FieldEditRequest req = new FieldEditorController.FieldEditRequest();

View File

@@ -98,6 +98,17 @@ class TemplateTreeControllerTest {
assertEquals("C2", c2List.get(0).get("value"));
}
@Test
@DisplayName("构树:实体表存在排序字段时按排序字段加载")
void tree_按排序字段加载层级() {
when(jdbc.queryForList(contains("information_schema"), eq(String.class), eq("CAT")))
.thenReturn(List.of("CAT_ID", "NAME", "PARENTID", "SEQ"));
controller.tree(TREE_REF);
verify(jdbc).queryForList(contains("FROM eip3.CAT ORDER BY SEQ NULLS LAST"));
}
/**
* 菜单维护树(b38737be 形态):两个 &lt;node&gt; 根 + SYS_MENU 条件父列。
* SYS_MENU 行有 PARENTID 则挂父菜单(自引用),否则按 MENUROOT 挂 sys_menu_root 根分组。