优化:完善分类规则与Excel配置维护并加速查询翻页

This commit is contained in:
2026-10-31 17:50:14 +08:00
parent 9dbe85c65c
commit aa7c320dc6
56 changed files with 1709 additions and 440 deletions
+4
View File
@@ -51,3 +51,7 @@
- 指标名称保持“亮片合格率”“综合合格率”,切换后增加11px/400的“(去本部报废)”小字。顶部卡片与跨行综合表头分行显示,亮片二级表头单行显示以维持行高;说明图标样式与黑片总重一致,保留原有计算口径入口。
- 所有查询页导出按钮显示下载图标与“Excel导出”文字。
- 2026-10-31 已按用户要求发布;构建及31项相关检查通过,线上字段顺序、口径切换、导出入口与顶部冻结已核对。
- 后续本地调整:顶部指标卡片恢复随页面滚动,移除其标题下的“(去本部报废)”,保留表头口径提示及切换功能;口径切换与卡片共用一行,移除上方独占空行并压缩卡片内边距。顶部筛选栏及表格冻结列不变。实际滚动检查、类型检查及31项相关检查通过,2026-10-31 已发布。
- 原口径隐藏亮片与综合合格率的说明图标,去本部报废口径保留图标;黑片总重量规则图标始终保留。构建及32项检查通过,已实测线上切换,2026-10-31 已发布。
@@ -215,7 +215,7 @@ public class AnalysisFineDefectsService {
&& !categories(stage).stream().anyMatch(item -> item.name().equals(category))) throw bad("category 不属于所选 stage");
String productSearch = optionalText(input, "productSearch");
if (productSearch != null && productSearch.length() > SelectionValues.MAX_VALUE_LENGTH) throw bad("产品搜索文本过长");
boolean includeUnassigned = bool(input, "includeUnassigned", false);
boolean includeUnassigned = bool(input, "includeUnassigned", true);
int limit = integer(input, "limit") == null ? 20 : integer(input, "limit");
if (limit < 1 || limit > 100) throw bad("limit 必须在 1 到 100 之间");
return new Request(releaseId, mode, year, month, quarter, optionalText(input, "department"),
@@ -0,0 +1,53 @@
package cn.yunsheng.quality.common;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.function.Supplier;
/** Bounded process-local cache for immutable release data. Authorization must precede every lookup. */
public final class ImmutableQueryCache<K, V> {
private final int capacity;
private final Map<K, V> values = new LinkedHashMap<>(16, .75f, true);
private final Map<K, FutureTask<V>> loading = new HashMap<>();
public ImmutableQueryCache(int capacity) {
if (capacity < 1) throw new IllegalArgumentException("capacity");
this.capacity = capacity;
}
public V get(K key, Supplier<V> loader) {
FutureTask<V> task;
boolean owner = false;
synchronized (this) {
if (values.containsKey(key)) return values.get(key);
task = loading.get(key);
if (task == null) {
task = new FutureTask<>(() -> Objects.requireNonNull(loader.get()));
loading.put(key, task);
owner = true;
}
}
if (owner) task.run(); // Different releases/queries can load concurrently.
try {
V value = task.get();
if (owner) synchronized (this) {
values.put(key, value);
while (values.size() > capacity) values.remove(values.keySet().iterator().next());
}
return value;
} catch (InterruptedException interrupted) {
Thread.currentThread().interrupt();
throw new IllegalStateException("查询等待被中断", interrupted);
} catch (ExecutionException failure) {
if (failure.getCause() instanceof RuntimeException cause) throw cause;
if (failure.getCause() instanceof Error cause) throw cause;
throw new IllegalStateException(failure.getCause());
} finally {
if (owner) synchronized (this) { loading.remove(key, task); }
}
}
}
@@ -1,6 +1,7 @@
package cn.yunsheng.quality.filters;
import cn.yunsheng.quality.common.DataScope;
import cn.yunsheng.quality.common.ImmutableQueryCache;
import cn.yunsheng.quality.common.DepartmentLabels;
import cn.yunsheng.quality.common.SelectionValues;
import cn.yunsheng.quality.governance.GovernanceAccessService;
@@ -20,6 +21,7 @@ import java.util.Objects;
import java.util.Set;
import org.springframework.http.HttpStatus;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
@@ -30,6 +32,34 @@ import tools.jackson.databind.json.JsonMapper;
/** Actor-scoped catalog and parameterized filters for published source columns. */
@Service
public class SourceColumnQueries {
private record QueryKey(String sql, List<Object> args, String projection) {
QueryKey { args = List.copyOf(args); }
}
private final ImmutableQueryCache<QueryKey, Long> totals = new ImmutableQueryCache<>(128);
private final ImmutableQueryCache<QueryKey, List<String>> rawHeaders = new ImmutableQueryCache<>(32);
private final ImmutableQueryCache<QueryKey, List<Map<String, Object>>> pages = new ImmutableQueryCache<>(64);
private long total(String sql, List<Object> args) {
return totals.get(new QueryKey(sql,args,"count"), () -> {
Long value=jdbc.queryForObject(sql,Long.class,args.toArray());return value==null?0L:value;
});
}
/** Reuse a bounded window; authorization/catalog validation still runs before this method. */
private List<Map<String,Object>> page(String sql,List<Object> args,String projection,int offset,int limit,
RowMapper<Map<String,Object>> mapper) {
List<Map<String,Object>> result=new ArrayList<>();
for(long start=offset,end=(long)offset+limit;start<end;) {
long windowStart=start/240*240;List<Object> parameters=new ArrayList<>(args);
parameters.add(240);parameters.add(windowStart);
List<Map<String,Object>> window=pages.get(new QueryKey(sql,parameters,projection),
()->List.copyOf(jdbc.query(sql,mapper,parameters.toArray())));
int first=(int)(start-windowStart),last=(int)Math.min(window.size(),end-windowStart);
if(first>=last)break;
result.addAll(window.subList(first,last));start=windowStart+last;
}
return List.copyOf(result);
}
private static final List<String> STAGES = List.of("black", "bright");
private static final List<String> OPERATORS = List.of("in", "not_in", "is_empty", "not_empty");
private static final Map<String, String> CANONICAL_COLUMNS = Map.of(
@@ -74,7 +104,8 @@ public class SourceColumnQueries {
+ "ON f2.build_id=rm.transform_build_id AND f2.source_revision_id=rm.source_revision_id"
+ representativeScope + " GROUP BY f2.build_id,f2.source_revision_id) representative "
+ "JOIN fact_" + stage + " f ON f.id=representative.representative_id ORDER BY representative.build_id";
List<String> keyLists = jdbc.query(sql, (rs, row) -> rs.getString(1), args.toArray());
List<String> keyLists = rawHeaders.get(new QueryKey(sql,args,"raw-headers"),
() -> Collections.unmodifiableList(new ArrayList<>(jdbc.query(sql, (rs, row) -> rs.getString(1), args.toArray()))));
stagePresent.put(stage, !keyLists.isEmpty());
for (String rawKeys : keyLists) {
if (rawKeys == null) continue;
@@ -308,13 +339,10 @@ public class SourceColumnQueries {
String unionSql = String.join(" UNION ALL ", selects);
String tupleColumns = tupleColumns(memberFields.size());
String grouped = "SELECT " + tupleColumns + " FROM (" + unionSql + ") member_rows GROUP BY " + tupleColumns;
Long total = jdbc.queryForObject("SELECT COUNT(*) FROM (" + grouped + ") member_tuples", Long.class, queryArgs.toArray());
List<Object> pageArgs = new ArrayList<>(queryArgs);
pageArgs.add(limit);
pageArgs.add(offset);
long total = total("SELECT COUNT(*) FROM (" + grouped + ") member_tuples", queryArgs);
String pageSql = "SELECT " + tupleColumns + ", GROUP_CONCAT(DISTINCT member_stage ORDER BY member_stage SEPARATOR ',') AS member_stages "
+ "FROM (" + unionSql + ") member_rows GROUP BY " + tupleColumns + " ORDER BY " + tupleColumns + " LIMIT ? OFFSET ?";
List<Map<String, Object>> rows = jdbc.query(pageSql, (rs, row) -> {
List<Map<String, Object>> rows = page(pageSql,queryArgs,memberFields.toString(),offset,limit,(rs, row) -> {
Map<String, Object> values = new LinkedHashMap<>();
for (int index = 0; index < memberFields.size(); index++) values.put(memberFields.get(index), rs.getString("c" + index));
String joinedStages = rs.getString("member_stages");
@@ -324,8 +352,8 @@ public class SourceColumnQueries {
result.put("values", Collections.unmodifiableMap(values));
result.put("stages", rowStages);
return Collections.unmodifiableMap(result);
}, pageArgs.toArray());
long totalCount = total == null ? 0 : total;
});
long totalCount = total;
Map<String, Object> filters = queryContext.filtersResponse();
Map<String, Object> response = new LinkedHashMap<>();
response.put("rows", List.copyOf(rows));
@@ -402,16 +430,11 @@ public class SourceColumnQueries {
}
String candidates = "SELECT candidate_value FROM (" + union + ") source_values" + condition;
String grouped = "SELECT candidate_value FROM (" + candidates + ") matches GROUP BY candidate_value";
Long total = jdbc.queryForObject("SELECT COUNT(*) FROM (" + grouped + ") distinct_values", Long.class,
combine(unionArgs, searchArgs).toArray());
List<Object> pageArgs = combine(unionArgs, searchArgs);
pageArgs.add(limit);
pageArgs.add(offset);
List<Map<String, Object>> rows = jdbc.query("SELECT candidate_value FROM (" + candidates + ") matches "
long total = total("SELECT COUNT(*) FROM (" + grouped + ") distinct_values", combine(unionArgs,searchArgs));
List<Map<String, Object>> rows = page("SELECT candidate_value FROM (" + candidates + ") matches "
+ "GROUP BY candidate_value ORDER BY candidate_value LIMIT ? OFFSET ?",
(rs, row) -> Map.of("value", rs.getString("candidate_value")),
pageArgs.toArray());
long totalCount = total == null ? 0 : total;
combine(unionArgs,searchArgs),fieldKey,offset,limit,(rs, row) -> Map.of("value", rs.getString("candidate_value")));
long totalCount = total;
Map<String, Object> response = new LinkedHashMap<>();
response.put("rows", List.copyOf(rows));
response.put("totalCount", totalCount);
@@ -0,0 +1,61 @@
package cn.yunsheng.quality.governance;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import cn.yunsheng.quality.common.ImmutableQueryCache;
import java.math.BigDecimal;
import java.util.*;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
/** Counts current immutable source rows, including already classified rows, without changing mappings. */
@Service
public class ClassificationPreviewService {
private final GovernanceSupport g;
private final CandidateAcceptanceService access;
private record Key(String release,String stage,List<Condition> conditions) { Key { conditions=List.copyOf(conditions); } }
private final ImmutableQueryCache<Key,List<Map<String,Object>>> previews=new ImmutableQueryCache<>(64);
public ClassificationPreviewService(GovernanceSupport g,CandidateAcceptanceService access){this.g=g;this.access=access;}
public record Condition(String field,String operator,String value){}
public record Request(String stage,String target,List<Condition> conditions){}
private static final Map<String,String> FIELDS=Map.of("@department","f.department","@product","f.product",
"PDT代码","JSON_VALUE(f.components,'$.sourcePdtCode')","销售片区","JSON_VALUE(f.components,'$.sourceSalesRegion')");
public Map<String,Object> preview(Request request,AppUserPrincipal actor){
validate(request);access.requireSourceDetail(actor);
var current=g.rows("SELECT release_id FROM current_release WHERE dataset_id='quality'");
if(current.isEmpty())return Map.of("items",List.of(),"matchedRows",0,"changedRows",0,"totalWeight",BigDecimal.ZERO);
String release=String.valueOf(current.getFirst().get("releaseId"));access.requireRead(release,actor);
var items=previews.get(new Key(release,request.stage(),request.conditions()),()->load(release,request));
long matched=0,changed=0;BigDecimal weight=BigDecimal.ZERO;
for(var row:items){long count=((Number)row.get("rowCount")).longValue();matched+=count;
if(!Objects.equals(row.get("currentGroup"),request.target()))changed+=count;
if(row.get("totalWeight") instanceof Number n)weight=weight.add(new BigDecimal(n.toString()));}
return Map.of("releaseId",release,"items",items,"matchedRows",matched,"changedRows",changed,"totalWeight",weight);
}
static void validate(Request request){
if(request==null||!Set.of("all","black","bright").contains(Objects.toString(request.stage(),""))
||request.target()==null||request.target().isBlank()||request.target().length()>160
||request.conditions()==null||request.conditions().isEmpty()||request.conditions().size()>4)
throw GovernanceSupport.error(HttpStatus.BAD_REQUEST,"分类规则条件无效");
Set<String> fields=new HashSet<>();
for(var condition:request.conditions())if(condition==null||condition.field()==null||!FIELDS.containsKey(condition.field())||!fields.add(condition.field())
||!Set.of("EQUALS","IS_BLANK").contains(Objects.toString(condition.operator(),""))
||condition.value()==null||condition.value().length()>200
||("EQUALS".equals(condition.operator())&&condition.value().isBlank())
||("IS_BLANK".equals(condition.operator())&&!condition.value().isEmpty()))
throw GovernanceSupport.error(HttpStatus.BAD_REQUEST,"分类条件字段、判断方式或判断值无效");
}
private List<Map<String,Object>> load(String release,Request request){
List<String> queries=new ArrayList<>();List<Object> args=new ArrayList<>();
for(String stage:request.stage().equals("all")?List.of("black","bright"):List.of(request.stage())){
StringBuilder where=new StringBuilder(" WHERE rm.release_id=?");args.add(release);
for(var condition:request.conditions()){
String expression=FIELDS.get(condition.field());
if(condition.operator().equals("IS_BLANK"))where.append(" AND (").append(expression).append(" IS NULL OR REGEXP_LIKE(").append(expression).append(",'^[[:space:]]*$'))");
else{where.append(" AND CAST(").append(expression).append(" AS BINARY)=CAST(? AS BINARY)");args.add(condition.value());}
}
queries.add("SELECT '"+stage+"' AS stage,f.department,f.month_key AS month,COALESCE(f.group_name,'') AS current_group,COUNT(*) AS row_count,SUM(f.total_weight) AS total_weight "
+"FROM release_member rm JOIN fact_"+stage+" f ON f.build_id=rm.transform_build_id"+where+" GROUP BY f.department,f.month_key,COALESCE(f.group_name,'')");
}
return g.rows("SELECT * FROM ("+String.join(" UNION ALL ",queries)+") impact ORDER BY stage,month,current_group",args.toArray());
}
}
@@ -1,30 +0,0 @@
package cn.yunsheng.quality.governance;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import java.time.YearMonth;
import java.util.*;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import static cn.yunsheng.quality.governance.GovernanceSupport.error;
/** Registers inputs and readiness only. No amount, conversion or savings calculation is exposed. */
@RestController @RequestMapping("/api/governance/cost-configurations")
public class CostConfigurationController {
private final GovernanceSupport g;public CostConfigurationController(GovernanceSupport g){this.g=g;}
@GetMapping @PreAuthorize("hasAnyRole('SYSTEM_ADMIN','DATA_MAINTAINER','RULE_MAINTAINER','AUDIT_VIEWER')")
public Map<String,Object> list(){return Map.of("items",g.rows("SELECT * FROM cost_configuration_version ORDER BY created_at DESC"),"calculationEnabled",false);}
@PostMapping @PreAuthorize("hasAnyRole('SYSTEM_ADMIN','DATA_MAINTAINER','RULE_MAINTAINER')") @Transactional
public Map<String,Object> create(@RequestBody Draft r,@AuthenticationPrincipal AppUserPrincipal actor){g.required(r.name(),"名称");g.required(r.reason(),"理由");if(r.definition()==null)throw error(HttpStatus.BAD_REQUEST,"配置定义不能为空");String id=UUID.randomUUID().toString();g.jdbc.update("INSERT INTO cost_configuration_version(id,name,definition,created_by)VALUES(?,?,CAST(? AS JSON),?)",id,r.name(),g.encode(r.definition()),actor.getId().toString());g.audit(actor.getId().toString(),"COST_CONFIGURATION_REGISTERED","cost_configuration_version",id,Map.of("reason",r.reason(),"calculationEnabled",false));return Map.of("configuration",get(id));}
@PutMapping("/{id}") @PreAuthorize("hasAnyRole('SYSTEM_ADMIN','DATA_MAINTAINER','RULE_MAINTAINER')") @Transactional
public Map<String,Object> update(@PathVariable String id,@RequestBody Draft r,@AuthenticationPrincipal AppUserPrincipal actor){Map<String,Object> before=get(id);g.required(r.name(),"名称");g.required(r.reason(),"理由");if(r.definition()==null||r.expectedRowVersion()==null||((Number)before.get("rowVersion")).longValue()!=r.expectedRowVersion()||!"DRAFT".equals(before.get("status")))throw error(HttpStatus.CONFLICT,"仅可修改未验证的同版本草稿");int changed=g.jdbc.update("UPDATE cost_configuration_version SET name=?,definition=CAST(? AS JSON),validation=NULL,row_version=row_version+1 WHERE id=? AND row_version=? AND status='DRAFT'",r.name(),g.encode(r.definition()),id,r.expectedRowVersion());if(changed==0)throw error(HttpStatus.CONFLICT,"配置版本已变化");g.audit(actor.getId().toString(),"COST_CONFIGURATION_UPDATED","cost_configuration_version",id,Map.of("reason",r.reason()));return Map.of("configuration",get(id));}
@SuppressWarnings("unchecked") @PostMapping("/{id}/validate") @PreAuthorize("hasAnyRole('SYSTEM_ADMIN','DATA_MAINTAINER','RULE_MAINTAINER')") @Transactional
public Map<String,Object> validate(@PathVariable String id,@RequestBody Map<String,Object> r,@AuthenticationPrincipal AppUserPrincipal actor){Map<String,Object> c=g.one("SELECT * FROM cost_configuration_version WHERE id=? FOR UPDATE",id);if(!(r.get("expectedRowVersion") instanceof Number expected)||expected.longValue()!=((Number)c.get("rowVersion")).longValue()||!"DRAFT".equals(c.get("status")))throw error(HttpStatus.CONFLICT,"配置状态或版本已变化");
Map<String,Object> def=(Map<String,Object>)c.get("definition");List<String> errors=validateDefinition(def);String readiness=errors.isEmpty()?"READY_FOR_FUTURE_INTEGRATION":"MATERIALS_REQUIRED";Map<String,Object> v=Map.of("valid",errors.isEmpty(),"readiness",readiness,"errors",errors,"calculationEnabled",false,"numericalAcceptanceCompleted",false);
g.jdbc.update("UPDATE cost_configuration_version SET status=?,validation=CAST(? AS JSON),row_version=row_version+1 WHERE id=?",errors.isEmpty()?"VERIFIED_INPUTS":"DRAFT",g.encode(v),id);g.audit(actor.getId().toString(),"COST_CONFIGURATION_VALIDATED","cost_configuration_version",id,v);return Map.of("configuration",get(id),"validation",v);}
static List<String> validateDefinition(Map<String,Object> definition){List<String> errors=new ArrayList<>();Object raw=definition.get("sources");if(!(raw instanceof List<?> sources)||sources.isEmpty())return List.of("至少登记一个成本来源");Map<String,List<Period>> byObject=new HashMap<>();for(Object item:sources){if(!(item instanceof Map<?,?> source)){errors.add("来源须为对象");continue;}for(String key:List.of("sourceReference","objectCode","costField","unit","currency","approvalReference","originalFormula","baselineReference"))if(source.get(key)==null||String.valueOf(source.get(key)).isBlank())errors.add("来源缺少"+key);try{String object=String.valueOf(source.get("objectCode"));YearMonth from=YearMonth.parse(String.valueOf(source.get("fromMonth"))),to=YearMonth.parse(String.valueOf(source.get("toMonth")));if(from.isAfter(to)){errors.add("成本来源有效期倒置");continue;}List<Period> periods=byObject.computeIfAbsent(object,k->new ArrayList<>());if(periods.stream().anyMatch(p->!from.isAfter(p.to)&&!to.isBefore(p.from)))errors.add("成本来源配置重复或有效期重叠:"+object);periods.add(new Period(from,to));}catch(Exception e){errors.add("成本来源须有有效的fromMonth/toMonth");}}return errors.stream().distinct().toList();}
private Map<String,Object> get(String id){return g.one("SELECT * FROM cost_configuration_version WHERE id=?",id);}
public record Draft(String name,Map<String,Object> definition,Long expectedRowVersion,String reason){}
private record Period(YearMonth from,YearMonth to){}
}
@@ -26,7 +26,7 @@ public class GovernanceDependencyService {
List<Map<String,Object>> references=g.rows("SELECT id,name,status,created_at FROM data_release WHERE "+column+"=? ORDER BY created_at DESC",id);
List<Map<String,Object>> affected=g.rows("SELECT DISTINCT d.id AS source_document_id,d.source_slot,d.stage,d.source_year,d.source_month,r.id AS source_revision_id,b.id AS build_id FROM release_member rm JOIN source_document d ON d.id=rm.source_document_id JOIN source_revision r ON r.id=rm.source_revision_id JOIN transform_build b ON b.id=rm.transform_build_id WHERE rm.release_id=(SELECT release_id FROM current_release WHERE dataset_id='quality') ORDER BY d.source_year,d.source_month,d.stage");
List<Map<String,Object>> frozen=g.rows("SELECT snapshot,snapshot_sha256,frozen_by,frozen_at FROM governance_dependency WHERE owner_kind=? AND owner_id=?",kind,id);
Map<String,Object> result=new LinkedHashMap<>(Map.of("kind",kind,"version",version,"references",references,"affectedSources",affected,"frozen",frozen,"requiresNewRelease",true,"currentReleaseUnchanged",true,"costCalculationEnabled",false));if(kind.equals("mapping")){Map<String,Object> preview=mappingImpact.preview(id,actor);result.putAll(preview);Set<Object> documents=new HashSet<>();if(preview.get("hits") instanceof List<?> hits)for(Object hit:hits)if(hit instanceof Map<?,?> h)documents.add(h.get("sourceDocumentId"));result.put("affectedSources",affected.stream().filter(source->documents.contains(source.get("sourceDocumentId"))).toList());}return result;
Map<String,Object> result=new LinkedHashMap<>(Map.of("kind",kind,"version",version,"references",references,"affectedSources",affected,"frozen",frozen,"requiresNewRelease",true,"currentReleaseUnchanged",true));if(kind.equals("mapping")){Map<String,Object> preview=mappingImpact.preview(id,actor);result.putAll(preview);Set<Object> documents=new HashSet<>();if(preview.get("hits") instanceof List<?> hits)for(Object hit:hits)if(hit instanceof Map<?,?> h)documents.add(h.get("sourceDocumentId"));result.put("affectedSources",affected.stream().filter(source->documents.contains(source.get("sourceDocumentId"))).toList());}return result;
}
public Map<String,Object> inspect(String id){
Map<String,Object> release=g.one("SELECT id,name,status,parent_release_id,expected_current_release_id,rule_version,mapping_version,target_version_id,acceptance_id,complete,validation,row_version,created_at,published_at FROM data_release WHERE id=?",id);
@@ -7,8 +7,14 @@ import org.springframework.web.bind.annotation.*;
@RestController @RequestMapping("/api/governance/mapping-issues")
@PreAuthorize("hasAnyRole('SYSTEM_ADMIN','DATA_PUBLISHER','DATA_MAINTAINER','RULE_MAINTAINER','AUDIT_VIEWER')")
public class MappingIssueController {
private final MappingIssueService service;public MappingIssueController(MappingIssueService service){this.service=service;}
@GetMapping public Map<String,Object> list(@RequestParam(required=false) String importRunId,@RequestParam(required=false) String status,@RequestParam(defaultValue="1") int page,@RequestParam(defaultValue="50") int pageSize,@AuthenticationPrincipal AppUserPrincipal actor){return service.list(importRunId,status,page,pageSize,actor);}
private final MappingIssueService service;
private final UnclassifiedSourceService unclassified;
private final ClassificationPreviewService preview;
public MappingIssueController(MappingIssueService service, UnclassifiedSourceService unclassified,ClassificationPreviewService preview){this.service=service;this.unclassified=unclassified;this.preview=preview;}
@PostMapping("/classification-preview") public Map<String,Object> classificationPreview(@RequestBody ClassificationPreviewService.Request request,@AuthenticationPrincipal AppUserPrincipal actor){return preview.preview(request,actor);}
@GetMapping("/classification-metadata") public Map<String,Object> classificationMetadata(){return cn.yunsheng.quality.ingestion.ConfigurableMatchRules.metadata();}
@GetMapping("/unclassified") public Map<String,Object> unclassified(@RequestParam(defaultValue="1") int page,@RequestParam(defaultValue="50") int pageSize,@AuthenticationPrincipal AppUserPrincipal actor){return unclassified.list(page,pageSize,actor);}
@GetMapping public Map<String,Object> list(@RequestParam(required=false) String importRunId,@RequestParam(required=false) String status,@RequestParam(defaultValue="1") int page,@RequestParam(defaultValue="50") int pageSize,@RequestParam(required=false) String mappingType,@RequestParam(required=false) String customerCode,@RequestParam(required=false) String businessGroup,@RequestParam(required=false) String department,@AuthenticationPrincipal AppUserPrincipal actor){return service.list(importRunId,status,page,pageSize,customerCode,businessGroup,department,mappingType,actor);}
@GetMapping("/{id}") public Map<String,Object> detail(@PathVariable String id,@AuthenticationPrincipal AppUserPrincipal actor){return service.detail(id,actor);}
@PostMapping("/{id}/resolve") @PreAuthorize("hasAnyRole('SYSTEM_ADMIN','DATA_MAINTAINER','RULE_MAINTAINER')")
public Map<String,Object> resolve(@PathVariable String id,@RequestBody MappingIssueService.Resolution request,@AuthenticationPrincipal AppUserPrincipal actor){return service.resolve(id,request,actor);}
@@ -0,0 +1,72 @@
package cn.yunsheng.quality.governance;
import java.util.*;
import cn.yunsheng.quality.common.ImmutableQueryCache;
/** Derive maintenance filters from immutable source facts, never from name prefixes. */
final class MappingIssueDimensions {
private final GovernanceSupport g;
private final ImmutableQueryCache<String, Map<String, Set<String>>> indexes = new ImmutableQueryCache<>(12);
MappingIssueDimensions(GovernanceSupport g) { this.g = g; }
List<Map<String,Object>> decorate(List<Map<String,Object>> issues) {
List<Map<String,Object>> result = new ArrayList<>();
for (var issue : issues) {
Map<String,Object> row = new LinkedHashMap<>(issue);
String type = Objects.toString(row.get("mappingType"), "");
String sourceKey = Objects.toString(row.get("sourceKey"), "");
if (type.equals("PROJECT_GROUP")) {
int separator = sourceKey.indexOf('|');
row.put("customerCode", separator < 0 ? sourceKey : sourceKey.substring(0, separator));
row.put("businessGroup", separator < 0 ? "" : sourceKey.substring(separator + 1));
}
Object details = row.get("details");
String release = details instanceof Map<?,?> values ? Objects.toString(values.get("releaseId"), "") : "";
Map<String,Set<String>> index = release.isBlank() ? Map.of() : indexes.get(release, () -> {
Map<String,Set<String>> values = new HashMap<>();
for (var source : g.rows(sourceSql("black") + " UNION " + sourceSql("bright"), release, release)) {
String department = Objects.toString(source.get("department"), "");
if (department.isBlank()) continue;
index(values, "CATEGORY", key(source.get("pdtCode"), source.get("salesRegion")), department);
index(values, "PROJECT_GROUP", key(source.get("customerCode"), source.get("groupName")), department);
}
return values;
});
row.put("departments", List.copyOf(index.getOrDefault(type + "\n" + sourceKey, Set.of())));
result.add(row);
}
return result;
}
private static void index(Map<String,Set<String>> index, String type, String key, String department) {
index.computeIfAbsent(type + "\n" + key, ignored -> new TreeSet<>()).add(department);
}
private static String key(Object first, Object second) {
return Objects.toString(first, "") + "|" + Objects.toString(second, "");
}
static String sourceSql(String stage) {
if (!Set.of("black", "bright").contains(stage)) throw new IllegalArgumentException("stage");
return "SELECT DISTINCT f.department,f.group_name,JSON_VALUE(f.components,'$.sourcePdtCode') AS pdt_code,"
+ "JSON_VALUE(f.components,'$.sourceSalesRegion') AS sales_region,"
+ "JSON_VALUE(f.components,'$.sourceCustomerCode') AS customer_code "
+ "FROM release_member rm JOIN fact_" + stage + " f ON f.build_id=rm.transform_build_id "
+ "WHERE rm.release_id=? AND (JSON_VALUE(f.components,'$.categoryMappingStatus')='unmapped' "
+ "OR JSON_VALUE(f.components,'$.projectGroupStatus')='unmapped')";
}
static boolean matches(Map<String,Object> row, String customer, String group, String department) {
return (customer == null || customer.isBlank() || customer.equals(row.get("customerCode")))
&& (group == null || group.isBlank() || group.equals(row.get("businessGroup")))
&& (department == null || department.isBlank() || ((List<?>) row.getOrDefault("departments", List.of())).contains(department));
}
static Map<String,Object> options(List<Map<String,Object>> rows) {
Set<String> customers = new TreeSet<>(), groups = new TreeSet<>(), departments = new TreeSet<>();
for (var row : rows) {
String customer = Objects.toString(row.get("customerCode"), "");
String group = Objects.toString(row.get("businessGroup"), "");
if (!customer.isBlank()) customers.add(customer);
if (!group.isBlank()) groups.add(group);
for (Object value : (List<?>) row.getOrDefault("departments", List.of())) departments.add(String.valueOf(value));
}
return Map.of("customerCodes", customers, "businessGroups", groups, "departments", departments);
}
}
@@ -17,8 +17,8 @@ import static cn.yunsheng.quality.governance.GovernanceSupport.error;
@Service
public class MappingIssueService {
private final GovernanceSupport g;private final CandidateAcceptanceService acceptance;private final ImportRunService imports;private final ObjectProvider<ImportWorker> worker;private final TransactionTemplate transaction;
public MappingIssueService(GovernanceSupport g,CandidateAcceptanceService acceptance,ImportRunService imports,ObjectProvider<ImportWorker> worker,PlatformTransactionManager manager){this.g=g;this.acceptance=acceptance;this.imports=imports;this.worker=worker;this.transaction=new TransactionTemplate(manager);this.transaction.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);}
private final MappingIssueDimensions dimensions;private final GovernanceSupport g;private final CandidateAcceptanceService acceptance;private final ImportRunService imports;private final ObjectProvider<ImportWorker> worker;private final TransactionTemplate transaction;
public MappingIssueService(GovernanceSupport g,CandidateAcceptanceService acceptance,ImportRunService imports,ObjectProvider<ImportWorker> worker,PlatformTransactionManager manager){this.g=g;this.dimensions=new MappingIssueDimensions(g);this.acceptance=acceptance;this.imports=imports;this.worker=worker;this.transaction=new TransactionTemplate(manager);this.transaction.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);}
/** Aggregated from immutable transformed facts, retaining their exact source/build lineage. */
public void capture(String runId,String releaseId){
Map<String,Group> groups=new TreeMap<>();for(String stage:List.of("black","bright"))for(Map<String,Object> f:g.rows("SELECT f.group_name,f.total_weight,f.components,f.raw_values,f.month_key,rm.source_document_id,rm.source_revision_id,rm.transform_build_id FROM release_member rm JOIN fact_"+stage+" f ON f.build_id=rm.transform_build_id WHERE rm.release_id=? AND (JSON_UNQUOTE(JSON_EXTRACT(f.components,'$.categoryMappingStatus'))='unmapped' OR JSON_UNQUOTE(JSON_EXTRACT(f.components,'$.projectGroupStatus'))='unmapped')",releaseId)){
@@ -34,7 +34,9 @@ public class MappingIssueService {
}
private void add(Map<String,Group> groups,String type,String key,Map<String,Object> f,Map<String,Object> raw,String stage){Group group=groups.computeIfAbsent(type+"\n"+key,k->new Group(type,key));group.count++;if(f.get("totalWeight") instanceof Number n)group.weight=group.weight.add(new BigDecimal(n.toString()));group.documents.add(String.valueOf(f.get("sourceDocumentId")));group.revisions.add(String.valueOf(f.get("sourceRevisionId")));group.builds.add(String.valueOf(f.get("transformBuildId")));group.periods.add(String.valueOf(f.get("monthKey")));
boolean hasOriginal=false;for(String field:type.equals("CATEGORY")?List.of("产业部",stage.equals("black")?"黑片产业部":"亮片产业部"):List.of("项目组","项目组项目组")){Object value=raw.get(field);if(value instanceof String s&&!s.isBlank()){group.originals.add(s);hasOriginal=true;}}if(hasOriginal)group.originalValueRows++;}
public Map<String,Object> list(String runId,String status,int page,int size,AppUserPrincipal actor){if(runId!=null){String release=g.jdbc.queryForObject("SELECT result_release_id FROM import_run WHERE id=?",String.class,runId);if(release!=null){acceptance.requireRead(release,actor);acceptance.requireSourceDetail(actor);capture(runId,release);}}if(page<1||size<1||size>100)throw error(HttpStatus.BAD_REQUEST,"分页无效");List<Object> args=new ArrayList<>();StringBuilder sql=new StringBuilder("SELECT * FROM mapping_issue_group WHERE 1=1");if(runId!=null){sql.append(" AND import_run_id=?");args.add(runId);}if(status!=null){sql.append(" AND status=?");args.add(status);}sql.append(" ORDER BY created_at DESC,id");List<Map<String,Object>> allowed=new ArrayList<>();for(var row:g.rows(sql.toString(),args.toArray())){try{authorize(row,actor);allowed.add(row);}catch(org.springframework.web.server.ResponseStatusException e){if(e.getStatusCode().value()!=403)throw e;}}int offset=(int)Math.min(allowed.size(),(long)(page-1)*size);return Map.of("items",allowed.subList(offset,Math.min(allowed.size(),offset+size)),"total",allowed.size(),"page",page,"pageSize",size);}
public Map<String,Object> list(String runId,String status,int page,int size,AppUserPrincipal actor){return list(runId,status,page,size,null,null,null,actor);}
public Map<String,Object> list(String runId,String status,int page,int size,String customerCode,String businessGroup,String department,AppUserPrincipal actor){return list(runId,status,page,size,customerCode,businessGroup,department,null,actor);}
public Map<String,Object> list(String runId,String status,int page,int size,String customerCode,String businessGroup,String department,String mappingType,AppUserPrincipal actor){if(runId!=null){String release=g.jdbc.queryForObject("SELECT result_release_id FROM import_run WHERE id=?",String.class,runId);if(release!=null){acceptance.requireRead(release,actor);acceptance.requireSourceDetail(actor);capture(runId,release);}}if(page<1||size<1||size>100)throw error(HttpStatus.BAD_REQUEST,"分页无效");List<Object> args=new ArrayList<>();StringBuilder sql=new StringBuilder("SELECT * FROM mapping_issue_group WHERE 1=1");if(runId!=null){sql.append(" AND import_run_id=?");args.add(runId);}if(status!=null){sql.append(" AND status=?");args.add(status);}if(mappingType!=null){if(!Set.of("CATEGORY","PROJECT_GROUP","DEPARTMENT","PRODUCT").contains(mappingType))throw error(HttpStatus.BAD_REQUEST,"映射类型无效");sql.append(" AND mapping_type=?");args.add(mappingType);}sql.append(" ORDER BY created_at DESC,id");List<Map<String,Object>> allowed=new ArrayList<>();Map<String,Boolean> releaseAccess=new HashMap<>();for(var row:g.rows(sql.toString(),args.toArray())){String releaseId=Objects.toString(map(row.get("details")).get("releaseId"),"");try{if(!releaseAccess.containsKey(releaseId)){authorize(row,actor);releaseAccess.put(releaseId,true);}if(Boolean.TRUE.equals(releaseAccess.get(releaseId)))allowed.add(row);}catch(org.springframework.web.server.ResponseStatusException e){if(e.getStatusCode().value()!=403)throw e;releaseAccess.put(releaseId,false);}}List<Map<String,Object>> decorated=dimensions.decorate(allowed);List<Map<String,Object>> filtered=decorated.stream().filter(row->MappingIssueDimensions.matches(row,customerCode,businessGroup,department)).toList();int offset=(int)Math.min(filtered.size(),(long)(page-1)*size);return Map.of("items",filtered.subList(offset,Math.min(filtered.size(),offset+size)),"total",filtered.size(),"page",page,"pageSize",size,"filterOptions",MappingIssueDimensions.options(decorated));}
public Map<String,Object> detail(String id,AppUserPrincipal actor){Map<String,Object> issue=g.one("SELECT * FROM mapping_issue_group WHERE id=?",id);authorize(issue,actor);return Map.of("issue",issue,"decisions",g.rows("SELECT * FROM mapping_issue_decision WHERE issue_key=? ORDER BY created_at DESC",id),"buildAttempts",g.rows("SELECT a.* FROM mapping_issue_build_attempt a JOIN mapping_issue_decision d ON d.id=a.decision_id WHERE d.issue_key=? ORDER BY a.created_at DESC",id));}
@Transactional public Map<String,Object> resolve(String id,Resolution request,AppUserPrincipal actor){
Map<String,Object> issue=g.one("SELECT * FROM mapping_issue_group WHERE id=? FOR UPDATE",id);authorize(issue,actor);g.required(request.reason(),"裁决理由");
@@ -0,0 +1,53 @@
package cn.yunsheng.quality.governance;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import cn.yunsheng.quality.common.ImmutableQueryCache;
import java.util.*;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
/** Read-only inventory of blank business groups in the current published release. */
@Service
public class UnclassifiedSourceService {
private final GovernanceSupport g;
private final CandidateAcceptanceService access;
private final ImmutableQueryCache<String, List<Map<String, Object>>> inventories = new ImmutableQueryCache<>(12);
public UnclassifiedSourceService(GovernanceSupport g, CandidateAcceptanceService access) {
this.g = g;
this.access = access;
}
public Map<String, Object> list(int page, int size, AppUserPrincipal actor) {
if (page < 1 || size < 1 || size > 100)
throw GovernanceSupport.error(HttpStatus.BAD_REQUEST, "分页无效");
access.requireSourceDetail(actor);
List<Map<String, Object>> current = g.rows("SELECT release_id FROM current_release WHERE dataset_id='quality'");
if (current.isEmpty()) return Map.of("items", List.of(), "total", 0, "page", page, "pageSize", size);
String release = (String) current.getFirst().get("releaseId");
access.requireRead(release, actor);
List<Map<String, Object>> rows = inventories.get(release, () -> {
String sql = sourceSql("black") + " UNION ALL " + sourceSql("bright");
return g.rows("SELECT * FROM (" + sql + ") sources ORDER BY stage,department,product,source_key",
release, release).stream().map(row -> Collections.unmodifiableMap(new LinkedHashMap<>(row))).toList();
});
int offset = (int) Math.min(rows.size(), (long) (page - 1) * size);
return Map.of("items", rows.subList(offset, Math.min(rows.size(), offset + size)), "total", (long) rows.size(),
"page", page, "pageSize", size, "releaseId", release);
}
static String sourceSql(String stage) {
if (!Set.of("black", "bright").contains(stage)) throw new IllegalArgumentException("stage");
return """
SELECT '%s' AS stage, f.department, f.product,
COALESCE(JSON_VALUE(f.components,'$.sourcePdtCode'),'') AS pdt_code,
COALESCE(JSON_VALUE(f.components,'$.sourceSalesRegion'),'') AS sales_region,
CONCAT(COALESCE(JSON_VALUE(f.components,'$.sourcePdtCode'),''),'|',
COALESCE(JSON_VALUE(f.components,'$.sourceSalesRegion'),'')) AS source_key,
COUNT(*) AS row_count, SUM(f.total_weight) AS total_weight,
GROUP_CONCAT(DISTINCT f.month_key ORDER BY f.month_key SEPARATOR '、') AS periods
FROM release_member rm JOIN fact_%s f ON f.build_id=rm.transform_build_id
WHERE rm.release_id=? AND (f.group_name IS NULL OR f.group_name='')
GROUP BY f.department,f.product,pdt_code,sales_region,source_key
""".formatted(stage, stage);
}
}
@@ -60,7 +60,7 @@ public class MetricsController {
@RequestParam(required = false) String productSearch,
@RequestParam(required = false) String projectGroup,
@RequestParam(defaultValue = "overall") String view,
@RequestParam(defaultValue = "false") boolean includeUnassigned,
@RequestParam(defaultValue = "true") boolean includeUnassigned,
@RequestParam(required = false) String releaseId,
@RequestParam(defaultValue = "month") String trendGranularity,
@RequestParam(required = false) Integer rowLimit,
@@ -378,7 +378,9 @@ public class MetricsService {
if (view.equals("overall")) return List.of(aggregates.summary().toMetricRow("overall", "总体", null, null, null));
List<MetricRow> rows = new ArrayList<>();
aggregates.dimensions().forEach((key, stats) -> {
boolean unassigned = key.equals("__UNASSIGNED__");
boolean unassigned = key == null || key.isBlank() || key.equals("__UNASSIGNED__");
// Display filtering only: summary, coverage and trend retain every matching source row.
if (view.equals("group") && unassigned) return;
String label = unassigned ? "未映射/未填写" : key;
String department = view.equals("department") && !unassigned ? DepartmentLabels.display(key) : null;
String group = view.equals("group") && !unassigned ? key : null;
@@ -1,6 +1,7 @@
package cn.yunsheng.quality.records;
import cn.yunsheng.quality.common.DataScope;
import cn.yunsheng.quality.common.ImmutableQueryCache;
import cn.yunsheng.quality.filters.QueryFilterFields;
import cn.yunsheng.quality.common.DepartmentLabels;
import cn.yunsheng.quality.common.SelectionValues;
@@ -38,6 +39,12 @@ import org.springframework.web.server.ResponseStatusException;
@RestController
@RequestMapping("/api/records")
public class RecordController {
private record QueryKey(String sql, List<Object> args) {
QueryKey { args = List.copyOf(args); }
}
private static final int PAGE_WINDOW = 240;
private final ImmutableQueryCache<QueryKey, Long> totals = new ImmutableQueryCache<>(128);
private final ImmutableQueryCache<QueryKey, List<String>> pageIndexes = new ImmutableQueryCache<>(128);
private static final Set<String> COMPUTED_WEIGHT_FIELDS = Set.of(
"qualifiedWeight", "scrapWeight", "totalWeight", "qualified", "scrap", "total");
private final JdbcTemplate jdbc;
@@ -95,7 +102,7 @@ public class RecordController {
@RequestParam(required = false) String product,
@RequestParam(required = false) String productSearch,
@RequestParam(required = false) String projectGroup,
@RequestParam(defaultValue = "false") boolean includeUnassigned,
@RequestParam(defaultValue = "true") boolean includeUnassigned,
@RequestParam(required = false) String releaseId,
@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "50") int pageSize,
@@ -149,17 +156,39 @@ public class RecordController {
if(!sourceFilters.isEmpty())new cn.yunsheng.quality.filters.SourceColumnQueries(jdbc,governance,mapper).append(predicate,args,stage,sourceFilters,selectedRelease,actor);
DataScope.append(predicate, args, actor, "f.department COLLATE utf8mb4_0900_as_cs", "f.group_name COLLATE utf8mb4_0900_as_cs");
Long total = jdbc.queryForObject("""
String countSql = """
SELECT COUNT(*) FROM release_member rm
JOIN source_revision sr ON sr.id = rm.source_revision_id
JOIN %s f ON f.build_id = rm.transform_build_id
%s
""".formatted(fact, predicate), Long.class, args.toArray());
List<Object> pageArgs = new ArrayList<>(args);
String orderBy = orderBy(sort, sortDirection, selectedField, actor, pageArgs);
pageArgs.add(pageSize);
pageArgs.add((long) (page - 1) * pageSize);
List<Map<String, Object>> rows = jdbc.query("""
""".formatted(fact, predicate);
Long total = totals.get(new QueryKey(countSql, args), () -> {
Long count = jdbc.queryForObject(countSql, Long.class, args.toArray());
return count == null ? 0L : count;
});
List<Object> indexArgs = new ArrayList<>(args);
String orderBy = orderBy(sort, sortDirection, selectedField, actor, indexArgs) + ", f.id";
long offset = (long) (page - 1) * pageSize;
List<String> ids = new ArrayList<>();
long end = Math.min(total, offset + pageSize);
for (long start = offset; start < end;) {
long windowStart = start / PAGE_WINDOW * PAGE_WINDOW;
List<Object> windowArgs = new ArrayList<>(indexArgs);
windowArgs.add(PAGE_WINDOW); windowArgs.add(windowStart);
String indexSql = "SELECT f.id FROM release_member rm JOIN source_revision sr ON sr.id=rm.source_revision_id "
+ "JOIN " + fact + " f ON f.build_id=rm.transform_build_id " + predicate
+ " ORDER BY " + orderBy + " LIMIT ? OFFSET ?";
List<String> window = pageIndexes.get(new QueryKey(indexSql, windowArgs),
() -> List.copyOf(jdbc.queryForList(indexSql, String.class, windowArgs.toArray())));
int first = (int) (start - windowStart);
int last = (int) Math.min(window.size(), end - windowStart);
if (first >= last) break;
ids.addAll(window.subList(first, last)); start = windowStart + last;
}
// Field permissions remain current on every request; no authorized row JSON is cached.
Set<String> allowedFields = governance.allowedFields(actor);
List<Object> rowArgs = new ArrayList<>(args); rowArgs.addAll(ids);
List<Map<String, Object>> fetched = ids.isEmpty() ? List.of() : jdbc.query("""
SELECT f.id, sr.id AS source_revision_id, sr.source_file, f.sheet_name, f.physical_row_no, f.month_key,
f.department, f.group_name, f.project_group, f.product, f.qualified_weight, f.scrap_weight,
f.total_weight, f.raw_values, f.components
@@ -167,9 +196,12 @@ public class RecordController {
JOIN source_revision sr ON sr.id = rm.source_revision_id
JOIN %s f ON f.build_id = rm.transform_build_id
%s
ORDER BY %s
LIMIT ? OFFSET ?
""".formatted(fact, predicate, orderBy), (rs, row) -> mapRow(rs, stage, actor, selectedColumns), pageArgs.toArray());
AND f.id IN (%s)
""".formatted(fact, predicate, String.join(",", java.util.Collections.nCopies(ids.size(), "?"))),
(rs, row) -> mapRow(rs, stage, allowedFields, selectedColumns), rowArgs.toArray());
Map<String, Map<String, Object>> byId = new LinkedHashMap<>();
fetched.forEach(row -> byId.put(String.valueOf(row.get("id")), row));
List<Map<String, Object>> rows = ids.stream().map(byId::get).filter(java.util.Objects::nonNull).toList();
return Map.of("rows", rows, "total", total == null ? 0 : total, "page", page,
"pageSize", pageSize, "releaseId", selectedRelease);
}
@@ -187,7 +219,7 @@ public class RecordController {
@RequestParam(required = false) String product,
@RequestParam(required = false) String productSearch,
@RequestParam(required = false) String projectGroup,
@RequestParam(defaultValue = "false") boolean includeUnassigned,
@RequestParam(defaultValue = "true") boolean includeUnassigned,
@RequestParam(required = false) String releaseId,
@RequestParam(required=false) String extraFilters,
@RequestParam(required=false) String stage,
@@ -232,7 +264,7 @@ public class RecordController {
if (month != null && quarter != null) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "月份与季度筛选不可同时指定");
}
private Map<String, Object> mapRow(ResultSet rs, String stage, AppUserPrincipal actor, List<String> selectedColumns) throws SQLException {
private Map<String, Object> mapRow(ResultSet rs, String stage, Set<String> allowedFields, List<String> selectedColumns) throws SQLException {
Map<String, Object> row = new LinkedHashMap<>();
String sourceFile = rs.getString("source_file");
String sheet = rs.getString("sheet_name");
@@ -248,13 +280,13 @@ public class RecordController {
row.put("projectGroup", rs.getString("project_group"));
row.put("group", rs.getString("group_name"));
row.put("product", rs.getString("product"));
Map<String, Object> visibleRaw = governance.filterFields(actor, readJson(rs.getString("raw_values")));
Map<String, Object> visibleRaw = filterFields(readJson(rs.getString("raw_values")), allowedFields);
if (selectedColumns != null) {
Map<String, Object> selectedRaw = new LinkedHashMap<>();
for (String column : selectedColumns) if (visibleRaw.containsKey(column)) selectedRaw.put(column, visibleRaw.get(column));
visibleRaw = selectedRaw;
}
Map<String, Object> visibleComponents = governance.filterFields(actor, readJson(rs.getString("components")));
Map<String, Object> visibleComponents = filterFields(readJson(rs.getString("components")), allowedFields);
row.put("raw", visibleRaw);
row.put("components", visibleComponents);
Map<String, Object> normalized = new LinkedHashMap<>();
@@ -266,13 +298,18 @@ public class RecordController {
normalized.put("qualifiedWeight", rs.getBigDecimal("qualified_weight"));
normalized.put("scrapWeight", rs.getBigDecimal("scrap_weight"));
normalized.put("totalWeight", rs.getBigDecimal("total_weight"));
Set<String> allowedFields = governance.allowedFields(actor);
row.put("normalized", restrictComputedWeightFields(normalized, allowedFields));
hideLegacyTopLevelWeightAliases(row, allowedFields);
row.put("transformNotes", transformNotes(stage, visibleRaw, visibleComponents));
return row;
}
private static Map<String, Object> filterFields(Map<String, Object> fields, Set<String> allowed) {
Map<String, Object> visible = new LinkedHashMap<>(fields);
if (!allowed.contains("*")) visible.keySet().retainAll(allowed);
return visible;
}
static Map<String, Object> restrictComputedWeightFields(Map<String, Object> values, Set<String> allowedFields) {
Map<String, Object> visible = new LinkedHashMap<>(values);
if (allowedFields.contains("*")) return visible;
@@ -0,0 +1,26 @@
package cn.yunsheng.quality.common;
import static org.junit.jupiter.api.Assertions.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
class ImmutableQueryCacheTest {
@Test void sharesOneConcurrentLoadAndEvictsLeastRecentlyUsedEntries() throws Exception {
var cache=new ImmutableQueryCache<String,Integer>(2);var calls=new AtomicInteger();
var entered=new CountDownLatch(1);var proceed=new CountDownLatch(1);
try(var pool=Executors.newFixedThreadPool(2)) {
var first=pool.submit(()->cache.get("a",()->{calls.incrementAndGet();entered.countDown();try{proceed.await();}catch(InterruptedException e){throw new IllegalStateException(e);}return 1;}));
assertTrue(entered.await(2,TimeUnit.SECONDS));
var second=pool.submit(()->cache.get("a",()->{calls.incrementAndGet();return 2;}));
proceed.countDown();assertEquals(1,first.get());assertEquals(1,second.get());assertEquals(1,calls.get());
}
cache.get("b",()->2);cache.get("a",()->99);cache.get("c",()->3);
assertEquals(1,cache.get("a",()->99));assertEquals(4,cache.get("b",()->4));
}
@Test void failedLoadIsNeverRetained() {
var cache=new ImmutableQueryCache<String,Integer>(1);
assertThrows(IllegalStateException.class,()->cache.get("a",()->{throw new IllegalStateException();}));
assertEquals(1,cache.get("a",()->1));
}
}
@@ -197,6 +197,27 @@ class SourceColumnQueriesTest {
return sourceField(SOURCE, "black", "委托单");
}
@Test void valuePagesReuseCountAndWindowButRevalidateFieldsAndSeparateSearches() {
JdbcTemplate jdbc=mock(JdbcTemplate.class);
AppUserPrincipal admin=new AppUserPrincipal(UUID.randomUUID(),"admin","","admin",true,false,
List.of("SYSTEM_ADMIN"),List.of(),List.of(),List.of());
SourceColumnQueries queries=spy(new SourceColumnQueries(jdbc,governance,JsonMapper.builder().build()));
doReturn(List.of(sourceField())).when(queries).scopedCatalog("release",admin);
when(jdbc.queryForObject(startsWith("SELECT COUNT(*)"),eq(Long.class),any(Object[].class))).thenReturn(40L);
List<Map<String,Object>> values=java.util.stream.IntStream.range(0,40)
.mapToObj(n->Map.<String,Object>of("value",String.format("%04d",n))).toList();
when(jdbc.query(startsWith("SELECT candidate_value"),any(org.springframework.jdbc.core.RowMapper.class),any(Object[].class))).thenReturn(values);
queries.values("release",Map.of(),SOURCE,List.of("black"),null,15,0,admin);
var second=queries.values("release",Map.of(),SOURCE,List.of("black"),null,15,15,admin);
assertEquals("0015",((List<Map<String,Object>>)second.get("rows")).getFirst().get("value"));
verify(jdbc,times(1)).queryForObject(startsWith("SELECT COUNT(*)"),eq(Long.class),any(Object[].class));
verify(jdbc,times(1)).query(startsWith("SELECT candidate_value"),any(org.springframework.jdbc.core.RowMapper.class),any(Object[].class));
queries.values("release",Map.of(),SOURCE,List.of("black"),"001",15,0,admin);
verify(jdbc,times(2)).queryForObject(startsWith("SELECT COUNT(*)"),eq(Long.class),any(Object[].class));
doReturn(List.of()).when(queries).scopedCatalog("release",admin);
assertThrows(ResponseStatusException.class,()->queries.values("release",Map.of(),SOURCE,List.of("black"),null,15,15,admin));
}
private Map<String, Object> sourceField(String key, String stage, String header) {
Map<String, Object> columns = new LinkedHashMap<>();
columns.put("black", stage.equals("black") ? header : null);
@@ -0,0 +1,49 @@
package cn.yunsheng.quality.governance;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import java.util.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import tools.jackson.databind.json.JsonMapper;
/** Opt-in disposable database; no application database or source file is modified. */
@EnabledIfEnvironmentVariable(named="QUALITY_SOURCE_COLUMN_TEST_CREATE_DATABASE",matches="true")
class ClassificationPreviewMySqlTest {
private JdbcTemplate admin,jdbc;private String database;private ClassificationPreviewService service;
private final AppUserPrincipal actor=mock(AppUserPrincipal.class);
@BeforeEach void create(){
String url=System.getenv("QUALITY_SOURCE_COLUMN_TEST_MYSQL_SERVER_URL");assertTrue(url.startsWith("jdbc:mysql://127.0.0.1:")&&url.contains("/?"));
String user=System.getenv("QUALITY_SOURCE_COLUMN_TEST_MYSQL_USER"),password=System.getenv("QUALITY_SOURCE_COLUMN_TEST_MYSQL_PASSWORD");
admin=new JdbcTemplate(new DriverManagerDataSource(url,user,password));database="quality_classification_preview_"+UUID.randomUUID().toString().replace("-","");admin.execute("CREATE DATABASE "+database+" CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci");
jdbc=new JdbcTemplate(new DriverManagerDataSource(url.replaceFirst("/\\?","/"+database+"?"),user,password));
jdbc.execute("CREATE TABLE current_release(dataset_id VARCHAR(20),release_id VARCHAR(20))");jdbc.update("INSERT INTO current_release VALUES('quality','r')");
jdbc.execute("CREATE TABLE release_member(release_id VARCHAR(20),transform_build_id VARCHAR(20))");jdbc.update("INSERT INTO release_member VALUES('r','b'),('r','l')");
for(String stage:List.of("black","bright"))jdbc.execute("CREATE TABLE fact_"+stage+"(build_id VARCHAR(20),department VARCHAR(40),product VARCHAR(40),group_name VARCHAR(40),month_key VARCHAR(7),total_weight DECIMAL(20,4),components JSON)");
seed("black","b","A","P","已有分类","2026-01","0001","X",10);
seed("black","b","A","Q","","2026-02","0002","X",20);
seed("black","b","B","P","其他","2026-01","0001","X",100);
seed("bright","l","A","R","G","2026-03",null,"X",30);
seed("bright","l","A","R","旧组","2026-04"," ","X",40);
seed("bright","l","A","R","小写区域","2026-04","0001","x",200);
service=new ClassificationPreviewService(new GovernanceSupport(jdbc,JsonMapper.builder().build()),mock(CandidateAcceptanceService.class));
}
@AfterEach void cleanup(){if(database!=null&&database.matches("quality_classification_preview_[a-f0-9]{32}"))admin.execute("DROP DATABASE "+database);}
private void seed(String stage,String build,String department,String product,String group,String month,String code,String region,int weight){
Map<String,Object> components=new HashMap<>();components.put("sourcePdtCode",code);components.put("sourceSalesRegion",region);
jdbc.update("INSERT INTO fact_"+stage+" VALUES(?,?,?,?,?,?,CAST(? AS JSON))",build,department,product,group,month,weight,JsonMapper.builder().build().writeValueAsString(components));
}
private ClassificationPreviewService.Condition equal(String field,String value){return new ClassificationPreviewService.Condition(field,"EQUALS",value);}
@Test void removingTwoConditionsBroadensAcrossMonthsStagesAndExistingGroupsWithExactCaseMatching(){
var exact=List.of(equal("@department","A"),equal("@product","P"),equal("PDT代码","0001"),equal("销售片区","X"));
assertEquals(1L,service.preview(new ClassificationPreviewService.Request("all","G",exact),actor).get("matchedRows"));
var broad=service.preview(new ClassificationPreviewService.Request("all","G",List.of(equal("@department","A"),equal("销售片区","X"))),actor);
assertEquals(4L,broad.get("matchedRows"));assertEquals(3L,broad.get("changedRows"));assertEquals(0,new java.math.BigDecimal("100").compareTo((java.math.BigDecimal)broad.get("totalWeight")));
assertEquals(2L,service.preview(new ClassificationPreviewService.Request("all","G",List.of(new ClassificationPreviewService.Condition("PDT代码","IS_BLANK",""))),actor).get("matchedRows"));
assertEquals(0L,service.preview(new ClassificationPreviewService.Request("all","G",List.of(equal("@department","A' OR 1=1 --"))),actor).get("matchedRows"));
assertEquals(3,jdbc.queryForObject("SELECT COUNT(*) FROM fact_black",Integer.class));
}
}
@@ -0,0 +1,35 @@
package cn.yunsheng.quality.governance;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import java.util.*;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.server.ResponseStatusException;
import tools.jackson.databind.json.JsonMapper;
class ClassificationPreviewTest {
@Test void removedConditionsBroadenAcrossBothStagesAndAllMonthsIncludingExistingGroupsWithoutWrites(){
var jdbc=mock(JdbcTemplate.class);var access=mock(CandidateAcceptanceService.class);var actor=mock(AppUserPrincipal.class);
when(jdbc.queryForList(startsWith("SELECT release_id"),any(Object[].class))).thenReturn(List.of(Map.of("release_id","r")));
when(jdbc.queryForList(startsWith("SELECT * FROM ("),any(Object[].class))).thenReturn(List.of(
Map.of("stage","black","month","2026-01","current_group","已有分类","row_count",10L,"total_weight",100),
Map.of("stage","bright","month","2026-09","current_group","G","row_count",5L,"total_weight",20)));
var service=new ClassificationPreviewService(new GovernanceSupport(jdbc,JsonMapper.builder().build()),access);
var request=new ClassificationPreviewService.Request("all","G",List.of(new ClassificationPreviewService.Condition("@department","EQUALS","A")));
var result=service.preview(request,actor);assertEquals(15L,result.get("matchedRows"));assertEquals(10L,result.get("changedRows"));
var sql=org.mockito.ArgumentCaptor.forClass(String.class);verify(jdbc).queryForList(sql.capture(),eq("r"),eq("A"),eq("r"),eq("A"));
assertTrue(sql.getValue().contains("fact_black"));assertTrue(sql.getValue().contains("fact_bright"));assertFalse(sql.getValue().contains("group_name=''"));assertFalse(sql.getValue().contains("sourcePdtCode"));assertFalse(sql.getValue().contains("f.month_key=?"));
service.preview(new ClassificationPreviewService.Request("all","其他",request.conditions()),actor);
verify(jdbc,times(1)).queryForList(startsWith("SELECT * FROM ("),any(Object[].class));verify(access,times(2)).requireRead("r",actor);
doThrow(new ResponseStatusException(org.springframework.http.HttpStatus.FORBIDDEN)).when(access).requireRead("r",actor);assertThrows(ResponseStatusException.class,()->service.preview(request,actor));
verify(jdbc,never()).update(anyString(),any(Object[].class));
}
@Test void rejectsEmptyDuplicateAndUnboundConditions(){
assertThrows(ResponseStatusException.class,()->ClassificationPreviewService.validate(new ClassificationPreviewService.Request("all","G",List.of())));
var condition=new ClassificationPreviewService.Condition("@department","EQUALS","A");
assertThrows(ResponseStatusException.class,()->ClassificationPreviewService.validate(new ClassificationPreviewService.Request("all","G",List.of(condition,condition))));
assertThrows(ResponseStatusException.class,()->ClassificationPreviewService.validate(new ClassificationPreviewService.Request("all","G",List.of(new ClassificationPreviewService.Condition("sql","EQUALS","A")))));
}
}
@@ -19,12 +19,6 @@ class GovernanceValidationTest {
assertFalse(MappingGovernanceValidator.applyRanges(Map.of("001","原组"),good,"CATEGORY",java.time.YearMonth.of(2026,5)).containsKey("001"));
assertEquals("原组",MappingGovernanceValidator.applyRanges(Map.of("001","原组"),good,"CATEGORY",java.time.YearMonth.of(2026,2)).get("001"));
}
@Test void costReadinessRequiresSourceFieldUnitsApprovalAndNonoverlappingPeriod(){
Map<String,Object> source=new LinkedHashMap<>(Map.of("sourceReference","library-cost-sheet","objectCode","0001","costField","材料成本","unit","元/kg","currency","CNY","approvalReference","APP-1","originalFormula","源工作表既有公式","fromMonth","2026-01","toMonth","2026-12"));source.put("baselineReference","原基准参数工作表/字段");
assertTrue(CostConfigurationController.validateDefinition(Map.of("sources",List.of(source))).isEmpty());
assertFalse(CostConfigurationController.validateDefinition(Map.of("sources",List.of(source,source))).isEmpty());
assertFalse(CostConfigurationController.validateDefinition(Map.of("sources",List.of(Map.of("objectCode","0001")))).isEmpty());
}
@Test void credentialManagementAcceptsReferencesAndRejectsSecretsAndTraversal(){
assertDoesNotThrow(()->GovernanceIdentityController.reference("env:QUALITY_MODEL_KEY","READY"));
assertDoesNotThrow(()->GovernanceIdentityController.reference("file:apikey.txt","READY"));
@@ -0,0 +1,57 @@
package cn.yunsheng.quality.governance;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import cn.yunsheng.quality.ingestion.ImportRunService;
import java.util.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.web.server.ResponseStatusException;
import tools.jackson.databind.json.JsonMapper;
class MappingIssueFilterTest {
@Test void filtersApplyBeforePaginationAndPreserveLeadingZeroCustomerCodes() {
JdbcTemplate jdbc=mock(JdbcTemplate.class);var access=mock(CandidateAcceptanceService.class);var actor=mock(AppUserPrincipal.class);
when(jdbc.queryForList(startsWith("SELECT * FROM mapping_issue_group"),any(Object[].class))).thenReturn(List.of(issue("1","001|G1","r"),issue("2","002|G2","r"),issue("3","001|G1","r")));
when(jdbc.queryForList(startsWith("SELECT DISTINCT f.department"),eq("r"),eq("r"))).thenReturn(List.of(
Map.of("department","产业部A","group_name","G1","customer_code","001"),
Map.of("department","产业部B","group_name","G2","customer_code","002")));
var service=service(jdbc,access);
var result=service.list(null,"OPEN",2,1,"001","G1","产业部A",actor);
assertEquals(2,result.get("total"));
verify(access,times(1)).requireRead("r",actor);
verify(access,times(1)).requireSourceDetail(actor);
var items=(List<Map<String,Object>>)result.get("items");assertEquals("3",items.getFirst().get("id"));
assertEquals("001",items.getFirst().get("customerCode"));assertEquals("G1",items.getFirst().get("businessGroup"));
assertEquals(List.of("产业部A"),items.getFirst().get("departments"));
assertEquals(0,service.list(null,"OPEN",1,20,"001",null,"产业部B",actor).get("total"));
verify(jdbc,times(1)).queryForList(startsWith("SELECT DISTINCT f.department"),eq("r"),eq("r"));
when(jdbc.queryForList(startsWith("SELECT * FROM mapping_issue_group"),any(Object[].class)))
.thenReturn(List.of(issue("1","001|G1","r")));
assertEquals(1,service.list(null,"OPEN",1,20,"001",null,null,actor).get("total"));
doThrow(new ResponseStatusException(org.springframework.http.HttpStatus.FORBIDDEN)).when(access).requireRead("r",actor);
var denied=service.list(null,"OPEN",1,20,null,null,null,actor);
assertEquals(0,denied.get("total"));
assertEquals(Set.of(),((Map<?,?>)denied.get("filterOptions")).get("customerCodes"));
verify(jdbc,times(1)).queryForList(startsWith("SELECT DISTINCT f.department"),eq("r"),eq("r"));
verify(jdbc,never()).update(anyString(),any(Object[].class));
}
@Test void unauthorizedIssuesCannotLeakThroughFilterOptionsOrSourceLookup() {
JdbcTemplate jdbc=mock(JdbcTemplate.class);var access=mock(CandidateAcceptanceService.class);var actor=mock(AppUserPrincipal.class);
when(jdbc.queryForList(startsWith("SELECT * FROM mapping_issue_group"),any(Object[].class))).thenReturn(List.of(issue("private","secret|G","denied")));
doThrow(new ResponseStatusException(org.springframework.http.HttpStatus.FORBIDDEN)).when(access).requireRead("denied",actor);
var result=service(jdbc,access).list(null,"OPEN",1,20,null,null,null,actor);
assertEquals(0,result.get("total"));
assertEquals(Set.of(),((Map<?,?>)result.get("filterOptions")).get("customerCodes"));
verify(jdbc,never()).queryForList(startsWith("SELECT DISTINCT f.department"),any(Object[].class));
}
private Map<String,Object> issue(String id,String key,String release) {
return Map.of("id",id,"mapping_type","PROJECT_GROUP","source_key",key,"details",Map.of("releaseId",release));
}
private MappingIssueService service(JdbcTemplate jdbc,CandidateAcceptanceService access) {
return new MappingIssueService(new GovernanceSupport(jdbc,JsonMapper.builder().build()),access,mock(ImportRunService.class),mock(ObjectProvider.class),mock(PlatformTransactionManager.class));
}
}
@@ -0,0 +1,42 @@
package cn.yunsheng.quality.governance;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import java.util.*;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.server.ResponseStatusException;
import tools.jackson.databind.json.JsonMapper;
class UnclassifiedSourceServiceTest {
@Test void paginationReusesImmutableInventoryButRechecksAccessAndPublishedRelease() {
JdbcTemplate jdbc=mock(JdbcTemplate.class);
var access=mock(CandidateAcceptanceService.class); var actor=mock(AppUserPrincipal.class);
when(jdbc.queryForList(contains("current_release"),any(Object[].class))).thenReturn(List.of(Map.of("release_id","release")));
when(jdbc.queryForList(startsWith("SELECT * FROM ("),any(Object[].class))).thenReturn(List.of(
Map.of("stage","black","source_key","A","row_count",2L),Map.of("stage","bright","source_key","B","row_count",3L)));
var service=new UnclassifiedSourceService(new GovernanceSupport(jdbc,JsonMapper.builder().build()),access);
var first=service.list(1,1,actor);var second=service.list(2,1,actor);
assertEquals(2L,first.get("total"));assertEquals(2L,second.get("total"));
assertEquals("B",((List<Map<String,Object>>)second.get("items")).getFirst().get("sourceKey"));
verify(jdbc,times(1)).queryForList(startsWith("SELECT * FROM ("),eq("release"),eq("release"));
verify(jdbc,never()).queryForObject(startsWith("SELECT COUNT(*)"),eq(Long.class),any(Object[].class));
verify(access,times(2)).requireRead("release",actor);
doThrow(new ResponseStatusException(org.springframework.http.HttpStatus.FORBIDDEN)).when(access).requireRead("release",actor);
assertThrows(ResponseStatusException.class,()->service.list(2,1,actor));
reset(access);
when(jdbc.queryForList(contains("current_release"),any(Object[].class))).thenReturn(List.of(Map.of("release_id","new-release")));
service.list(1,1,actor);
verify(jdbc).queryForList(startsWith("SELECT * FROM ("),eq("new-release"),eq("new-release"));
verify(jdbc,never()).update(anyString(),any(Object[].class));
}
@Test void deniedInventoryCannotReadFacts() {
JdbcTemplate jdbc=mock(JdbcTemplate.class);var access=mock(CandidateAcceptanceService.class);var actor=mock(AppUserPrincipal.class);
doThrow(new ResponseStatusException(org.springframework.http.HttpStatus.FORBIDDEN)).when(access).requireSourceDetail(actor);
assertThrows(ResponseStatusException.class,()->new UnclassifiedSourceService(new GovernanceSupport(jdbc,JsonMapper.builder().build()),access).list(1,50,actor));
verifyNoInteractions(jdbc);
assertTrue(UnclassifiedSourceService.sourceSql("black").contains("f.group_name IS NULL OR f.group_name=''"));
assertThrows(IllegalArgumentException.class,()->UnclassifiedSourceService.sourceSql("wrong"));
}
}
@@ -163,6 +163,48 @@ class ConfigurableMatchRulesTest {
rule("blank-no-value", 1, "all", "group", "空备注", List.of(blankWithoutValue))))).isEmpty());
}
@Test void maintenanceDraftClassifiesBlankKeyRowsAndFeedsProjectLookupWithoutChangingOtherScopes() throws Exception {
var json = JsonMapper.builder().build();
Map<String,Object> definition;
try (var stream = getClass().getResourceAsStream("/classification-maintenance.json")) {
definition = json.readValue(stream, Map.class);
}
var mapper = mapper((Map<String,String>) definition.get("categoryLookup"), (Map<String,String>) definition.get("projectGroupLookup"), definition);
var source = row("001", "产品A", "销售区A", "产业部A");
var values = new LinkedHashMap<>(source.values()); values.put("PDT代码", cell(""));
var raw = new LinkedHashMap<>(source.rawView()); raw.put("PDT代码", "");
var missing = new XlsxStreamingReader.RawRow(source.sheet(), source.physicalRow(), values, raw);
var classified = mapper.transform("black", missing, 2026, 3);
assertEquals("业务组A", classified.group());
assertEquals("项目组A", classified.components().get("projectGroup"));
assertFalse(classified.unmappedProjectGroup());
assertNull(mapper.transform("bright", missing, 2026, 3).group());
assertEquals("已有分组", mapper.transform("black", source, 2026, 3).group());
values.put("成品规格", cell("其他产品")); raw.put("成品规格", "其他产品");
assertNull(mapper.transform("black", new XlsxStreamingReader.RawRow(source.sheet(),source.physicalRow(),values,raw),2026,3).group());
}
@Test void frontendExcelRoundTripRulesApplyAcrossFilesMonthsAndStagesWithSpecificityAndLegacyDefaults() throws Exception {
Map<String,Object> definition;
try (var stream = getClass().getResourceAsStream("/classification-workbook-roundtrip.json")) {
definition = JsonMapper.builder().build().readValue(stream, Map.class);
}
assertTrue(ConfigurableMatchRules.validate(definition).isEmpty());
var mapper = mapper((Map<String,String>) definition.get("categoryLookup"), (Map<String,String>) definition.get("projectGroupLookup"), definition);
for (int month : List.of(1, 9)) {
var source = row("001", "其他产品", "销售区A", "产业部A");
for (String stage : List.of("black", "bright")) {
var result = mapper.transform(stage, source, 2026, month);
assertEquals("区域组", result.group()); // Replaces the existing lookup classification too.
assertEquals("区域项目", result.components().get("projectGroup"));
}
assertEquals("特定产品组", mapper.transform("black", row("001", "产品A", "销售区A", "产业部A"), 2026, month).group());
assertEquals("区域组", mapper.transform("bright", row("001", "产品A", "销售区A", "产业部A"), 2026, month).group());
}
assertEquals("工业电机-VCM", mapper.transform("black", row("001", "VCM-42", "其他销售区", "产业部A"), 2026, 3).group());
assertEquals("原查表分组", mapper.transform("black", row("001", "其他产品", "销售区A", "其他产业部"), 2026, 3).group());
}
private static BusinessRowMapper mapper(Map<String, String> groups, Map<String, String> projects, Map<String, Object> mappingDefinition) {
return new BusinessRowMapper(groups, projects, "test-mapping", Map.of(), Map.of(), ConfigurableRuleEngine.baseline(), mappingDefinition);
}
@@ -0,0 +1,55 @@
package cn.yunsheng.quality.metrics;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import cn.yunsheng.quality.governance.GovernanceAccessService;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import cn.yunsheng.quality.ingestion.BusinessRowMapper;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.util.*;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.*;
class MetricsUnclassifiedDisplayTest {
@Test void blankGroupsAreHiddenWithoutRemovingTheirWeightFromSummaryOrTrend() throws Exception {
JdbcTemplate jdbc = mock(JdbcTemplate.class);
MetricsService service = new MetricsService(jdbc, mock(GovernanceAccessService.class));
var actor = new AppUserPrincipal(UUID.randomUUID(),"admin","","Admin",true,false,List.of("SYSTEM_ADMIN"),List.of("*"),List.of("*"),List.of());
when(jdbc.query(anyString(), any(RowMapper.class), any(Object[].class))).thenAnswer(call -> {
RowMapper<?> mapper = call.getArgument(1);
ResultSet rs = mock(ResultSet.class);
when(rs.getString(1)).thenReturn("release"); when(rs.getString(2)).thenReturn("Published"); when(rs.getString(3)).thenReturn("PUBLISHED");
return List.of(mapper.mapRow(rs,0));
});
when(jdbc.queryForObject(contains("metric_rollup_build"),eq(Integer.class),any(Object[].class))).thenReturn(0);
when(jdbc.queryForObject(eq("SELECT rule_version FROM data_release WHERE id=?"),eq(String.class),eq("release"))).thenReturn(BusinessRowMapper.RULE_VERSION);
doAnswer(call -> {
String sql = call.getArgument(0);
RowCallbackHandler callback = call.getArgument(1);
assertFalse(sql.contains("AND (f.group_name IS NOT NULL AND f.group_name<>'')"));
if(sql.contains("AS bucket_key")) {
for(String key:List.of("G1","","__UNASSIGNED__")) {
ResultSet rs=mock(ResultSet.class);
when(rs.getString("trend_period")).thenReturn("2026-09"); when(rs.getString("bucket_key")).thenReturn(key);
when(rs.getLong("row_count")).thenReturn(1L);
when(rs.getBigDecimal("qualified_weight")).thenReturn(BigDecimal.valueOf(90));
when(rs.getBigDecimal("scrap_weight")).thenReturn(BigDecimal.TEN);
when(rs.getBigDecimal("total_weight")).thenReturn(BigDecimal.valueOf(100));
callback.processRow(rs);
}
} else if(sql.contains("SELECT f.month_key,")) {
ResultSet rs=mock(ResultSet.class); when(rs.getString("month_key")).thenReturn("2026-09");
when(rs.getLong("row_count")).thenReturn(3L); when(rs.getLong("excluded_blank_rows")).thenReturn(2L); callback.processRow(rs);
} else throw new AssertionError(sql);
return null;
}).when(jdbc).query(anyString(),any(RowCallbackHandler.class),any(Object[].class));
var query=new MetricsService.MetricsQuery(Integer.valueOf(2026),null,null,null,null,null,null,"group",true,"release");
var result=service.calculate(query,actor);
assertEquals(List.of("G1"),result.rows().stream().map(MetricsService.MetricRow::key).toList());
assertEquals(0,BigDecimal.valueOf(300).compareTo(result.summary().black().totalWeight()));
assertEquals(0,BigDecimal.valueOf(300).compareTo(result.trend().getFirst().black().totalWeight()));
assertEquals(6L,result.coverage().get("includedRows"));
assertEquals(4L,result.coverage().get("excludedBlankRows"));
}
}
@@ -0,0 +1,49 @@
package cn.yunsheng.quality.records;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import cn.yunsheng.quality.governance.GovernanceAccessService;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import cn.yunsheng.quality.release.ReleaseService;
import java.util.*;
import java.util.stream.IntStream;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.web.server.ResponseStatusException;
import tools.jackson.databind.json.JsonMapper;
class RecordPaginationTest {
@Test void adjacentPagesReuseCountAndNarrowIndexWhileAuthorizationRemainsLive() {
JdbcTemplate jdbc=mock(JdbcTemplate.class);var releases=mock(ReleaseService.class);
var access=mock(GovernanceAccessService.class);var actor=mock(AppUserPrincipal.class);
when(actor.getRoleCodes()).thenReturn(List.of("SYSTEM_ADMIN"));when(access.allowedFields(actor)).thenReturn(Set.of("*"));
when(releases.resolvePublishedRelease(null)).thenReturn("release");
when(jdbc.queryForObject(startsWith("SELECT COUNT(*)"),eq(Long.class),any(Object[].class))).thenReturn(40L);
List<String> ids=IntStream.range(0,40).mapToObj(n->"id-"+n).toList();
when(jdbc.queryForList(startsWith("SELECT f.id"),eq(String.class),any(Object[].class))).thenReturn(ids);
when(jdbc.query(startsWith("SELECT f.id, sr.id"),any(RowMapper.class),any(Object[].class))).thenAnswer(call->{
Object[] args=java.util.Arrays.copyOfRange(call.getArguments(),2,call.getArguments().length);List<Map<String,Object>> rows=new ArrayList<>();
for(Object arg:args)if(arg instanceof String id&&id.startsWith("id-"))rows.add(Map.of("id",id));
Collections.reverse(rows);return rows;
});
var controller=new RecordController(jdbc,releases,JsonMapper.builder().build(),access,mock(RecordsWorkbookExportService.class));
var first=page(controller,actor,1,null);var second=page(controller,actor,2,null);
assertEquals(40L,second.get("total"));
assertEquals("id-0",((List<Map<String,Object>>)first.get("rows")).getFirst().get("id"));
assertEquals("id-15",((List<Map<String,Object>>)second.get("rows")).getFirst().get("id"));
verify(jdbc,times(1)).queryForObject(startsWith("SELECT COUNT(*)"),eq(Long.class),any(Object[].class));
verify(jdbc,times(1)).queryForList(startsWith("SELECT f.id"),eq(String.class),any(Object[].class));
verify(access,times(2)).requireDetail(actor);
page(controller,actor,2,"规格A");
verify(jdbc,times(2)).queryForObject(startsWith("SELECT COUNT(*)"),eq(Long.class),any(Object[].class));
when(releases.resolvePublishedRelease(null)).thenReturn("new-release");page(controller,actor,1,null);
verify(jdbc,times(3)).queryForObject(startsWith("SELECT COUNT(*)"),eq(Long.class),any(Object[].class));
doThrow(new ResponseStatusException(org.springframework.http.HttpStatus.FORBIDDEN)).when(access).requireDetail(actor);
assertThrows(ResponseStatusException.class,()->page(controller,actor,1,null));
}
private Map<String,Object> page(RecordController controller,AppUserPrincipal actor,int page,String product) {
return controller.records("black",2026,null,null,null,null,product,null,true,null,page,15,null,null,null,"asc",null,actor);
}
}
@@ -0,0 +1,46 @@
{
"categoryLookup": {
"P1|销售区A": "已有分组"
},
"projectGroupLookup": {
"001|业务组A": "项目组A"
},
"matchRules": [
{
"id": "classification-c94c311d",
"name": "业务分组未分类维护",
"enabled": true,
"priority": -10000,
"stage": "black",
"conditions": [
{
"field": "@department",
"operator": "EQUALS",
"value": "产业部A"
},
{
"field": "@product",
"operator": "EQUALS",
"value": "产品A"
},
{
"field": "PDT代码",
"operator": "IS_BLANK",
"value": ""
},
{
"field": "销售片区",
"operator": "EQUALS",
"value": "销售区A"
},
{
"field": "@group",
"operator": "IS_BLANK",
"value": ""
}
],
"target": "group",
"value": "业务组A"
}
]
}
@@ -0,0 +1,78 @@
{
"categoryLookup": {
"P1|销售区A": "原查表分组"
},
"projectGroupLookup": {
"001|区域组": "区域项目"
},
"matchRules": [
{
"id": "classification-782ed979",
"name": "特定产品组分类规则",
"priority": -10000,
"stage": "black",
"target": "group",
"value": "特定产品组",
"enabled": true,
"conditions": [
{
"field": "@department",
"operator": "EQUALS",
"value": "产业部A"
},
{
"field": "@product",
"operator": "EQUALS",
"value": "产品A"
},
{
"field": "PDT代码",
"operator": "EQUALS",
"value": "P1"
},
{
"field": "销售片区",
"operator": "EQUALS",
"value": "销售区A"
}
]
},
{
"id": "classification-94305728",
"name": "区域组分类规则",
"priority": -9790,
"stage": "all",
"target": "group",
"value": "区域组",
"enabled": true,
"conditions": [
{
"field": "@department",
"operator": "EQUALS",
"value": "产业部A"
},
{
"field": "销售片区",
"operator": "EQUALS",
"value": "销售区A"
}
]
},
{
"id": "default-vcm-group",
"name": "VCM默认",
"priority": 100,
"stage": "all",
"target": "group",
"value": "工业电机-VCM",
"enabled": true,
"conditions": [
{
"field": "成品规格",
"operator": "CONTAINS",
"value": "VCM"
}
]
}
]
}
+2 -2
View File
@@ -24,7 +24,7 @@ export function AnalysisMemberListPanel({ turn, value, onUnauthorized }: {
const turnRef = useRef(turn);
turnRef.current = turn;
const requestId = useRef(0);
const [view, setView] = useState<ViewState>({ status: 'checking', page: 1, pageSize: 50, error: '' });
const [view, setView] = useState<ViewState>({ status: 'checking', page: 1, pageSize: 15, error: '' });
const verify = useCallback(async (page: number, pageSize: number) => {
const currentRequest = ++requestId.current;
@@ -87,7 +87,7 @@ export function AnalysisMemberListPanel({ turn, value, onUnauthorized }: {
</div>
<div className="analysis-member-list-controls">
<span>显示 {page.offset + 1}–{page.offset + rows.length} / {parsed.rowsReturned} 条已读取记录</span>
<Select aria-label="每页显示数量" value={view.pageSize} options={[20, 50, 100].map(size => ({ label: `${size} 条/页`, value: size }))}
<Select aria-label="每页显示数量" value={view.pageSize} options={[15, 30, 50, 100].map(size => ({ label: `${size} 条/页`, value: size }))}
onChange={size => void verify(1, size)} />
{page.pageCount > 1 && <Pagination current={page.page} pageSize={page.pageSize} total={parsed.rowsReturned} showSizeChanger={false} showQuickJumper={page.pageCount > 10}
onChange={nextPage => { if (nextPage !== view.page) void verify(nextPage, view.pageSize); }} />}
+3 -3
View File
@@ -68,7 +68,7 @@ export function AnalysisRecordListPanel({ turn, value, onUnauthorized }: {
identityRef.current = { turnId, snapshot };
const requestId = useRef(0);
const [selectedSetId, setSelectedSetId] = useState('');
const [view, setView] = useState<ViewState>({ status: 'checking', turnId: '', snapshot: '', setId: '', page: 1, pageSize: 50, error: '' });
const [view, setView] = useState<ViewState>({ status: 'checking', turnId: '', snapshot: '', setId: '', page: 1, pageSize: 15, error: '' });
const selectedSet = parsed?.sets.find(set => set.id === selectedSetId) || parsed?.sets[0];
const activeSetId = selectedSet?.id || '';
@@ -104,7 +104,7 @@ export function AnalysisRecordListPanel({ turn, value, onUnauthorized }: {
? page.rows.map((row, index) => ({ ...row, index: page.offset + index }))
: [];
const viewMatches = view.turnId === turnId && view.snapshot === snapshot && view.setId === activeSetId
&& view.page === (page?.page || 1) && view.pageSize === (page?.pageSize || 50);
&& view.page === (page?.page || 1) && view.pageSize === (page?.pageSize || 15);
const viewStatus = viewMatches ? view.status : 'checking';
const fields = selectedSet?.fields || [];
const columns: ColumnsType<VisibleRow> = [
@@ -145,7 +145,7 @@ export function AnalysisRecordListPanel({ turn, value, onUnauthorized }: {
</div>
<div className="analysis-member-list-controls">
<span>显示 {page.offset + 1}–{page.offset + rows.length} / {selectedSet.loadedRows} 条已读取记录</span>
<Select aria-label="每页显示数量" value={viewMatches ? view.pageSize : 50} options={[20, 50, 100].map(size => ({ label: `${size} 条/页`, value: size }))}
<Select aria-label="每页显示数量" value={viewMatches ? view.pageSize : 15} options={[15, 30, 50, 100].map(size => ({ label: `${size} 条/页`, value: size }))}
onChange={size => void verify(activeSetId, 1, size)} />
{page.pageCount > 1 && <Pagination current={page.page} pageSize={page.pageSize} total={selectedSet.loadedRows} showSizeChanger={false} showQuickJumper={page.pageCount > 10}
onChange={nextPage => { if (nextPage !== view.page) void verify(activeSetId, nextPage, view.pageSize); }} />}
+1 -1
View File
@@ -12,7 +12,7 @@ import { readableSelectPopup } from './readableSelectPopup';
import './completion.css';
type Row = Record<string, unknown>;
const TABLE_PAGE_SIZE = 20;
const TABLE_PAGE_SIZE = 15;
function text(value: unknown, empty = '—'): string {
if (value === null || value === undefined || value === '') return empty;
+1 -1
View File
@@ -9,7 +9,7 @@ const QueryPage = lazy(() => import('./QueryPage'));
const StageProductPage = lazy(() => import('./StageProductPage'));
const ManagementPages = lazy(() => import('./ManagementPages'));
const QUERY_PAGES: PageKey[] = ['overall', 'department', 'group', 'product', 'blackProduct', 'brightProduct'];
const retained = (page: PageKey) => QUERY_PAGES.includes(page) || page === 'analysis';
const retained = (_page: PageKey) => true;
type ResultRef = { kind: 'TOP'; resultId: string; releaseId: string };
type Entry = { initialSearch: string; search: string; key: number; scroll: number };
export type WorkspaceNavigation = { navigate: (page: PageKey, query?: QueryParams, resultRef?: ResultRef) => void; syncLocation: () => void };
+69
View File
@@ -0,0 +1,69 @@
import { useContext, useRef, useState, type ReactNode } from 'react';
import { Alert, App, AutoComplete, Button, Input, Modal, Select, Space, Tag } from 'antd';
import { DeleteOutlined, PlusOutlined } from '@ant-design/icons';
import { QualityTable as Table } from './QualityTable';
import { api } from './api';
import { MappingEditorContext } from './MappingEditorContext';
import { classificationConditions, editClassification, type ClassificationCondition, type ClassificationSelection } from './classificationMaintenance';
const labels: Record<string,string> = {'@department':'产业部','@product':'产品','PDT代码':'PDT代码','销售片区':'销售区域'};
export function ClassificationRuleEditor({ children, onUnauthorized }: {
children: (edit: (selection: ClassificationSelection)=>void)=>ReactNode;
onUnauthorized: (error: unknown)=>boolean;
}) {
const context=useContext(MappingEditorContext);
const {message}=App.useApp();
const [selection,setSelection]=useState<ClassificationSelection|null>(null);
const [conditions,setConditions]=useState<ClassificationCondition[]>([]);
const [stage,setStage]=useState('all');const [target,setTarget]=useState('');const [name,setName]=useState('');
const [preview,setPreview]=useState<Record<string,unknown>|null>(null);
const project=selection?.kind==='projectGroup';
const [busy,setBusy]=useState(false);const [error,setError]=useState('');const revision=useRef(0);
const resetPreview=()=>{revision.current++;setPreview(null);setError('');};
const open=(next:ClassificationSelection)=>{
setSelection(next);setConditions(classificationConditions(next.row));setStage('all');setTarget(next.kind==='projectGroup'?String((context?.definition.projectGroupLookup as Record<string,unknown>|undefined)?.[String(next.row.sourceKey)]??''):'');setName('');resetPreview();
};
const validate=()=>{
if(!context?.ready||!selection)throw new Error('请等待当前配置读取完成');
return editClassification(context.definition,selection,target,context.defaultRules,{conditions,stage,name});
};
const inspect=async()=>{
const generation=++revision.current;setError('');setPreview(null);
try{
validate();setBusy(true);
const result=await api.previewClassification({stage,target:target.trim(),conditions});
if(generation===revision.current)setPreview(result);
}catch(value){if(generation===revision.current&&!onUnauthorized(value))setError(value instanceof Error?value.message:'影响预览失败');}
finally{if(generation===revision.current)setBusy(false);}
};
const save=()=>{
try{if(!project&&!preview)throw new Error('请先预览当前条件的影响范围');const definition=validate();context!.update(definition);setSelection(null);message.success(project?'对应关系已加入当前编辑,保存并更新报表后生效':'规则已加入条件匹配,保存并更新报表后统一生效');}
catch(value){setError(value instanceof Error?value.message:'规则保存失败');}
};
return <>{children(open)}<Modal className="management-modal classification-rule-modal" width={880} open={!!selection}
title={project?'编辑项目组分类':'编辑业务分组分类'} okText={project?'保存到基础对应':'保存到条件匹配'} okButtonProps={{disabled:busy||(!project&&!preview)||!context?.ready}}
onOk={save} onCancel={()=>{revision.current++;setBusy(false);setSelection(null);}}>
{project?<p>客户代码 {String(selection?.row.customerCode)} · 业务分组 {String(selection?.row.businessGroup)}</p>:<><div className="classification-rule-fields">
<label>规则名称<Input aria-label="分类规则名称" value={name} maxLength={160} placeholder="默认使用业务分组名称" onChange={e=>{setName(e.target.value);}}/></label>
<label>适用范围<Select aria-label="分类适用范围" value={stage} options={[{value:'all',label:'黑片和亮片'},{value:'black',label:'黑片'},{value:'bright',label:'亮片'}]} onChange={value=>{setStage(value);resetPreview();setBusy(false);}}/></label>
</div>
<div className="classification-condition-heading"><strong>匹配条件</strong><Select aria-label="添加分类条件" placeholder="添加条件" value={undefined} style={{width:150}}
options={Object.entries(labels).filter(([key])=>!conditions.some(c=>c.field===key)).map(([value,label])=>({value,label}))}
disabled={conditions.length===4} suffixIcon={<PlusOutlined/>} onChange={field=>{setConditions([...conditions,classificationConditions(selection!.row).find(c=>c.field===field)!]);resetPreview();setBusy(false);}}/></div>
<div className="classification-conditions">{conditions.map(condition=><div className="classification-condition-row" key={condition.field}>
<span>{labels[condition.field]}</span><Select aria-label={`${labels[condition.field]}匹配方式`} value={condition.operator} style={{width:110}}
options={[{value:'EQUALS',label:'等于'},{value:'IS_BLANK',label:'为空'}]} onChange={operator=>{setConditions(conditions.map(c=>c===condition?{...c,operator,value:operator==='IS_BLANK'?'':c.value}:c));resetPreview();setBusy(false);}}/>
<Input aria-label={`${labels[condition.field]}匹配值`} disabled={condition.operator==='IS_BLANK'} value={condition.value} maxLength={200}
placeholder={condition.operator==='IS_BLANK'?'空值':'填写匹配值'} onChange={e=>{setConditions(conditions.map(c=>c===condition?{...c,value:e.target.value}:c));resetPreview();setBusy(false);}}/>
<Button aria-label={`移除${labels[condition.field]}条件`} icon={<DeleteOutlined/>} disabled={conditions.length===1} onClick={()=>{setConditions(conditions.filter(c=>c!==condition));resetPreview();setBusy(false);}}>移除</Button>
</div>)}</div></>}
<label className="classification-target">{project?'项目组':'业务分组'}<AutoComplete aria-label={project?'目标项目组':'目标业务分组'} value={target} maxLength={160} options={(project?context?.projectGroups??[]:context?.groups??[]).map(value=>({value}))}
filterOption={(input,option)=>String(option?.value).includes(input)} placeholder={project?'选择项目组':'选择业务分组'} onChange={value=>{setTarget(value);resetPreview();setBusy(false);}}/></label>
{!project&&<div className="classification-preview-action"><span>保留条件同时满足时匹配,覆盖全部月份和来源文件。</span><Button loading={busy} onClick={()=>void inspect()}>预览影响</Button></div>}
{error&&<Alert type="error" showIcon message={error}/>}
{preview&&<div className="classification-impact"><Space wrap><Tag color="blue">命中 {Number(preview.matchedRows).toLocaleString()} 行</Tag><Tag>总重量 {Number(preview.totalWeight).toLocaleString()} kg</Tag><Tag color={Number(preview.changedRows)>0?'orange':'default'}>当前归属不同 {Number(preview.changedRows).toLocaleString()} 行</Tag></Space>
<Table size="small" bordered scroll={{x:720}} pagination={{pageSize:15,showSizeChanger:false}} rowKey={row=>JSON.stringify([row.stage,row.department,row.month,row.currentGroup])} dataSource={(preview.items??[]) as Record<string,unknown>[]}
columns={[{title:'阶段',dataIndex:'stage',render:value=>value==='black'?'黑片':'亮片'},{title:'产业部',dataIndex:'department'},{title:'月份',dataIndex:'month'},{title:'当前业务分组',dataIndex:'currentGroup',render:value=>value||'未分类'},{title:'行数',dataIndex:'rowCount',align:'right'},{title:'目标业务分组',render:()=>target}]}/>
</div>}
</Modal></>;
}
+44
View File
@@ -0,0 +1,44 @@
import { useMemo, useRef, useState } from 'react';
import { Alert, App, Button, Modal, Radio, Select, Space, Tag } from 'antd';
import { DownloadOutlined, UploadOutlined } from '@ant-design/icons';
import { QualityTable as Table } from './QualityTable';
import { downloadConfigurationWorkbook, importConfigurationTables, readConfigurationWorkbook, type ConfigurationScope, type ConfigurationTable, type Definition, type ImportMode } from './configurationWorkbook';
export function ConfigurationWorkbookActions({value,onChange,scope,label,defaults=[],readOnly=false,validate,onPaste,whole=false,disabled=false}: {
value:Definition;onChange:(value:Definition)=>void;scope:ConfigurationScope;label:string;defaults?:unknown[];readOnly?:boolean;
validate?:(value:Definition)=>string[];onPaste?:()=>void;whole?:boolean;disabled?:boolean;
}) {
const {message}=App.useApp();const input=useRef<HTMLInputElement>(null);
const [open,setOpen]=useState(false);const [busy,setBusy]=useState(false);const [error,setError]=useState('');
const [tables,setTables]=useState<ConfigurationTable[]|null>(null);const [mode,setMode]=useState<ImportMode>('MERGE');const [selected,setSelected]=useState('');
const evaluated=useMemo(()=>{
if(!tables)return null;
try{const result=importConfigurationTables(value,tables,scope,mode,defaults);const errors=validate?.(result.definition)??[];if(errors.length)throw new Error(errors.slice(0,8).join(';'));return{result,error:''};}
catch(value){return{result:null,error:value instanceof Error?value.message:'导入文件无效'};}
},[tables,value,scope,mode,defaults,validate]);
const choose=async(file?:File)=>{
if(!file)return;setBusy(true);setError('');setTables(null);
try{if(!/\.xlsx$/i.test(file.name))throw new Error('请选择.xlsx格式的Excel文件');const loaded=await readConfigurationWorkbook(await file.arrayBuffer());setTables(loaded);setSelected(loaded[0]?.name??'');}
catch(value){setError(value instanceof Error?value.message:'Excel读取失败');}finally{setBusy(false);if(input.current)input.current.value='';}
};
const table=tables?.find(table=>table.name===selected);
const apply=()=>{if(!evaluated?.result)return;onChange(evaluated.result.definition);setOpen(false);message.success('已导入当前编辑,保存配置后生效');};
return <>
<Button icon={<DownloadOutlined/>} disabled={disabled} loading={busy&&!open} onClick={()=>{setBusy(true);void downloadConfigurationWorkbook(label,value,scope,defaults).catch(error=>message.error(error instanceof Error?error.message:'导出失败')).finally(()=>setBusy(false));}}>{whole?'导出整本Excel':'导出Excel'}</Button>
{!readOnly&&<Button icon={<UploadOutlined/>} disabled={disabled} onClick={()=>{setTables(null);setMode('MERGE');setError('');setOpen(true);}}>{whole?'导入整本Excel':'导入Excel'}</Button>}
<Modal className="management-modal configuration-import-modal" width={1000} title={`导入${label}`} open={open} onCancel={()=>setOpen(false)} onOk={apply}
okText="应用到当前编辑" okButtonProps={{disabled:busy||!evaluated?.result}} footer={(_, {OkBtn,CancelBtn})=><>{onPaste&&<Button onClick={()=>{setOpen(false);onPaste();}}>粘贴表格</Button>}<CancelBtn/><OkBtn/></>}>
<div className="configuration-import-toolbar"><input ref={input} className="visually-hidden-file" type="file" accept=".xlsx" aria-label={`选择${label}Excel文件`} onChange={event=>void choose(event.target.files?.[0])}/><Button icon={<UploadOutlined/>} loading={busy} onClick={()=>input.current?.click()}>选择Excel文件</Button>
<Radio.Group aria-label="配置导入方式" value={mode} onChange={event=>setMode(event.target.value)} options={[{value:'MERGE',label:'合并更新'},{value:'REPLACE',label:'替换文件中的表'}]}/></div>
<p className="structured-config-note">合并按编号或源条件更新;替换只影响文件包含的工作表。导入后由页面保存按钮生效。</p>
{(error||evaluated?.error)&&<Alert showIcon type="error" message="尚未写入" description={error||evaluated?.error}/>}
{evaluated?.result&&<Table size="small" bordered rowKey="name" pagination={false} dataSource={evaluated.result.changes} columns={[
{title:'配置表',dataIndex:'name'},{title:'表格行数',dataIndex:'rows',align:'right'},{title:'新增',dataIndex:'added',align:'right'},{title:'更新',dataIndex:'updated',align:'right'},
{title:'移除',dataIndex:'removed',align:'right',render:value=><Tag color={value?'orange':'default'}>{value}</Tag>},
]}/>}
{table&&<div className="configuration-table-preview"><Space><span>工作表预览</span><Select aria-label="预览配置工作表" style={{width:200}} value={selected} onChange={setSelected} options={tables?.map(table=>({value:table.name,label:table.name}))}/></Space>
<Table<{index:number;cells:string[]}> size="small" bordered rowKey="index" scroll={{x:Math.max(760,table.headers.length*150)}} pagination={{pageSize:15,showSizeChanger:false}} dataSource={table.rows.map((cells,index)=>({index,cells}))}
columns={table.headers.map((header,index)=>({title:header,key:String(index),width:150,render:(_,row)=>row.cells[index]}))}/></div>}
</Modal>
</>;
}
+3 -2
View File
@@ -1,3 +1,4 @@
import { ConfigurationWorkbookActions } from './ConfigurationWorkbookActions';
import { useEffect, useState } from 'react';
import { Alert, App, Button, Checkbox, Input, Select, Space } from 'antd';
import { QualityTable as Table } from './QualityTable';
@@ -29,10 +30,10 @@ export function FilterFieldSettings({ canManage, onUnauthorized }: { canManage:
finally { setSaving(false); }
};
return <section className="panel filter-field-settings">
<div className="section-heading-row"><strong>筛选字段</strong>{canManage && <Space size={6}><Button size="small" icon={<PlusOutlined />} disabled={loading || saving || fields.length >= 20} onClick={() => { setFields(items => [...items, { key: `field_${Date.now().toString(36)}`, label: '', blackColumn: '', brightColumn: '', enabled: true, allowEmpty: true, sortOrder: items.length }]); setDirty(true); }}>新增字段</Button><Button size="small" type="primary" loading={saving} disabled={!dirty || loading || fields.some(f => !f.label.trim() || !f.blackColumn || !f.brightColumn)} onClick={() => void save()}>保存筛选字段</Button></Space>}</div>
<div className="section-heading-row"><strong>筛选字段</strong><Space size={6}><ConfigurationWorkbookActions scope="filterFields" label="查询字段" disabled={loading||saving} value={{filterFields:fields}} readOnly={!canManage} onChange={definition=>{setFields(definition.filterFields as FilterField[]);setDirty(true);}} validate={definition=>(definition.filterFields as FilterField[]).flatMap(field=>[columns.black.includes(field.blackColumn)?null:`黑片来源列不存在:${field.blackColumn}`,columns.bright.includes(field.brightColumn)?null:`亮片来源列不存在:${field.brightColumn}`].filter((value):value is string=>!!value))}/>{canManage && <><Button size="small" icon={<PlusOutlined />} disabled={loading || saving || fields.length >= 20} onClick={() => { setFields(items => [...items, { key: `field_${Date.now().toString(36)}`, label: '', blackColumn: '', brightColumn: '', enabled: true, allowEmpty: true, sortOrder: items.length }]); setDirty(true); }}>新增字段</Button><Button size="small" type="primary" loading={saving} disabled={!dirty || loading || fields.some(f => !f.label.trim() || !f.blackColumn || !f.brightColumn)} onClick={() => void save()}>保存筛选字段</Button></>}</Space></div>
<p>配置后可在查询页面的“更多筛选”中使用。黑片、亮片按各自来源列筛选,支持搜索、多选和空值;代码按文本保留。</p>
{error && <Alert type="error" showIcon message={error} action={<Button onClick={() => setRevision(n => n + 1)}>重新加载</Button>} />}
<Table size="small" rowKey="key" loading={loading} dataSource={fields} pagination={false} scroll={{ x: 988 }} columns={[
<Table size="small" rowKey="key" loading={loading} dataSource={fields} pagination={{pageSize:15,showSizeChanger:false}} scroll={{ x: 988 }} columns={[
{ title: '筛选名称', key: 'label', width: 180, render: (_, f) => <Input size="small" aria-label={`筛选名称 ${f.key}`} value={f.label} maxLength={80} disabled={!canManage || saving} onChange={e => update(f.key, { label: e.target.value })} /> },
...(['black', 'bright'] as const).map(stage => ({ title: stage === 'black' ? '黑片来源列' : '亮片来源列', key: stage, width: 240, render: (_: unknown, f: FilterField) => <Select size="small" aria-label={`${stage === 'black' ? '黑片' : '亮片'}来源列 ${f.key}`} showSearch optionFilterProp="label" style={{ width: '100%' }} value={f[stage === 'black' ? 'blackColumn' : 'brightColumn'] || undefined} disabled={!canManage || saving} placeholder="选择来源列" options={[...new Set([...columns[stage], f[stage === 'black' ? 'blackColumn' : 'brightColumn']].filter(Boolean))].map(v => ({ label: v, value: v }))} onChange={v => update(f.key, { [stage === 'black' ? 'blackColumn' : 'brightColumn']: v })} /> })),
{ title: '允许空值', key: 'empty', width: 100, render: (_, f) => <Checkbox aria-label={`允许空值 ${f.label || f.key}`} checked={f.allowEmpty} disabled={!canManage || saving} onChange={e => update(f.key, { allowEmpty: e.target.checked })} /> },
+21 -69
View File
@@ -1,3 +1,4 @@
import { ManagementSections } from './ManagementSections';
import { configurationDateTime } from './configurationName';
import { alertObject, alertsForStatus, monitorLabel } from './taskMonitorModel';
import { useEffect, useRef, useState } from 'react';
@@ -12,7 +13,7 @@ import { isInternalDataField } from './publicDataDisplay';
import './completion.css';
type Row = Record<string, unknown>;
const TABLE_PAGE_SIZE = 20;
const TABLE_PAGE_SIZE = 15;
const INTERNAL_REVISION_FIELDS = new Set(['sourcerevisionid', 'targetrevisionid', 'expectedrevisionid', 'currentrevisionid', 'candidaterevisionid', 'baserevisionid']);
const obj = (value: unknown): Row => value && typeof value === 'object' && !Array.isArray(value) ? value as Row : {};
const rows = (value: unknown): Row[] => Array.isArray(value) ? value.filter(item => item && typeof item === 'object') as Row[] : [];
@@ -212,7 +213,7 @@ function InspectionStrategyPanel({ user, onUnauthorized }: { user: AuthUser; onU
{loading ? <div className="completion-loading"><Spin />读取巡检策略…</div> : canManage ? strategies.length ? <Table size="small" bordered columns={strategyColumns} dataSource={strategies} rowKey={row => String(row.id)} pagination={false} scroll={{ x: 1300 }} /> : <Empty description="服务端当前没有已登记的巡检策略" /> : null}
<div className="completion-filter-row">{canRun && <Button type="primary" loading={running} disabled={!releaseId} onClick={() => void run()}>对当前数据执行巡检</Button>}</div>
{!canRun && <Alert showIcon type="info" message="当前账号不能执行巡检" description="运行和结果查看需要服务端授权的质量查看或业务分析角色及明细查询权限。" />}
{canRun && (runs.length ? <Table size="small" bordered columns={runColumns} dataSource={runs} rowKey={row => String(row.id)} pagination={{ pageSize: 8, showSizeChanger: false }} scroll={{ x: 1120 }} /> : <Empty description={releaseId ? '当前数据尚无巡检运行记录' : '当前还没有可巡检的数据'} />)}
{canRun && (runs.length ? <Table size="small" bordered columns={runColumns} dataSource={runs} rowKey={row => String(row.id)} pagination={{ pageSize: 15, showSizeChanger: false }} scroll={{ x: 1120 }} /> : <Empty description={releaseId ? '当前数据尚无巡检运行记录' : '当前还没有可巡检的数据'} />)}
<Modal className="management-modal" open={editing !== null} title={editing?.id ? `编辑巡检策略 · ${text(editing.name)}` : '新建巡检策略'} okText="保存策略" confirmLoading={saving} onCancel={() => { setEditing(null); form.resetFields(); }} onOk={() => void save()} width={Math.min(820, window.innerWidth - 24)}>
<Form form={form} layout="vertical"><Form.Item name="name" label="策略名称" rules={[{ required: true, whitespace: true }]}><Input maxLength={100} /></Form.Item><Form.Item name="active" label="启用策略" valuePropName="checked"><Switch checkedChildren="启用" unCheckedChildren="停用" /></Form.Item><h3>巡检规则</h3><div className="governance-form-grid">{booleanFields.map(([key, label]) => <Form.Item key={key} name={key} label={label} valuePropName="checked"><Switch /></Form.Item>)}{metricFields.map(field => <Form.Item key={field.key} name={field.key} label={field.label} rules={[{ required: true }]}><InputNumber min={field.min} max={field.max} step={field.step} style={{ width: '100%' }} /></Form.Item>)}</div><p className="completion-note">阈值范围由服务端校验;保存后对后续巡检生效。</p></Form><Alert showIcon type="info" message="受控 AI 下钻" description="启用后仅为巡检发现创建绑定当前用户、当前数据与授权范围的持久化分析轮次;模型不接收业务数据行或业务数值。最多选择 1–3 项。" />
</Modal>
@@ -397,7 +398,7 @@ export function SourceGovernancePanel({ user, currentReleaseId, onUnauthorized }
{ key: 'coverage', label: '到齐与告警', children: <div className="governance-panel-body"><div className="governance-status-strip"><span>适用数据 <strong>当前数据</strong></span><Tag color={coverage?.complete === true ? 'green' : 'red'}>{coverage?.complete === true ? '覆盖完整' : coverage?.complete === false ? '覆盖未齐' : '未返回'}</Tag></div>{coverageItems.length ? <Table size="small" bordered columns={coverageColumns} dataSource={coverageItems} rowKey={row => String(row.protocolId || row.sourceSlot)} pagination={{ pageSize: TABLE_PAGE_SIZE, showSizeChanger: false }} scroll={{ x: 980 }} /> : <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="当前数据没有已登记的来源协议覆盖项" />}</div> },
{ key: 'protocols', label: `来源协议(${protocols.length})`, children: <div className="governance-panel-body">{protocols.length ? <Table size="small" bordered columns={protocolColumns} dataSource={protocols} rowKey={row => String(row.id)} pagination={{ pageSize: TABLE_PAGE_SIZE, showSizeChanger: false }} scroll={{ x: 1120 }} /> : <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="服务端尚未登记来源协议" />}</div> },
{ key: 'changes', label: `来源变更与撤销(${sourceChanges.length})`, children: <div className="governance-panel-body">{conflictCandidates.length > 0 && <Alert showIcon type="warning" message={`${conflictCandidates.length} 个来源身份待裁决`} description={<span>{conflictCandidates.map(item => <Button type="link" key={item.id} onClick={() => { setResolveItem(item); resolveForm.setFieldsValue({ decision: 'REPLACE' }); }}>{item.sourceFile} · 裁决</Button>)}</span>} />}{errors.changes ? <Alert type="error" message={errors.changes} /> : sourceChanges.length ? <Table size="small" bordered columns={changeColumns} dataSource={sourceChanges} rowKey={row => String(row.id)} pagination={{ pageSize: TABLE_PAGE_SIZE, showSizeChanger: false }} scroll={{ x: 1080 }} /> : <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="暂无来源变更或撤销申请" />}</div> },
{ key: 'revisions', label: `原文件记录与证据(${revisions.length})`, children: <div className="governance-panel-body">{revisions.length ? <Table size="small" bordered columns={revisionColumns} dataSource={revisions} rowKey={row => String(row.id)} pagination={{ pageSize: 10, showSizeChanger: false }} scroll={{ x: 1280 }} /> : <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="服务端尚未返回归档原文件记录" />}</div> },
{ key: 'revisions', label: `原文件记录与证据(${revisions.length})`, children: <div className="governance-panel-body">{revisions.length ? <Table size="small" bordered columns={revisionColumns} dataSource={revisions} rowKey={row => String(row.id)} pagination={{ pageSize: 15, showSizeChanger: false }} scroll={{ x: 1280 }} /> : <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="服务端尚未返回归档原文件记录" />}</div> },
]} />
<Modal className="management-modal" open={protocolOpen} title="登记来源协议" width={760} okText="保存协议" confirmLoading={actionBusy === 'protocol'} onCancel={() => setProtocolOpen(false)} onOk={() => void submitProtocol()}>
<Alert showIcon type="info" message="协议定义用于来源身份和到齐校验" description="当前解析器仅验证月度快照和分片快照;来源槽位由阶段、期间、范围和分片共同确定。" />
@@ -418,61 +419,6 @@ export function SourceGovernancePanel({ user, currentReleaseId, onUnauthorized }
</section>;
}
export function CostConfigurationPanel({ user, onUnauthorized }: { user: AuthUser; onUnauthorized: (error: unknown) => boolean }) {
const { message } = App.useApp();
const [items, setItems] = useState<Row[]>([]);
const [loading, setLoading] = useState(true);
const [busy, setBusy] = useState(false);
const [error, setError] = useState('');
const [editing, setEditing] = useState<Row | null>(null);
const [validation, setValidation] = useState<Row | null>(null);
const [form] = Form.useForm<Row>();
const canManage = user.roles.some(role => ['SYSTEM_ADMIN', 'DATA_MAINTAINER', 'RULE_MAINTAINER'].includes(role));
const load = async () => {
setLoading(true);
try { setItems(collectionItems(await api.costConfigurations())); setError(''); }
catch (value) { if (!onUnauthorized(value)) setError(value instanceof Error ? value.message : '成本来源配置读取失败'); }
finally { setLoading(false); }
};
useEffect(() => { void load(); }, []);
const open = (item?: Row) => {
setEditing(item || {}); setValidation(null);
const definition = obj(item?.definition);
form.setFieldsValue({ name: text(item?.name, ''), reason: '', sources: rows(definition.sources) });
};
const save = async () => {
try {
const values = await form.validateFields(); setBusy(true);
const body = { name: String(values.name || '').trim(), reason: String(values.reason || '').trim(), definition: { sources: values.sources || [] }, ...(editing?.id ? { expectedRowVersion: Number(editing.rowVersion || 0) } : {}) };
if (editing?.id) await api.updateCostConfiguration(String(editing.id), body); else await api.createCostConfiguration(body);
message.success('成本来源与公式基线已保存;未执行成本金额计算'); setEditing(null); await load();
} catch (value) { if (value && typeof value === 'object' && 'errorFields' in value) return; if (!onUnauthorized(value)) message.error(value instanceof ApiError && value.status === 409 ? '配置已被其他操作更新,请刷新后重新编辑。' : value instanceof Error ? value.message : '成本配置保存失败'); }
finally { setBusy(false); }
};
const validate = async (item: Row) => {
setBusy(true);
try { const result = await api.validateCostConfiguration(String(item.id), { expectedRowVersion: Number(item.rowVersion || 0) }); setValidation(obj(result.validation)); await load(); }
catch (value) { if (!onUnauthorized(value)) message.error(value instanceof ApiError && value.status === 409 ? '配置已被其他操作更新,请刷新后重试。' : value instanceof Error ? value.message : '来源配置校验失败'); }
finally { setBusy(false); }
};
const columns: ColumnsType<Row> = [
{ title: '配置名称', dataIndex: 'name', key: 'name', fixed: 'left', width: 220 },
{ title: '状态', dataIndex: 'status', key: 'status', width: 150, render: value => <Tag>{text(value)}</Tag> },
{ title: '来源数', key: 'sources', align: 'right', width: 90, render: (_, row) => rows(obj(row.definition).sources).length },
{ title: '操作', key: 'actions', width: 180, render: (_, row) => <span className="completion-actions"><Button type="link" disabled={!canManage || row.status !== 'DRAFT'} onClick={() => open(row)}>编辑</Button><Button type="link" loading={busy} onClick={() => void validate(row)}>校验</Button></span> },
];
return <section className="panel section-panel completion-tools">
<div className="completion-panel-heading"><div><strong>成本来源与计算基线登记</strong><span>仅记录来源字段、单位、审批引用与原公式;成本金额和降本计算保持关闭</span></div><div className="completion-actions"><Button icon={<ReloadOutlined />} onClick={() => void load()}>刷新</Button>{canManage && <Button type="primary" onClick={() => open()}>新建成本来源配置</Button>}</div></div>
{error && <Alert showIcon type="error" message="成本来源配置不可用" description={error} />}
{loading ? <div className="completion-loading"><Spin />读取成本来源配置…</div> : items.length ? <Table size="small" bordered columns={columns} dataSource={items} rowKey={row => String(row.id)} pagination={false} scroll={{ x: 740 }} /> : <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="服务端当前没有成本来源登记" />}
{validation && <Alert showIcon type={validation.valid === true ? 'success' : 'warning'} message={text(validation.readiness, validation.valid === true ? '来源字段已验证' : '来源信息需补充')} description={`错误 ${rows(validation.errors).length} 项 · 成本金额计算:${validation.calculationEnabled === false ? '未启用' : '未由服务端确认'}`} />}
<Modal className="management-modal" open={Boolean(editing)} title={editing?.id ? '编辑成本来源配置' : '登记成本来源配置'} width={900} okText="保存配置" confirmLoading={busy} onCancel={() => setEditing(null)} onOk={() => void save()}>
<Alert showIcon type="info" message="成本核算未启用" description="此表单只登记依赖和原公式,不接受成本金额,不生成成本或降本结论。" />
<Form form={form} layout="vertical"><Form.Item name="name" label="配置名称" rules={[{ required: true, whitespace: true }]}><Input maxLength={180} /></Form.Item><Form.List name="sources">{(fields, { add, remove }) => <div className="target-rule-list"><div className="resource-toolbar"><strong>成本来源字段映射</strong><Button onClick={() => add({ unit: 'CNY/kg' })}>添加来源</Button></div>{fields.map(field => <div className="target-rule-row" key={field.key}><Form.Item {...field} name={[field.name, 'sourceReference']} label="来源引用" rules={[{ required: true, whitespace: true }]}><Input /></Form.Item><Form.Item {...field} name={[field.name, 'objectCode']} label="对象编码" rules={[{ required: true, whitespace: true }]}><Input /></Form.Item><Form.Item {...field} name={[field.name, 'costField']} label="成本字段" rules={[{ required: true, whitespace: true }]}><Input /></Form.Item><Form.Item {...field} name={[field.name, 'unit']} label="单位" rules={[{ required: true, whitespace: true }]}><Input /></Form.Item><Form.Item {...field} name={[field.name, 'currency']} label="币种"><Input placeholder="CNY" /></Form.Item><Form.Item {...field} name={[field.name, 'approvalReference']} label="审批引用"><Input /></Form.Item><Form.Item {...field} name={[field.name, 'originalFormula']} label="原公式"><Input /></Form.Item><Form.Item {...field} name={[field.name, 'fromMonth']} label="起始月 YYYY-MM"><Input /></Form.Item><Form.Item {...field} name={[field.name, 'toMonth']} label="结束月 YYYY-MM"><Input /></Form.Item><Button danger onClick={() => remove(field.name)}>移除</Button></div>)}</div>}</Form.List><Form.Item name="reason" label="登记原因" rules={[{ required: true, whitespace: true }]}><Input.TextArea rows={2} maxLength={300} /></Form.Item></Form>
</Modal>
</section>;
}
export function BackupOperationsPanel({ user, onUnauthorized, onNavigate }: { user: AuthUser; onUnauthorized: (error: unknown) => boolean; onNavigate?: () => void }) {
const { message } = App.useApp();
const [data, setData] = useState<Row | null>(null);
@@ -514,7 +460,7 @@ export function BackupOperationsPanel({ user, onUnauthorized, onNavigate }: { us
return <section className="panel section-panel completion-tools">
<div className="completion-panel-heading"><div><strong>备份、恢复验证与计划</strong><span>恢复验证仅在隔离目录执行;当前不开放在线业务库覆盖恢复</span></div><div className="completion-actions"><Button icon={<ReloadOutlined />} onClick={() => void load()}>刷新</Button>{canManage && <><Button onClick={() => { form.setFieldsValue({ ...plan, reason: '' }); setPlanOpen(true); }}>备份计划</Button><Button type="primary" loading={busy} onClick={() => void runJob('BACKUP_CREATE_VERIFY')}>创建并验证备份</Button></>}</div></div>
{error && <Alert showIcon type="error" message="备份状态不可用" description={error} />}
{loading ? <div className="completion-loading"><Spin />读取备份与恢复状态…</div> : <><div className="governance-monitor-grid"><div><small>计划</small><strong>{plan.active === true ? '已启用' : plan.active === false ? '已停用' : '未配置'}</strong></div><div><small>周期 / RPO</small><strong>{text(plan.intervalHours)} 小时 / {text(plan.rpoHours)} 小时</strong></div><div><small>RTO</small><strong>{text(plan.rtoHours)} 小时</strong></div><div><small>在线恢复</small><strong>{obj(data).liveRestoreEnabled === true ? '开放' : '关闭'}</strong></div></div>{backups.length ? <Table size="small" bordered columns={columns} dataSource={backups} rowKey={row => String(row.id)} pagination={{ pageSize: 10, showSizeChanger: false }} scroll={{ x: 920 }} /> : <Empty description="服务端当前没有备份记录" />}</>}
{loading ? <div className="completion-loading"><Spin />读取备份与恢复状态…</div> : <><div className="governance-monitor-grid"><div><small>计划</small><strong>{plan.active === true ? '已启用' : plan.active === false ? '已停用' : '未配置'}</strong></div><div><small>周期 / RPO</small><strong>{text(plan.intervalHours)} 小时 / {text(plan.rpoHours)} 小时</strong></div><div><small>RTO</small><strong>{text(plan.rtoHours)} 小时</strong></div><div><small>在线恢复</small><strong>{obj(data).liveRestoreEnabled === true ? '开放' : '关闭'}</strong></div></div>{backups.length ? <Table size="small" bordered columns={columns} dataSource={backups} rowKey={row => String(row.id)} pagination={{ pageSize: 15, showSizeChanger: false }} scroll={{ x: 920 }} /> : <Empty description="服务端当前没有备份记录" />}</>}
{onNavigate && <Button type="link" onClick={onNavigate}>查看备份任务进度</Button>}
<Modal className="management-modal" open={planOpen} title="备份计划配置" okText="保存计划" confirmLoading={busy} onCancel={() => setPlanOpen(false)} onOk={() => void savePlan()}><Form form={form} layout="vertical"><Form.Item name="active" label="计划状态" valuePropName="checked"><Switch checkedChildren="启用" unCheckedChildren="停用" /></Form.Item><div className="governance-form-grid"><Form.Item name="intervalHours" label="备份周期(小时)" rules={[{ required: true }]}><InputNumber min={1} max={8760} /></Form.Item><Form.Item name="rpoHours" label="RPO(小时)" rules={[{ required: true }]}><InputNumber min={1} max={8760} /></Form.Item><Form.Item name="rtoHours" label="RTO(小时)" rules={[{ required: true }]}><InputNumber min={1} max={8760} /></Form.Item></div><Form.Item name="reason" label="修改原因" rules={[{ required: true, whitespace: true }]}><Input.TextArea rows={2} /></Form.Item></Form></Modal>
</section>;
@@ -590,16 +536,18 @@ export function OperationsMonitorPanel({ user, onUnauthorized }: { user: AuthUse
{ title: '最近请求', dataIndex: 'lastSeen', key: 'lastSeen', width: 190, render: value => configurationDateTime(value) || '—' },
];
return <>
<section className="panel section-panel governance-monitor">
<ManagementSections items={[
{ key: 'health', label: '运行状态与告警', children: <><section className="panel section-panel governance-monitor">
<div className="completion-panel-heading"><div><strong>运行监控与告警</strong><span>只读取并更新服务端运行状态;不发送外部通知</span></div><div className="completion-actions"><Button icon={<ReloadOutlined />} loading={loading} onClick={() => void load()}>刷新</Button>{canRefresh && <><Button onClick={() => void editPolicy()} disabled={!alertPolicy}>告警阈值</Button><Button type="primary" loading={refreshing} onClick={() => void refresh()}>系统健康检查</Button></>}</div></div>
{error && <Alert showIcon type="error" message="运行监控暂不可用" description={error} />}
{policyError && <Alert showIcon type="error" message="告警策略不可用" description={policyError} />}
{loading ? <div className="completion-loading"><Spin />读取平台状态…</div> : <><div className="governance-monitor-grid">{summaryEntries.length ? summaryEntries.map(([key, value]) => <div key={key}><small>{summaryLabels[key] || '其他指标'}</small><strong>{text(value)}</strong></div>) : <div className="completion-placeholder">服务端没有返回运行指标</div>}</div><Tabs activeKey={alertStatus} onChange={setAlertStatus} items={[{key:'OPEN',label:`当前告警(${alertsForStatus(allAlerts,'OPEN').length})`},{key:'RECOVERED',label:`已恢复(${alertsForStatus(allAlerts,'RECOVERED').length})`}]}/>{alertRows.length ? <Table size="small" bordered columns={alertColumns} dataSource={alertRows} rowKey={row => String(row.id || row.code)} pagination={{ pageSize: 10, showSizeChanger: false }} scroll={{ x: 1260 }} /> : <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description={alertStatus === 'OPEN' ? '当前没有未恢复告警' : '暂无已恢复告警'} />}</>}
<h3>接口请求指标</h3><p className="completion-note">累计请求数、失败数与耗时;最近请求时间不代表统计起点。</p>
{loading ? <div className="completion-loading"><Spin />读取接口指标…</div> : requestMetricRows.length ? <Table size="small" bordered columns={requestMetricColumns} dataSource={requestMetricRows} rowKey={row => `${text(row.routePattern)}-${text(row.method)}`} pagination={{ pageSize: 12, showSizeChanger: false }} scroll={{ x: 1200 }} /> : <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="服务端尚未记录接口请求指标" />}
{loading ? <div className="completion-loading"><Spin />读取平台状态…</div> : <><div className="governance-monitor-grid">{summaryEntries.length ? summaryEntries.map(([key, value]) => <div key={key}><small>{summaryLabels[key] || '其他指标'}</small><strong>{text(value)}</strong></div>) : <div className="completion-placeholder">服务端没有返回运行指标</div>}</div><Tabs activeKey={alertStatus} onChange={setAlertStatus} items={[{key:'OPEN',label:`当前告警(${alertsForStatus(allAlerts,'OPEN').length})`},{key:'RECOVERED',label:`已恢复(${alertsForStatus(allAlerts,'RECOVERED').length})`}]}/>{alertRows.length ? <Table size="small" bordered columns={alertColumns} dataSource={alertRows} rowKey={row => String(row.id || row.code)} pagination={{ pageSize: 15, showSizeChanger: false }} scroll={{ x: 1260 }} /> : <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description={alertStatus === 'OPEN' ? '当前没有未恢复告警' : '暂无已恢复告警'} />}</>}</section></> },
{ key: 'requests', label: '接口请求指标', children: <section className="panel section-panel"><div className="completion-actions"><Button icon={<ReloadOutlined />} onClick={() => void load()}>刷新</Button></div> <h3>接口请求指标</h3><p className="completion-note">累计请求数、失败数与耗时;最近请求时间不代表统计起点。</p>
{loading ? <div className="completion-loading"><Spin />读取接口指标…</div> : requestMetricRows.length ? <Table size="small" bordered columns={requestMetricColumns} dataSource={requestMetricRows} rowKey={row => `${text(row.routePattern)}-${text(row.method)}`} pagination={{ pageSize: 15, showSizeChanger: false }} scroll={{ x: 1200 }} /> : <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="服务端尚未记录接口请求指标" />}
</section> },
{ key: 'inspection', label: '巡检策略', children: <InspectionStrategyPanel user={user} onUnauthorized={onUnauthorized} /> },
]} />
<Modal className="management-modal" open={policyOpen} title="告警阈值与静默策略" okText="保存策略" confirmLoading={policyBusy} onCancel={() => setPolicyOpen(false)} onOk={() => void savePolicy()}><Form form={policyForm} layout="vertical"><div className="governance-form-grid"><Form.Item name="queueAgeMinutes" label="队列告警(分钟)" rules={[{ required: true }]}><InputNumber min={1} max={1440} /></Form.Item><Form.Item name="diskFreeBytes" label="最低可用磁盘空间(字节)" rules={[{ required: true }]}><InputNumber min={104857600} max={1125899906842624} style={{ width: '100%' }} /></Form.Item><Form.Item name="backupAgeHours" label="备份超时(小时)" rules={[{ required: true }]}><InputNumber min={1} max={720} /></Form.Item><Form.Item name="quietPeriodMinutes" label="重复告警静默(分钟)" rules={[{ required: true }]}><InputNumber min={0} max={10080} /></Form.Item><Form.Item name="slowRequestMillis" label="慢请求阈值(毫秒)" rules={[{ required: true }]}><InputNumber min={100} max={120000} /></Form.Item><Form.Item name="silenceUntil" label="临时静默截止(ISO,可选)"><Input placeholder="2026-10-21T09:00:00Z" /></Form.Item></div><Form.Item name="severityOverrides" label="告警严重度覆盖(JSON)" rules={[{ required: true }]}><Input.TextArea rows={5} spellCheck={false} /></Form.Item><Form.Item name="reason" label="修改原因" rules={[{ required: true, whitespace: true }]}><Input.TextArea rows={2} /></Form.Item><p className="completion-note">保存后按新策略检查;严重度 key 只能使用服务端列出的告警类型。</p></Form></Modal>
</section>
<InspectionStrategyPanel user={user} onUnauthorized={onUnauthorized} />
</>;
}
@@ -676,19 +624,23 @@ export function ServiceIdentityPanel({ user, onUnauthorized }: { user: AuthUser;
{ title: '操作', key: 'action', fixed: 'right', width: 230, render: (_, row) => <span className="completion-actions">{canManage && <><Button type="link" onClick={() => open(row)}>编辑</Button><Button type="link" onClick={() => { setRotating(row); rotateForm.setFieldsValue({ credentialReference: text(row.credentialReference, ''), credentialStatus: text(row.credentialStatus, 'UNCONFIGURED'), reason: '' }); }}>轮换</Button></>}<Button type="link" onClick={async () => { try { setRotations(collectionItems(await api.serviceRotations(String(row.id)))); setRotating({ ...row, showRotations: true }); } catch (value) { if (!onUnauthorized(value)) message.error(value instanceof Error ? value.message : '轮换记录读取失败'); } }}>历史</Button></span> },
];
return <div className="governance-service-identities">
<section className="panel section-panel">
<ManagementSections items={[
{ key: 'identities', label: '服务身份', children: <> <section className="panel section-panel">
<div className="completion-panel-heading"><div><strong>服务身份与最小动作授权</strong><span>仅保存 env: 或 file: 凭据引用,不在浏览器或服务端表单中接收密钥文本</span></div><div className="completion-actions"><Button icon={<ReloadOutlined />} onClick={() => void load()}>刷新</Button>{canManage && <Button type="primary" onClick={() => open()}>新建服务身份</Button>}</div></div>
{error && <Alert showIcon type="error" message="服务身份管理不可用" description={error} />}
{loading ? <div className="completion-loading"><Spin />读取服务身份…</div> : items.length ? <Table size="small" bordered columns={columns} dataSource={items} rowKey={row => String(row.id)} pagination={false} scroll={{ x: 1200 }} /> : <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="尚未创建服务身份" />}
{loading ? <div className="completion-loading"><Spin />读取服务身份…</div> : items.length ? <Table size="small" bordered columns={columns} dataSource={items} rowKey={row => String(row.id)} pagination={{ pageSize: 15, showSizeChanger: false }} scroll={{ x: 1200 }} /> : <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="尚未创建服务身份" />}
</section>
<section className="panel section-panel"><div className="completion-panel-heading"><div><strong>服务接入绑定</strong><span>只允许类型匹配且授权状态可用的身份</span></div></div>{serviceTypes.length ? <div className="governance-binding-list">{serviceTypes.map(type => <div key={type}><strong>{type}</strong><span>{text(bindings.find(item => item.serviceType === type)?.serviceIdentityId, '未绑定')}</span>{canManage && <Select aria-label={`${type}服务身份`} placeholder="选择身份" value={String(bindings.find(item => item.serviceType === type)?.serviceIdentityId || '') || undefined} loading={busy} options={items.filter(item => item.serviceType === type).map(item => ({ label: `${text(item.name)} · ${text(item.credentialStatus)}`, value: String(item.id) }))} onChange={value => void bind(type, value)} />}</div>)}</div> : <Empty description="服务类型尚未由服务端返回" />}</section>
</> },
{ key: 'bindings', label: '接入绑定', children: <> <section className="panel section-panel"><div className="completion-panel-heading"><div><strong>服务接入绑定</strong><span>只允许类型匹配且授权状态可用的身份</span></div></div>{serviceTypes.length ? <div className="governance-binding-list">{serviceTypes.map(type => <div key={type}><strong>{type}</strong><span>{text(bindings.find(item => item.serviceType === type)?.serviceIdentityId, '未绑定')}</span>{canManage && <Select aria-label={`${type}服务身份`} placeholder="选择身份" value={String(bindings.find(item => item.serviceType === type)?.serviceIdentityId || '') || undefined} loading={busy} options={items.filter(item => item.serviceType === type).map(item => ({ label: `${text(item.name)} · ${text(item.credentialStatus)}`, value: String(item.id) }))} onChange={value => void bind(type, value)} />}</div>)}</div> : <Empty description="服务类型尚未由服务端返回" />}</section>
</> },
]} />
<Modal className="management-modal" open={Boolean(editing) && !editing?.showRotations} title={editing?.id ? '编辑服务身份' : '新建服务身份'} okText="保存配置" confirmLoading={busy} onCancel={() => setEditing(null)} onOk={() => void save()}>
<Alert showIcon type="warning" message="服务身份默认停用" description="启用需要 READY 凭据引用和服务端允许的最小动作。输入凭据引用,不要粘贴密钥内容。" />
<Form form={form} layout="vertical"><Form.Item name="name" label="身份名称" rules={[{ required: true, whitespace: true }]}><Input maxLength={190} /></Form.Item><Form.Item name="serviceType" label="服务类型" rules={[{ required: true }]}><Select disabled={Boolean(editing?.id)} options={serviceTypes.map(value => ({ label: value, value }))} /></Form.Item><Form.Item name="allowedActions" label="允许动作" rules={[{ required: true, type: 'array', min: 1 }]}><Select mode="multiple" options={(ACTIONS_BY_TYPE[String(serviceType || '')] || allowedActions).filter((value: string) => allowedActions.includes(value)).map((value: string) => ({ label: value, value }))} /></Form.Item><Form.Item name="credentialReference" label="凭据引用" rules={[{ pattern: /^(?:env:[A-Z][A-Z0-9_]{0,100}|file:[A-Za-z0-9_./-]{1,240})$/, message: '使用 env:NAME 或 file:relative/path 引用,不填密钥内容' }]}><Input placeholder="env:QUALITY_MODEL_KEY" autoComplete="off" /></Form.Item><Form.Item name="credentialStatus" label="凭据状态" rules={[{ required: true }]}><Select options={['UNCONFIGURED', 'READY', 'EXPIRED', 'REVOKED'].map(value => ({ label: value, value }))} /></Form.Item>{Boolean(editing?.id) && <Form.Item name="active" label="身份状态" valuePropName="checked"><Switch checkedChildren="启用" unCheckedChildren="停用" /></Form.Item>}<Form.Item name="reason" label="操作原因" rules={[{ required: true, whitespace: true }]}><Input.TextArea rows={2} maxLength={300} /></Form.Item></Form>
</Modal>
<Modal className="management-modal" open={Boolean(rotating?.id) && !rotating?.showRotations} title="轮换凭据引用" okText="提交轮换" confirmLoading={busy} onCancel={() => setRotating(null)} onOk={() => void rotate()}><Form form={rotateForm} layout="vertical"><Form.Item name="credentialReference" label="凭据引用" rules={[{ required: true, pattern: /^(?:env:[A-Z][A-Z0-9_]{0,100}|file:[A-Za-z0-9_./-]{1,240})$/, message: '使用 env:NAME 或 file:relative/path 引用' }]}><Input autoComplete="off" /></Form.Item><Form.Item name="credentialStatus" label="状态"><Select options={['READY', 'EXPIRED', 'REVOKED', 'UNCONFIGURED'].map(value => ({ label: value, value }))} /></Form.Item><Form.Item name="reason" label="轮换理由" rules={[{ required: true, whitespace: true }]}><Input.TextArea rows={2} /></Form.Item></Form></Modal>
<Modal className="management-modal" open={Boolean(bindingEdit)} title="变更服务身份绑定" okText="保存绑定" confirmLoading={busy} onCancel={() => setBindingEdit(null)} onOk={() => void submitBinding()}><p>服务类型:{text(bindingEdit?.type)} · 身份 ID:{text(bindingEdit?.identityId)}</p><Form form={bindingForm} layout="vertical"><Form.Item name="reason" label="绑定原因" rules={[{ required: true, whitespace: true }]}><Input.TextArea rows={2} maxLength={300} /></Form.Item></Form></Modal>
<Drawer className="management-drawer" open={rotating?.showRotations === true} title="凭据引用变更历史" width={Math.min(680, window.innerWidth)} onClose={() => setRotating(null)}>{rotations.length ? <Table size="small" bordered dataSource={rotations} rowKey={row => String(row.id)} pagination={{ pageSize: 8, showSizeChanger: false }} columns={[{ title: '引用', dataIndex: 'credentialReference', key: 'credentialReference' }, { title: '状态', dataIndex: 'credentialStatus', key: 'credentialStatus' }, { title: '原因', dataIndex: 'reason', key: 'reason' }, { title: '时间', dataIndex: 'createdAt', key: 'createdAt' }]} /> : <Empty description="没有已返回的轮换记录" />}</Drawer>
<Drawer className="management-drawer" open={rotating?.showRotations === true} title="凭据引用变更历史" width={Math.min(680, window.innerWidth)} onClose={() => setRotating(null)}>{rotations.length ? <Table size="small" bordered dataSource={rotations} rowKey={row => String(row.id)} pagination={{ pageSize: 15, showSizeChanger: false }} columns={[{ title: '引用', dataIndex: 'credentialReference', key: 'credentialReference' }, { title: '状态', dataIndex: 'credentialStatus', key: 'credentialStatus' }, { title: '原因', dataIndex: 'reason', key: 'reason' }, { title: '时间', dataIndex: 'createdAt', key: 'createdAt' }]} /> : <Empty description="没有已返回的轮换记录" />}</Drawer>
</div>;
}
+160 -159
View File
@@ -1,3 +1,8 @@
import { ConfigurationWorkbookActions } from './ConfigurationWorkbookActions';
import { MappingEditorContext } from './MappingEditorContext';
import { ClassificationRuleEditor } from './ClassificationRuleEditor';
import { ManagementSections } from './ManagementSections';
import type { ClassificationSelection } from './classificationMaintenance';
import { TasksMonitorPage } from './TasksMonitorPage';
import { configurationDateTime, currentConfigurationName } from './configurationName';
import { BusinessValue, currentMetricFormula } from './BusinessValue';
@@ -6,7 +11,7 @@ import { AnalysisClarification } from './AnalysisClarification';
import { pendingClarificationTurnId } from './analysisClarificationModel';
import { useAnalysisVisualRecovery } from './analysisVisualRecovery';
import { mergeModelThinking, modelThinkingEntries } from './analysisModelThinking';
import { usePageCache } from './pageCache';
import { usePageCache, useMaintenanceRefresh } from './pageCache';
import { FilterFieldSettings } from './FilterFieldSettings';
import { isInternalDataField, publicJson, readableRuleDescription } from './publicDataDisplay';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
@@ -26,7 +31,6 @@ import {
Select,
Spin,
Switch,
Tabs,
Tag,
} from 'antd';
import type { ColumnsType } from 'antd/es/table';
@@ -53,11 +57,11 @@ import { beginAnalysisSubmission, requestSubmissionCancellation, submissionOutco
import { activeAnalysisTurn, analysisIsRunning, beginAnalysisSelection, defaultAnalysisSession, rememberAnalysisSession, rememberPendingAnalysisSession, settledAnalysisTurn } from './analysisConversationState';
import './analysis-chat.css';
const TABLE_PAGE_SIZE = 20;
const TABLE_PAGE_SIZE = 15;
import { DataWorkflowHub, MasterWorkbookUpload, ReadableConfigDefinition, SourceFilesPanel, type PendingSourceDeletion } from './NoCodeDataWorkflow';
import { MappingDefinitionEditor, MasterDataDefinitionEditor, MatchRulesDefinitionEditor, RuleDefinitionEditor, TechnicalDefinitionEditor, validateStructuredMappingDefinition, type ConfigDefinition } from './StructuredConfigEditors';
import { useDataWorkflowOperation } from './useDataWorkflowOperation';
import { CostConfigurationPanel, ServiceIdentityPanel, SourceGovernancePanel, UserAccessPolicyPanel } from './GovernanceCompletion';
import { ServiceIdentityPanel, SourceGovernancePanel, UserAccessPolicyPanel } from './GovernanceCompletion';
function apiMessage(error: unknown): string {
if (error instanceof ApiError && error.status === 409) return '当前数据或配置已变化,请刷新后重新操作。';
@@ -225,9 +229,9 @@ function ManagementPageContents({ page, user, onUnauthorized, onNavigate }: Mana
if (page === 'analysis') return <AnalysisPage onUnauthorized={onUnauthorized} user={user} />;
if (page === 'access') return <AccessPage user={user} onUnauthorized={onUnauthorized} />;
if (page === 'basis') return <BasisPage onUnauthorized={onUnauthorized} />;
if (page === 'rules') return <><ConfigManagementPage kind={page} user={user} onUnauthorized={onUnauthorized} onNavigate={onNavigate} /><MetricDictionaryPanel user={user} onUnauthorized={onUnauthorized} />{user.roles.some(role => ['SYSTEM_ADMIN', 'RULE_MAINTAINER'].includes(role)) && <TargetVersionAdmin onUnauthorized={onUnauthorized} />}</>;
if (page === 'masterData') return <><FilterFieldSettings canManage={user.roles.some(role => ['SYSTEM_ADMIN','DATA_MAINTAINER'].includes(role))} onUnauthorized={onUnauthorized} /><ConfigManagementPage kind={page} user={user} onUnauthorized={onUnauthorized} onNavigate={onNavigate} /></>;
if (page === 'mappings') return <><ConfigManagementPage kind={page} user={user} onUnauthorized={onUnauthorized} onNavigate={onNavigate} /><MappingIssueResolutionPanel user={user} onUnauthorized={onUnauthorized} /><CostConfigurationPanel user={user} onUnauthorized={onUnauthorized} /></>;
if (page === 'rules') return <ManagementSections items={[{ key: 'rules', label: '计算规则', children: <ConfigManagementPage kind={page} user={user} onUnauthorized={onUnauthorized} onNavigate={onNavigate} /> },{ key: 'dictionary', label: '指标定义', children: <MetricDictionaryPanel user={user} onUnauthorized={onUnauthorized} /> },...(user.roles.some(role => ['SYSTEM_ADMIN', 'RULE_MAINTAINER'].includes(role)) ? [{ key: 'targets', label: '目标管理', children: <TargetVersionAdmin onUnauthorized={onUnauthorized} /> }] : [])]} />;
if (page === 'masterData') return <ManagementSections items={[{ key: 'catalog', label: '基础名录', children: <ConfigManagementPage kind={page} user={user} onUnauthorized={onUnauthorized} onNavigate={onNavigate} /> },{ key: 'filters', label: '查询字段', children: <FilterFieldSettings canManage={user.roles.some(role => ['SYSTEM_ADMIN','DATA_MAINTAINER'].includes(role))} onUnauthorized={onUnauthorized} /> }]} />;
if (page === 'mappings') return <MappingMaintenancePage user={user} onUnauthorized={onUnauthorized} onNavigate={onNavigate} />;
const resource: Record<string, { endpoint: 'imports' | 'tasks'; title: string; subtitle: string }> = {
imports: { endpoint: 'imports', title: '导入与校验记录', subtitle: '接收后的来源文件及其校验任务状态。' },
tasks: { endpoint: 'tasks', title: '后台任务', subtitle: '任务状态、进度和服务端返回的失败信息。' },
@@ -237,7 +241,7 @@ function ManagementPageContents({ page, user, onUnauthorized, onNavigate }: Mana
}
function PageHeader({ actions }: { title: string; description?: string; actions?: React.ReactNode }) {
return actions ? <div className="page-heading page-actions-only"><div className="page-heading-actions">{actions}</div></div> : null;
return actions ? <div className="management-module-actions"><div className="page-heading-actions">{actions}</div></div> : null;
}
function SourcesPage({ onUnauthorized, user }: { onUnauthorized: (error: unknown) => boolean; user: AuthUser }) {
@@ -246,7 +250,7 @@ function SourcesPage({ onUnauthorized, user }: { onUnauthorized: (error: unknown
const [loading, setLoading] = useState(true);
const [error, setError] = useState('');
const [maintenanceBusy, setMaintenanceBusy] = useState(false);
const [historyOpen, setHistoryOpen] = useState(() => new URLSearchParams(window.location.search).get('showImportHistory') === '1');
const [historyOpen] = useState(() => new URLSearchParams(window.location.search).get('showImportHistory') === '1');
const pendingKey = `quality-pending-source-deletions:${user.id}`;
const [pendingDeletion, setPendingDeletion] = useState<PendingSourceDeletion>(() => {
try {
@@ -270,16 +274,20 @@ function SourcesPage({ onUnauthorized, user }: { onUnauthorized: (error: unknown
finally { setLoading(false); }
};
useEffect(() => { void load(); }, []);
useMaintenanceRefresh(load);
return <div className="management-page">
{error && <Alert showIcon type="error" message="来源清单读取失败" description={error} action={<Button size="small" onClick={() => void load()}>重试</Button>} />}
<DataWorkflowHub user={user} sources={sources} currentReleaseId={currentReleaseId} pendingDeletion={pendingDeletion} onDeleted={onDeleted} onBusyChange={setMaintenanceBusy} onUnauthorized={onUnauthorized} onRefresh={() => void load()} />
<SourceFilesPanel user={user} sources={sources} loading={loading} pendingIds={pendingDeletion.ids} maintenanceBusy={maintenanceBusy} onToggleDelete={toggleDelete} onClearDeletes={() => setPendingDeletion({ ids: [], releaseId: null })} onUnauthorized={onUnauthorized} onRefresh={() => void load()} />
<details className="nc-advanced-records" open={historyOpen} onToggle={event => setHistoryOpen(event.currentTarget.open)}><summary>导入记录与失败详情</summary>{historyOpen && <ImportHistoryPanel user={user} currentReleaseId={currentReleaseId} onUnauthorized={onUnauthorized} />}</details>
<details className="nc-advanced-records"><summary>高级来源记录与审计</summary><SourceGovernancePanel user={user} currentReleaseId={currentReleaseId} onUnauthorized={onUnauthorized} /></details>
<ManagementSections initialKey={historyOpen ? 'history' : 'intake'} items={[
{ key: 'intake', label: '数据接入', children: <DataWorkflowHub user={user} sources={sources} currentReleaseId={currentReleaseId} pendingDeletion={pendingDeletion} onDeleted={onDeleted} onBusyChange={setMaintenanceBusy} onUnauthorized={onUnauthorized} onRefresh={() => void load()} /> },
{ key: 'files', label: '来源文件', children: <SourceFilesPanel user={user} sources={sources} loading={loading} pendingIds={pendingDeletion.ids} maintenanceBusy={maintenanceBusy} onToggleDelete={toggleDelete} onClearDeletes={() => setPendingDeletion({ ids: [], releaseId: null })} onUnauthorized={onUnauthorized} onRefresh={() => void load()} /> },
{ key: 'history', label: '导入记录', children: <ImportHistoryPanel user={user} currentReleaseId={currentReleaseId} onUnauthorized={onUnauthorized} /> },
{ key: 'audit', label: '来源审计', children: <SourceGovernancePanel user={user} currentReleaseId={currentReleaseId} onUnauthorized={onUnauthorized} /> },
]} />
</div>;
}
function ImportHistoryPanel({ user, currentReleaseId, onUnauthorized }: { user: AuthUser; currentReleaseId: string | null; onUnauthorized: (error: unknown) => boolean }) {
const { active } = usePageCache();
const [items, setItems] = useState<Record<string, unknown>[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState('');
@@ -289,7 +297,7 @@ function ImportHistoryPanel({ user, currentReleaseId, onUnauthorized }: { user:
finally { setLoading(false); }
}, [onUnauthorized]);
useEffect(() => { void load(); }, [load, currentReleaseId]);
useEffect(() => { if (items.some(item => ['QUEUED', 'RUNNING'].includes(String(item.status).toUpperCase()))) { const timer = window.setTimeout(() => void load(), 2000); return () => window.clearTimeout(timer); } }, [items, load]);
useEffect(() => { if (active && items.some(item => ['QUEUED', 'RUNNING'].includes(String(item.status).toUpperCase()))) { const timer = window.setTimeout(() => void load(), 2000); return () => window.clearTimeout(timer); } }, [items, load, active]);
return <section className="panel section-panel"><div className="resource-toolbar"><strong>导入记录</strong><span>{items.length} 条 · 可查看处理结果和逐行校验问题</span></div>
{error && <Alert showIcon type="error" message="导入记录读取失败" description={error} action={<Button onClick={() => void load()}>重试</Button>} />}
{loading ? <Spin /> : <TaskTable items={items} importOnly user={user} onUnauthorized={onUnauthorized} onRefresh={() => void load()} />}
@@ -360,7 +368,7 @@ function ruleDisplayName(value: unknown): string {
type PublishedConfigNames = Pick<OptionsResponse, 'departments' | 'groups' | 'products' | 'projectGroups'> & { groupDepartments?: Record<string, string | null> };
function ConfigManagementPage({ kind, user, onUnauthorized, onNavigate }: { kind: 'rules' | 'mappings' | 'masterData'; user: AuthUser; onUnauthorized: (error: unknown) => boolean; onNavigate: (page: PageKey) => void }) {
function ConfigManagementPage({ kind, user, onUnauthorized, onNavigate, extraSections }: { extraSections?: { key: string; label: string; children: React.ReactNode }[]; kind: 'rules' | 'mappings' | 'masterData'; user: AuthUser; onUnauthorized: (error: unknown) => boolean; onNavigate: (page: PageKey) => void }) {
const { message } = App.useApp();
const [items, setItems] = useState<Record<string, unknown>[]>([]);
const [loading, setLoading] = useState(true);
@@ -377,14 +385,15 @@ function ConfigManagementPage({ kind, user, onUnauthorized, onNavigate }: { kind
const [regressionBusy, setRegressionBusy] = useState(false);
const [form] = Form.useForm<{ name: string; description: string }>();
const [draftDefinition, setDraftDefinition] = useState<ConfigDefinition>({});
const loadedDefinitionKey=useRef('');
const [ruleSchema, setRuleSchema] = useState<Record<string, unknown> | null>(null);
const [ruleSchemaError, setRuleSchemaError] = useState('');
const [publishedNames, setPublishedNames] = useState<PublishedConfigNames | null>(null);
const [matchMetadata,setMatchMetadata]=useState<Record<string,unknown>|null>(null);
const [differenceItem, setDifferenceItem] = useState<Record<string, unknown> | null>(null);
const [differenceConfirmed, setDifferenceConfirmed] = useState(false);
const [workflowStatus, setWorkflowStatus] = useState<DataWorkflowStatus | null>(null);
const [workflowStatusLoaded, setWorkflowStatusLoaded] = useState(false);
const [simpleTab, setSimpleTab] = useState<'catalog' | 'lookups' | 'matchRules'>('catalog');
const isRules = kind === 'rules';
const title = isRules ? '计算规则' : kind === 'masterData' ? '基础数据' : '对应关系';
const testMode = workflowStatus?.simplified === true && workflowStatus.mode === 'TEST';
@@ -407,7 +416,7 @@ function ConfigManagementPage({ kind, user, onUnauthorized, onNavigate }: { kind
const loadEditorSupport = async () => {
try {
if (isRules) { setRuleSchema(await api.ruleSchema()); setRuleSchemaError(''); }
else setPublishedNames(await api.referenceNames());
else {const [names,metadata]=await Promise.all([api.referenceNames(),api.classificationMetadata()]);setPublishedNames(names);setMatchMetadata(metadata);}
} catch (value) { if (!onUnauthorized(value)) { if (isRules) setRuleSchemaError(apiMessage(value)); else setError(`当前发布名录读取失败:${apiMessage(value)}`); } }
};
const refreshWorkflowStatus = async () => {
@@ -419,7 +428,6 @@ function ConfigManagementPage({ kind, user, onUnauthorized, onNavigate }: { kind
void load(); void refreshWorkflowStatus();
});
useEffect(() => { void load(); }, [kind]);
useEffect(() => { setSimpleTab(kind === 'mappings' ? 'lookups' : 'catalog'); }, [kind]);
useEffect(() => { void loadEditorSupport(); }, [isRules]);
useEffect(() => { void refreshWorkflowStatus(); }, [kind]);
useEffect(() => {
@@ -429,7 +437,8 @@ function ConfigManagementPage({ kind, user, onUnauthorized, onNavigate }: { kind
const storedDefinition = active?.definition ?? fallback?.definition;
const definition = isRules && testMode && String(active?.id ?? fallback?.id) === 'pq-dax-v1' && ruleSchema?.defaultDefinition
? ruleSchema.defaultDefinition : storedDefinition;
if (definition && typeof definition === 'object') setDraftDefinition(JSON.parse(JSON.stringify(definition)) as ConfigDefinition);
const definitionKey=JSON.stringify([kind,active?.id??fallback?.id,definition]);
if (definition && typeof definition === 'object' && loadedDefinitionKey.current!==definitionKey) {loadedDefinitionKey.current=definitionKey;setDraftDefinition(JSON.parse(JSON.stringify(definition)) as ConfigDefinition);}
const metadata = items.find(row => String(row.id) === String(active?.id)) ?? fallback;
form.setFieldsValue({ name: testMode ? currentConfigurationName(active?.name ?? metadata?.name, metadata?.createdAt) : isRules ? ruleDisplayName(active?.name || fallback?.name || '测试计算规则') : String(active?.name || fallback?.name || '测试基础数据与对应关系'), description: isRules ? readableRuleDescription(active?.description || fallback?.description) : String(active?.description || fallback?.description || '') });
}, [workflowStatus, items, kind, isRules, testMode, ruleSchema]);
@@ -558,31 +567,42 @@ function ConfigManagementPage({ kind, user, onUnauthorized, onNavigate }: { kind
}
};
useMaintenanceRefresh(() => { void load(); void refreshWorkflowStatus(); void loadEditorSupport(); });
if (!workflowStatusLoaded) return <div className="management-page"><div className="loading-panel"><Spin />正在读取当前维护模式…</div></div>;
if (testMode) return <div className="management-page nc-simplified-config-page">
<PageHeader title={isRules ? '计算规则' : '基础数据与对应关系'} description={isRules ? '直接编辑当前使用的公式,保存后自动保存配置、重新计算并更新报表。' : '在同一处维护系统名录、Excel 对应关系和条件匹配规则,保存后自动更新报表。'} actions={<Button icon={<ReloadOutlined />} onClick={() => { void load(); void refreshWorkflowStatus(); void loadEditorSupport(); }}>刷新</Button>} />
{error && <Alert showIcon type="error" message="配置清单读取失败" description={error} />}
{isRules && ruleSchemaError && <Alert showIcon type="error" message="规则编辑结构读取失败" description={ruleSchemaError} />}
{!canApplyWorkflow && <Alert showIcon type="info" message="当前账号可查看配置;保存和更新报表需要发布权限。" />}
{workflowOperation.error && <Alert showIcon type="error" message={workflowOperation.operation?.status === 'FAILED' ? '保存或报表更新失败' : '进度读取失败'} description={workflowOperation.error} action={workflowOperation.operation?.operationId ? <Button size="small" onClick={workflowOperation.resume}>重试读取进度</Button> : undefined} />}
{workflowOperation.operation && <section className="nc-operation-status panel"><div><strong>{workflowOperation.busy ? (simpleStageLabel[String(workflowOperation.operation.stage || workflowOperation.operation.status)] || '正在处理') : String(workflowOperation.operation.status).toUpperCase() === 'COMPLETED' ? '报表已更新' : '操作结束'}</strong></div><Progress percent={workflowOperation.busy ? undefined : String(workflowOperation.operation.status).toUpperCase() === 'COMPLETED' ? 100 : 0} status={String(workflowOperation.operation.status).toUpperCase() === 'FAILED' ? 'exception' : String(workflowOperation.operation.status).toUpperCase() === 'COMPLETED' ? 'success' : 'active'} /></section>}
<section className="panel section-panel nc-simple-current-config">
{isRules && <div className="resource-toolbar"><strong>当前计算规则</strong></div>}
<Form form={form} component={isRules ? 'form' : false} layout="vertical">{isRules && <div className="structured-config-form-grid"><Form.Item name="name" label="配置名称"><Input readOnly placeholder="保存时自动生成" /></Form.Item><Form.Item name="description" label="说明(可选)"><Input disabled={!canApplyWorkflow} maxLength={1000} /></Form.Item></div>}</Form>
{isRules ? canApplyWorkflow ? <RuleDefinitionEditor value={draftDefinition} onChange={setDraftDefinition} schema={ruleSchema} schemaError={ruleSchemaError} /> : <RuleContents value={draftDefinition} /> : <>
<Tabs className="nc-simple-config-tabs" activeKey={simpleTab} onChange={key => setSimpleTab(key as typeof simpleTab)} items={[
{ key: 'catalog', label: '名录', children: <><div className="nc-simple-config-intro">{canApplyWorkflow && <MasterWorkbookUpload kind="masterData" base={workflowMappingBase} expectedCurrentReleaseId={workflowStatus?.currentReleaseId ?? null} onUnauthorized={onUnauthorized} onComplete={() => { void load(); void refreshWorkflowStatus(); }} simplified />}</div><MasterDataDefinitionEditor value={draftDefinition} onChange={setDraftDefinition} publishedNames={publishedNames ?? undefined} simplified readOnly={!canApplyWorkflow} /></> },
{ key: 'lookups', label: '基础对应', children: <><div className="nc-simple-config-intro">{canApplyWorkflow && <MasterWorkbookUpload kind="mappings" base={workflowMappingBase} expectedCurrentReleaseId={workflowStatus?.currentReleaseId ?? null} onUnauthorized={onUnauthorized} onComplete={() => { void load(); void refreshWorkflowStatus(); }} simplified />}</div><MappingDefinitionEditor value={draftDefinition} onChange={setDraftDefinition} simplified readOnly={!canApplyWorkflow} /></> },
{ key: 'matchRules', label: '条件匹配', children: <MatchRulesDefinitionEditor value={draftDefinition} onChange={setDraftDefinition} metadata={workflowStatus} readOnly={!canApplyWorkflow} /> },
]} />
{validateStructuredMappingDefinition(draftDefinition, publishedNames?.groupDepartments).length > 0 && <Alert showIcon type="warning" message="有名录引用需要留意" description={<ul className="structured-config-error-list">{validateStructuredMappingDefinition(draftDefinition, publishedNames?.groupDepartments).slice(0, 12).map((issue, index) => <li key={`${issue}:${index}`}>{issue}</li>)}</ul>} />}
</>}
{canApplyWorkflow && <div className="nc-simple-save-row"><span>保存会自动检查配置、保存配置、按当前来源重算并更新报表。</span><Button type="primary" size="large" disabled={workflowOperation.busy || (isRules && !ruleSchema)} loading={workflowOperation.busy} onClick={() => void applyCurrent()}>保存并更新报表</Button></div>}
</section>
if (testMode) {
const notices = <>
{error && <Alert showIcon type="error" message="配置清单读取失败" description={error} />}
{isRules && ruleSchemaError && <Alert showIcon type="error" message="规则编辑结构读取失败" description={ruleSchemaError} />}
{!canApplyWorkflow && <Alert showIcon type="info" message="当前账号可查看配置;保存和更新报表需要发布权限。" />}
{workflowOperation.error && <Alert showIcon type="error" message="保存或报表更新失败" description={workflowOperation.error} action={workflowOperation.operation?.operationId ? <Button size="small" onClick={workflowOperation.resume}>重试读取进度</Button> : undefined} />}
{workflowOperation.operation && <section className="nc-operation-status panel"><div><strong>{simpleStageLabel[String(workflowOperation.operation.stage || workflowOperation.operation.status)] || '正在处理'}</strong></div><Progress percent={workflowOperation.busy ? undefined : String(workflowOperation.operation.status).toUpperCase() === 'COMPLETED' ? 100 : 0} status={String(workflowOperation.operation.status).toUpperCase() === 'FAILED' ? 'exception' : workflowOperation.busy ? 'active' : 'success'} /></section>}
</>;
const editor = (mode: 'catalog' | 'lookups' | 'matchRules' | 'rules') => <div className="management-page nc-simplified-config-page flat-config-page">
{notices}
<section className="panel section-panel flat-config-panel">
<div className="flat-config-toolbar">
<div className="completion-actions"><ConfigurationWorkbookActions value={draftDefinition} onChange={setDraftDefinition} scope={mode==='rules'?'rules':mode==='catalog'?'catalogs':'all'} label={mode==='rules'?'计算规则':mode==='catalog'?'基础名录':'对应关系配置'} defaults={Array.isArray(matchMetadata?.defaultRules)?matchMetadata.defaultRules:[]} readOnly={!canApplyWorkflow} disabled={loading||(isRules?!ruleSchema:!matchMetadata)} validate={isRules?undefined:validateStructuredMappingDefinition} whole /></div>
<Button icon={<ReloadOutlined />} onClick={() => { void load(); void refreshWorkflowStatus(); void loadEditorSupport(); }}>刷新</Button>
</div>
{mode === 'rules' && <Form form={form} layout="vertical"><div className="structured-config-form-grid"><Form.Item name="name" label="配置名称"><Input readOnly /></Form.Item><Form.Item name="description" label="说明(可选)"><Input disabled={!canApplyWorkflow} maxLength={1000} /></Form.Item></div></Form>}
{mode === 'rules' ? canApplyWorkflow ? <RuleDefinitionEditor value={draftDefinition} onChange={setDraftDefinition} schema={ruleSchema} schemaError={ruleSchemaError} showWorkbookActions={false} /> : <RuleContents value={draftDefinition} />
: mode === 'catalog' ? <MasterDataDefinitionEditor value={draftDefinition} onChange={setDraftDefinition} publishedNames={publishedNames ?? undefined} simplified readOnly={!canApplyWorkflow} />
: mode === 'lookups' ? <MappingDefinitionEditor value={draftDefinition} onChange={setDraftDefinition} simplified readOnly={!canApplyWorkflow} />
: <MatchRulesDefinitionEditor value={draftDefinition} onChange={setDraftDefinition} metadata={matchMetadata??workflowStatus} readOnly={!canApplyWorkflow} />}
{!isRules && validateStructuredMappingDefinition(draftDefinition, publishedNames?.groupDepartments).length > 0 && <Alert showIcon type="warning" message="有名录引用需要留意" description={<ul className="structured-config-error-list">{validateStructuredMappingDefinition(draftDefinition, publishedNames?.groupDepartments).slice(0,12).map((issue,index)=><li key={index}>{issue}</li>)}</ul>} />}
{canApplyWorkflow && <div className="nc-simple-save-row"><span>保存后检查配置并更新报表。</span><Button type="primary" size="large" disabled={workflowOperation.busy || (isRules && !ruleSchema)} loading={workflowOperation.busy} onClick={() => void applyCurrent()}>保存并更新报表</Button></div>}
</section>
</div>;
if (kind === 'masterData') return <><Form form={form} component={false} />{editor('catalog')}</>;
if (kind === 'mappings') return <MappingEditorContext.Provider value={{definition:draftDefinition,update:setDraftDefinition,ready:!loading && !!workflowMappingBase?.id && !!matchMetadata,defaultRules:Array.isArray(matchMetadata?.defaultRules)?matchMetadata.defaultRules:[],projectGroups:publishedNames?.projectGroups??[],groups:Array.isArray((draftDefinition.masterData as Record<string,unknown>|undefined)?.groups)?((draftDefinition.masterData as Record<string,unknown>).groups as Record<string,unknown>[]).filter(row=>row.enabled!==false).map(row=>String(row.name)):publishedNames?.groups??[]}}><Form form={form} component={false} /><ManagementSections items={[
{ key: 'lookups', label: '基础对应', children: editor('lookups') },
{ key: 'matchRules', label: '条件匹配', children: editor('matchRules') },
...(extraSections ?? []),
]} /></MappingEditorContext.Provider>;
return editor('rules');
}
</div>;
return <div className="management-page">
const standardContent = <div className="management-page">
<PageHeader title={title} description={isRules ? '维护来源字段、行计算与指标公式;保留原公式,试算后确认差异再启用。' : kind === 'masterData' ? '独立维护产业部、业务分组、产品和项目组名录;与对应关系共同形成配置。' : '维护源字段到业务分组、项目组、产业部和产品的对应关系。'} actions={<>
{canManage && !isRules && <MasterWorkbookUpload kind={kind === 'masterData' ? 'masterData' : 'mappings'} base={items.find(item => String(item.status).toUpperCase() === 'ACTIVE')} onUnauthorized={onUnauthorized} onComplete={() => void load()} />}
{canManage && <Button type={isRules ? 'primary' : 'default'} disabled={loading || (isRules && !ruleSchema) || (kind === 'masterData' && !publishedNames)} onClick={() => openDraft()}>从启用版新建草稿</Button>}
@@ -649,7 +669,7 @@ function ConfigManagementPage({ kind, user, onUnauthorized, onNavigate }: { kind
<Drawer className="management-drawer" open={impactOpen} title={`${title}依赖影响预览`} width={Math.min(980, window.innerWidth)} onClose={() => { setImpactOpen(false); setImpact(null); }}>{impact ? <>
<div className="governance-monitor-grid"><div><small>影响来源</small><strong>{Array.isArray(impact.affectedSources) ? impact.affectedSources.length : toText(impact.affectedSources)}</strong></div><div><small>冻结状态</small><strong>{impact.frozen === true ? '已冻结' : impact.frozen === false ? '未冻结' : '—'}</strong></div><div><small>需要更新报表</small><strong>{impact.requiresNewRelease === true ? '是' : '否'}</strong></div></div>
{impact.status === 'NO_PUBLISHED_RELEASE' && <Alert showIcon type="info" message="暂无可比较的数据" description="服务端没有可用于新旧映射对比的数据;当前没有可计算的映射影响。" />}
<Alert showIcon type="info" message="当前数据保持不变" description={`命中变更键 ${changedKeyRows.length} 项 · 影响来源明细 ${impactHits.length} 项 · 重分组预览 ${regroupingPreview.length} 项 · 成本计算 ${impact.costCalculationEnabled === false ? '未启用' : '以服务端返回状态为准'}`} />
<Alert showIcon type="info" message="当前数据保持不变" description={`命中变更键 ${changedKeyRows.length} 项 · 影响来源明细 ${impactHits.length} 项 · 重分组预览 ${regroupingPreview.length} 项`} />
{isRules ? <><h3>依赖与来源</h3><DynamicTable items={[...(Array.isArray(impact.references) ? impact.references as Record<string, unknown>[] : []), ...(Array.isArray(impact.affectedSources) ? impact.affectedSources as Record<string, unknown>[] : [])]} /></> : <>
<h3>映射键前后值</h3><Table size="small" bordered dataSource={changedKeyRows} rowKey={(row, index) => `${toText(row.mappingType)}:${toText(row.sourceKey)}:${index}`} pagination={false} scroll={{ x: 920 }} columns={[{ title: '映射类型', dataIndex: 'mappingType', key: 'mappingType', width: 150 }, { title: '源键', dataIndex: 'sourceKey', key: 'sourceKey', width: 240 }, { title: '变更前', dataIndex: 'before', key: 'before', width: 200, render: (value, row) => <>{toText(value)}{row.beforeEnabled === false && <Tag>停用</Tag>}</> }, { title: '变更后', dataIndex: 'after', key: 'after', width: 200, render: (value, row) => <>{toText(value)}{row.afterEnabled === false && <Tag>停用</Tag>}</> }, { title: '命中范围', key: 'ranges', render: (_, row) => `前:${toText(row.beforeRanges)} · 后:${toText(row.afterRanges)}` }]} locale={{ emptyText: '服务端未报告映射键变化' }} />
<h3>命中来源与行数</h3><Table size="small" bordered dataSource={impactHits} rowKey={(row, index) => `${toText(row.sourceDocumentId)}:${toText(row.sourceKey)}:${index}`} pagination={false} scroll={{ x: 920 }} columns={[{ title: '来源槽位', dataIndex: 'sourceSlot', key: 'sourceSlot', width: 180 }, { title: '期间', dataIndex: 'month', key: 'month', width: 110 }, { title: '阶段', dataIndex: 'stage', key: 'stage', width: 90, render: value => value === 'black' ? '黑片' : value === 'bright' ? '亮片' : toText(value) }, { title: '源键', dataIndex: 'sourceKey', key: 'sourceKey', width: 220 }, { title: '命中行数', dataIndex: 'changedRows', key: 'changedRows', align: 'right', width: 110 }, { title: '源行数', dataIndex: 'rowCount', key: 'rowCount', align: 'right', width: 110 }, { title: '总重量', dataIndex: 'totalWeight', key: 'totalWeight', align: 'right', width: 120 }]} locale={{ emptyText: '无来源命中记录' }} />
@@ -658,141 +678,121 @@ function ConfigManagementPage({ kind, user, onUnauthorized, onNavigate }: { kind
<details className="completion-technical-detail"><summary>影响预览技术字段</summary><pre className="json-result">{publicJson(impact)}</pre></details>
</> : <div className="completion-loading"><Spin />由服务端重新核对配置依赖…</div>}</Drawer>
</div>;
return extraSections ? <MappingEditorContext.Provider value={{definition:draftDefinition,ready:!loading && !!matchMetadata,defaultRules:Array.isArray(matchMetadata?.defaultRules)?matchMetadata.defaultRules:[],groups:publishedNames?.groups??[],projectGroups:publishedNames?.projectGroups??[],update:definition=>{void api.createMapping({name:`分类规则 ${configurationDateTime(new Date())}`,definition}).then(raw=>{const result=raw as Record<string,unknown>;setDraftDefinition(definition);setDrafting(result);form.setFieldsValue({name:String(result.name),description:''});void load();}).catch(value=>{if(!onUnauthorized(value))message.error(apiMessage(value));});}}}><ManagementSections items={[{ key: 'configuration', label: '对应关系', children: standardContent }, ...extraSections]} /></MappingEditorContext.Provider> : standardContent;
}
function MappingIssueResolutionPanel({ user, onUnauthorized }: { user: AuthUser; onUnauthorized: (error: unknown) => boolean }) {
const { message } = App.useApp();
function MappingMaintenancePage({ user, onUnauthorized, onNavigate }: Omit<ManagementPageProps, 'page'>) {
const canEdit=user.roles.some(role=>['SYSTEM_ADMIN','DATA_MAINTAINER','RULE_MAINTAINER'].includes(role));
return <ConfigManagementPage kind="mappings" user={user} onUnauthorized={onUnauthorized} onNavigate={onNavigate} extraSections={[
{key:'groups',label:'业务分组未分类',children:<ClassificationRuleEditor onUnauthorized={onUnauthorized}>{edit=><UnclassifiedSourcesPanel onUnauthorized={onUnauthorized} onEdit={canEdit?edit:undefined}/>}</ClassificationRuleEditor>},
{key:'projects',label:'项目组未分类',children:<ClassificationRuleEditor onUnauthorized={onUnauthorized}>{edit=><MappingIssueResolutionPanel onUnauthorized={onUnauthorized} onEdit={canEdit?edit:undefined}/>}</ClassificationRuleEditor>},
]}/>;
}
function UnclassifiedSourcesPanel({ onUnauthorized, onEdit }: { onUnauthorized: (error: unknown) => boolean; onEdit?: (selection: ClassificationSelection) => void }) {
const [items, setItems] = useState<Record<string, unknown>[]>([]);
const [total, setTotal] = useState(0);
const [page, setPage] = useState(1);
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
const { refreshKey } = usePageCache();
const pages = useRef(new Map<string, Promise<ReturnType<typeof unwrapItems>>>());
const loadRevision = useRef(0);
const load = async (force = false) => {
const revision = ++loadRevision.current;
const key = `${refreshKey}:${page}`;
if (force) pages.current.clear();
setLoading(true);
let request: Promise<ReturnType<typeof unwrapItems>> | undefined;
try {
request = pages.current.get(key) ?? api.unclassifiedSources(page, TABLE_PAGE_SIZE).then(unwrapItems);
pages.current.set(key, request);
if (pages.current.size > 12) pages.current.delete(pages.current.keys().next().value!);
const result = await request;
if (revision !== loadRevision.current) return;
setItems(result.items); setTotal(Number(result.total ?? result.items.length)); setError('');
} catch (value) { if (pages.current.get(key) === request) pages.current.delete(key); if (revision === loadRevision.current && !onUnauthorized(value)) setError(apiMessage(value)); }
finally { if (revision === loadRevision.current) setLoading(false); }
};
useEffect(() => { void load(); }, [page]);
const columns: ColumnsType<Record<string, unknown>> = [
{ title: '阶段', dataIndex: 'stage', width: 85, render: value => value === 'black' ? '黑片' : '亮片' },
{ title: '产业部', dataIndex: 'department', width: 140, render: toText },
{ title: '产品', dataIndex: 'product', width: 180, render: toText },
{ title: 'PDT代码', dataIndex: 'pdtCode', width: 90, render: toText },
{ title: '销售区域', dataIndex: 'salesRegion', width: 110, render: toText },
{ title: '原因', key: 'reason', width: 120, render: () => '业务分组未分类' },
{ title: '期间', dataIndex: 'periods', width: 180 },
{ title: '行数', dataIndex: 'rowCount', width: 75, align: 'right' },
{ title: '总重量 / kg', dataIndex: 'totalWeight', width: 140, align: 'right' },
{ title: '操作', key: 'action', width: 125, fixed: 'right', render: (_, row) => <Button type="link" disabled={!onEdit} onClick={() => onEdit?.({ kind: 'group', row })}>编辑分类</Button> },
];
useMaintenanceRefresh(() => { void load(true); });
return <div className="management-page">
{error && <Alert type="error" showIcon message={error} />}
<section className="panel section-panel">
<div className="resource-toolbar"><strong>业务分组未分类汇总</strong><div className="completion-actions"><span>共 {total} 条</span><Button icon={<ReloadOutlined />} onClick={() => void load(true)}>刷新</Button></div></div>
<div className="resource-table"><Table size="small" bordered loading={loading} columns={columns} dataSource={items} rowKey={row => JSON.stringify([row.stage, row.department, row.product, row.sourceKey])} pagination={false} indexOffset={(page - 1) * TABLE_PAGE_SIZE} scroll={{ x: 1280 }} /></div>
{total > TABLE_PAGE_SIZE && <Pagination className="records-pagination" current={page} pageSize={TABLE_PAGE_SIZE} total={total} onChange={setPage} />}
</section>
</div>;
}
function MappingIssueResolutionPanel({ onUnauthorized, onEdit }: { onUnauthorized: (error: unknown) => boolean; onEdit?: (selection: ClassificationSelection) => void }) {
const [items, setItems] = useState<Record<string, unknown>[]>([]);
const [total, setTotal] = useState(0);
const [page, setPage] = useState(1);
const [status, setStatus] = useState('OPEN');
const [customerCode, setCustomerCode] = useState<string>();
const [businessGroup, setBusinessGroup] = useState<string>();
const [department, setDepartment] = useState<string>();
const [filterOptions, setFilterOptions] = useState<{ customerCodes: string[]; businessGroups: string[]; departments: string[] }>({ customerCodes: [], businessGroups: [], departments: [] });
const [loading, setLoading] = useState(true);
const [busy, setBusy] = useState(false);
const loadRevision = useRef(0);
const [error, setError] = useState('');
const [currentReleaseId, setCurrentReleaseId] = useState<string | null>(null);
const [activeMappings, setActiveMappings] = useState<Record<string, unknown>[]>([]);
const [selectedId, setSelectedId] = useState('');
const [detail, setDetail] = useState<Record<string, unknown> | null>(null);
const [resolutionOpen, setResolutionOpen] = useState(false);
const [resolution, setResolution] = useState<'ADD_MAPPING' | 'KEEP_ORIGINAL' | 'RETURN_SOURCE'>('ADD_MAPPING');
const [form] = Form.useForm<{ decision: 'ADD_MAPPING' | 'KEEP_ORIGINAL' | 'RETURN_SOURCE'; mappedValue?: string; reason: string; baseMappingVersionId?: string }>();
const canResolve = user.roles.some(role => ['SYSTEM_ADMIN', 'RULE_MAINTAINER', 'DATA_MAINTAINER'].includes(role));
const load = async () => {
const { refreshKey } = usePageCache();
const pages = useRef(new Map<string, ReturnType<typeof api.mappingIssues>>());
const load = async (force = false) => {
const revision = ++loadRevision.current;
const key = JSON.stringify([refreshKey, page, status, customerCode, businessGroup, department]);
if (force) pages.current.clear();
setLoading(true);
let request: ReturnType<typeof api.mappingIssues> | undefined;
try {
const result = await api.mappingIssues({ status: status || undefined, page, pageSize: TABLE_PAGE_SIZE });
request = pages.current.get(key) ?? api.mappingIssues({ status: status || undefined, page, pageSize: TABLE_PAGE_SIZE, customerCode, businessGroup, department, mappingType: 'PROJECT_GROUP' });
pages.current.set(key, request);
if (pages.current.size > 12) pages.current.delete(pages.current.keys().next().value!);
const result = await request;
if (revision !== loadRevision.current) return;
setFilterOptions(result.filterOptions ?? { customerCodes: [], businessGroups: [], departments: [] });
const normalized = unwrapItems(result);
setItems(normalized.items); setTotal(Number(normalized.total || normalized.items.length)); setError('');
} catch (value) { if (!onUnauthorized(value)) setError(apiMessage(value)); }
finally { setLoading(false); }
} catch (value) { if (pages.current.get(key) === request) pages.current.delete(key); if (revision === loadRevision.current && !onUnauthorized(value)) setError(apiMessage(value)); }
finally { if (revision === loadRevision.current) setLoading(false); }
};
useEffect(() => { void load(); }, [page, status]);
useEffect(() => {
let alive = true;
Promise.allSettled([api.releases(), api.mappings()]).then(([releaseResult, mappingResult]) => {
if (!alive) return;
if (releaseResult.status === 'fulfilled') setCurrentReleaseId(releaseResult.value.currentReleaseId ?? null);
if (mappingResult.status === 'fulfilled') setActiveMappings(unwrapItems(mappingResult.value).items.filter(item => String(item.status).toUpperCase() === 'ACTIVE'));
});
return () => { alive = false; };
}, []);
const openIssue = async (id: string) => {
setSelectedId(id); setDetail(null); setError('');
try { setDetail(await api.mappingIssue(id)); }
catch (value) { if (!onUnauthorized(value)) setError(apiMessage(value)); }
};
const openResolution = (issue: Record<string, unknown>) => {
const decision = String(issue.status).toUpperCase() === 'OPEN' ? 'ADD_MAPPING' : 'KEEP_ORIGINAL';
setResolution(decision);
form.setFieldsValue({ decision, mappedValue: '', reason: '', baseMappingVersionId: undefined });
setResolutionOpen(true);
};
const resolve = async () => {
const issue = detail?.issue && typeof detail.issue === 'object' ? detail.issue as Record<string, unknown> : {};
if (!issue.id) return;
try {
const values = await form.validateFields();
setBusy(true);
const result = await api.resolveMappingIssue(String(issue.id), {
expectedRowVersion: Number(issue.rowVersion || 0),
decision: values.decision,
...(values.mappedValue?.trim() ? { mappedValue: values.mappedValue.trim() } : {}),
...(values.baseMappingVersionId ? { baseMappingVersionId: values.baseMappingVersionId } : {}),
reason: values.reason.trim(),
});
setDetail(result); setResolutionOpen(false); message.success('映射问题裁决已记录;发布数据尚未改变。'); await load();
} catch (value) {
if (value && typeof value === 'object' && 'errorFields' in value) return;
if (!onUnauthorized(value)) { setError(apiMessage(value)); message.error(apiMessage(value)); }
if (value instanceof ApiError && value.status === 409) { await load(); if (selectedId) await openIssue(selectedId); }
} finally { setBusy(false); }
};
const revalidate = async (decision: Record<string, unknown>) => {
const issue = detail?.issue && typeof detail.issue === 'object' ? detail.issue as Record<string, unknown> : {};
const decisionId = String(decision.id || '');
if (!issue.id || !decisionId || !currentReleaseId) return;
setBusy(true);
try {
const latestMappings = unwrapItems(await api.mappings()).items;
setActiveMappings(latestMappings.filter(mapping => String(mapping.status).toUpperCase() === 'ACTIVE'));
if (!latestMappings.some(mapping => String(mapping.id) === String(decision.newMappingVersionId) && String(mapping.status).toUpperCase() === 'ACTIVE')) {
throw new Error('对应映射尚未启用;请先在上方映射清单完成校验和启用。');
}
const result = await api.revalidateMappingIssue(String(issue.id), { decisionId, expectedCurrentReleaseId: currentReleaseId, idempotencyKey: crypto.randomUUID() });
setDetail(result); message.success('已重新校验候选数据;请查看运行状态和检查结果。'); await load();
} catch (value) {
if (!onUnauthorized(value)) { setError(apiMessage(value)); message.error(apiMessage(value)); }
if (value instanceof ApiError && value.status === 409) { await load(); if (selectedId) await openIssue(selectedId); }
} finally { setBusy(false); }
};
useEffect(() => { void load(); }, [page, status, customerCode, businessGroup, department]);
const columns: ColumnsType<Record<string, unknown>> = [
{ title: '源键', dataIndex: 'sourceKey', key: 'sourceKey', fixed: 'left', width: 250 },
{ title: '映射类型', dataIndex: 'mappingType', key: 'mappingType', width: 170 },
{ title: '客户代码', dataIndex: 'customerCode', key: 'customerCode', fixed: 'left', width: 120, render: toText },
{ title: '业务分组', dataIndex: 'businessGroup', key: 'businessGroup', width: 190, render: toText },
{ title: '产业部', dataIndex: 'departments', key: 'departments', width: 190, render: value => Array.isArray(value) && value.length ? value.join('、') : '—' },
{ title: '映射类型', dataIndex: 'mappingType', key: 'mappingType', width: 120, render: value => ({ PROJECT_GROUP: '项目组', CATEGORY: '业务分组', DEPARTMENT: '产业部', PRODUCT: '产品' }[String(value)] ?? toText(value)) },
{ title: '涉及行数', dataIndex: 'rowCount', key: 'rowCount', align: 'right', width: 110 },
{ title: '涉及总重量', dataIndex: 'totalWeight', key: 'totalWeight', align: 'right', width: 130 },
{ title: '问题期间', key: 'periods', width: 170, render: (_, row) => { const details = row.details && typeof row.details === 'object' ? row.details as Record<string, unknown> : {}; return Array.isArray(details.periods) ? (details.periods as unknown[]).map(toText).join('、') : '—'; } },
{ title: '状态', dataIndex: 'status', key: 'status', width: 120, render: value => <Tag color={value === 'OPEN' ? 'orange' : value === 'REVALIDATED' ? 'green' : 'blue'}>{toText(value)}</Tag> },
{ title: '操作', key: 'action', width: 120, fixed: 'right', render: (_, row) => <Button type="link" onClick={() => void openIssue(String(row.id || ''))}>查看与处理</Button> },
{ title: '操作', key: 'action', width: 120, fixed: 'right', render: (_, row) => <Button type="link" disabled={!onEdit} onClick={() => onEdit?.({ kind: 'projectGroup', row })}>编辑分类</Button> },
];
const issue = detail?.issue && typeof detail.issue === 'object' ? detail.issue as Record<string, unknown> : {};
const details = issue.details && typeof issue.details === 'object' ? issue.details as Record<string, unknown> : {};
const decisions = Array.isArray(detail?.decisions) ? detail.decisions.filter(item => item && typeof item === 'object') as Record<string, unknown>[] : [];
useMaintenanceRefresh(() => { void load(true); });
return <div className="management-page">
<PageHeader title="未命中映射问题" description="按源键汇总真实未命中行;裁决会创建独立映射草稿,不直接修改已发布事实。" actions={<div className="completion-actions"><Select aria-label="映射问题状态" value={status} options={[{ label: '待处理', value: 'OPEN' }, { label: '已裁决', value: 'DECIDED' }, { label: '已退回', value: 'RETURNED' }, { label: '已重验', value: 'REVALIDATED' }, { label: '全部状态', value: '' }]} onChange={value => { setStatus(value); setPage(1); }} /><Button icon={<ReloadOutlined />} onClick={() => void load()}>刷新</Button></div>} />
{error && <Alert showIcon type="error" message="映射问题操作失败" description={error} />}
{!canResolve && <Alert showIcon type="info" message="当前账号仅可查看" description="问题裁决需映射维护、数据维护或系统管理授权。" />}
<section className="panel section-panel"><div className="resource-toolbar"><strong>源键命中汇总</strong><span>共 {total} 条</span></div>{loading ? <div className="loading-panel"><Spin />正在读取未命中问题…</div> : items.length ? <div className="resource-table"><Table size="small" bordered columns={columns} dataSource={items} rowKey={row => String(row.id)} pagination={false} indexOffset={(page - 1) * TABLE_PAGE_SIZE} scroll={{ x: 1050 }} /></div> : <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="当前状态没有未命中映射问题" />}{total > TABLE_PAGE_SIZE && <Pagination className="records-pagination" current={page} pageSize={TABLE_PAGE_SIZE} total={total} showTotal={count => `共 ${count} 条问题`} onChange={setPage} />}</section>
<Drawer className="management-drawer" open={Boolean(selectedId)} title="未命中映射问题处理" width={Math.min(1000, window.innerWidth)} onClose={() => { setSelectedId(''); setDetail(null); }}>
{!detail ? <div className="completion-loading"><Spin />读取源键命中与历史裁决…</div> : <>
<div className="governance-monitor-grid"><div><small>源键</small><strong>{toText(issue.sourceKey)}</strong></div><div><small>映射类型</small><strong>{toText(issue.mappingType)}</strong></div><div><small>汇总行数</small><strong>{toText(issue.rowCount)}</strong></div><div><small>总重量</small><strong>{toText(issue.totalWeight)}</strong></div></div>
<p>状态 {toText(issue.status)}</p>
<h3>涉及来源与期间</h3><DynamicTable items={[
...(Array.isArray(details.sourceCandidateIds) ? (details.sourceCandidateIds as unknown[]).map(value => ({ kind: '待接入候选', id: value })) : []),
...(Array.isArray(details.sourceDocumentIds) ? (details.sourceDocumentIds as unknown[]).map(value => ({ kind: '已归档来源', id: value })) : []),
...(Array.isArray(details.periods) ? (details.periods as unknown[]).map(value => ({ kind: '期间', id: value })) : []),
]} />
<h3>可证明的原始分类值</h3><DynamicTable items={Array.isArray(details.originalValues) ? (details.originalValues as unknown[]).map((value, index) => value && typeof value === 'object' ? { index, ...(value as Record<string, unknown>) } : { index, value }) : []} />
{canResolve && ['OPEN', 'DECIDED'].includes(String(issue.status || '').toUpperCase()) && <Button type="primary" onClick={() => openResolution(issue)}>记录裁决</Button>}
<h3>裁决与重验历史</h3>{decisions.length ? <Table size="small" bordered dataSource={decisions} rowKey={row => String(row.id)} pagination={false} scroll={{ x: 900 }} columns={[
{ title: '裁决', dataIndex: 'decision', key: 'decision', width: 150 }, { title: '理由', dataIndex: 'reason', key: 'reason', width: 260 }, { title: '处理人', dataIndex: 'actorUserId', key: 'actor', width: 150 }, { title: '重验运行', dataIndex: 'newImportRunId', key: 'run', width: 190 },
{ title: '操作', key: 'action', width: 140, render: (_, row) => { const canRun = (row.decision === 'ADD_MAPPING' || row.decision === 'KEEP_ORIGINAL') && Boolean(row.newMappingVersionId); return canRun ? <Button type="link" disabled={!canResolve || !currentReleaseId} loading={busy} title={!currentReleaseId ? '当前还没有报表数据' : '执行前重新读取映射状态,必须已启用'} onClick={() => void revalidate(row)}>重验并构建</Button> : null; } },
]} /> : <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="尚无裁决记录" />}
<p className="completion-note">新映射配置需先在本页上方“映射与成本配置”中完成编辑、校验和启用;重验会用全部来源创建新候选并重新检查源键是否解决,不会直接发布。</p>
</>}
</Drawer>
<Modal className="management-modal" open={resolutionOpen} title="保存映射问题裁决" okText="记录裁决" confirmLoading={busy} onCancel={() => setResolutionOpen(false)} onOk={() => void resolve()}>
<Form form={form} layout="vertical" onValuesChange={changed => { if (changed.decision) setResolution(changed.decision as typeof resolution); }}>
<Form.Item name="decision" label="裁决方式" rules={[{ required: true }]}><Select options={[{ label: '新增映射', value: 'ADD_MAPPING' }, { label: '保留原分类(需原值可证明)', value: 'KEEP_ORIGINAL' }, { label: '退回来源', value: 'RETURN_SOURCE' }]} /></Form.Item>
{resolution === 'ADD_MAPPING' && <Form.Item name="mappedValue" label="映射目标分类" rules={[{ required: true, whitespace: true, message: '新增映射请填写目标分类' }]}><Input maxLength={160} /></Form.Item>}
{resolution !== 'RETURN_SOURCE' && <Form.Item name="baseMappingVersionId" label="映射配置(可选)"><Select {...readableSelectPopup} allowClear options={activeMappings.map(item => ({ label: `${toText(item.name)}`, value: String(item.id) }))} placeholder="留空由服务端使用当前映射配置" /></Form.Item>}
<Form.Item name="reason" label="裁决理由" rules={[{ required: true, whitespace: true, message: '请说明裁决依据' }]}><Input.TextArea rows={3} maxLength={500} showCount /></Form.Item>
</Form>
{resolution === 'KEEP_ORIGINAL' && <Alert showIcon type="warning" message="原分类值验证" description="服务端要求该源键存在唯一可证明的原分类值;不可证明时将拒绝裁决。" />}
</Modal>
<section className="panel section-panel"><div className="resource-toolbar"><strong>项目组未分类汇总</strong><div className="completion-actions"><span>共 {total} 条</span><div className="completion-actions"><Select aria-label="映射问题状态" value={status} options={[{ label: '待处理', value: 'OPEN' }, { label: '已裁决', value: 'DECIDED' }, { label: '已退回', value: 'RETURNED' }, { label: '已重验', value: 'REVALIDATED' }, { label: '全部状态', value: '' }]} onChange={value => { setStatus(value); setPage(1); }} /><Button icon={<ReloadOutlined />} onClick={() => void load(true)}>刷新</Button></div></div></div>
<div className="resource-toolbar mapping-issue-filters">
<Select {...readableSelectPopup} showSearch allowClear aria-label="筛选客户代码" placeholder="客户代码" value={customerCode} options={filterOptions.customerCodes.map(value => ({ label: value, value }))} onChange={value => { setCustomerCode(value); setPage(1); }} />
<Select {...readableSelectPopup} showSearch allowClear aria-label="筛选业务分组" placeholder="业务分组" value={businessGroup} options={filterOptions.businessGroups.map(value => ({ label: value, value }))} onChange={value => { setBusinessGroup(value); setPage(1); }} />
<Select {...readableSelectPopup} showSearch allowClear aria-label="筛选产业部" placeholder="产业部" value={department} options={filterOptions.departments.map(value => ({ label: value, value }))} onChange={value => { setDepartment(value); setPage(1); }} />
<Button onClick={() => { setCustomerCode(undefined); setBusinessGroup(undefined); setDepartment(undefined); setPage(1); }}>重置</Button>
</div>{loading ? <div className="loading-panel"><Spin />正在读取未命中问题…</div> : items.length ? <div className="resource-table"><Table size="small" bordered columns={columns} dataSource={items} rowKey={row => String(row.id)} pagination={false} indexOffset={(page - 1) * TABLE_PAGE_SIZE} scroll={{ x: 1320 }} /></div> : <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="当前状态没有项目组未分类记录" />}{total > TABLE_PAGE_SIZE && <Pagination className="records-pagination" current={page} pageSize={TABLE_PAGE_SIZE} total={total} showTotal={count => `共 ${count} 条问题`} onChange={setPage} />}</section>
</div>;
}
@@ -856,6 +856,7 @@ function MetricDictionaryPanel({ user, onUnauthorized }: { user: AuthUser; onUna
{ title: '状态', dataIndex: 'enabled', key: 'enabled', width: 70, render: value => <Tag color={value === true ? 'green' : 'default'}>{value === true ? '启用' : value === false ? '停用' : '—'}</Tag> },
{ title: '操作', key: 'actions', fixed: 'right', width: 100, render: (_, row) => <span className="task-actions"><Button type="link" onClick={() => setInspecting(row)}>查看</Button>{canManage && <Button type="link" onClick={() => openEditor(row)}>编辑术语</Button>}</span> },
];
useMaintenanceRefresh(load);
return <section className="panel section-panel completion-tools">
<div className="completion-panel-heading"><div><strong>指标术语字典</strong><span>维护展示名称与别名;计算公式在上方计算规则中维护</span></div><Button icon={<ReloadOutlined />} onClick={() => void load()}>刷新字典</Button></div>
{dictionaryError && <Alert showIcon type="error" message="术语字典读取失败" description={dictionaryError} />}
@@ -1773,7 +1774,7 @@ function AccessPage({ user, onUnauthorized }: { user: AuthUser; onUnauthorized:
const canReadAudit = user.roles.some(role => ['SYSTEM_ADMIN', 'AUDIT_VIEWER'].includes(role));
const canViewServiceIdentities = user.roles.some(role => ['SYSTEM_ADMIN', 'SYSTEM_OPERATOR', 'AUDIT_VIEWER'].includes(role));
const defaultTab = canManageUsers ? 'users' : canReadAudit ? 'audit' : canViewServiceIdentities ? 'services' : '';
const [activeTab, setActiveTab] = useState(defaultTab);
const [activeTab] = useState(defaultTab);
const tabs = [
...(canManageUsers ? [{ key: 'users', label: '用户', children: <UsersResourceTab user={user} onUnauthorized={onUnauthorized} /> }] : []),
...(canManageUsers ? [{ key: 'policy', label: '明细与证据范围', children: <UserAccessPolicyPanel onUnauthorized={onUnauthorized} /> }] : []),
@@ -1782,8 +1783,8 @@ function AccessPage({ user, onUnauthorized }: { user: AuthUser; onUnauthorized:
];
return <div className="management-page">
<PageHeader title="权限与审计" />
<section className="panel access-summary"><div><span>当前账号</span><strong>{user.displayName || user.username}</strong>{user.displayName && user.displayName !== user.username && <small>{user.username}</small>}</div><div><span>角色</span><strong>{user.roles.length} 项</strong><small className="role-list role-summary-list">{user.roles.length ? user.roles.map((role, index) => <span key={`${role}-${index}`}>{shownRole(role)}</span>) : '服务端未返回角色'}</small></div><div><span>授权产业部</span><strong>{user.departments.length ? user.departments.map(name => name === '质量管理部' ? '质量中心' : name).join('、') : '未返回范围'}</strong></div><div><span>授权分组</span><strong>{user.groups.length ? user.groups.join('、') : user.roles.includes('SYSTEM_ADMIN') ? '未限制' : '未返回范围'}</strong></div></section>
{tabs.length > 0 ? <section className="panel section-panel"><Tabs activeKey={activeTab} onChange={setActiveTab} items={tabs} /></section> : <Alert showIcon type="info" message="当前账号没有用户管理或审计记录读取权限" />}
{tabs.length > 0 ? <ManagementSections initialKey={activeTab} items={[...tabs, { key: 'summary', label: '当前授权', children: <section className="panel access-summary"><div><span>当前账号</span><strong>{user.displayName || user.username}</strong>{user.displayName && user.displayName !== user.username && <small>{user.username}</small>}</div><div><span>角色</span><strong>{user.roles.length} 项</strong><small className="role-list role-summary-list">{user.roles.length ? user.roles.map((role, index) => <span key={`${role}-${index}`}>{shownRole(role)}</span>) : '服务端未返回角色'}</small></div><div><span>授权产业部</span><strong>{user.departments.length ? user.departments.map(name => name === '质量管理部' ? '质量中心' : name).join('、') : '未返回范围'}</strong></div><div><span>授权分组</span><strong>{user.groups.length ? user.groups.join('、') : user.roles.includes('SYSTEM_ADMIN') ? '未限制' : '未返回范围'}</strong></div></section> }]} /> : <Alert showIcon type="info" message="当前账号没有用户管理或审计记录读取权限" />}
</div>;
}
+10
View File
@@ -0,0 +1,10 @@
import { Tabs } from 'antd';
import { useState, type ReactNode } from 'react';
import { PageCacheContext, usePageCache } from './pageCache';
export function ManagementSections({ items, initialKey, onSelect }: { items: { key: string; label: ReactNode; children: ReactNode }[]; initialKey?: string; onSelect?: (key: string) => void }) {
const cache = usePageCache();
const [selected, setSelected] = useState(initialKey ?? items[0]?.key);
const activeKey = items.some(item => item.key === selected) ? selected : items[0]?.key;
return <Tabs className="management-sections" activeKey={activeKey} destroyOnHidden={false} onChange={key => { setSelected(key); onSelect?.(key); window.scrollTo({ top: 0 }); }} items={items.map(item => ({ ...item, children: <PageCacheContext.Provider value={{ ...cache, active: cache.active && item.key === activeKey }}>{item.children}</PageCacheContext.Provider> }))} />;
}
+10
View File
@@ -0,0 +1,10 @@
import { createContext } from 'react';
export type MappingEditorState = {
definition: Record<string, unknown>;
update: (definition: Record<string, unknown>) => void;
ready: boolean;
defaultRules: unknown[];
groups: string[];
projectGroups: string[];
};
export const MappingEditorContext = createContext<MappingEditorState | null>(null);
+5 -5
View File
@@ -23,7 +23,7 @@ const matchFields = (fields: DataField[], headers: string[]) => Object.fromEntri
const statusLabel: Record<string, string> = { QUEUED: '等待处理', RUNNING: '正在处理', COMPLETED: '校验完成', FAILED: '处理失败', CANCELLED: '已取消', CANDIDATE_READY: '候选已生成', AUTO_PASSED: '对账通过', CONFIRMED: '差异已确认', REVIEW_REQUIRED: '差异待确认', BLOCKED: '存在阻断问题' };
function SampleTable({ headers, rows }: { headers: string[]; rows: Row[] }) {
return <Table size="small" bordered dataSource={rows.map((row, index) => ({ ...row, __previewKey: index }))} rowKey="__previewKey" pagination={{ pageSize: 20, showSizeChanger: false }} scroll={{ x: 'max-content' }} columns={headers.map(header => ({ title: header, dataIndex: header, key: header, width: 150, render: txt }))} locale={{ emptyText: '当前工作表没有可预览数据' }} />;
return <Table size="small" bordered dataSource={rows.map((row, index) => ({ ...row, __previewKey: index }))} rowKey="__previewKey" pagination={{ pageSize: 15, showSizeChanger: false }} scroll={{ x: 'max-content' }} columns={headers.map(header => ({ title: header, dataIndex: header, key: header, width: 150, render: txt }))} locale={{ emptyText: '当前工作表没有可预览数据' }} />;
}
function ColumnMatching({ fields, headers, value, onChange, disabled = false }: { fields: DataField[]; headers: string[]; value: Record<string, string>; onChange: (value: Record<string, string>) => void; disabled?: boolean }) {
@@ -264,7 +264,7 @@ export function SourceFilesPanel({ user, sources, loading, pendingIds, maintenan
{simplified && pendingIds.length > 0 && <div className="nc-source-maintenance-note"><Button disabled={maintenanceBusy} onClick={onClearDeletes}>撤销全部待删除({pendingIds.length})</Button></div>}
{deletedError && <Alert showIcon type="error" message="已删除来源读取失败" description={deletedError} action={<Button size="small" onClick={() => void loadDeleted()}>重试</Button>} />}
{sourceOperation.operation && <WorkflowOperationFeedback operation={sourceOperation.operation} error={sourceOperation.error} busy={sourceOperation.busy} onResume={sourceOperation.resume} />}
<Table<DeletionSource> size="small" bordered loading={showDeleted ? deletedLoading : loading} dataSource={showDeleted ? deleted.map(item => item.source) : sources} rowKey="id" pagination={{ pageSize: 12, showSizeChanger: false }} scroll={{ x: 980 }} columns={[
<Table<DeletionSource> size="small" bordered loading={showDeleted ? deletedLoading : loading} dataSource={showDeleted ? deleted.map(item => item.source) : sources} rowKey="id" pagination={{ pageSize: 15, showSizeChanger: false }} scroll={{ x: 980 }} columns={[
{ title: '来源文件', dataIndex: 'sourceFile', key: 'sourceFile', width: 340, ellipsis: true },
{ title: '阶段', dataIndex: 'stage', key: 'stage', align: 'center', width: 90, render: value => <Tag>{stageName(value)}</Tag> },
{ title: '统计期间', key: 'period', align: 'center', width: 140, render: (_, row) => period(row) },
@@ -463,8 +463,8 @@ function DataUploadWizard({ user, mode, purpose, handoffSourceId, initialReason,
{step === 1 && <Form layout="vertical"><div className="nc-form-grid">{mode === 'REPLACE' ? <Form.Item className="nc-full-width" label="选择当前数据中的替换目标" required><Select {...readableSelectPopup} aria-label="选择当前数据中的替换目标" showSearch optionFilterProp="label" value={targetId || undefined} placeholder="选择年月、阶段和原文件" options={options.targets.map(item => ({ value: item.documentId, label: `${item.year}年${item.month}月 · ${stageName(item.stage)} · ${item.sourceFile}` }))} onChange={chooseTarget} /></Form.Item> : <><Form.Item label="数据阶段" required><Select aria-label="数据阶段" value={stage} options={[{ value: 'black', label: '黑片' }, { value: 'bright', label: '亮片' }]} onChange={value => { setStage(value); setFieldMap(matchFields(options.fields[value] || [], headers)); }} /></Form.Item><Form.Item label="统计年份" required><InputNumber aria-label="统计年份" min={2000} max={2100} value={year} onChange={value => { if (value) setYear(value); }} /></Form.Item><Form.Item label="统计月份" required><Select aria-label="统计月份" value={month} options={Array.from({ length: 12 }, (_, index) => ({ value: index + 1, label: `${index + 1}月` }))} onChange={setMonth} /></Form.Item>{options.scopes.length > 1 && <Form.Item label="数据范围" required><Select aria-label="数据范围" value={scopeCode} options={options.scopes.map(value => ({ value, label: value === 'ALL' ? '全部范围' : value }))} onChange={setScopeCode} /></Form.Item>}</>}</div>{target && <div className="nc-replacement"><span>原文件</span><strong>{target.sourceFile}</strong><ArrowRightOutlined /><span>新文件</span><strong>{workbook?.sourceFile}</strong></div>}<Form.Item label={mode === 'REPLACE' ? '替换理由' : '本次说明'} required={mode === 'REPLACE'}><Input.TextArea aria-label={mode === 'REPLACE' ? '替换理由' : '本次说明'} value={reason} rows={2} maxLength={1000} placeholder={mode === 'REPLACE' ? '例如:补齐遗漏记录,使用更新后的完整月表' : '可填写本次数据说明'} onChange={event => setReason(event.target.value)} /></Form.Item></Form>}
{step === 2 && workbook && <><div className="nc-form-grid"><Form.Item label="工作表" required><Select aria-label="数据工作表" value={sheetName} options={workbook.sheets.map(sheet => ({ value: sheet.sheetName, label: sheet.sheetName }))} onChange={value => void changePreview(value, headerRow)} /></Form.Item><Form.Item label="表头在第几行" required><InputNumber aria-label="表头所在行" min={1} max={100} value={headerRow} onChange={value => { if (value) setHeaderRow(value); }} onBlur={() => { if (headerRow !== workbook.preview.headerRow) void changePreview(sheetName, headerRow); }} onPressEnter={() => void changePreview(sheetName, headerRow)} /></Form.Item></div><div className="nc-section-title"><h3>业务字段对应 Excel 中哪一列</h3><span>{duplicateColumns ? '同一 Excel 列不能对应多个业务字段' : missingFields.length ? `还需选择 ${missingFields.length} 个必填字段` : '必填字段已全部匹配'}</span></div><Spin spinning={busy}><ColumnMatching fields={fields} headers={headers} value={fieldMap} onChange={setFieldMap} /></Spin><h3>原表前几行</h3><SampleTable headers={headers} rows={workbook.preview.sampleRows.map(row => row.values)} /></>}
{step === 3 && <>{!runId && workbook && <><div className="nc-review-facts"><div><small>统计期间</small><strong>{year}年{month}月 · {stageName(stage)}</strong></div><div><small>工作表与表头</small><strong>{sheetName} · 第{headerRow}行</strong></div><div><small>已匹配字段</small><strong>{Object.keys(fieldMap).length} 项</strong></div></div>{target && <Alert type="warning" showIcon message="即将使用新文件替换该期间的完整数据" description={`原文件:${target.sourceFile};新文件:${workbook.sourceFile}。本次先生成候选,最后发布后才更新查询结果。`} />}<SampleTable headers={Object.keys(fieldMap)} rows={workbook.preview.sampleRows.map(row => Object.fromEntries(Object.entries(fieldMap).map(([field, header]) => [field, row.values[header]])))} /><p className="nc-note">确认后由系统校验文件内容、数据范围及对应关系,并计算候选结果。</p></>}{(runId || (busy && mode === 'REBUILD')) && <div className="nc-processing"><Spin spinning={busy} /><h3>{statusLabel[String(run?.status)] || '正在创建计算任务'}</h3><Progress percent={Number(run?.total) > 0 ? Math.min(100, Math.round(Number(run?.processed || 0) / Number(run?.total) * 100)) : 0} status={run?.status === 'FAILED' ? 'exception' : 'active'} /><p>已处理 {txt(run?.processed || 0)} / {txt(run?.total)} 行</p>{run?.errorMessage ? <Alert type="error" showIcon message={txt(run.errorMessage)} /> : null}{!busy && runId && !['FAILED', 'CANCELLED'].includes(String(run?.status)) && <Button onClick={() => { setBusy(true); setPollAttempt(previous => previous + 1); }}>重新获取处理结果</Button>}</div>}<ValidationNotice value={run?.validation} /></>}
{step === 4 && <Spin spinning={busy}><div className="nc-review-facts"><div><small>候选数据</small><strong>待核对</strong></div><div><small>来源校验</small><strong>{release?.complete === true ? '通过' : '尚未通过'}</strong></div><div><small>基线对账</small><strong>{statusLabel[String(reconciliation?.status)] || '待执行'}</strong></div></div><ValidationNotice value={release?.validation || run?.validation} />{reconciliationErrors.length > 0 && <Alert showIcon type="error" message="对账存在阻断事项" description={reconciliationErrors.map(txt).join(';')} />}<div className="nc-section-title"><h3>与当前数据的差异</h3><Button icon={<ReloadOutlined />} loading={busy} disabled={!release?.id} onClick={() => void reconcile()}>{reconciliation ? '重新对账' : '执行基线对账'}</Button></div>{comparisons.length > 0 ? <Table size="small" bordered dataSource={comparisons} rowKey={(row, index) => `${row.dimension}:${row.key}:${index}`} pagination={{ pageSize: 8, showSizeChanger: false }} scroll={{ x: 900 }} columns={[{ title: '期间', width: 130, render: (_, row) => { const period = asRow(row.period); return `${txt(period.year)}年${period.month ? `${txt(period.month)}月` : period.quarter ? `第${txt(period.quarter)}季度` : '全年'}`; } }, { title: '维度 / 对象', width: 200, render: (_, row) => `${({ overall: '总体', department: '产业部', group: '业务分组', product: '产品', projectGroup: '项目组' } as Record<string, string>)[String(row.dimension)] || txt(row.dimension)} / ${txt(row.key)}` }, { title: '原综合率', width: 120, render: (_, row) => ratio(asRow(row.before).compositeRate) }, { title: '新综合率', width: 120, render: (_, row) => ratio(asRow(row.after).compositeRate) }, { title: '差异', render: (_, row) => asRows(row.differences).map(item => `${metricLabel(item.path)}:${txt(item.before)} → ${txt(item.after)}`).join(';') || '一致' }]} /> : <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description={reconciliation ? '本次没有可展示的期间比较;以对账结论为准' : '执行对账后在这里查看期间和指标差异'} />}{reconciliation?.status === 'REVIEW_REQUIRED' && <div className="nc-difference-confirm"><Alert type="warning" showIcon message={`发现 ${txt(evidence.differenceCount)} 项差异,需要发布者确认`} />{canPublish ? <><Input.TextArea aria-label="业务差异确认理由" rows={2} value={differenceReason} maxLength={1000} placeholder="填写差异原因及确认依据" onChange={event => setDifferenceReason(event.target.value)} /><Checkbox checked={confirmDifferences} onChange={event => setConfirmDifferences(event.target.checked)}>我已核对上方差异,并确认新数据正确</Checkbox><Button type="primary" disabled={!confirmDifferences || !differenceReason.trim() || busy} onClick={() => void confirm()}>确认差异并验收</Button></> : <p>候选已保存,请交由有数据发布权限的同事完成差异确认。</p>}</div>}{canPublish ? <div className="nc-publish-confirm"><Checkbox disabled={!publishReady} checked={publishConfirmed} onChange={event => setPublishConfirmed(event.target.checked)}>{purpose === 'WITHDRAW' ? '我确认发布此删除候选,从当前查询结果移除已批准的来源' : '我确认将此候选发布为当前查询数据'}{target ? `,替换${target.year}年${target.month}月${stageName(target.stage)}` : ''}</Checkbox><Button size="large" type="primary" disabled={!publishReady || !publishConfirmed || busy} onClick={() => void publish()}>确认发布</Button>{!publishReady && <span>来源校验和基线对账通过后可发布。</span>}</div> : <Alert showIcon type="info" message="候选数据已保存,等待有权限的同事发布" description={<><span>当前账号没有数据发布权限。可复制处理链接交接,正式查询数据尚未更新。</span><Button type="link" onClick={() => { const link = new URL(window.location.href); link.searchParams.set('page', 'sources'); if (handoffSourceId) { link.searchParams.set('sourceDeleteId', handoffSourceId); link.searchParams.delete('candidateReleaseId'); } else { link.searchParams.set('candidateReleaseId', String(release?.id || '')); link.searchParams.delete('sourceDeleteId'); } void navigator.clipboard.writeText(link.toString()).then(() => message.success('处理链接已复制')).catch(reportError); }}>复制处理链接</Button></>} />}</Spin>}
{issues.length > 0 && <><h3>需要处理的来源行</h3><Table size="small" bordered dataSource={issues} rowKey={(row, index) => String(row.id || index)} pagination={{ pageSize: 8 }} columns={[{ title: '工作表', dataIndex: 'sheet', width: 120 }, { title: '行号', dataIndex: 'rowNumber', width: 80 }, { title: '字段', dataIndex: 'field', width: 130 }, { title: '问题说明', dataIndex: 'message' }]} /></>}
{step === 4 && <Spin spinning={busy}><div className="nc-review-facts"><div><small>候选数据</small><strong>待核对</strong></div><div><small>来源校验</small><strong>{release?.complete === true ? '通过' : '尚未通过'}</strong></div><div><small>基线对账</small><strong>{statusLabel[String(reconciliation?.status)] || '待执行'}</strong></div></div><ValidationNotice value={release?.validation || run?.validation} />{reconciliationErrors.length > 0 && <Alert showIcon type="error" message="对账存在阻断事项" description={reconciliationErrors.map(txt).join(';')} />}<div className="nc-section-title"><h3>与当前数据的差异</h3><Button icon={<ReloadOutlined />} loading={busy} disabled={!release?.id} onClick={() => void reconcile()}>{reconciliation ? '重新对账' : '执行基线对账'}</Button></div>{comparisons.length > 0 ? <Table size="small" bordered dataSource={comparisons} rowKey={(row, index) => `${row.dimension}:${row.key}:${index}`} pagination={{ pageSize: 15, showSizeChanger: false }} scroll={{ x: 900 }} columns={[{ title: '期间', width: 130, render: (_, row) => { const period = asRow(row.period); return `${txt(period.year)}年${period.month ? `${txt(period.month)}月` : period.quarter ? `第${txt(period.quarter)}季度` : '全年'}`; } }, { title: '维度 / 对象', width: 200, render: (_, row) => `${({ overall: '总体', department: '产业部', group: '业务分组', product: '产品', projectGroup: '项目组' } as Record<string, string>)[String(row.dimension)] || txt(row.dimension)} / ${txt(row.key)}` }, { title: '原综合率', width: 120, render: (_, row) => ratio(asRow(row.before).compositeRate) }, { title: '新综合率', width: 120, render: (_, row) => ratio(asRow(row.after).compositeRate) }, { title: '差异', render: (_, row) => asRows(row.differences).map(item => `${metricLabel(item.path)}:${txt(item.before)} → ${txt(item.after)}`).join(';') || '一致' }]} /> : <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description={reconciliation ? '本次没有可展示的期间比较;以对账结论为准' : '执行对账后在这里查看期间和指标差异'} />}{reconciliation?.status === 'REVIEW_REQUIRED' && <div className="nc-difference-confirm"><Alert type="warning" showIcon message={`发现 ${txt(evidence.differenceCount)} 项差异,需要发布者确认`} />{canPublish ? <><Input.TextArea aria-label="业务差异确认理由" rows={2} value={differenceReason} maxLength={1000} placeholder="填写差异原因及确认依据" onChange={event => setDifferenceReason(event.target.value)} /><Checkbox checked={confirmDifferences} onChange={event => setConfirmDifferences(event.target.checked)}>我已核对上方差异,并确认新数据正确</Checkbox><Button type="primary" disabled={!confirmDifferences || !differenceReason.trim() || busy} onClick={() => void confirm()}>确认差异并验收</Button></> : <p>候选已保存,请交由有数据发布权限的同事完成差异确认。</p>}</div>}{canPublish ? <div className="nc-publish-confirm"><Checkbox disabled={!publishReady} checked={publishConfirmed} onChange={event => setPublishConfirmed(event.target.checked)}>{purpose === 'WITHDRAW' ? '我确认发布此删除候选,从当前查询结果移除已批准的来源' : '我确认将此候选发布为当前查询数据'}{target ? `,替换${target.year}年${target.month}月${stageName(target.stage)}` : ''}</Checkbox><Button size="large" type="primary" disabled={!publishReady || !publishConfirmed || busy} onClick={() => void publish()}>确认发布</Button>{!publishReady && <span>来源校验和基线对账通过后可发布。</span>}</div> : <Alert showIcon type="info" message="候选数据已保存,等待有权限的同事发布" description={<><span>当前账号没有数据发布权限。可复制处理链接交接,正式查询数据尚未更新。</span><Button type="link" onClick={() => { const link = new URL(window.location.href); link.searchParams.set('page', 'sources'); if (handoffSourceId) { link.searchParams.set('sourceDeleteId', handoffSourceId); link.searchParams.delete('candidateReleaseId'); } else { link.searchParams.set('candidateReleaseId', String(release?.id || '')); link.searchParams.delete('sourceDeleteId'); } void navigator.clipboard.writeText(link.toString()).then(() => message.success('处理链接已复制')).catch(reportError); }}>复制处理链接</Button></>} />}</Spin>}
{issues.length > 0 && <><h3>需要处理的来源行</h3><Table size="small" bordered dataSource={issues} rowKey={(row, index) => String(row.id || index)} pagination={{ pageSize: 15 }} columns={[{ title: '工作表', dataIndex: 'sheet', width: 120 }, { title: '行号', dataIndex: 'rowNumber', width: 80 }, { title: '字段', dataIndex: 'field', width: 130 }, { title: '问题说明', dataIndex: 'message' }]} /></>}
</>}
</Modal>;
}
@@ -609,6 +609,6 @@ export function ReadableConfigDefinition({ kind, value }: { kind: 'masterData' |
const isLookup = item.value.endsWith('Lookup');
const stopped = new Set(Array.isArray(disabled[item.value]) ? disabled[item.value] as string[] : []);
const rows = isLookup ? Object.entries(asRow(definition[item.value])).map(([key, target]) => { const split = key.indexOf('|'); return { code: key, source: split >= 0 ? key.slice(0, split) : key, secondary: split >= 0 ? key.slice(split + 1) : '', target, enabled: !stopped.has(key) }; }) : asRows(master[item.value]);
return <section key={item.value}><h3>{item.label} · {rows.length} 条</h3><Table size="small" bordered dataSource={rows} rowKey={(row, index) => String(row.code || index)} pagination={{ pageSize: 8, showSizeChanger: false }} scroll={{ x: 600 }} columns={item.columns.map(column => ({ title: column.label.replace('(可选)', ''), dataIndex: column.key, key: column.key, render: column.key === 'enabled' ? (enabled: unknown) => <Tag>{enabled === false ? '停用' : '启用'}</Tag> : txt }))} locale={{ emptyText: '当前配置未单独维护此类记录' }} /></section>;
return <section key={item.value}><h3>{item.label} · {rows.length} 条</h3><Table size="small" bordered dataSource={rows} rowKey={(row, index) => String(row.code || index)} pagination={{ pageSize: 15, showSizeChanger: false }} scroll={{ x: 600 }} columns={item.columns.map(column => ({ title: column.label.replace('(可选)', ''), dataIndex: column.key, key: column.key, render: column.key === 'enabled' ? (enabled: unknown) => <Tag>{enabled === false ? '停用' : '启用'}</Tag> : txt }))} locale={{ emptyText: '当前配置未单独维护此类记录' }} /></section>;
})}</div>;
}
+2 -2
View File
@@ -39,7 +39,7 @@ export function QualityTable<T extends object>({
}: QualityTableProps<T>) {
const compact = useContext(CompactManagementTables);
const pageConfig = typeof pagination === 'object' && pagination !== null ? pagination : undefined;
const [innerPage, setInnerPage] = useState(() => ({ current: pageConfig?.defaultCurrent ?? 1, pageSize: pageConfig?.defaultPageSize ?? 10 }));
const [innerPage, setInnerPage] = useState(() => ({ current: pageConfig?.defaultCurrent ?? 1, pageSize: pageConfig?.defaultPageSize ?? 15 }));
const pageSize = pageConfig?.pageSize ?? innerPage.pageSize;
const total = pageConfig?.total ?? dataSource?.length ?? 0;
const page = Math.min(pageConfig?.current ?? innerPage.current, Math.max(1, Math.ceil(total / pageSize)));
@@ -66,7 +66,7 @@ export function QualityTable<T extends object>({
tableLayout={props.tableLayout ?? (compact ? 'fixed' : undefined)}
columns={[indexColumn, ...(compact ? compactColumns(columns ?? []) : columns ?? [])]}
dataSource={dataSource}
pagination={pagination}
pagination={pagination === false ? false : { defaultPageSize: 15, pageSizeOptions: ['15', '30', '50', '100'], ...pageConfig }}
onChange={handleChange}
size={size}
bordered
-2
View File
@@ -26,7 +26,6 @@ import {
MenuUnfoldOutlined,
RobotOutlined,
SettingOutlined,
TeamOutlined,
ToolOutlined,
UserOutlined,
} from '@ant-design/icons';
@@ -254,7 +253,6 @@ export default function QualityWorkspace({ appearance, onAppearanceChange }: Qua
<BgColorsOutlined /><span>{appearance.name}</span><DownOutlined />
</Button>
</Dropdown>
{user.departments.length > 0 && <span className="top-organization"><TeamOutlined />{user.departments.map(shownDepartment).join('、')}</span>}
<Dropdown trigger={['click']} menu={{ items: [
{ key: 'profile', icon: <UserOutlined />, label: '个人资料' },
{ key: 'password', icon: <KeyOutlined />, label: '修改密码' },
+17 -16
View File
@@ -1,5 +1,4 @@
import { queryScrapWeight, queryTotalScrapWeight, type QueryRateBasis } from './queryMetricValues';
import { useStickyQueryMetrics } from './useStickyQueryMetrics';
import { BusinessValue } from './BusinessValue';
import { usePageCache } from './pageCache';
import { useCallback, useEffect, useImperativeHandle, useLayoutEffect, useMemo, useRef, useState } from 'react';
@@ -11,7 +10,6 @@ import {
Alert,
App,
Button,
Checkbox,
Drawer,
Empty,
Input,
@@ -66,7 +64,7 @@ function queryChartTypography(node: HTMLElement) {
tooltipFontSize: size('--font-reading', 14),
};
}
const TABLE_PAGE_SIZE = 20;
const TABLE_PAGE_SIZE = 15;
const SUMMARY_PAGE_SIZE = 15;
const SUMMARY_PAGE_SIZE_OPTIONS = ['10', '15', '20', '50', '100'];
const EMPTY_PRODUCT_SEARCH = '__codex_no_product_search_match__';
@@ -287,7 +285,6 @@ export default function QueryPage({ page, onUnauthorized }: {
onUnauthorized: (error: unknown) => boolean;
onNavigate?: (page: PageKey, query: QueryParams, resultRef?: TopResultRef) => void;
}) {
const stickyPageRef = useStickyQueryMetrics();
const { message } = App.useApp();
const { active, initialSearch, refreshKey, onLocationChange } = usePageCache();
const baseView = SCOPE[page] || 'overall';
@@ -311,7 +308,7 @@ export default function QueryPage({ page, onUnauthorized }: {
const [product, setProduct] = useState<string[]>(() => decodeQueryValueList(initialQuery.product));
const [projectGroup, setProjectGroup] = useState<string[]>(() => decodeQueryValueList(initialQuery.projectGroup));
const [extraFilters, setExtraFilters] = useState<Record<string,string[]>>(initialQuery.extraFilters || {});
const [includeUnassigned, setIncludeUnassigned] = useState(initialQuery.includeUnassigned === true);
const includeUnassigned = true;
const [trendGranularity, setTrendGranularity] = useState<'month' | 'quarter' | 'year'>(initialQuery.trendGranularity || (initialQuery.quarter ? 'quarter' : 'month'));
const [search, setSearch] = useState(initialQuery.productSearch || '');
const [appliedSearch, setAppliedSearch] = useState(initialQuery.productSearch || '');
@@ -700,7 +697,7 @@ export default function QueryPage({ page, onUnauthorized }: {
].filter(Boolean);
return (
<div className="query-page" ref={stickyPageRef}>
<div className="query-page">
{optionsError && <Alert showIcon type="error" message="查询选项加载失败" description={optionsError} action={<Button size="small" icon={<ReloadOutlined />} onClick={() => { setOptionsLoading(true); api.options().then(result => { setOptions(result); setOptionsError(''); }).catch(e => { if (!onUnauthorized(e)) setOptionsError(chooseError(e)); }).finally(() => setOptionsLoading(false)); }}>重试</Button>} />}
<section className="query-toolbar panel" aria-label="查询条件">
<Segmented
@@ -730,10 +727,9 @@ export default function QueryPage({ page, onUnauthorized }: {
{filterSelect('产品', product, department.length || group.length ? organizationPathOptions.products : options?.products, setProduct)}
<ExtraQueryFilters key={queryRevision} projectGroup={projectGroup} projectGroups={options?.projectGroupStatus === 'READY' ? options.projectGroups : undefined} onProjectGroup={setProjectGroup} value={extraFilters} onChange={setExtraFilters} onUnauthorized={onUnauthorized} />
<Checkbox className="query-unassigned" checked={includeUnassigned} onChange={event => setIncludeUnassigned(event.target.checked)}>包含未分类行</Checkbox>
<div className="query-toolbar-tail">
<span className={`query-status ${queryBusy || summaryBusy ? 'busy' : currentMetricsError || summaryMetricsError ? 'failed' : ''}`}><i />{queryBusy || summaryBusy ? '正在查询' : currentMetricsError || summaryMetricsError ? '查询失败' : '查询完成'}</span>
<Button type="text" onClick={() => { const previous = previousCompleteMonth(); const previousQuarter = previousCompleteQuarter(); setDepartment([]); setGroup([]); setProduct([]); setProjectGroup([]); setExtraFilters({}); setIncludeUnassigned(false); setSearch(''); setAppliedSearch(''); setTrendObject(''); setYear(page === 'overall' ? new Date().getFullYear() : previous.year); setMonth(previous.month); setQuarter(previousQuarter.quarter); setPeriod(page === 'overall' ? 'year' : 'month'); setTrendGranularity('month'); }}>重置</Button>
<Button type="text" onClick={() => { const previous = previousCompleteMonth(); const previousQuarter = previousCompleteQuarter(); setDepartment([]); setGroup([]); setProduct([]); setProjectGroup([]); setExtraFilters({}); setSearch(''); setAppliedSearch(''); setTrendObject(''); setYear(page === 'overall' ? new Date().getFullYear() : previous.year); setMonth(previous.month); setQuarter(previousQuarter.quarter); setPeriod(page === 'overall' ? 'year' : 'month'); setTrendGranularity('month'); }}>重置</Button>
</div>
{organizationSummary.length > 0 && <div className="organization-breadcrumb" aria-label="当前组织路径">
<span>组织路径</span><Button type="link" onClick={() => { setDepartment([]); setGroup([]); setProduct([]); }}>全部组织</Button>
@@ -745,9 +741,9 @@ export default function QueryPage({ page, onUnauthorized }: {
{drilledProduct && summaryMetricsError && <Alert showIcon type="error" message="当前产品指标查询失败" description={summaryMetricsError} action={<Button size="small" onClick={() => setQueryRevision(value => value + 1)}>重试</Button>} />}
<section className={`metric-strip ${!summaryMetricsData ? 'query-awaiting-data' : ''}`} aria-label="质量指标" aria-busy={summaryBusy} inert={!summaryMetricsData}>
<div className="rate-basis-control"><Segmented size="small" value={rateBasis} options={[{ label: '原口径', value: 'original' }, { label: '去本部报废', value: 'withoutHeadquartersScrap' }]} onChange={value => setRateBasis(value as typeof rateBasis)} aria-label="合格率口径" /></div>
<MetricCard primary label={rateMetricLabel('综合合格率', rateBasis)} value={rateText(visibleCompositeRate(rowForSummary, rateBasis))} onClick={() => setBasisOpen(true)} />
<MetricCard primary label="综合合格率" value={rateText(visibleCompositeRate(rowForSummary, rateBasis))} onClick={() => setBasisOpen(true)} />
<MetricCard label="黑片合格率" value={rateText(visibleStageRate(rowForSummary, 'black'))} note={!summaryMetricsData ? (summaryBusy ? '正在更新…' : '指标暂不可用') : hasStageRows(rowForSummary, 'black') ? hasValidRateDenominator(rowForSummary, 'black') ? `合格重量 ${numberText(rowForSummary?.black?.qualifiedWeight)} kg` : '按当前启用规则及空值策略计算' : '缺少黑片来源'} stage="black" onClick={() => openRecords(null, 'black')} />
<MetricCard label={rateMetricLabel('亮片合格率', rateBasis)} value={rateText(visibleStageRate(rowForSummary, 'bright', rateBasis))} note={!summaryMetricsData ? (summaryBusy ? '正在更新…' : '指标暂不可用') : hasStageRows(rowForSummary, 'bright') ? hasValidRateDenominator(rowForSummary, 'bright') ? `合格重量 ${numberText(rowForSummary?.bright?.qualifiedWeight)} kg` : '按当前启用规则及空值策略计算' : '缺少亮片来源'} stage="bright" onClick={() => openRecords(null, 'bright')} />
<MetricCard label="亮片合格率" value={rateText(visibleStageRate(rowForSummary, 'bright', rateBasis))} note={!summaryMetricsData ? (summaryBusy ? '正在更新…' : '指标暂不可用') : hasStageRows(rowForSummary, 'bright') ? hasValidRateDenominator(rowForSummary, 'bright') ? `合格重量 ${numberText(rowForSummary?.bright?.qualifiedWeight)} kg` : '按当前启用规则及空值策略计算' : '缺少亮片来源'} stage="bright" onClick={() => openRecords(null, 'bright')} />
<MetricCard label="报废总重" value={rowForSummary && (hasStageRows(rowForSummary, 'black') || hasStageRows(rowForSummary, 'bright')) ? `${numberText(queryTotalScrapWeight(rowForSummary, rateBasis))} kg` : '—'} note={!summaryMetricsData ? (summaryBusy ? '正在更新…' : '指标暂不可用') : hasStageRows(rowForSummary, 'black') || hasStageRows(rowForSummary, 'bright') ? `黑片 ${numberText(rowForSummary?.black?.scrapWeight)} kg · 亮片 ${numberText(queryScrapWeight(rowForSummary?.bright, 'bright', rateBasis))} kg` : '缺少阶段来源'} onClick={() => openRecords(null, 'black')} />
</section>
@@ -885,14 +881,14 @@ export default function QueryPage({ page, onUnauthorized }: {
<dt>统计范围</dt><dd>{year}年{period === 'month' ? ` ${MONTHS[month - 1]}` : period === 'quarter' ? ` 第${quarter}季度` : '度'} · {drilledProduct || scopeName}</dd>
<dt>总重量</dt><dd>黑片 {numberText(rowForSummary?.black?.totalWeight)} kg · 亮片 {numberText(rowForSummary?.bright?.totalWeight)} kg</dd>
<dt>覆盖行数</dt><dd>黑片 {rowForSummary?.coverage.blackRows ?? '—'} · 亮片 {rowForSummary?.coverage.brightRows ?? '—'}</dd>
<dt>来源行覆盖</dt><dd>原始 {summaryMetricsData?.coverage.rawRows ?? '—'} · 参与 {summaryMetricsData?.coverage.includedRows ?? '—'} · 未分类行 {includeUnassigned ? '(已选择包含)' : '(本次排除)'} {summaryMetricsData?.coverage.excludedBlankRows ?? '—'}</dd>
<dt>来源行覆盖</dt><dd>原始 {summaryMetricsData?.coverage.rawRows ?? '—'} · 参与 {summaryMetricsData?.coverage.includedRows ?? '—'} · 未分类行 (已计入) {summaryMetricsData?.coverage.excludedBlankRows ?? '—'}</dd>
<dt>业务分类映射</dt><dd>未映射 {summaryMetricsData?.coverage.unmappedRows ?? '—'} 行</dd>
<dt>项目组维度</dt><dd>{summaryMetricsData?.coverage.projectGroupUnconfiguredRows ?? '—'} 行未配置项目组归属。该计数不等于排除行数;是否参与指标由当前筛选及规则决定。</dd>
</dl>
</Modal>
{recordsOpen && recordQuerySnapshot && <RecordsDrawer
{recordQuerySnapshot && <RecordsDrawer
key={JSON.stringify({ query: recordQuerySnapshot, stage: recordStage })}
open={recordsOpen}
row={selectedRecord}
@@ -1092,11 +1088,10 @@ function useCompactTable() {
return compact;
}
export const rateMetricLabel = (label: string, basis: QueryRateBasis) => <span className="query-rate-label">{label}{basis !== 'original' && <small className="query-rate-subtitle">(去本部报废)</small>}</span>;
const totalWeightTitle = (label: string) => <span className="query-total-weight-title">{label}<Tooltip trigger={['hover', 'focus']} title={<div>黑片总重量为合格率计算使用的适用总重量。<br />按来源逐行计算:合格重量+报废重量-利用料及废品库对应的合格重量、报废重量;发料地点包含“利用料”或“废品”时扣减。<br />检验备注包含“过账”时,外废不计入报废重量;月、季、年均先汇总来源分量。具体取值以已发布数据版本及生效规则为准。</div>}><InfoCircleOutlined tabIndex={0} role="img" aria-label="黑片总重量计算说明" /></Tooltip></span>;
const rateBasisTitle = (label: string, basis: QueryRateBasis, kind: 'bright' | 'composite', onBasis?: () => void) => <span className="query-total-weight-title query-rate-rule-title"><span className="query-rate-label">{onBasis ? <button className="header-link" onClick={onBasis}>{label}</button> : label}{basis !== 'original' && <small className="query-rate-subtitle">(去本部报废)</small>}</span><Tooltip trigger={['hover', 'focus']} title={<div>{kind === 'composite' && <>综合合格率=黑片合格率×亮片合格率。<br /></>}原口径:亮片合格重量÷亮片总重量。<br />去本部报废:(亮片合格重量+本部报废)÷亮片总重量,同时从亮片报废重量中扣除本部报废,总重量不变。<br />当前:{basis === 'original' ? '原口径' : '去本部报废'}。计算结果以已发布数据版本及生效规则为准。</div>}><InfoCircleOutlined tabIndex={0} role="img" aria-label={`${kind === 'composite' ? '综合合格率' : '亮片合格率'}计算说明`} /></Tooltip></span>;
const rateBasisTitle = (label: string, basis: QueryRateBasis, kind: 'bright' | 'composite', onBasis?: () => void) => <span className="query-total-weight-title query-rate-rule-title"><span className="query-rate-label">{onBasis ? <button className="header-link" onClick={onBasis}>{label}</button> : label}{basis !== 'original' && <small className="query-rate-subtitle">(去本部报废)</small>}</span>{basis !== 'original' && <Tooltip trigger={['hover', 'focus']} title={<div>{kind === 'composite' && <>综合合格率=黑片合格率×亮片合格率。<br /></>}原口径:亮片合格重量÷亮片总重量。<br />去本部报废:(亮片合格重量+本部报废)÷亮片总重量,同时从亮片报废重量中扣除本部报废,总重量不变。<br />当前:去本部报废。计算结果以已发布数据版本及生效规则为准。</div>}><InfoCircleOutlined tabIndex={0} role="img" aria-label={`${kind === 'composite' ? '综合合格率' : '亮片合格率'}计算说明`} /></Tooltip>}</span>;
export function MetricTable({
ref, exportFilename, rows, view, layoutView = view, pageSizeOverride, period, month, quarter, fullFields, rateBasis, onDetail, onBasis, onDrilldown, onlyStage, defectDisplay = 'weight', emptyText = '当前条件下暂无数据',
@@ -1428,6 +1423,8 @@ export function RecordsDrawer({
query: QueryParams; month?: number; onUnauthorized: (error: unknown) => boolean; lockedStage?: boolean;
}) {
const { message } = App.useApp();
const { refreshKey } = usePageCache();
const recordPages = useRef(new Map<string, RecordsResponse>());
const [page, setPage] = useState(1);
const [response, setResponse] = useState<{ key: string; data: RecordsResponse } | null>(null);
const [loading, setLoading] = useState(false);
@@ -1447,20 +1444,24 @@ export function RecordsDrawer({
...(field ? { field } : {}), ...(field && fieldSearch.trim() ? { search: fieldSearch.trim() } : {}),
...(sort ? { sort, direction } : {}),
}), [recordQuery, stage, page, field, fieldSearch, sort, direction]);
const requestKey = JSON.stringify(requestParams);
const requestKey = JSON.stringify([requestParams, revision, refreshKey]);
const result = response?.key === requestKey ? response.data : null;
const error = failure?.key === requestKey ? failure.message : '';
const fetching = loading || !result && !error;
useEffect(() => {
if (!open) return;
const cached = recordPages.current.get(requestKey);
if (cached) { setResponse({ key: requestKey, data: cached }); setFailure(null); setLoading(false); return; }
const controller = new AbortController();
setLoading(true);
setResponse(null); setFailure(null);
api.records(requestParams, controller.signal).then(value => {
if (controller.signal.aborted) return;
const lastPage = Math.max(1, Math.ceil(value.total / value.pageSize));
if (page > lastPage) { setPage(lastPage); return; }
if (page > lastPage) { recordPages.current.clear(); setPage(lastPage); return; }
recordPages.current.set(requestKey, value);
if (recordPages.current.size > 8) recordPages.current.delete(recordPages.current.keys().next().value!);
setResponse({ key: requestKey, data: value });
const pageFields = value.rows.flatMap(record => Object.keys(record.raw || {}).filter(key => !isSystemVersionField(key)));
if (pageFields.length) setRawFields(existing => [...new Set([...existing, ...pageFields])]);
+8 -11
View File
@@ -1,10 +1,9 @@
import { queryScrapWeight } from './queryMetricValues';
import { useStickyQueryMetrics } from './useStickyQueryMetrics';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { Alert, App, Button, Checkbox, Empty, Input, Modal, Segmented, Select, Spin } from 'antd';
import { Alert, App, Button, Empty, Input, Modal, Segmented, Select, Spin } from 'antd';
import { DownloadOutlined, ReloadOutlined, SearchOutlined } from '@ant-design/icons';
import { api, decodeQueryValueList, encodeQueryValueList } from './api';
import { MetricTable, MetricCard, rateMetricLabel, DefectBreakdown, DefectParetoPanel, SectionHeading, RecordsDrawer, TrendChart, fullYearTrend, queryFromLocation } from './QueryPage';
import { MetricTable, MetricCard, DefectBreakdown, DefectParetoPanel, SectionHeading, RecordsDrawer, TrendChart, fullYearTrend, queryFromLocation } from './QueryPage';
import { StageSourceFilters } from './StageSourceFilters';
import { stageProductQuery, sortStageRowsByTotalWeight } from './stageProductQuery';
import { usePageCache } from './pageCache';
@@ -17,20 +16,19 @@ import './stage-product.css';
const weight=(v:number|null|undefined)=>typeof v==='number'?v.toLocaleString('zh-CN',{maximumFractionDigits:2}):'—';
const rate=(v:number|null|undefined)=>typeof v==='number'?`${(v*100).toFixed(2)}%`:'—';
export default function StageProductPage({stage,onUnauthorized}:{stage:Stage;onUnauthorized:(error:unknown)=>boolean}) {
const stickyPageRef = useStickyQueryMetrics();
const {message}=App.useApp();const {active,initialSearch,refreshKey,onLocationChange}=usePageCache();
const [initial]=useState(()=>queryFromLocation(initialSearch));const defaults=previousCompleteMonth();
const [period,setPeriod]=useState<PeriodKind>(initial.quarter?'quarter':initial.month?'month':initial.year?'year':'month');
const [year,setYear]=useState(initial.year||defaults.year);const [month,setMonth]=useState(initial.month||defaults.month);const [quarter,setQuarter]=useState(initial.quarter||previousCompleteQuarter().quarter);
const [department,setDepartment]=useState(()=>decodeQueryValueList(initial.department));const [group,setGroup]=useState(()=>decodeQueryValueList(initial.group));const [product,setProduct]=useState(()=>decodeQueryValueList(initial.product));const [projectGroup,setProjectGroup]=useState(()=>decodeQueryValueList(initial.projectGroup));
const [sourceFilters,setSourceFilters]=useState<SourceColumnFilter[]>(initial.columnFilters||[]);const [search,setSearch]=useState(initial.productSearch||'');const [includeUnassigned,setIncludeUnassigned]=useState(!!initial.includeUnassigned);
const [sourceFilters,setSourceFilters]=useState<SourceColumnFilter[]>(initial.columnFilters||[]);const [search,setSearch]=useState(initial.productSearch||'');const includeUnassigned=true;
const [options,setOptions]=useState<OptionsResponse|null>(null);const [catalog,setCatalog]=useState<{releaseId:string;sourceFields:SourceColumnField[]}|null>(null);const [supportError,setSupportError]=useState('');const [supportLoading,setSupportLoading]=useState(true);
const [applied,setApplied]=useState<QueryParams|null>(null);const [revision,setRevision]=useState(0);const [response,setResponse]=useState<{key:string;value:MetricsResponse}|null>(null);const [loading,setLoading]=useState(false);const [failure,setFailure]=useState<{key:string;message:string}|null>(null);
const [defectDisplay,setDefectDisplay]=useState<'weight'|'rate'>('weight');
const [fullFields,setFullFields]=useState(true);const [rateBasis,setRateBasis]=useState<'original'|'withoutHeadquartersScrap'>('original');const [basisOpen,setBasisOpen]=useState(false);const [trendMode,setTrendMode]=useState<'production'|'defects'|'data'>('production');
const [trendGranularity,setTrendGranularity]=useState<'month'|'quarter'|'year'>('month');
const [trendResponse,setTrendResponse]=useState<{key:string;value:MetricsResponse}|null>(null);const [trendError,setTrendError]=useState('');const [trendLoading,setTrendLoading]=useState(false);
const [records,setRecords]=useState<{row:MetricRow|null;query:QueryParams}|null>(null);const exporter=useRef<{exportExcel:()=>Promise<void>}>(null);const [exporting,setExporting]=useState(false);
const [recordVisible,setRecordVisible]=useState(false);const [records,setRecords]=useState<{row:MetricRow|null;query:QueryParams}|null>(null);const exporter=useRef<{exportExcel:()=>Promise<void>}>(null);const [exporting,setExporting]=useState(false);
const label=stage==='black'?'黑片':'亮片';
const loadSupport=useCallback(async()=>{
setSupportLoading(true);setSupportError('');
@@ -61,10 +59,10 @@ export default function StageProductPage({stage,onUnauthorized}:{stage:Stage;onU
const select=(name:string,value:string[],choices:string[],change:(v:string[])=>void)=><label className="query-field query-primary-field"><span>{name}</span><Select {...readableSelectPopup} mode="multiple" allowClear showSearch optionFilterProp="label" aria-label={name} placeholder="全部" maxTagCount="responsive" value={value} options={[...new Set([...choices,...value])].map(v=>({label:v==='__UNASSIGNED__'?'未分类':v,value:v}))} onChange={change}/></label>;
const sortedRows=useMemo(()=>sortStageRowsByTotalWeight(current?.rows||[],stage),[current,stage]);
const summary=current?.summary[stage];const stageRate=stage==='bright'&&rateBasis==='withoutHeadquartersScrap'?summary?.rateWithoutHeadquartersScrap:summary?.rate;
const openDetail=(row:MetricRow|null,_stage:Stage,selectedMonth?:number)=>{if(!applied||!current?.release)return;setRecords({row,query:{...applied,releaseId:current.release.id,...(row&&selectedMonth==null?{product:row.product||'__UNASSIGNED__'}:{}),...(selectedMonth?{month:selectedMonth,quarter:undefined}: {})}});};
const openDetail=(row:MetricRow|null,_stage:Stage,selectedMonth?:number)=>{if(!applied||!current?.release)return;setRecordVisible(true);setRecords({row,query:{...applied,releaseId:current.release.id,...(row&&selectedMonth==null?{product:row.product||'__UNASSIGNED__'}:{}),...(selectedMonth?{month:selectedMonth,quarter:undefined}: {})}});};
const runExport=async()=>{setExporting(true);try{await exporter.current?.exportExcel();}catch(e){message.error(e instanceof Error?e.message:'导出失败');}finally{setExporting(false);}};
const formulaKey=stage==='black'?'blackRate':rateBasis==='original'?'brightRate':'brightRateWithoutHeadquartersScrap';
return <div className="query-page stage-product-page" ref={stickyPageRef}>
return <div className="query-page stage-product-page">
{supportError&&<Alert showIcon type="error" message="查询条件读取失败" description={supportError} action={<Button onClick={()=>void loadSupport()}>重试</Button>}/>}
<section className="query-toolbar panel" aria-label={`${label}汇总条件`}>
<Segmented aria-label="统计周期" value={period} options={[{label:'年度',value:'year'},{label:'季度',value:'quarter'},{label:'月度',value:'month'}]} onChange={v=>setPeriod(v as PeriodKind)}/>
@@ -76,7 +74,6 @@ export default function StageProductPage({stage,onUnauthorized}:{stage:Stage;onU
{select('产品',product,products,setProduct)}
{options?.projectGroupStatus==='READY'&&select('项目组',projectGroup,options.projectGroups||[],setProjectGroup)}
{catalog&&<StageSourceFilters part="picker" stage={stage} fields={fields} query={draft} value={sourceFilters} onChange={setSourceFilters} onUnauthorized={onUnauthorized}/>}
<Checkbox checked={includeUnassigned} onChange={e=>setIncludeUnassigned(e.target.checked)}>包含未分类行</Checkbox>
<div className="query-toolbar-tail"><Button type="text" icon={<ReloadOutlined/>} loading={supportLoading} onClick={()=>void loadSupport()}>刷新</Button><Button type="primary" icon={<SearchOutlined/>} disabled={supportLoading||!!supportError||!catalog} loading={loading} onClick={submit}>查询</Button></div>
{catalog&&<StageSourceFilters part="rows" stage={stage} fields={fields} query={draft} value={sourceFilters} onChange={setSourceFilters} onUnauthorized={onUnauthorized}/>}
{dirty&&<div className="stage-query-pending">筛选条件已修改,点击“查询”更新统计结果</div>}
@@ -84,7 +81,7 @@ export default function StageProductPage({stage,onUnauthorized}:{stage:Stage;onU
{error&&<Alert showIcon type="error" message="查询失败" description={error} action={<Button onClick={()=>setRevision(v=>v+1)}>重试</Button>}/>}
<section className="metric-strip stage-metric-strip" aria-label="质量指标" aria-busy={loading} inert={!current}>
{stage==='bright'&&<div className="rate-basis-control"><Segmented size="small" aria-label="合格率口径" value={rateBasis} options={[{label:'原口径',value:'original'},{label:'去本部报废',value:'withoutHeadquartersScrap'}]} onChange={v=>setRateBasis(v as typeof rateBasis)}/></div>}
<MetricCard primary stage={stage} label={rateMetricLabel(`${label}合格率`, stage === 'bright' ? rateBasis : 'original')} value={rate(stageRate)} onClick={()=>setBasisOpen(true)}/>
<MetricCard primary stage={stage} label={`${label}合格率`} value={rate(stageRate)} onClick={()=>setBasisOpen(true)}/>
<MetricCard label="合格重量" value={`${weight(summary?.qualifiedWeight)} kg`} onClick={()=>openDetail(null,stage)}/>
<MetricCard label="报废重量" value={`${weight(queryScrapWeight(summary,stage,rateBasis))} kg`} onClick={()=>openDetail(null,stage)}/>
<MetricCard label="总重量" value={`${weight(summary?.totalWeight)} kg`} onClick={()=>openDetail(null,stage)}/>
@@ -103,6 +100,6 @@ export default function StageProductPage({stage,onUnauthorized}:{stage:Stage;onU
{current&&applied&&<DefectParetoPanel releaseId={current.release?.id} query={applied} onlyStage={stage} externalScrapWeight={stage==='black'?current.defects.black.find(item=>item.name==='外废')?.weight:undefined} onUnauthorized={onUnauthorized}/>}
</section></div>
<Modal open={basisOpen} title={`${label}计算口径`} onCancel={()=>setBasisOpen(false)} footer={<Button onClick={()=>setBasisOpen(false)}>关闭</Button>}><p>{readableMetricFormula((current?.context.metricFormulaDescriptions as Record<string,unknown>|undefined)?.[formulaKey])}</p></Modal>
{records&&<RecordsDrawer key={JSON.stringify(records)} open row={records.row} query={records.query} stage={stage} lockedStage onStage={()=>{}} onClose={()=>setRecords(null)} onUnauthorized={onUnauthorized}/>}
{records&&<RecordsDrawer key={JSON.stringify(records)} open={recordVisible} row={records.row} query={records.query} stage={stage} lockedStage onStage={()=>{}} onClose={()=>setRecordVisible(false)} onUnauthorized={onUnauthorized}/>}
</div>;
}
+21 -22
View File
@@ -1,8 +1,10 @@
import { ConfigurationWorkbookActions } from './ConfigurationWorkbookActions';
import { matchRuleConflicts } from './classificationMaintenance';
import { useEffect, useMemo, useState } from 'react';
import { Alert, App, AutoComplete, Button, Checkbox, Form, Input, InputNumber, Modal, Select, Switch, Tabs, Tag } from 'antd';
import type { ColumnsType } from 'antd/es/table';
import { QualityTable as Table } from './QualityTable';
import { DownloadOutlined, PlusOutlined, UploadOutlined } from '@ant-design/icons';
import { PlusOutlined } from '@ant-design/icons';
import { readableSelectPopup } from './readableSelectPopup';
import './structured-config-editors.css';
@@ -63,11 +65,6 @@ function setMasterRows(value: ConfigDefinition, kind: MasterKind, rows: MasterRo
function setMappingRows(value: ConfigDefinition, kind: MappingKind, rows: MappingRow[]): ConfigDefinition {
return { ...value, [kind]: Object.fromEntries(rows.map(row => [row.key, row.target])), disabledMappings: { ...objectValue(value.disabledMappings), [kind]: rows.filter(row => !row.enabled).map(row => row.key) } };
}
function csvText(rows: (string | boolean | null)[][]): string { return rows.map(row => row.map(cell => `"${String(cell ?? '').replaceAll('"', '""')}"`).join(',')).join('\r\n'); }
function downloadCsv(filename: string, rows: (string | boolean | null)[][]) {
const url = URL.createObjectURL(new Blob(['\uFEFF', csvText(rows)], { type: 'text/csv;charset=utf-8' }));
const link = document.createElement('a'); link.href = url; link.download = filename; link.click(); URL.revokeObjectURL(url);
}
export function parseConfigDelimited(text: string): string[][] {
const source = text.replace(/^\uFEFF/, '').replace(/^[\r\n]+|[\r\n]+$/g, '');
if (!source.trim()) throw new Error('请粘贴需要导入的内容');
@@ -164,9 +161,8 @@ export function MappingDefinitionEditor({ value, onChange, simplified = false, r
];
const headers = [info.source, ...(info.secondary ? [info.secondary] : []), info.target, '启用'];
return <div className="structured-config-editor">
<Tabs activeKey={kind} onChange={key => { setKind(key as MappingKind); setSearch(''); }} items={mappingKinds.map(key => ({ key, label: mappingInfo[key].label }))} />
<div className="structured-config-toolbar"><Input.Search value={search} onChange={event => setSearch(event.target.value)} placeholder="查找来源或目标" allowClear aria-label="查找对应关系" /><Select value={status} onChange={setStatus} aria-label="对应关系状态" options={[{ value: 'all', label: '全部状态' }, { value: 'enabled', label: '启用中' }, { value: 'disabled', label: '已停用' }]} /><span>{rows.length} 条 · 启用 {rows.filter(row => row.enabled).length}</span><div className="structured-config-actions"><Button icon={<DownloadOutlined />} onClick={() => downloadCsv(`${info.label}.csv`, [headers, ...rows.map(row => [row.source, ...(info.secondary ? [row.secondary] : []), row.target, row.enabled])])}>导出 CSV</Button>{!readOnly && <><Button icon={<UploadOutlined />} onClick={() => setImporting(true)}>批量导入</Button><Button type="primary" icon={<PlusOutlined />} onClick={() => openRow()}>新增对应</Button></>}</div></div>
<Table size="small" bordered rowKey="key" columns={columns} dataSource={visibleRows} pagination={{ pageSize: 10, showSizeChanger: false }} scroll={{ x: info.secondary ? 860 : 680 }} locale={{ emptyText: '没有符合条件的对应关系' }} />
<div className="structured-config-toolbar"><label className="config-type-filter"><span>对应类型</span><Select {...readableSelectPopup} aria-label="对应类型" value={kind} options={mappingKinds.map(key=>({value:key,label:mappingInfo[key].label}))} onChange={key=>{setKind(key);setSearch('');}} /></label><Input.Search value={search} onChange={event => setSearch(event.target.value)} placeholder="查找来源或目标" allowClear aria-label="查找对应关系" /><Select value={status} onChange={setStatus} aria-label="对应关系状态" options={[{ value: 'all', label: '全部状态' }, { value: 'enabled', label: '启用中' }, { value: 'disabled', label: '已停用' }]} /><span>{rows.length} 条 · 启用 {rows.filter(row => row.enabled).length}</span><div className="structured-config-actions"><ConfigurationWorkbookActions value={value} onChange={onChange} scope={kind} label={info.label} readOnly={readOnly} validate={validateStructuredMappingDefinition} onPaste={()=>setImporting(true)} />{!readOnly && <><Button type="primary" icon={<PlusOutlined />} onClick={() => openRow()}>新增对应</Button></>}</div></div>
<Table size="small" bordered rowKey="key" columns={columns} dataSource={visibleRows} pagination={{ pageSize: 15, showSizeChanger: false }} scroll={{ x: info.secondary ? 860 : 680 }} locale={{ emptyText: '没有符合条件的对应关系' }} />
<p className="structured-config-note">{simplified ? '新增的对应目标会自动补入对应名录;全部变更由页面主按钮一次保存并更新报表。' : '停用保留原对应值;变更经保存、校验并启用后用于新的数据构建。'}</p>
<Modal className="management-modal structured-config-modal" open={Boolean(editing)} title={`${newRow ? '新增' : '编辑'}${info.label}`} width={560} okText={simplified ? '保存此行' : '写入草稿'} onCancel={() => setEditing(null)} onOk={() => { void form.validateFields().then(fields => {
const source = kind === 'productLookup' ? fields.source.trim().toUpperCase() : fields.source.trim(); const secondary = fields.secondary?.trim() ?? '';
@@ -204,7 +200,7 @@ export function MappingDefinitionEditor({ value, onChange, simplified = false, r
type MatchCondition = { field: string; operator: string; value?: string };
type MatchRule = { id?: string; name: string; enabled: boolean; priority: number; stage: string; conditions: MatchCondition[]; target: string; value: string };
type MatchMetadata = { fields?: unknown[]; operators?: unknown[]; targets?: unknown[]; stages?: unknown[]; defaultRules?: unknown[]; limits?: Record<string, unknown> };
const defaultMatchRules: MatchRule[] = [{ id: 'vcm-category', name: '成品规格含VCM归工业电机-VCM', enabled: true, priority: 100, stage: 'all', conditions: [{ field: '成品规格', operator: 'CONTAINS', value: 'VCM' }], target: 'group', value: '工业电机-VCM' }];
const defaultMatchRules: MatchRule[] = [{ id: 'default-vcm-group', name: '成品规格含VCM归入工业电机-VCM', enabled: true, priority: 100, stage: 'all', conditions: [{ field: '成品规格', operator: 'CONTAINS', value: 'VCM' }], target: 'group', value: '工业电机-VCM' }];
const plainObject = (value: unknown) => value && typeof value === 'object' && !Array.isArray(value) ? value as Record<string, unknown> : {};
type MatchOption = { value: string; label: string };
const metadataOptions = (value: unknown, fallback: MatchOption[]): MatchOption[] => Array.isArray(value) && value.length
@@ -213,6 +209,7 @@ const metadataOptions = (value: unknown, fallback: MatchOption[]): MatchOption[]
export function MatchRulesDefinitionEditor({ value, onChange, metadata, readOnly = false }: DefinitionProps & { metadata?: MatchMetadata | null; readOnly?: boolean }) {
const [editing, setEditing] = useState<MatchRule | null>(null);
const [search,setSearch]=useState('');
const [error, setError] = useState('');
const fields = metadataOptions(metadata?.fields, [
{ value: 'PDT代码', label: 'PDT代码' }, { value: '销售片区', label: '销售片区' }, { value: '客户代码', label: '客户代码' },
@@ -244,8 +241,8 @@ export function MatchRulesDefinitionEditor({ value, onChange, metadata, readOnly
};
const operatorDescription = (operator: string) => ['IS_BLANK', 'IS_NOT_BLANK'].includes(operator) ? '' : operator;
return <div className="structured-config-editor structured-match-editor">
<div className="structured-config-toolbar"><span>按优先级从小到大执行;同一目标类型只采用首条命中规则。条件之间按“且”组合。</span><div className="structured-config-actions">{!readOnly && <Button type="primary" icon={<PlusOutlined />} onClick={() => open()}>新增匹配规则</Button>}</div></div>
<Table size="small" bordered rowKey="id" dataSource={rules} pagination={false} scroll={{ x: 1080 }} columns={[
<div className="structured-config-toolbar"><Input.Search aria-label="搜索条件匹配规则" placeholder="规则名称、条件或目标分类" value={search} allowClear onChange={event=>setSearch(event.target.value)}/><span>{rules.length} 条规则</span><div className="structured-config-actions"><ConfigurationWorkbookActions value={value} onChange={onChange} scope="matchRules" label="条件匹配规则" defaults={metadata?.defaultRules??defaultMatchRules} readOnly={readOnly} />{!readOnly && <Button type="primary" icon={<PlusOutlined />} onClick={() => open()}>新增匹配规则</Button>}</div></div>
<Table size="small" bordered rowKey="id" dataSource={rules.filter(rule=>(rule.name+' '+readableRule(rule)).toLowerCase().includes(search.toLowerCase()))} pagination={{ pageSize: 15, showSizeChanger: false }} scroll={{ x: 1080 }} columns={[
{ title: '顺序', dataIndex: 'priority', width: 84, align: 'right', sorter: (a, b) => a.priority - b.priority },
{ title: '规则名称', dataIndex: 'name', width: 220 },
{ title: '匹配条件', width: 410, render: (_, row) => <span className="structured-match-readable">{readableRule(row)}</span> },
@@ -259,7 +256,9 @@ export function MatchRulesDefinitionEditor({ value, onChange, metadata, readOnly
if (!next.name) { setError('请填写规则名称'); return; }
if (!next.conditions.length || next.conditions.some(condition => !condition.field || (!['IS_BLANK', 'IS_NOT_BLANK'].includes(condition.operator) && !condition.value))) { setError('请补全每条条件的字段和判断值'); return; }
if (!next.value) { setError('请填写匹配结果'); return; }
commit(editing.id ? rules.map(rule => rule.id === editing.id ? next : rule) : [...rules, { ...next, id: `match-${crypto.randomUUID()}` }]); setEditing(null);
const updated=editing.id ? rules.map(rule => rule.id === editing.id ? next : rule) : [...rules, { ...next, id: `match-${crypto.randomUUID()}` }];
const conflicts=matchRuleConflicts(updated);if(conflicts.length){setError(conflicts.join(';'));return;}
commit(updated); setEditing(null);
}}>
{editing && <Form layout="vertical"><div className="structured-config-form-grid"><Form.Item label="规则名称" required><Input value={editing.name} maxLength={160} onChange={event => setEditing({ ...editing, name: event.target.value })} /></Form.Item><Form.Item label="优先级" required><InputNumber min={-10000} max={10000} style={{ width: '100%' }} value={editing.priority} onChange={priority => setEditing({ ...editing, priority: Number(priority ?? 0) })} /></Form.Item><Form.Item label="阶段"><Select value={editing.stage} options={stages} onChange={stage => setEditing({ ...editing, stage })} /></Form.Item><Form.Item label="状态"><Switch checked={editing.enabled} checkedChildren="启用" unCheckedChildren="停用" onChange={enabled => setEditing({ ...editing, enabled })} /></Form.Item></div>
<div className="structured-config-toolbar"><strong>命中条件(条件之间按“且”组合)</strong><Button icon={<PlusOutlined />} onClick={() => setEditing({ ...editing, conditions: [...editing.conditions, { field: '', operator: 'EQUALS', value: '' }] })}>新增条件</Button></div>
@@ -306,10 +305,9 @@ export function MasterDataDefinitionEditor({ value, onChange, publishedNames, si
if (selectedDepartment && !departmentOptions.some(option => option.value === selectedDepartment)) departmentOptions.push({ value: selectedDepartment, label: `${selectedDepartment}(未在产业部名录中)`, disabled: true });
const visibleRows = rows.filter(row => `${row.code} ${row.name} ${kind === 'groups' ? departmentFor(row) : ''}`.toLowerCase().includes(search.toLowerCase()));
return <div className="structured-config-editor">
<div className="structured-config-intro"><p>维护产业部、业务分组、产品及项目组的统一名录。编码与名称必须唯一。</p>{!simplified && <Button onClick={loadExisting}>从当前配置及对应关系载入名录</Button>}</div>
<Tabs activeKey={kind} onChange={key => { setKind(key as MasterKind); setSearch(''); }} items={masterKinds.map(key => ({ key, label: `${masterLabels[key]}(${masterRows(value, key).length})` }))} />
<div className="structured-config-toolbar"><Input.Search value={search} onChange={event => setSearch(event.target.value)} placeholder="查找编码、名称或所属产业部" allowClear aria-label={`查找${label}`} /><span>{rows.length} 条 · 启用 {rows.filter(row => row.enabled).length}</span><div className="structured-config-actions"><Button icon={<DownloadOutlined />} onClick={() => downloadCsv(`${label}名录.csv`, [csvHeaders, ...rows.map(row => [row.code, row.name, row.enabled, ...(kind === 'groups' ? [departmentFor(row)] : [])])])}>导出 CSV</Button>{!readOnly && <><Button icon={<UploadOutlined />} onClick={() => setImporting(true)}>批量导入</Button><Button type="primary" icon={<PlusOutlined />} onClick={() => openRow()}>新增{label}</Button></>}</div></div>
<Table size="small" bordered rowKey="code" columns={columns} dataSource={visibleRows} pagination={{ pageSize: 10, showSizeChanger: false }} scroll={{ x: kind === 'groups' ? 940 : 720 }} locale={{ emptyText: `尚未维护${label}名录` }} />
{!simplified && <div className="structured-config-intro"><p>维护产业部、业务分组、产品及项目组的统一名录。编码与名称必须唯一。</p>{!simplified && <Button onClick={loadExisting}>从当前配置及对应关系载入名录</Button>}</div>}
<div className="structured-config-toolbar"><label className="config-type-filter"><span>名录类型</span><Select {...readableSelectPopup} aria-label="名录类型" value={kind} options={masterKinds.map(key=>({value:key,label:masterLabels[key]}))} onChange={key=>{setKind(key);setSearch('');}} /></label><Input.Search value={search} onChange={event => setSearch(event.target.value)} placeholder="查找编码、名称或所属产业部" allowClear aria-label={`查找${label}`} /><span>{rows.length} 条 · 启用 {rows.filter(row => row.enabled).length}</span><div className="structured-config-actions"><ConfigurationWorkbookActions value={value} onChange={onChange} scope={kind} label={`${label}名录`} readOnly={readOnly} validate={validateStructuredMappingDefinition} onPaste={()=>setImporting(true)} />{!readOnly && <><Button type="primary" icon={<PlusOutlined />} onClick={() => openRow()}>新增{label}</Button></>}</div></div>
<Table size="small" bordered rowKey="code" columns={columns} dataSource={visibleRows} pagination={{ pageSize: 15, showSizeChanger: false }} scroll={{ x: kind === 'groups' ? 940 : 720 }} locale={{ emptyText: `尚未维护${label}名录` }} />
<p className="structured-config-note">停用项保留在名录中;启用中的对应关系如引用停用名称,校验将指出具体位置。已有分析记录保留生成时使用的名称。</p>
<Modal className="management-modal structured-config-modal" open={Boolean(editing)} title={`${originalCode === null ? '新增' : '编辑'}${label}`} width={520} okText={simplified ? '保存此行' : '写入草稿'} onCancel={() => setEditing(null)} onOk={() => { void form.validateFields().then(fields => {
const existing = originalCode === null ? undefined : rows.find(item => item.code === originalCode);
@@ -356,12 +354,13 @@ const zeroPolicyLabels: Record<string, string> = { ONE: '返回 1', ZERO: '返
const operatorLabels: Record<string, string> = { EQ: '等于', NE: '不等于', CONTAINS: '包含', NOT_CONTAINS: '不包含', IS_BLANK: '为空', IS_NOT_BLANK: '不为空', GT: '大于', GTE: '大于等于', LT: '小于', LTE: '小于等于' };
function stringList(value: unknown): string[] { return Array.isArray(value) ? value.map(String) : []; }
export function RuleDefinitionEditor({ value, onChange, schema, schemaError }: DefinitionProps & { schema: Record<string, unknown> | null; schemaError: string }) {
export function RuleDefinitionEditor({ value, onChange, schema, schemaError, showWorkbookActions = true }: DefinitionProps & { schema: Record<string, unknown> | null; schemaError: string; showWorkbookActions?: boolean }) {
const [tab, setTab] = useState('source'); const [editing, setEditing] = useState<number | null>(null); const [ruleError, setRuleError] = useState('');
const [form] = Form.useForm<{ stage: string; target: string; formula: string; nullPolicy: string; zeroDenominatorPolicy: string; nullResultPolicy: string; conditional: boolean; conditionField: string; conditionOperator: string; conditionValue: string; otherwiseFormula: string }>();
const selectedStage = Form.useWatch('stage', form) || 'black'; const conditional = Form.useWatch('conditional', form); const conditionOperator = Form.useWatch('conditionOperator', form);
if (!schema) return <Alert showIcon type={schemaError ? 'error' : 'info'} message={schemaError ? '规则编辑结构读取失败' : '正在读取规则编辑结构'} description={schemaError || '编辑项与可用公式变量由服务端提供。'} />;
const configured = value.schemaVersion === schema.schemaVersion;
const workbookActions=showWorkbookActions?<div className="structured-config-toolbar"><ConfigurationWorkbookActions value={configured?value:objectValue(schema.defaultDefinition)} onChange={onChange} scope="rules" label="计算规则" /></div>:null;
const fields = objectValue(schema.sourceFields); const targets = objectValue(schema.rowTargets); const componentLabels = objectValue(schema.componentLabels); const metricLabels = objectValue(schema.metricLabels); const baselineDescriptions = objectValue(schema.baselineMetricDescriptions);
const nullOptions = stringList(schema.nullPolicies).map(key => ({ value: key, label: policyLabels[key] || key })); const zeroOptions = stringList(schema.zeroDenominatorPolicies).map(key => ({ value: key, label: zeroPolicyLabels[key] || key }));
const nullResultOptions = stringList(schema.nullResultPolicies).map(key => ({ value: key, label: nullResultLabels[key] || key }));
@@ -382,14 +381,14 @@ export function RuleDefinitionEditor({ value, onChange, schema, schemaError }: D
{ title: '触发条件', key: 'when', width: 200, render: (_, row) => { const when = objectValue(row.when); return row.when ? `${String(objectValue(fields[String(row.stage)])[String(when.field)] || when.field)} ${operatorLabels[String(when.operator)] || when.operator} ${String(when.value ?? '')}` : '所有行'; } },
{ title: '操作', key: 'actions', width: 205, render: (_, row) => <span className="task-actions"><Button type="link" onClick={() => editRow(row.index)}>编辑</Button><Button type="link" disabled={row.index === 0} onClick={() => { const next = [...rowRules]; [next[row.index - 1], next[row.index]] = [next[row.index], next[row.index - 1]]; replaceRules(next); }}>上移</Button><Button type="link" disabled={row.index === rowRules.length - 1} onClick={() => { const next = [...rowRules]; [next[row.index], next[row.index + 1]] = [next[row.index + 1], next[row.index]]; replaceRules(next); }}>下移</Button><Button type="link" onClick={() => replaceRules(rowRules.filter((_, index) => index !== row.index))}>移除</Button></span> },
];
if (!configured) return <div className="structured-config-editor"><Alert showIcon type="info" message="当前草稿沿用原始公式" description="当前公式继续生效。需要调整字段或公式时,先转换为可维护规则;未覆盖的计算继续沿用原逻辑。" /><Button style={{ marginTop: 12 }} onClick={() => onChange(JSON.parse(JSON.stringify(objectValue(schema.defaultDefinition))) as ConfigDefinition)}>开始维护字段与公式</Button></div>;
return <div className="structured-config-editor">
if (!configured) return <div className="structured-config-editor">{workbookActions}<Alert showIcon type="info" message="当前草稿沿用原始公式" description="当前公式继续生效。需要调整字段或公式时,先转换为可维护规则;未覆盖的计算继续沿用原逻辑。" /><Button style={{ marginTop: 12 }} onClick={() => onChange(JSON.parse(JSON.stringify(objectValue(schema.defaultDefinition))) as ConfigDefinition)}>开始维护字段与公式</Button></div>;
return <div className="structured-config-editor">{workbookActions}
<Tabs activeKey={tab} onChange={setTab} items={[
{ key: 'source', label: '来源字段', children: <div><p className="structured-config-note" style={{ marginBottom: 12 }}>将计算字段对应到来源表列名。未修改项沿用原列名,字段别名用于公式引用。</p><Tabs items={['black', 'bright'].map(stage => ({ key: stage, label: stage === 'black' ? '黑片字段' : '亮片字段', children: <Table size="small" bordered rowKey="alias" pagination={false} scroll={{ x: 670 }} dataSource={Object.entries(objectValue(fields[stage])).map(([alias, header]) => ({ alias, header: String(header) }))} columns={[
{ key: 'source', label: '来源字段', children: <div><p className="structured-config-note" style={{ marginBottom: 12 }}>将计算字段对应到来源表列名。未修改项沿用原列名,字段别名用于公式引用。</p><Tabs items={['black', 'bright'].map(stage => ({ key: stage, label: stage === 'black' ? '黑片字段' : '亮片字段', children: <Table size="small" bordered rowKey="alias" pagination={{pageSize:15,showSizeChanger:false}} scroll={{ x: 670 }} dataSource={Object.entries(objectValue(fields[stage])).map(([alias, header]) => ({ alias, header: String(header) }))} columns={[
{ title: '计算字段', dataIndex: 'header', width: 240 }, { title: '公式别名', dataIndex: 'alias', width: 200, render: alias => <code>{alias}</code> },
{ title: '来源表列名', key: 'source', render: (_, row) => <Input aria-label={`${stage === 'black' ? '黑片' : '亮片'} ${row.header}来源列名`} value={String(objectValue(sourceFields[stage])[row.alias] ?? row.header)} maxLength={200} onChange={event => onChange({ ...value, sourceFields: { ...sourceFields, [stage]: { ...objectValue(sourceFields[stage]), [row.alias]: event.target.value } } })} /> },
]} /> }))} /></div> },
{ key: 'rows', label: `行计算(${rowRules.length})`, children: <div><div className="structured-config-toolbar"><span>按顺序执行;未覆盖的结果沿用原计算。</span><div className="structured-config-actions"><Button type="primary" icon={<PlusOutlined />} disabled={rowRules.length >= maxRowRules} onClick={() => editRow(null)}>新增行公式</Button></div></div><Table size="small" bordered rowKey="index" columns={ruleColumns} dataSource={rowRules.map((row, index) => ({ ...row, index }))} pagination={false} scroll={{ x: 980 }} locale={{ emptyText: '当前全部沿用原始行计算' }} /><p className="structured-config-note">修改报废等分量后,如需改变合计结果,请同时维护依赖它的合计公式。</p></div> },
{ key: 'rows', label: `行计算(${rowRules.length})`, children: <div><div className="structured-config-toolbar"><span>按顺序执行;未覆盖的结果沿用原计算。</span><div className="structured-config-actions"><Button type="primary" icon={<PlusOutlined />} disabled={rowRules.length >= maxRowRules} onClick={() => editRow(null)}>新增行公式</Button></div></div><Table size="small" bordered rowKey="index" columns={ruleColumns} dataSource={rowRules.map((row, index) => ({ ...row, index }))} pagination={{pageSize:15,showSizeChanger:false}} scroll={{ x: 980 }} locale={{ emptyText: '当前全部沿用原始行计算' }} /><p className="structured-config-note">修改报废等分量后,如需改变合计结果,请同时维护依赖它的合计公式。</p></div> },
{ key: 'metrics', label: '指标公式', children: <div className="structured-config-rule-list"><p className="structured-config-note">未开启覆盖的指标沿用原始公式。公式可引用重量变量及排在前面的指标结果。</p>{metricCodes.map((metric, index) => {
const rule = objectValue(metricRules[metric]); const enabled = Object.hasOwn(metricRules, metric);
const updateMetric = (patch: Record<string, unknown>) => onChange({ ...value, metricRules: { ...metricRules, [metric]: { ...rule, ...patch } } });
+20 -12
View File
@@ -1,10 +1,11 @@
import { useCallback, useEffect, useState } from 'react';
import { Alert, App, Button, Drawer, Empty, Progress, Select, Spin, Tabs, Tag } from 'antd';
import { ReloadOutlined, SettingOutlined } from '@ant-design/icons';
import { ManagementSections } from './ManagementSections';
import { useCallback, useEffect, useState, useRef } from 'react';
import { Alert, App, Button, Drawer, Empty, Progress, Select, Spin, Tag } from 'antd';
import { ReloadOutlined } from '@ant-design/icons';
import { QualityTable as Table } from './QualityTable';
import type { ColumnsType } from 'antd/es/table';
import { api } from './api';
import { usePageCache } from './pageCache';
import { usePageCache, useMaintenanceRefresh } from './pageCache';
import type { AuthUser } from './types';
import { BackupOperationsPanel, OperationsMonitorPanel } from './GovernanceCompletion';
import { configurationDateTime } from './configurationName';
@@ -12,12 +13,14 @@ import { backupSummary, isActive, isFailed, monitorLabel, orderedTasks, taskFail
export function TasksMonitorPage({user,onUnauthorized}:{user:AuthUser;onUnauthorized:(error:unknown)=>boolean}) {
const {message}=App.useApp();
const {active,refreshKey}=usePageCache();
const {active}=usePageCache();
const [busy,setBusy]=useState(false);
const [items,setItems]=useState<MonitorRow[]>([]);
const [backup,setBackup]=useState<MonitorRow>({});
const [taskError,setTaskError]=useState(''); const [backupError,setBackupError]=useState('');
const [loading,setLoading]=useState(true); const [advanced,setAdvanced]=useState(false);
const [loading,setLoading]=useState(true);
const loaded = useRef(false);
const [section,setSection] = useState('tasks');
const [filter,setFilter]=useState('all'); const [selected,setSelected]=useState<MonitorRow|null>(null);
const canManage=user.roles.some(role=>['SYSTEM_ADMIN','SYSTEM_OPERATOR'].includes(role));
const load=useCallback(async()=>{
@@ -27,8 +30,9 @@ export function TasksMonitorPage({user,onUnauthorized}:{user:AuthUser;onUnauthor
api.latestBackup().then(value=>{setBackup(value);setBackupError('');}).catch(error=>{if(!onUnauthorized(error))setBackupError(error instanceof Error?error.message:'备份状态读取失败');}),
]);setLoading(false);
},[onUnauthorized]);
useEffect(()=>{if(active)void load();},[load,active,refreshKey]);
useEffect(()=>{if(!active)return;const timer=window.setInterval(()=>{if(document.visibilityState==='visible')void load();},15000);return()=>window.clearInterval(timer);},[load,active]);
useEffect(()=>{if(!loaded.current){loaded.current=true;void load();}},[load]);
useMaintenanceRefresh(load);
useEffect(()=>{if(!active || !['tasks','latestBackup'].includes(section))return;const timer=window.setInterval(()=>{if(document.visibilityState==='visible')void load();},15000);return()=>window.clearInterval(timer);},[load,active,section]);
const runAction=async(row:MonitorRow,action:'retry'|'cancel')=>{
setBusy(true);
try {
@@ -47,16 +51,20 @@ export function TasksMonitorPage({user,onUnauthorized}:{user:AuthUser;onUnauthor
{title:'',key:'action',width:75,render:(_,row)=><Button type="link" onClick={()=>setSelected(row)}>详情</Button>},
];
return <>
<section className="panel section-panel">
<ManagementSections onSelect={setSection} items={[
{ key:'tasks', label:'任务进度', children: <> <section className="panel section-panel">
<div className="completion-panel-heading"><strong>任务进度</strong><div className="completion-actions"><Select aria-label="筛选任务" value={filter} onChange={setFilter} options={[{value:'all',label:'全部任务'},{value:'active',label:'进行中'},{value:'failed',label:'失败 / 未完成'},{value:'completed',label:'已完成'}]}/><Button icon={<ReloadOutlined/>} loading={loading} onClick={()=>void load()}>刷新</Button></div></div>
{taskError&&<Alert showIcon type="error" message="任务暂不可用" description={taskError}/>}
{loading&&!items.length?<Spin/>:!taskError&&<Table size="small" bordered columns={columns} dataSource={orderedTasks(items,filter)} rowKey={row=>`${row.sourceKind}-${row.id}`} pagination={{pageSize:8,showSizeChanger:false}} scroll={{x:1000}} locale={{emptyText:<Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="暂无符合条件的任务"/>}}/>}
{loading&&!items.length?<Spin/>:!taskError&&<Table size="small" bordered columns={columns} dataSource={orderedTasks(items,filter)} rowKey={row=>`${row.sourceKind}-${row.id}`} pagination={{pageSize:15,showSizeChanger:false}} scroll={{x:1000}} locale={{emptyText:<Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="暂无符合条件的任务"/>}}/>}
<p className="completion-note">每类最多显示 50 条记录,进行中及失败任务优先。智能分析仅显示本人任务。</p>
</section>
<section className="panel section-panel"><div className="completion-panel-heading"><strong>最近备份状态</strong></div>
</> },
{ key:'latestBackup', label:'最近备份', children: <> <section className="panel section-panel"><div className="completion-panel-heading"><strong>最近备份状态</strong></div>
{backupError?<Alert showIcon type="error" message="备份状态暂不可用" description={backupError}/>:loading&&!latest.createdAt?<Spin/>:<div className="governance-monitor-grid"><div><small>最近备份</small><strong>{backupSummary(latest)}</strong></div><div><small>备份时间</small><strong>{configurationDateTime(latest.createdAt)||'—'}</strong></div><div><small>恢复验证时间</small><strong>{configurationDateTime(latest.restoreVerifiedAt)||'—'}</strong></div><div><small>自动备份</small><strong>{backup.planActive===true?'已启用':'未启用'}</strong></div></div>}
</section>
{canManage&&<><Button icon={<SettingOutlined/>} aria-expanded={advanced} onClick={()=>setAdvanced(!advanced)}>{advanced?'收起高级设置':'管理员高级设置'}</Button>{advanced&&<Tabs items={[{key:'monitor',label:'运行监控与告警',children:<OperationsMonitorPanel user={user} onUnauthorized={onUnauthorized}/>},{key:'backups',label:'备份管理',children:<BackupOperationsPanel user={user} onUnauthorized={onUnauthorized}/>} ]}/>}</>}
</> },
...(canManage ? [{ key:'monitor', label:'运行监控与告警', children:<OperationsMonitorPanel user={user} onUnauthorized={onUnauthorized}/> },{ key:'backups', label:'备份管理', children:<BackupOperationsPanel user={user} onUnauthorized={onUnauthorized}/> }] : []),
]} />
<Drawer open={!!selected} title="任务详情" width={Math.min(620,window.innerWidth)} onClose={()=>setSelected(null)}>{selected&&<dl className="nc-form-grid"><dt>任务</dt><dd>{monitorLabel(selected.type,'后台任务')}</dd><dt>状态</dt><dd>{monitorLabel(selected.status)}</dd><dt>阶段</dt><dd>{monitorLabel(selected.stage,monitorLabel(selected.status))}</dd>{Number(selected.total)>0&&<><dt>已处理 / 总数</dt><dd>{String(selected.processed??0)} / {String(selected.total)}</dd></>}<dt>提交时间</dt><dd>{configurationDateTime(selected.createdAt)}</dd>{isFailed(selected)&&<><dt>失败原因</dt><dd style={{whiteSpace:'pre-wrap',overflowWrap:'anywhere'}}>{taskFailure(selected)}</dd></>}<dt>任务编号</dt><dd style={{overflowWrap:'anywhere'}}>{String(selected.id)}</dd></dl>}{selected?.sourceKind==='TASK'&&<div className="completion-actions">{selected.status==='FAILED'&&!!selected.importRunId&&user.roles.some(role=>['SYSTEM_ADMIN','DATA_MAINTAINER'].includes(role))&&<Button loading={busy} onClick={()=>void runAction(selected,'retry')}>重试导入</Button>}{selected.status==='QUEUED'&&user.roles.some(role=>['SYSTEM_ADMIN','SYSTEM_OPERATOR','DATA_MAINTAINER'].includes(role))&&<Button danger loading={busy} onClick={()=>void runAction(selected,'cancel')}>取消排队任务</Button>}</div>}{selected?.sourceKind==='ANALYSIS'&&!!selected.sessionId&&<Button href={`?page=analysis&sessionId=${encodeURIComponent(String(selected.sessionId))}&turnId=${encodeURIComponent(String(selected.id))}`}>查看分析记录</Button>}</Drawer>
</>;
}
+9 -14
View File
@@ -242,8 +242,15 @@ export const api = {
importIssues(id: string, page = 1, pageSize = 50) {
return request<Collection>(`/api/import-runs/${encodeURIComponent(id)}/issues?page=${page}&pageSize=${pageSize}`);
},
mappingIssues(params: { importRunId?: string; status?: string; page?: number; pageSize?: number } = {}) {
return request<Collection>(`/api/governance/mapping-issues?${toQuery({ page: 1, pageSize: 50, ...params })}`);
classificationMetadata() { return request<{ defaultRules: unknown[] }>('/api/governance/mapping-issues/classification-metadata'); },
previewClassification(body: {stage:string;target:string;conditions:{field:string;operator:string;value:string}[]}) {
return queryPost<Record<string,unknown>>('/api/governance/mapping-issues/classification-preview',body);
},
unclassifiedSources(page = 1, pageSize = 50) {
return request<Collection>(`/api/governance/mapping-issues/unclassified?${toQuery({ page, pageSize })}`);
},
mappingIssues(params: { mappingType?: string; importRunId?: string; status?: string; page?: number; pageSize?: number; customerCode?: string; businessGroup?: string; department?: string } = {}) {
return request<Collection & { filterOptions?: { customerCodes: string[]; businessGroups: string[]; departments: string[] } }>(`/api/governance/mapping-issues?${toQuery({ page: 1, pageSize: 50, ...params })}`);
},
mappingIssue(id: string) {
return request<Record<string, unknown>>(`/api/governance/mapping-issues/${encodeURIComponent(id)}`);
@@ -559,18 +566,6 @@ export const api = {
governanceDependencyImpact(kind: 'rule' | 'mapping', id: string) {
return request<Record<string, unknown>>(`/api/governance/dependencies/${kind}/${encodeURIComponent(id)}/impact`);
},
costConfigurations() {
return request<{ items: Record<string, unknown>[] }>('/api/governance/cost-configurations');
},
createCostConfiguration(body: Record<string, unknown>) {
return mutate<Record<string, unknown>>('/api/governance/cost-configurations', body);
},
updateCostConfiguration(id: string, body: Record<string, unknown>) {
return mutate<Record<string, unknown>>(`/api/governance/cost-configurations/${encodeURIComponent(id)}`, body, 'PUT');
},
validateCostConfiguration(id: string, body: { expectedRowVersion: number }) {
return mutate<Record<string, unknown>>(`/api/governance/cost-configurations/${encodeURIComponent(id)}/validate`, body);
},
governanceBackups() {
return request<Record<string, unknown>>('/api/governance/backups');
},
+57
View File
@@ -0,0 +1,57 @@
export type ClassificationSelection = { kind: 'group' | 'projectGroup'; row: Record<string, unknown> };
type Definition = Record<string, unknown>;
export type ClassificationCondition = { field: string; operator: string; value: string };
export type ClassificationRuleOptions = { conditions?: ClassificationCondition[]; stage?: string; name?: string };
const text = (value: unknown) => value == null ? '' : String(value);
export function classificationConditions(row: Record<string, unknown>) {
return [['@department', row.department], ['@product', row.product], ['PDT代码', row.pdtCode], ['销售片区', row.salesRegion]].map(([field,value]) => ({field: String(field), operator: text(value) ? 'EQUALS' : 'IS_BLANK', value: text(value)}));
}
export function classificationSignature(stage: string, conditions: ClassificationCondition[]) {
return JSON.stringify([stage, (Array.isArray(conditions) ? conditions : []).map(c => [c.field, c.operator, c.value ?? '']).sort((a,b)=>JSON.stringify(a).localeCompare(JSON.stringify(b)))]);
}
export function classificationRuleId(row: Record<string, unknown>, options: ClassificationRuleOptions = {}) {
const scope = classificationSignature(options.stage ?? text(row.stage), options.conditions ?? classificationConditions(row));
// Stable identifier; editClassification verifies full conditions before replacing a rule.
return `classification-${Array.from(scope).reduce((a,c) => Math.imul(a ^ c.charCodeAt(0),16777619) >>> 0,2166136261).toString(16)}`;
}
export function matchRuleConflicts(rules: unknown[]): string[] {
const seen=new Map<string,string>();const errors:string[]=[];
for(const raw of rules){const rule=raw as Definition;if(rule.enabled===false)continue;
const key=JSON.stringify([rule.target,classificationSignature(String(rule.stage),rule.conditions as ClassificationCondition[])]);
const target=text(rule.value);if(seen.has(key)&&seen.get(key)!==target)errors.push(`相同条件同时指向“${seen.get(key)}”和“${target}”`);seen.set(key,target);
}
return errors;
}
export function editClassification(base: Definition, selection: ClassificationSelection, target: string, defaultRules: unknown[], options: ClassificationRuleOptions = {}): Definition {
target = target.trim();
if (!target) throw new Error('请填写目标分类');
if (target.length > 160) throw new Error('目标分类最多160字');
const result = structuredClone(base);
const {row,kind} = selection;
const catalog = (result.masterData as Definition | undefined)?.[kind === 'group' ? 'groups' : 'projectGroups'];
if (Array.isArray(catalog) && !catalog.some(item => item.name === target && item.enabled === true)) throw new Error('目标分类未登记或已停用,请先在基础数据中维护');
if (kind === 'projectGroup') {
if (row.mappingType !== 'PROJECT_GROUP' || !text(row.customerCode) || !text(row.businessGroup) || row.sourceKey !== `${row.customerCode}|${row.businessGroup}`) throw new Error('客户代码或业务分组缺失');
result.projectGroupLookup = { ...(result.projectGroupLookup as Definition ?? {}), [text(row.sourceKey)]: target };
const disabled = result.disabledMappings as Definition | undefined;
if (disabled && Array.isArray(disabled.projectGroupLookup)) disabled.projectGroupLookup = disabled.projectGroupLookup.filter(key => key !== row.sourceKey);
// Remove an interval override only when it would mask this explicit edit: require dedicated maintenance instead.
if (Array.isArray(result.effectiveRanges) && result.effectiveRanges.some((raw: unknown) => { const range = raw as Definition; return range.mappingType === 'PROJECT_GROUP' && range.sourceKey === row.sourceKey; })) throw new Error('此分类有期间规则,请在对应关系草稿中修改有效期后再保存');
} else {
if (!['black','bright'].includes(text(row.stage))) throw new Error('来源阶段无效');
const conditions = options.conditions ?? classificationConditions(row);
const stage = options.stage ?? text(row.stage);
if (!['all','black','bright'].includes(stage)) throw new Error('请选择适用范围');
if (!conditions.length || conditions.length > 4 || new Set(conditions.map(c=>c.field)).size !== conditions.length) throw new Error('请保留至少一项且不重复的匹配条件');
if (conditions.some(c=>!['@department','@product','PDT代码','销售片区'].includes(c.field) || !['EQUALS','IS_BLANK'].includes(c.operator) || (c.operator === 'EQUALS' && !c.value.trim()))) throw new Error('请检查匹配条件');
const rules = Array.isArray(result.matchRules) ? result.matchRules as Definition[] : structuredClone(defaultRules) as Definition[];
const id = classificationRuleId(row, {conditions,stage});
const prior = rules.find(rule => rule.id === id);
if (prior && classificationSignature(String(prior.stage), prior.conditions as ClassificationCondition[]) !== classificationSignature(stage,conditions)) throw new Error('规则标识冲突,请在条件匹配中维护');
const duplicate = rules.find(rule=>rule.enabled !== false && rule.target === 'group' && rule.value !== target && classificationSignature(String(rule.stage), rule.conditions as ClassificationCondition[]) === classificationSignature(stage,conditions));
if (duplicate) throw new Error(`相同条件已指向“${String(duplicate.value)}”,请在条件匹配中编辑已有规则`);
result.matchRules = [{ id, name: options.name?.trim() || `${target}分类规则`, enabled: true, priority: -10000 + (4-conditions.length)*100 + (stage === 'all' ? 10 : 0), stage, conditions, target: 'group', value: target }, ...rules.filter(rule => rule.id !== id)];
if ((result.matchRules as unknown[]).length > 100) throw new Error('条件规则已满100条,请先整理规则');
}
return result;
}
+174
View File
@@ -0,0 +1,174 @@
/** Editable Excel exchange for configuration. Import never publishes or mutates the supplied base. */
export type Definition = Record<string, unknown>;
export type ConfigurationScope = 'all'|'catalogs'|'lookups'|'matchRules'|'rules'|'filterFields'|keyof typeof CATALOGS|keyof typeof LOOKUPS;
export type ConfigurationTable = { name: string; headers: string[]; rows: string[][] };
export type ImportMode = 'MERGE'|'REPLACE';
const CATALOGS = { departments:'产业部名录',groups:'业务分组名录',products:'产品名录',projectGroups:'项目组名录' } as const;
const LOOKUPS = {
categoryLookup:{name:'业务分组对应',columns:['PDT代码','销售区域'],master:'groups'},
projectGroupLookup:{name:'项目组对应',columns:['客户代码','业务分组'],master:'projectGroups'},
departmentLookup:{name:'产业部对应',columns:['来源产业部'],master:'departments'},
productLookup:{name:'产品对应',columns:['来源产品'],master:'products'},
} as const;
const STAGES:Record<string,string>={all:'黑片和亮片',black:'黑片',bright:'亮片'};
const TARGETS:Record<string,string>={department:'产业部',group:'业务分组',product:'产品',projectGroup:'项目组'};
const FIELDS:Record<string,string>={'@department':'产业部','@product':'产品','@group':'当前业务分组','@projectGroup':'当前项目组','销售片区':'销售区域'};
const OPERATORS:Record<string,string>={EQUALS:'等于',NOT_EQUALS:'不等于',CONTAINS:'包含',STARTS_WITH:'开头为',IS_BLANK:'为空',IS_NOT_BLANK:'不为空'};
const object=(value:unknown):Definition=>value&&typeof value==='object'&&!Array.isArray(value)?value as Definition:{};
const array=(value:unknown):Definition[]=>Array.isArray(value)?value.map(object):[];
const text=(value:unknown)=>value==null?'':String(value);
const state=(value:unknown)=>value===false?'停用':'启用';
const yes=(value:unknown)=>value?'是':'否';
const reverse=(labels:Record<string,string>,value:string)=>Object.entries(labels).find(([,label])=>label===value)?.[0]??value;
const equal=(a:unknown,b:unknown)=>JSON.stringify(a)===JSON.stringify(b);
function enabled(value:string,fallback=true):boolean {
if(!value.trim())return fallback;
if(['启用','是','true','1'].includes(value.trim().toLowerCase()))return true;
if(['停用','否','false','0'].includes(value.trim().toLowerCase()))return false;
throw new Error(`状态“${value}”无效,请填写启用/停用或是/否`);
}
function number(value:string,label:string,min:number,max:number){const n=Number(value);if(!value||!Number.isInteger(n)||n<min||n>max)throw new Error(`${label}须为${min}至${max}的整数`);return n;}
function unique<T>(rows:T[],key:(row:T)=>string,label:string){const keys=new Set<string>();for(const row of rows){const id=key(row);if(keys.has(id))throw new Error(`${label}存在重复项:${id}`);keys.add(id);}}
function merge<T>(base:T[],incoming:T[],key:(row:T)=>string,mode:ImportMode){if(mode==='REPLACE')return incoming;const index=new Map(incoming.map(row=>[key(row),row]));const existing=new Set(base.map(key));return [...base.map(row=>index.get(key(row))??row),...incoming.filter(row=>!existing.has(key(row)))];}
function include(scope:ConfigurationScope,key:string,group:string){return scope==='all'||scope===key||scope===group;}
export function configurationTables(value:Definition,scope:ConfigurationScope='all',defaults:unknown[]=[]):ConfigurationTable[]{
const tables:ConfigurationTable[]=[];
for(const [key,name] of Object.entries(CATALOGS))if(include(scope,key,'catalogs'))tables.push({name,headers:['编码','名称','状态',...(key==='groups'?['所属产业部']:[])],
rows:array(object(value.masterData)[key]).map(row=>[text(row.code),text(row.name),state(row.enabled),...(key==='groups'?[text(row.department)]:[])])});
for(const [key,info] of Object.entries(LOOKUPS))if(include(scope,key,'lookups')){
const disabled=object(value.disabledMappings)[key];const stopped=new Set(Array.isArray(disabled)?disabled:[]);
tables.push({name:info.name,headers:[...info.columns,'目标分类','明确不设归属','状态'],rows:Object.entries(object(value[key])).map(([source,target])=>{
const index=info.columns.length===2?source.indexOf('|'):-1;
return [...(info.columns.length===2?[index<0?source:source.slice(0,index),index<0?'':source.slice(index+1)]:[source]),text(target),yes(target===null),stopped.has(source)?'停用':'启用'];
})});
}
if(scope==='all'||scope==='matchRules'){
const rules=Array.isArray(value.matchRules)?value.matchRules:defaults;
tables.push({name:'条件匹配',headers:['规则编号','规则名称','优先级','适用范围','目标类型','目标分类','状态','条件序号','条件字段','字段说明','判断方式','判断值'],rows:array(rules).flatMap((rule,index)=>array(rule.conditions).map((condition,n)=>[
text(rule.id)||`match-${index+1}`,text(rule.name),text(rule.priority),STAGES[text(rule.stage)]??text(rule.stage),TARGETS[text(rule.target)]??text(rule.target),text(rule.value),state(rule.enabled),String(n+1),text(condition.field),FIELDS[text(condition.field)]??text(condition.field),OPERATORS[text(condition.operator)]??text(condition.operator),text(condition.value)]))});
}
if(scope==='all'){
tables.push({name:'组织层级',headers:['组织编码','上级组织编码'],rows:array(value.organizationHierarchy).map(row=>[text(row.code),text(row.parentCode)])});
tables.push({name:'对应有效期',headers:['映射类型','源条件','起始月','结束月','使用默认对应','目标分类','明确不设归属'],rows:array(value.effectiveRanges).map(row=>[text(row.mappingType)||'CATEGORY',text(row.sourceKey),text(row.fromMonth),text(row.toMonth),yes(!Object.hasOwn(row,'mappedValue')),text(row.mappedValue),yes(Object.hasOwn(row,'mappedValue')&&row.mappedValue===null)])});
}
if(scope==='filterFields')tables.push({name:'查询字段',headers:['字段编号','筛选名称','黑片来源列','亮片来源列','允许空值','状态','排序'],rows:array(value.filterFields).map(row=>[text(row.key),text(row.label),text(row.blackColumn),text(row.brightColumn),yes(row.allowEmpty),state(row.enabled),text(row.sortOrder??0)])});
if(scope==='rules'){
tables.push({name:'计算来源字段',headers:['阶段','公式别名','来源列名'],rows:Object.entries(object(value.sourceFields)).flatMap(([stage,fields])=>Object.entries(object(fields)).map(([alias,header])=>[STAGES[stage]??stage,alias,text(header)]))});
tables.push({name:'行计算公式',headers:['顺序','阶段','目标字段','公式','空值策略','零分母策略','空结果策略','条件字段','判断方式','判断值'],rows:array(value.rowRules).map((row,n)=>{const condition=object(row.when);return[String(n+1),STAGES[text(row.stage)]??text(row.stage),text(row.target),text(row.formula),text(row.nullPolicy),text(row.zeroDenominatorPolicy),text(row.nullResultPolicy),text(condition.field),text(condition.operator),text(condition.value)];})});
tables.push({name:'指标计算公式',headers:['指标编码','公式','空值策略','零分母策略','空结果策略'],rows:Object.entries(object(value.metricRules)).map(([key,raw])=>{const row=object(raw);return[key,text(row.formula),text(row.nullPolicy),text(row.zeroDenominatorPolicy),text(row.nullResultPolicy)];})});
}
return tables;
}
export function importConfigurationTables(base:Definition,tables:ConfigurationTable[],scope:ConfigurationScope,mode:ImportMode,defaults:unknown[]=[]){
const result=structuredClone(base);const expected=configurationTables(base,scope,defaults);const changes:{name:string;rows:number;added:number;updated:number;removed:number}[]=[];
const names=new Set<string>();
for(const table of tables){
const original=expected.find(item=>item.name===table.name);if(!original)throw new Error(`工作表“${table.name}”不属于当前配置类型`);
if(names.has(table.name))throw new Error(`重复工作表:${table.name}`);names.add(table.name);
if(!equal(table.headers,original.headers))throw new Error(`${table.name}表头不匹配,请使用本页面导出的表格`);
if(table.rows.length>20000)throw new Error(`${table.name}超过20000行上限`);
table.rows.forEach((row,n)=>{if(row.length!==table.headers.length)throw new Error(`${table.name}第${n+2}行列数不匹配`);if(row.some(cell=>cell.length>4000))throw new Error(`${table.name}第${n+2}行内容过长`);});
if(equal(table.rows,original.rows)){changes.push({name:table.name,rows:table.rows.length,added:0,updated:0,removed:0});continue;}
if(!table.rows.length&&mode==='MERGE'){changes.push({name:table.name,rows:0,added:0,updated:0,removed:0});continue;}
const catalog=Object.entries(CATALOGS).find(([,name])=>name===table.name);
const lookup=Object.entries(LOOKUPS).find(([,info])=>info.name===table.name);
let oldIds:string[]=[],newIds:string[]=[];
if(catalog){
const [kind]=catalog;const previous=array(object(base.masterData)[kind]);const byCode=new Map(previous.map(row=>[text(row.code),row]));oldIds=previous.map(row=>text(row.code));
const incoming=table.rows.map(([code,name,status,department],n)=>{
if(!code||!name||code.length>100||name.length>190)throw new Error(`${table.name}第${n+2}行编码或名称无效`);
const old=byCode.get(code);const row:Definition={...old,code,name,enabled:enabled(status,old?.enabled!==false)};
if(kind==='groups'&&(department!==text(old?.department)||Object.hasOwn(old??{},'department')||(!old&&department)))row.department=department;
return row;
});unique(incoming,row=>text(row.code),table.name);newIds=incoming.map(row=>text(row.code));
const merged=merge(previous,incoming,row=>text(row.code),mode);unique(merged,row=>text(row.name),table.name+'名称');
result.masterData={...object(result.masterData),[kind]:merged};
}else if(lookup){
const [kind,info]=lookup;const previous=object(base[kind]);oldIds=Object.keys(previous);const disabled=object(base.disabledMappings)[kind];const stopped=new Set(Array.isArray(disabled)?disabled.map(String):[]);
const incoming=table.rows.map((cells,n)=>{const key=cells.slice(0,info.columns.length).join('|');const target=cells[info.columns.length];const unassigned=enabled(cells[info.columns.length+1],false);const active=enabled(cells[info.columns.length+2]);
if(!cells[0]&&!Object.hasOwn(previous,key))throw new Error(`${table.name}第${n+2}行来源编码不能为空`);
if(info.columns.length===2&&cells.slice(0,2).some(cell=>cell.includes('|')))throw new Error(`${table.name}第${n+2}行请分列填写来源条件`);
if(!unassigned&&!target)throw new Error(`${table.name}第${n+2}行目标分类必填,或将明确不设归属设为是`);
return{key,target:unassigned?null:target,enabled:active};});unique(incoming,row=>row.key,table.name);newIds=incoming.map(row=>row.key);
result[kind]={...(mode==='MERGE'?previous:{}),...Object.fromEntries(incoming.map(row=>[row.key,row.target]))};
const inactive=new Set(mode==='MERGE'?stopped:[]);incoming.forEach(row=>{if(row.enabled)inactive.delete(row.key);else inactive.add(row.key);});
result.disabledMappings={...object(result.disabledMappings),[kind]:[...inactive]};
}else if(table.name==='条件匹配'){
const previous=array(Array.isArray(base.matchRules)?base.matchRules:defaults);oldIds=previous.map(row=>text(row.id));const byId=new Map<string,Definition>();
for(const [n,cells] of table.rows.entries()){
const [id,name,priority,stageLabel,targetLabel,value,status,position,fieldLabel,_fieldDescription,operatorLabel,conditionValue]=cells;
const stage=reverse(STAGES,stageLabel),target=reverse(TARGETS,targetLabel),field=fieldLabel,operator=reverse(OPERATORS,operatorLabel);
if(!/^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/.test(id)||!name||!value||name.length>200||value.length>200)throw new Error(`条件匹配第${n+2}行规则编号、名称或目标分类无效`);
if(!STAGES[stage]||!TARGETS[target]||!OPERATORS[operator]||!field||(field.startsWith('@')&&!Object.hasOwn(FIELDS,field))||field.length>200||(!['IS_BLANK','IS_NOT_BLANK'].includes(operator)&&!conditionValue)||conditionValue.length>200)throw new Error(`条件匹配第${n+2}行条件无效`);
if(['IS_BLANK','IS_NOT_BLANK'].includes(operator)&&conditionValue)throw new Error(`条件匹配第${n+2}行空值判断不应填写判断值`);
const metadata={id,name,priority:number(priority,'优先级',-10000,10000),stage,target,value,enabled:enabled(status)};
const row=byId.get(id);if(row&&!equal({...row,conditions:undefined},{...metadata,conditions:undefined}))throw new Error(`规则${id}各行的名称、目标、优先级或状态不一致`);
const rule=row??{...metadata,conditions:[]};const conditions=rule.conditions as Definition[];const order=number(position,'条件序号',1,20);
if(conditions.some(c=>c.order===order))throw new Error(`规则${id}条件序号重复`);conditions.push({order,field,operator,value:conditionValue});byId.set(id,rule);
}
const incoming:Definition[]=[...byId.values()].map(rule=>({...rule,conditions:(rule.conditions as Definition[]).sort((a,b)=>Number(a.order)-Number(b.order)).map(({order:_order,...condition})=>condition)}));newIds=incoming.map(row=>text(row.id));
result.matchRules=merge(previous,incoming,row=>text(row.id),mode);if((result.matchRules as unknown[]).length>100)throw new Error('条件匹配最多100条规则');
const signatures=new Map<string,string>();for(const rule of array(result.matchRules)){if(rule.enabled===false)continue;const signature=JSON.stringify([rule.stage,rule.target,array(rule.conditions).map(c=>[c.field,c.operator,c.value??'']).sort()]);const target=text(rule.value);if(signatures.has(signature)&&signatures.get(signature)!==target)throw new Error('相同匹配条件不能指向不同分类');signatures.set(signature,target);}
}else if(table.name==='组织层级'){
const previous=array(base.organizationHierarchy);oldIds=previous.map(row=>text(row.code));const incoming=table.rows.map(([code,parentCode])=>({...previous.find(row=>row.code===code),code,parentCode}));unique(incoming,row=>row.code,table.name);newIds=incoming.map(row=>row.code);
result.organizationHierarchy=merge(previous,incoming,row=>text(row.code),mode);const parents=new Map(array(result.organizationHierarchy).map(row=>[text(row.code),text(row.parentCode)]));
for(const code of parents.keys()){if(!code)throw new Error('组织编码不能为空');const seen=new Set<string>();let node=code;while(node){if(seen.has(node))throw new Error('组织层级存在循环');seen.add(node);if(!parents.has(node))throw new Error(`上级组织不存在:${node}`);node=parents.get(node)!;}}
}else if(table.name==='对应有效期'){
const id=(row:Definition)=>[row.mappingType??'CATEGORY',row.sourceKey,row.fromMonth,row.toMonth].join('|');const previous=array(base.effectiveRanges);oldIds=previous.map(id);
const incoming=table.rows.map(([mappingType,sourceKey,fromMonth,toMonth,useBase,target,clear])=>{if(!['CATEGORY','PROJECT_GROUP','DEPARTMENT','PRODUCT'].includes(mappingType)||!sourceKey||![fromMonth,toMonth].every(v=>/^\d{4}-(0[1-9]|1[0-2])$/.test(v))||fromMonth>toMonth)throw new Error('对应有效期的类型、源条件或月份无效');const row:Definition={mappingType,sourceKey,fromMonth,toMonth};if(!enabled(useBase,true)){if(!enabled(clear,false)&&!target)throw new Error('有效期目标分类必填');row.mappedValue=enabled(clear,false)?null:target;}return row;});unique(incoming,id,table.name);newIds=incoming.map(id);result.effectiveRanges=merge(previous,incoming,id,mode);
const ranges=array(result.effectiveRanges);for(let i=0;i<ranges.length;i++)for(let j=0;j<i;j++){const a=ranges[i],b=ranges[j];if(a.mappingType===b.mappingType&&a.sourceKey===b.sourceKey&&text(a.fromMonth)<=text(b.toMonth)&&text(a.toMonth)>=text(b.fromMonth))throw new Error('对应有效期重叠');}
}else if(table.name==='查询字段'){
const previous=array(base.filterFields);oldIds=previous.map(row=>text(row.key));const incoming=table.rows.map(([key,label,blackColumn,brightColumn,allowEmpty,status,sortOrder])=>{if(!key||!label||!blackColumn||!brightColumn)throw new Error('查询字段的编号、名称和来源列必填');return{...previous.find(row=>row.key===key),key,label,blackColumn,brightColumn,allowEmpty:enabled(allowEmpty),enabled:enabled(status),sortOrder:number(sortOrder,'排序',0,10000)};});unique(incoming,row=>row.key,table.name);newIds=incoming.map(row=>row.key);result.filterFields=merge(previous,incoming,row=>text(row.key),mode);if(array(result.filterFields).length>20)throw new Error('查询字段最多20个');
}else if(table.name==='计算来源字段'){
const fields:Definition=mode==='MERGE'?structuredClone(object(base.sourceFields)):{};
for(const [stageLabel,alias,header] of table.rows){const stage=reverse(STAGES,stageLabel);if(!['black','bright'].includes(stage)||!alias||!header)throw new Error('计算来源字段的阶段、别名和来源列必填');fields[stage]={...object(fields[stage]),[alias]:header};}result.sourceFields=fields;
}else if(table.name==='行计算公式'){
const previous=array(base.rowRules);const incoming=table.rows.map(([position,stageLabel,target,formula,nullPolicy,zeroDenominatorPolicy,nullResultPolicy,field,operator,value])=>{const index=number(position,'顺序',1,200);const stage=reverse(STAGES,stageLabel);if(!['all','black','bright'].includes(stage)||!target||!formula)throw new Error('行计算的阶段、目标字段和公式必填');const row:Definition={...previous[index-1],stage,target,formula};if(nullPolicy)row.nullPolicy=nullPolicy;else delete row.nullPolicy;if(zeroDenominatorPolicy)row.zeroDenominatorPolicy=zeroDenominatorPolicy;else delete row.zeroDenominatorPolicy;if(nullResultPolicy)row.nullResultPolicy=nullResultPolicy;else delete row.nullResultPolicy;if(field)row.when={field,operator,value};else delete row.when;return{index,row};});unique(incoming,row=>String(row.index),table.name);const combined=new Map(mode==='MERGE'?previous.map((row,i)=>[i+1,row] as const):[]);incoming.forEach(item=>combined.set(item.index,item.row));result.rowRules=[...combined].sort(([a],[b])=>a-b).map(([,row])=>row);
}else if(table.name==='指标计算公式'){
const previous=object(base.metricRules);const values:Definition=mode==='MERGE'?{...previous}:{};
unique(table.rows,row=>row[0],table.name);for(const [code,formula,nullPolicy,zeroDenominatorPolicy,nullResultPolicy] of table.rows){if(!code||!formula)throw new Error('指标编码和公式必填');const row:Definition={...object(previous[code]),formula};if(nullPolicy)row.nullPolicy=nullPolicy;else delete row.nullPolicy;if(zeroDenominatorPolicy)row.zeroDenominatorPolicy=zeroDenominatorPolicy;else delete row.zeroDenominatorPolicy;if(nullResultPolicy)row.nullResultPolicy=nullResultPolicy;else delete row.nullResultPolicy;values[code]=row;}result.metricRules=values;
}
const cellKey=(row:string[])=>lookup?row.slice(0,lookup[1].columns.length).join('|'):table.name==='对应有效期'?row.slice(0,4).join('|'):table.name==='计算来源字段'?row.slice(0,2).join('|'):row[0];
if(!oldIds.length)oldIds=original.rows.map(cellKey);if(!newIds.length)newIds=table.rows.map(cellKey);
const grouped=(rows:string[][])=>{const values=new Map<string,string[][]>();for(const row of rows){const key=cellKey(row);const group=values.get(key)??[];group.push(row);values.set(key,group);}return values;};const oldGroups=grouped(original.rows),newGroups=grouped(table.rows);
oldIds=[...new Set(oldIds)];newIds=[...new Set(newIds)];const oldSet=new Set(oldIds),newSet=new Set(newIds);changes.push({name:table.name,rows:table.rows.length,added:newIds.filter(id=>!oldSet.has(id)).length,updated:newIds.filter(id=>oldSet.has(id)&&!equal(oldGroups.get(id),newGroups.get(id))).length,removed:mode==='REPLACE'?oldIds.filter(id=>!newSet.has(id)).length:0});
}
if(!tables.length)throw new Error('没有找到可导入的配置表');
return{definition:structuredClone(result),changes};
}
export async function createConfigurationWorkbook(value:Definition,scope:ConfigurationScope='all',defaults:unknown[]=[]){
const {default:ExcelJS}=await import('exceljs');const workbook=new ExcelJS.Workbook();workbook.creator='质量数据分析平台';
for(const table of configurationTables(value,scope,defaults)){
const sheet=workbook.addWorksheet(table.name,{views:[{state:'frozen',ySplit:1,xSplit:1}]});
const numeric=new Set(['优先级','条件序号','顺序','排序']);
sheet.columns=table.headers.map(header=>({header,width:header.includes('编号')||header.includes('编码')?46:header.includes('条件')||header.includes('公式')||header.includes('分类')?32:22}));
table.rows.forEach(row=>sheet.addRow(row.map((value,index)=>numeric.has(table.headers[index])&&value!==''?Number(value):value)));sheet.columns.forEach((column,index)=>{column.numFmt=numeric.has(table.headers[index])?'0':'@';column.alignment={vertical:'middle',wrapText:true};});
sheet.getRow(1).font={bold:true,color:{argb:'FFFFFFFF'}};sheet.getRow(1).fill={type:'pattern',pattern:'solid',fgColor:{argb:'FF5769B8'}};sheet.getRow(1).height=30;
sheet.autoFilter={from:{row:1,column:1},to:{row:Math.max(1,table.rows.length+1),column:table.headers.length}};
sheet.eachRow((row,n)=>{if(n>1){let lines=1;row.eachCell((cell,index)=>{const width=Number(sheet.getColumn(index).width)||22;const length=Array.from(cell.text).reduce((sum,char)=>sum+(char.charCodeAt(0)>255?2:1),0);lines=Math.max(lines,Math.ceil(length/Math.max(8,width-2)));const header=table.headers[index-1];const choices=header==='状态'?['启用','停用']:['明确不设归属','允许空值','使用默认对应'].includes(header)?['是','否']:header==='适用范围'||header==='阶段'?Object.values(STAGES):header==='目标类型'?Object.values(TARGETS):header==='判断方式'&&table.name==='条件匹配'?Object.values(OPERATORS):null;if(choices)cell.dataValidation={type:'list',allowBlank:false,formulae:['"'+choices.join(',')+'"']};});row.height=Math.min(409,Math.max(24,lines*16));}});
}
return workbook;
}
export async function readConfigurationWorkbook(data:ArrayBuffer|Uint8Array){
if(data.byteLength>15*1024*1024)throw new Error('配置文件不能超过15MB');
const {default:ExcelJS}=await import('exceljs');const workbook=new ExcelJS.Workbook();await workbook.xlsx.load(data as never);
return workbook.worksheets.map(sheet=>{
if(sheet.rowCount>20001||sheet.columnCount>40)throw new Error(`工作表${sheet.name}尺寸超过上限`);
const headers=sheet.getRow(1).values as unknown[];const columns=headers.slice(1).map(value=>text(value).trim());if(!columns.length)throw new Error(`${sheet.name}缺少表头`);
const rows:string[][]=[];sheet.eachRow((row,n)=>{if(n===1)return;const cells:string[]=[];for(let c=1;c<=columns.length;c++){
const cell=row.getCell(c);const value=cell.value;
if(value&&typeof value==='object'&&('formula' in value||'sharedFormula' in value))throw new Error(`${sheet.name}第${n}行包含Excel公式,请将配置公式作为文本填写`);
if(value&&typeof value==='object'&&'error' in value)throw new Error(`${sheet.name}第${n}行包含错误单元格`);
if(value instanceof Date)cells.push(value.toISOString().slice(0,7));
else if(typeof value==='number'&&/^0+$/.test(cell.numFmt)&&Number.isInteger(value))cells.push(String(value).padStart(cell.numFmt.length,'0'));
else cells.push(cell.text??'');
}if(cells.some(cell=>cell!==''))rows.push(cells);});return{name:sheet.name,headers:columns,rows};
});
}
export async function downloadConfigurationWorkbook(filename:string,value:Definition,scope:ConfigurationScope,defaults:unknown[]=[]){
const workbook=await createConfigurationWorkbook(value,scope,defaults);const data=await workbook.xlsx.writeBuffer();const url=URL.createObjectURL(new Blob([new Uint8Array(data)],{type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}));
const link=document.createElement('a');link.href=url;link.download=filename.replace(/[\\/:*?"<>|]/g,'_')+'.xlsx';link.click();setTimeout(()=>URL.revokeObjectURL(url),30000);
}
+15 -1
View File
@@ -1,5 +1,19 @@
import { createContext, useContext } from 'react';
import { createContext, useContext, useEffect, useRef } from 'react';
export const WORKSPACE_INVALIDATED = 'quality-workspace-invalidated';
export const PageCacheContext = createContext({ active: true, initialSearch: undefined as string | undefined, refreshKey: 0, onLocationChange: (_search: string) => {} });
export const usePageCache = () => useContext(PageCacheContext);
/** Revalidate retained maintenance data on a real revision change, not on every visit. */
export function useMaintenanceRefresh(reload: () => void) {
const { active, refreshKey } = usePageCache();
const seen = useRef(refreshKey);
const latest = useRef(reload);
latest.current = reload;
useEffect(() => {
if (active && seen.current !== refreshKey) {
seen.current = refreshKey;
latest.current();
}
}, [active, refreshKey]);
}
+1 -1
View File
@@ -3,7 +3,7 @@ export function activeStageFilters(filters:SourceColumnFilter[],stage:Stage):Sou
return filters.filter(rule=>rule.operator==='is_empty'||rule.operator==='not_empty'||!!rule.values?.length).map(rule=>({...rule,stages:[stage],values:rule.values?[...rule.values]:[]}));
}
export function stageProductQuery(base:QueryParams,stage:Stage,filters:SourceColumnFilter[]):QueryParams {
return {...base,view:'product',stage,columnFilters:activeStageFilters(filters,stage)};
return {...base,includeUnassigned:true,view:'product',stage,columnFilters:activeStageFilters(filters,stage)};
}
/** Sort product summaries only; leave the source rows and chronological trends unchanged. */
-29
View File
@@ -1,29 +0,0 @@
import { useLayoutEffect, useRef } from 'react';
/** 跟随筛选栏实际高度定位指标区,兼容筛选展开、换行和页面缩放。 */
export function useStickyQueryMetrics() {
const pageRef = useRef<HTMLDivElement>(null);
useLayoutEffect(() => {
const page = pageRef.current;
const toolbar = page?.querySelector<HTMLElement>('.query-toolbar');
const metrics = page?.querySelector<HTMLElement>('.metric-strip');
if (!page || !toolbar || !metrics) return;
const update = () => {
const gap = Number.parseFloat(getComputedStyle(page).rowGap) || 0;
const top = (Number.parseFloat(getComputedStyle(toolbar).top) || 0) + toolbar.offsetHeight + gap;
const scale = Number.parseFloat(getComputedStyle(document.documentElement).zoom) || 1;
// 小屏幕至少保留一段内容阅读区,避免筛选栏和指标占满视口。
const fits = top + metrics.offsetHeight < window.innerHeight / scale - 160;
page.style.setProperty('--query-metrics-top', `${top}px`);
page.style.setProperty('--query-metrics-gap', `${gap}px`);
page.style.setProperty('--query-metrics-position', fits ? 'sticky' : 'static');
};
update();
const observer = new ResizeObserver(update);
observer.observe(toolbar);
observer.observe(metrics);
window.addEventListener('resize', update);
return () => { observer.disconnect(); window.removeEventListener('resize', update); };
}, []);
return pageRef;
}
+66 -5
View File
@@ -79,10 +79,71 @@ body, .app-shell, .main-column { color: var(--ink); background: var(--background
border-inline-start-color: var(--metric-grid-head) !important;
}
/* 与筛选栏组成连续的顶部冻结区,动态偏移由实际筛选栏高度计算。 */
/* 指标恢复随页面滚动;口径切换与卡片共用一行,不再独占顶部空行。 */
:root .query-page > .metric-strip {
position: var(--query-metrics-position, static);
top: var(--query-metrics-top, auto);
z-index: 34;
box-shadow: 0 calc(-1 * var(--query-metrics-gap, 11px)) 0 var(--background);
position: relative;
top: auto;
z-index: auto;
}
.query-page .metric-strip > .rate-basis-control {
position: absolute;
top: 6px;
right: 10px;
z-index: 1;
}
.query-page .metric-strip > .metric-card {
min-height: 84px;
padding: 8px 13px;
gap: 3px;
}
.query-page .metric-strip:has(> .rate-basis-control) > .metric-card:last-child .metric-label { padding-right: 140px; }
@media (max-width: 760px) {
.query-page .metric-strip > .rate-basis-control { position: static; padding: 0 8px; }
.query-page .metric-strip:has(> .rate-basis-control) > .metric-card:last-child .metric-label { padding-right: 0; }
}
.mapping-issue-filters { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.mapping-issue-filters .ant-select { min-width: 180px; flex: 0 1 240px; }
/* Retained maintenance sections share a compact, sticky navigation bar. */
.management-sections > .ant-tabs-nav { position: sticky; top: 53px; z-index: 34; margin: 0 0 12px; padding: 0 16px; background: var(--surface); border: 1px solid var(--line); }
.management-sections > .ant-tabs-body-holder > .ant-tabs-body > .ant-tabs-content { padding: 12px; background: var(--surface); border: 1px solid var(--line); }
.management-sections .management-page { gap: 10px; }
.management-module-actions { display: flex; justify-content: flex-end; margin: 0; padding: 0 0 6px; }
.management-module-actions .page-heading-actions { margin: 0; }
.management-sections .nc-simple-config-tabs > .ant-tabs-nav { position: sticky; top: 100px; z-index: 33; background: var(--surface); }
@media (max-width: 760px) { .management-sections > .ant-tabs-nav { top: 40px; } .management-sections .nc-simple-config-tabs > .ant-tabs-nav { top: 87px; } }
.management-sections > .ant-tabs-body-holder, .management-sections > .ant-tabs-body-holder > .ant-tabs-body { overflow: visible; }
.management-sections { overflow: visible; }
.management-sections .management-sections > .ant-tabs-nav { top: 100px; z-index: 33; }
.management-sections .management-sections .management-sections > .ant-tabs-nav { top: 147px; z-index: 32; }
@media (max-width: 760px) { .management-sections .management-sections > .ant-tabs-nav { top: 87px; } .management-sections .management-sections .management-sections > .ant-tabs-nav { top: 134px; } }
/* A clipping panel becomes a false scroll ancestor for sticky tab navigation. */
.management-sections .panel:has(.ant-tabs-nav) { overflow: visible; }
.management-sections .resource-toolbar > .completion-actions { margin-left: auto; }
.page-view .panel:has(> .management-sections) { overflow: visible; }
/* Flat maintenance navigation: one full-width frozen bar, plain tools underneath. */
.management-sections > .ant-tabs-nav { isolation: isolate; background: var(--surface); box-shadow: 0 1px 0 var(--line); }
.management-sections .flat-config-page .flat-config-panel { padding: 4px 8px 8px; border: 0; box-shadow: none; }
.flat-config-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; min-height: 32px; }
.config-type-filter { display: inline-flex; align-items: center; gap: 8px; flex: 0 0 auto; }
.config-type-filter > span { white-space: nowrap; }
.config-type-filter .ant-select { width: 166px; }
.flat-config-panel .structured-config-toolbar { margin-top: 0; }
.classification-rule-fields { display:grid; grid-template-columns:1fr 1fr; gap:16px; margin-bottom:20px; }
.classification-rule-fields label, .classification-target { display:flex; flex-direction:column; gap:8px; font-weight:600; }
.classification-rule-fields .ant-select { width:100%; }
.classification-condition-heading, .classification-preview-action, .configuration-import-toolbar { display:flex; align-items:center; justify-content:space-between; flex-wrap:wrap; gap:12px; margin:16px 0; }
.classification-conditions { display:grid; gap:10px; }
.classification-condition-row { display:grid; grid-template-columns:90px 110px minmax(100px,1fr) auto; gap:10px; align-items:center; padding:10px 12px; background:var(--surface-soft,var(--surface)); border:1px solid var(--line); border-radius:6px; }
.classification-condition-row > span { font-weight:600; }
.classification-target { margin-top:18px; }
.classification-preview-action > span { font-size:13px; color:var(--muted-ink); }
.classification-impact, .configuration-table-preview { margin-top:16px; display:grid; gap:12px; }
.ant-modal.classification-rule-modal, .ant-modal.configuration-import-modal { top:24px; padding-bottom:24px; }
.classification-rule-modal .ant-modal-body, .configuration-import-modal .ant-modal-body { max-height:calc(100dvh - 210px); overflow:auto; padding-right:3px; }
@media(max-width:680px) { .classification-rule-fields { grid-template-columns:1fr; } .classification-condition-row { grid-template-columns:80px 1fr; } .classification-condition-row > .ant-input { grid-column:1/-1; } }
@media (max-width: 960px) { .config-type-filter { flex-wrap: wrap; } .structured-config-actions { margin-left: auto; } }
@@ -0,0 +1,55 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { editClassification, classificationRuleId } from '../src/classificationMaintenance.ts';
const source = {stage:'black',department:'产业部A',product:'产品A',pdtCode:'',salesRegion:'销售区A'};
const selection={kind:'group',row:source};
const defaults=[{id:'default',target:'group',value:'默认分类'}];
test('blank-key source creates a narrowly scoped rule and preserves inherited defaults',()=>{
const base={projectGroupLookup:{'001|G':'项目组A'},extra:['keep']};
const result=editClassification(base,selection,'业务组A',defaults);
assert.deepEqual(result.matchRules[0].conditions,[
{field:'@department',operator:'EQUALS',value:'产业部A'},
{field:'@product',operator:'EQUALS',value:'产品A'},
{field:'PDT代码',operator:'IS_BLANK',value:''},
{field:'销售片区',operator:'EQUALS',value:'销售区A'},
]);
assert.equal(result.matchRules[0].stage,'black');assert.deepEqual(result.matchRules[1],defaults[0]);
assert.deepEqual(base,{projectGroupLookup:{'001|G':'项目组A'},extra:['keep']});
const changed=editClassification(result,selection,'业务组A',defaults);
assert.equal(changed.matchRules.length,2);assert.equal(changed.matchRules[0].value,'业务组A');
assert.throws(()=>editClassification(result,selection,'业务组B',defaults),/相同条件/);
assert.notEqual(classificationRuleId(source),classificationRuleId({...source,stage:'bright'}));
});
test('successive group and project edits accumulate without replacing other mappings or explicit empty rules',()=>{
let result=editClassification({matchRules:[],categoryLookup:{keep:'category'}},selection,'G',defaults);
result=editClassification(result,{kind:'projectGroup',row:{mappingType:'PROJECT_GROUP',customerCode:'001',businessGroup:'G',sourceKey:'001|G'}},'P',defaults);
assert.equal(result.projectGroupLookup['001|G'],'P');assert.equal(result.matchRules.length,1);assert.deepEqual(result.categoryLookup,{keep:'category'});
assert.throws(()=>editClassification(result,selection,' ',defaults));
assert.throws(()=>editClassification(result,{kind:'projectGroup',row:{customerCode:'001',businessGroup:'G',sourceKey:'002|G'}},'P',defaults));
});
test('interval overrides and conflicting rule identities cannot silently mask a classification edit',()=>{
const project={kind:'projectGroup',row:{mappingType:'PROJECT_GROUP',customerCode:'001',businessGroup:'G',sourceKey:'001|G'}};
assert.throws(()=>editClassification({effectiveRanges:[{mappingType:'PROJECT_GROUP',sourceKey:'001|G'}]},project,'P',[]),/期间规则/);
const id=classificationRuleId(source);
assert.throws(()=>editClassification({matchRules:[{id,stage:'bright',conditions:[]}]},selection,'G',[]),/冲突/);
});
test('editing a disabled project relationship enables only that relationship and rejects unavailable targets',()=>{
const selection={kind:'projectGroup',row:{mappingType:'PROJECT_GROUP',customerCode:'001',businessGroup:'G',sourceKey:'001|G'}};
const base={masterData:{projectGroups:[{code:'P',name:'项目组A',enabled:true}]},disabledMappings:{projectGroupLookup:['001|G','002|G']}};
const result=editClassification(base,selection,'项目组A',[]);
assert.deepEqual(result.disabledMappings.projectGroupLookup,['002|G']);
assert.deepEqual(base.disabledMappings.projectGroupLookup,['001|G','002|G']);
assert.throws(()=>editClassification(base,selection,'不存在的项目组',[]),/未登记/);
});
test('removable conditions apply to all matching rows without an implicit unclassified predicate',()=>{
const options={stage:'all',conditions:[{field:'@department',operator:'EQUALS',value:'产业部A'},{field:'销售片区',operator:'EQUALS',value:'销售区A'}]};
const result=editClassification({matchRules:[]},selection,'G',[],options);
assert.deepEqual(result.matchRules[0].conditions,options.conditions);assert.equal(result.matchRules[0].stage,'all');
assert.equal(result.matchRules[0].conditions.some(c=>c.field==='@group'),false);
assert.equal(classificationRuleId(source,options),classificationRuleId(source,{...options,conditions:[...options.conditions].reverse()}));
const specific=editClassification({matchRules:[]},selection,'G',[],{stage:'all'});
assert.ok(specific.matchRules[0].priority<result.matchRules[0].priority);
assert.throws(()=>editClassification({},selection,'G',[],{stage:'all',conditions:[]}),/至少一项/);
});
@@ -0,0 +1,48 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { configurationTables, importConfigurationTables, createConfigurationWorkbook, readConfigurationWorkbook } from '../src/configurationWorkbook.ts';
const rule={id:'rule_001',name:'指定分类',priority:-9900,stage:'all',enabled:true,target:'group',value:'G',conditions:[{field:'PDT代码',operator:'EQUALS',value:'00001'},{field:'销售片区',operator:'IS_BLANK',value:''}]};
const base={schemaVersion:3,unknown:{retain:[1,null]},masterData:{departments:[{code:'0001',name:'A',enabled:true}],groups:[{code:'002',name:'G',enabled:true,department:'A',unknown:'keep'}],products:[],projectGroups:[]},
categoryLookup:{'00001|欧洲':'G','00002|空':null},projectGroupLookup:{'00003|G':'P'},departmentLookup:{raw:'A'},productLookup:{RAW:'P'},disabledMappings:{categoryLookup:['00002|空']},matchRules:[rule],organizationHierarchy:[{code:'A',parentCode:'',name:'保留名称'}],effectiveRanges:[{mappingType:'CATEGORY',sourceKey:'00001|欧洲',fromMonth:'2026-01',toMonth:'2026-12',mappedValue:null}]};
test('all configuration sheets round-trip through a real xlsx with codes, nulls, conditions and omitted keys preserved',async()=>{
const source=structuredClone(base);const workbook=await createConfigurationWorkbook(source,'all');
const tables=await readConfigurationWorkbook(await workbook.xlsx.writeBuffer());
for(const mode of ['MERGE','REPLACE'])assert.deepEqual(importConfigurationTables(source,tables,'all',mode).definition,source);
assert.equal(tables.find(t=>t.name==='产业部名录').rows[0][0],'0001');
assert.deepEqual(source,base);assert.equal(workbook.getWorksheet('产业部名录').getCell('A2').numFmt,'@');
});
test('edits upsert by source key, and replace removes only records in supplied tables',()=>{
const tables=configurationTables(base,'categoryLookup');tables[0].rows[0][2]='新分组';
const imported=importConfigurationTables(base,tables,'categoryLookup','MERGE');
assert.equal(imported.definition.categoryLookup['00001|欧洲'],'新分组');assert.equal(imported.definition.categoryLookup['00002|空'],null);
assert.equal(imported.changes[0].updated,1);assert.equal(base.categoryLookup['00001|欧洲'],'G');
tables[0].rows=tables[0].rows.slice(0,1);const replaced=importConfigurationTables(base,tables,'all','REPLACE');
assert.equal(Object.keys(replaced.definition.categoryLookup).length,1);assert.deepEqual(replaced.definition.projectGroupLookup,base.projectGroupLookup);assert.equal(replaced.changes[0].removed,1);
});
test('multiple conditions are grouped by rule id; changing raw and computed fields cannot conflate them',async()=>{
const source={matchRules:[{...rule,conditions:[{field:'产业部',operator:'EQUALS',value:'原始A'},{field:'@department',operator:'EQUALS',value:'计算A'}]}]};
const book=await createConfigurationWorkbook(source,'matchRules');book.getWorksheet('条件匹配').getCell('F2').value='新分组';book.getWorksheet('条件匹配').getCell('F3').value='新分组';
const imported=importConfigurationTables(source,await readConfigurationWorkbook(await book.xlsx.writeBuffer()),'matchRules','MERGE').definition;
assert.deepEqual(imported.matchRules[0].conditions,source.matchRules[0].conditions);assert.equal(imported.matchRules[0].value,'新分组');
const rows=configurationTables(base,'matchRules');rows[0].rows[1][5]='不同分组';assert.throws(()=>importConfigurationTables(base,rows,'matchRules','MERGE'),/不一致/);
});
test('legacy defaults remain inherited on unchanged export/import; clearing the sheet explicitly disables them',async()=>{
const source={categoryLookup:{a:'G'}};const book=await createConfigurationWorkbook(source,'matchRules',[rule]);
const tables=await readConfigurationWorkbook(await book.xlsx.writeBuffer());assert.deepEqual(importConfigurationTables(source,tables,'matchRules','MERGE',[rule]).definition,source);
tables[0].rows=[];assert.deepEqual(importConfigurationTables(source,tables,'matchRules','REPLACE',[rule]).definition.matchRules,[]);
});
test('invalid headers, duplicate identities, hierarchy cycles and formulas are rejected before any change',async()=>{
let tables=configurationTables(base,'departments');tables[0].headers[0]='错误';assert.throws(()=>importConfigurationTables(base,tables,'departments','MERGE'),/表头/);
tables=configurationTables(base,'departments');tables[0].rows.push([...tables[0].rows[0]]);assert.throws(()=>importConfigurationTables(base,tables,'departments','MERGE'),/重复/);
tables=configurationTables(base,'all').filter(t=>t.name==='组织层级');tables[0].rows[0][1]='A';assert.throws(()=>importConfigurationTables(base,tables,'all','MERGE'),/循环/);
const book=await createConfigurationWorkbook(base,'departments');book.getWorksheet('产业部名录').getCell('A2').value={formula:'1+1',result:2};
await assert.rejects(async()=>readConfigurationWorkbook(await book.xlsx.writeBuffer()),/Excel公式/);
});
test('query fields and calculation definitions round-trip, preserve formulas as text, and accept edits',async()=>{
const rules={schemaVersion:'rule-v1',sourceFields:{black:{qualified:'合格重量'},bright:{qualified:'亮片重量'}},rowRules:[{stage:'black',target:'total',formula:'qualified+scrap',nullPolicy:'PROPAGATE',zeroDenominatorPolicy:'ONE'}],metricRules:{RATE:{formula:'qualified/total',zeroDenominatorPolicy:'ONE'}},unknown:'keep'};
const workbook=await createConfigurationWorkbook(rules,'rules');let tables=await readConfigurationWorkbook(await workbook.xlsx.writeBuffer());assert.deepEqual(importConfigurationTables(rules,tables,'rules','REPLACE').definition,rules);
tables.find(t=>t.name==='指标计算公式').rows[0][1]='qualified/(qualified+scrap)';const imported=importConfigurationTables(rules,tables,'rules','MERGE').definition;assert.equal(imported.metricRules.RATE.formula,'qualified/(qualified+scrap)');assert.equal(imported.metricRules.RATE.nullPolicy,undefined);
const fields={filterFields:[{key:'0001',label:'客户',blackColumn:'客户代码',brightColumn:'客户代码',enabled:false,allowEmpty:true,sortOrder:1}]};const book=await createConfigurationWorkbook(fields,'filterFields');tables=await readConfigurationWorkbook(await book.xlsx.writeBuffer());assert.deepEqual(importConfigurationTables(fields,tables,'filterFields','REPLACE').definition,fields);
});
+12 -5
View File
@@ -101,18 +101,18 @@ test('the actual summary click stores release A and filters until a new detail i
function drawerFixture() {
const source=read('QueryPage.tsx');const code=source.slice(source.indexOf('function RecordsDrawer(')).replace(/export \{ fullYearTrend \};/, '');
const requests=[],exports=[];let downloaded=0;
const requests=[],exports=[];let downloaded=0,refreshKey=0;
const props={open:true,row:null,stage:'black',onStage(){},onClose(){},query:freezeSourceRecordsQuery(baseQuery,'release-A','overall',null),onUnauthorized:()=>false};
const renderer=hookRenderer(hooks=>new Function('React','hooks','api',transpile(`
const {useState,useRef,useMemo,useEffect}=hooks;const TABLE_PAGE_SIZE=20,MONTHS=Array.from({length:12},(_,i)=>i+1+'月');
const {useState,useRef,useMemo,useEffect}=hooks;const usePageCache=()=>({refreshKey:api.refreshKey()});const TABLE_PAGE_SIZE=20,MONTHS=Array.from({length:12},(_,i)=>i+1+'月');
const Drawer='Drawer',Segmented='Segmented',Select='Select',Button='Button',Alert='Alert',Spin='Spin',Table='Table',Pagination='Pagination',SearchOutlined='SearchOutlined',DownloadOutlined='DownloadOutlined';
const Input='Input',readableSelectPopup={},Empty=Object.assign(()=>null,{PRESENTED_IMAGE_SIMPLE:'empty'}),App={useApp:()=>({message:{success(){},error(){}}})};
const isSystemVersionField=()=>false,chooseError=e=>e.message,withoutSystemVersionFields=v=>v;
const window={innerWidth:1400,setTimeout:fn=>fn()},document={createElement:()=>({click(){api.downloaded();},remove(){}}),body:{appendChild(){}}};
${code}
return RecordsDrawer;
`))(React,hooks,{records(params,signal){const d=deferred();requests.push({params,signal,...d});return d.promise;},async exportRecords(params,signal){exports.push({params,signal});return {blob:new Blob(['fixture']),filename:'fixture.xlsx'};},downloaded(){downloaded++;}}));
return {requests,exports,props,render:()=>renderer.render(props),unmount:()=>renderer.unmount(),get downloaded(){return downloaded;}};
`))(React,hooks,{refreshKey:()=>refreshKey,records(params,signal){const d=deferred();requests.push({params,signal,...d});return d.promise;},async exportRecords(params,signal){exports.push({params,signal});return {blob:new Blob(['fixture']),filename:'fixture.xlsx'};},downloaded(){downloaded++;}}));
return {requests,exports,props,render:()=>renderer.render(props),unmount:()=>renderer.unmount(),invalidate(){refreshKey++;},get downloaded(){return downloaded;}};
}
test('actual drawer clears prior rows during page load/failure and offers retry on the same frozen release',async()=>{
@@ -136,7 +136,14 @@ test('closing and reopening a different month remounts page/search state and ign
const next=drawerFixture();next.props.query=freezeSourceRecordsQuery({...baseQuery,month:2},'release-A','overall',null);next.render();
assert.equal(next.requests[0].params.page,1);assert.equal(next.requests[0].params.search,undefined);assert.equal(next.requests[0].params.month,2);
assert.equal(find(next.render(),n=>n.type==='Table'),undefined);next.unmount();
assert.match(read('QueryPage.tsx'),/recordsOpen && recordQuerySnapshot && <RecordsDrawer/);
assert.match(read('QueryPage.tsx'),/recordQuerySnapshot && <RecordsDrawer/);
assert.match(read('QueryPage.tsx'),/open=\{recordsOpen\}/);
});
test('reopening the same frozen drawer reuses its page until the workspace revision changes',async()=>{
const ui=drawerFixture();ui.render();ui.requests[0].resolve({rows:[{id:'cached'}],total:1,pageSize:20,page:1});await tick();ui.render();
ui.props.open=false;ui.render();ui.props.open=true;let tree=ui.render();assert.equal(ui.requests.length,1);assert.equal(find(tree,n=>n.type==='Table').props.dataSource[0].id,'cached');
ui.invalidate();tree=ui.render();assert.equal(ui.requests.length,2);assert.equal(find(tree,n=>n.type==='Table'),undefined);ui.unmount();
});
test('export from the actual drawer keeps the clicked release even after current publication changes',async()=>{
+13
View File
@@ -140,3 +140,16 @@ test('本部报废原口径紧随扩散废,调整口径隐藏并在表格及Ex
}
assert.equal(data.bright.scrapWeight,100);
});
test('合格率说明图标仅在去本部报废口径下显示,黑片总重说明始终保留', () => {
for(const basis of ['original','withoutHeadquartersScrap']) {
const ui=fixture({rows:[row],view:'overall',rateBasis:basis});
const columns=find(ui.tree,n=>n.type==='Table').props.columns;
const leaves=columns.flatMap(c=>c.children||[c]);
for(const key of ['bright-rate','compositeRate']) {
const header=leaves.find(c=>c.key===key).title;
assert.equal(Boolean(find(header,n=>n.type==='Info')),basis!=='original');
}
assert.ok(find(leaves.find(c=>c.key==='black-totalWeight').title,n=>n.type==='Info'));
}
});
@@ -23,3 +23,10 @@ test('product rows sort by selected stage total descending without mutating rows
assert.deepEqual(sortStageRowsByTotalWeight(rows,'bright').map(row=>row.key),['small','tie','large','unknown','negative']);
assert.deepEqual(rows.map(row=>row.key),['unknown','small','large','tie','negative']);
});
test('stage query includes unclassified source rows even when an old link excluded them',()=>{
for(const stage of ['black','bright']) {
const base={year:2026,includeUnassigned:false};
assert.equal(stageProductQuery(base,stage,[]).includeUnassigned,true);
assert.equal(base.includeUnassigned,false);
}
});