刘光辉
3 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import type { PrintAuditTarget } from '#/api/x/lims/printAudit';
 
export interface PrintAuditTargetMapping {
  bizCodeField?: string;
  idField?: string;
  titleField?: string;
}
 
function readField(row: Record<string, any>, field?: string) {
  if (!field) return undefined;
  return field.split('.').reduce<any>((value, key) => value?.[key], row);
}
 
function toText(value: any) {
  if (value === undefined || value === null) return undefined;
  const text = String(value).trim();
  return text || undefined;
}
 
export function buildPrintAuditTargets(data: any, mapping: PrintAuditTargetMapping = {}): PrintAuditTarget[] {
  const rows = Array.isArray(data) ? data : data ? [data] : [];
  const targets: PrintAuditTarget[] = [];
  for (const row of rows) {
    if (!row || typeof row !== 'object') continue;
    const id = toText(mapping.idField ? readField(row, mapping.idField) : (row.id ?? row.f_id));
    if (!id) continue;
    const target: PrintAuditTarget = { id };
    const bizCode = toText(readField(row, mapping.bizCodeField));
    const title = toText(readField(row, mapping.titleField));
    if (bizCode) target.bizCode = bizCode;
    if (title) target.title = title;
    targets.push(target);
  }
  return targets;
}