yyb
10 天以前 856f10107b7681f91114dc48991ebd121a3a8c3f
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import { parseTime } from "@/utils/ruoyi";
import {
  formatFieldDisplayValue,
  getFieldOptionLabel,
  isSelectField,
  mergeFormConfigForEdit,
} from "./approvalFormField.js";
import {
  formatKnownSelectLabel,
  resolveInstanceFormPayload,
  resolveListFieldRawValue,
} from "./approvalModuleListSearch.js";
 
export const DETAIL_STORAGE_KEY = "oa_approve_instance_detail_row";
 
export const INSTANCE_STATUS_TEXT = {
  PENDING: "进行中",
  APPROVED: "已通过",
  REJECTED: "已驳回",
  DRAFT: "草稿",
};
 
export const INSTANCE_STATUS_TAG = {
  PENDING: "warning",
  APPROVED: "success",
  REJECTED: "error",
  DRAFT: "info",
};
 
export const TASK_STATUS_TEXT = {
  PENDING: "待处理",
  APPROVED: "已通过",
  REJECTED: "已驳回",
};
 
export const TASK_STATUS_TAG = {
  PENDING: "warning",
  APPROVED: "success",
  REJECTED: "error",
};
 
export function instanceStatusText(status) {
  return INSTANCE_STATUS_TEXT[status] || status || "-";
}
 
export function instanceStatusTagType(status) {
  return INSTANCE_STATUS_TAG[status] || "info";
}
 
export function taskStatusText(status) {
  const key = String(status || "").toUpperCase();
  return TASK_STATUS_TEXT[key] || status || "待处理";
}
 
export function taskStatusTagType(status) {
  const key = String(status || "").toUpperCase();
  return TASK_STATUS_TAG[key] || "info";
}
 
export function formatDateTime(val) {
  if (!val) return "-";
  return parseTime(val, "{y}-{m}-{d} {h}:{i}:{s}") || String(val);
}
 
/** 解析实例为只读展示字段(合并 formPayload,支持传整行或仅 formConfig) */
export function resolveInstanceDisplayFields(formConfigOrRow) {
  const row =
    formConfigOrRow &&
    typeof formConfigOrRow === "object" &&
    (formConfigOrRow.formConfig != null ||
      formConfigOrRow.formPayload != null ||
      formConfigOrRow.formFieldDefs != null)
      ? formConfigOrRow
      : { formConfig: formConfigOrRow };
  const { fields } = resolveInstanceFormPayload(row);
  if (fields.length) return fields.filter(f => f?.key);
  const merged = mergeFormConfigForEdit("", row.formConfig);
  return (merged.fields || []).filter(f => f?.key);
}
 
export function displayFieldValue(field) {
  const val = field.value ?? field.defaultValue;
  if (val === undefined || val === null || val === "") return "-";
  if (isSelectField(field)) {
    const fromOptions = getFieldOptionLabel(field, val);
    if (fromOptions && fromOptions !== "-") return fromOptions;
    const known = formatKnownSelectLabel(field.key, val);
    if (known) return known;
    return String(val);
  }
  const known = formatKnownSelectLabel(field?.key, val);
  if (known) return known;
  const shown = formatFieldDisplayValue(field, val);
  return shown || String(val);
}
 
const DATETIME_LIST_PROPS = new Set([
  "startTime",
  "endTime",
  "overtimeDate",
  "applyTime",
]);
 
function formatListFieldDisplay(prop, val, field) {
  if (val === undefined || val === null || val === "") return "-";
  if (DATETIME_LIST_PROPS.has(prop)) {
    const shown = formatDateTime(val);
    if (shown && shown !== "-") return shown;
  }
  if (field?.type === "datetimerange") {
    const shown = formatFieldDisplayValue(field, val);
    if (shown) return shown;
  }
  if (field) return displayFieldValue({ ...field, value: val });
  const known = formatKnownSelectLabel(prop, val);
  if (known) return known;
  return String(val);
}
 
/** 审批记录 result:approved | rejected | pending */
export function mapRecordResult(action) {
  const s = String(action || "").toUpperCase();
  if (s === "APPROVED" || s === "APPROVE" || s === "PASS") return "approved";
  if (s === "REJECTED" || s === "REJECT" || s === "REFUSE") return "rejected";
  return "pending";
}
 
export function recordActionLabel(result) {
  if (result === "approved") return "通过";
  if (result === "rejected") return "驳回";
  return "待处理";
}
 
export function mapApprovalRecords(records) {
  const list = Array.isArray(records) ? records : [];
  return list.map((r, index) => ({
    id: r.id ?? index,
    operatorName: r.approverName || r.operatorName || r.createUserName || "—",
    result: mapRecordResult(r.approveAction ?? r.action ?? r.status),
    opinion: r.approveComment || r.comment || r.opinion || "",
    time: formatDateTime(r.approveTime || r.createTime || r.time),
  }));
}
 
export function getRejectReasonFromRecords(records) {
  const mapped = mapApprovalRecords(records);
  const hit = mapped.find(r => r.result === "rejected");
  return hit?.opinion || "";
}
 
/** 列表 tasks → 流程节点(与 apply 页节点结构接近) */
export function mapTasksToFlowNodes(tasks) {
  const list = Array.isArray(tasks) ? tasks : [];
  if (!list.length) return [];
 
  const byLevel = new Map();
  list.forEach(t => {
    const level = Number(t.levelNo ?? t.taskLevel ?? t.nodeOrder ?? 1);
    if (!byLevel.has(level)) {
      byLevel.set(level, {
        levelNo: level,
        approveType: t.approveType || "AND",
        approvers: [],
      });
    }
    const node = byLevel.get(level);
    node.approvers.push({
      approverName: t.approverName || "—",
      taskStatus: t.taskStatus ?? t.status,
      approveComment: t.approveComment,
      approveTime: t.approveTime,
    });
    if (t.approveType) node.approveType = t.approveType;
  });
 
  return [...byLevel.entries()]
    .sort(([a], [b]) => a - b)
    .map(([, node]) => node);
}
 
/** 组装审批提交 DTO(与 Web buildApproveInstanceDto 一致) */
export function buildApproveInstanceDto(id, uiResult, comment) {
  const opinion = (comment || "").trim();
  return {
    id,
    approveAction: uiResult === "rejected" ? "REJECTED" : "APPROVED",
    approveComment: opinion || (uiResult === "approved" ? "同意" : ""),
  };
}
 
/** 是否本人发起的审批 */
export function isOwnApplication(item, userStore) {
  const uid = userStore?.id;
  if (item?.applicantId != null && uid != null && uid !== "") {
    return String(item.applicantId) === String(uid);
  }
  const loginName = userStore?.nickName || userStore?.name;
  if (loginName && item?.applicantName) {
    return String(item.applicantName).trim() === String(loginName).trim();
  }
  return false;
}
 
/** 仅进行中且本人发起时可编辑 */
export function canModifyInstance(item, userStore) {
  return item?.status === "PENDING" && isOwnApplication(item, userStore);
}
 
/** 待当前用户审批 */
export function canApproveInstance(item) {
  return Boolean(item?.isApprove) && item?.status === "PENDING";
}
 
export function stashInstanceRow(item) {
  if (item) {
    uni.setStorageSync(DETAIL_STORAGE_KEY, item);
  }
}
 
export function loadInstanceRow(id) {
  const row = uni.getStorageSync(DETAIL_STORAGE_KEY);
  if (!row || String(row.id) !== String(id)) return null;
  return row;
}
 
export const EDIT_STORAGE_KEY = "oa_approve_instance_edit_row";
 
/** 业务申请页状态:进行中/已完成不可修改(与 Web canEditBusinessInstanceRow 一致) */
export function normalizeApprovalStatusKey(v) {
  if (v == null || v === "") return "pending";
  const upper = String(v).trim().toUpperCase();
  if (upper === "DRAFT") return "draft";
  if (upper === "APPROVED" || upper === "PASS") return "approved";
  if (upper === "REJECTED" || upper === "REJECT" || upper === "REFUSE") {
    return "rejected";
  }
  if (upper === "CANCELLED" || upper === "CANCEL") return "cancelled";
  if (upper === "PENDING" || upper === "IN_PROGRESS") return "pending";
  const lower = String(v).trim().toLowerCase();
  if (["draft", "pending", "approved", "rejected", "cancelled"].includes(lower)) {
    return lower;
  }
  return "pending";
}
 
export function canEditBusinessInstanceRow(row) {
  const key = normalizeApprovalStatusKey(row?.status ?? row?.approvalStatus);
  return key !== "pending" && key !== "approved";
}
 
export function businessStatusText(status) {
  const key = normalizeApprovalStatusKey(status);
  if (key === "draft") return "草稿";
  if (key === "pending") return "进行中";
  if (key === "approved") return "已完成";
  if (key === "rejected") return "已驳回";
  if (key === "cancelled") return "已撤销";
  return instanceStatusText(status);
}
 
export function businessStatusTagType(status) {
  const key = normalizeApprovalStatusKey(status);
  if (key === "approved") return "success";
  if (key === "rejected") return "error";
  if (key === "draft" || key === "cancelled") return "info";
  return "warning";
}
 
/** OA 列表自定义状态角标 class */
export function businessStatusClass(status) {
  return `status-${normalizeApprovalStatusKey(status)}`;
}
 
/**
 * 与 Web buildApprovalInstanceListParams 一致:扁平 query(current/size/businessType/...)
 * 审批列表不传 businessType 即查全部
 */
export function buildInstanceListParams({
  page,
  businessType,
  extraDto = {},
  searchForm,
}) {
  const extra = { ...(extraDto && typeof extraDto === "object" ? extraDto : {}) };
  if (extra.createTime != null && extra.createTimeStart == null) {
    extra.createTimeStart = extra.createTime;
  }
  delete extra.createTime;
 
  const params = {
    current: page.current,
    size: page.size,
    ...extra,
  };
 
  const bizType = businessType ?? searchForm?.businessType;
  if (bizType != null && bizType !== "") {
    params.businessType = bizType;
  }
  if (searchForm?.status) {
    params.status = searchForm.status;
  }
 
  const range =
    searchForm?.createTimeRange ??
    searchForm?.applyDateRange ??
    searchForm?.transferDateRange;
  if (Array.isArray(range) && range[0] && params.createTimeStart == null) {
    params.createTimeStart = range[0];
  }
  if (Array.isArray(range) && range[1] && params.createTimeEnd == null) {
    params.createTimeEnd = range[1];
  }
 
  return params;
}
 
export function unwrapInstancePage(res) {
  const data = res?.data ?? res;
  return {
    records: Array.isArray(data?.records) ? data.records : [],
    total: Number(data?.total ?? 0),
  };
}
 
/** 从实例行提取列表展示字段(label + value,含 formPayload) */
export function buildFormDisplayRows(row, listFields = []) {
  const { fields, formPayload } = resolveInstanceFormPayload(row);
  const fieldByKey = new Map((fields || []).map(f => [f.key, f]));
  const rows = [];
  const defs = listFields || [];
 
  if (defs.length) {
    defs.forEach(def => {
      if (!def?.prop) return;
      const prop = def.prop;
      const hit = fieldByKey.get(prop);
      const raw = resolveListFieldRawValue(prop, row, fields, formPayload);
      rows.push({
        label: def.label || hit?.label || prop,
        value: formatListFieldDisplay(prop, raw, hit),
      });
    });
  } else {
    fields.slice(0, 3).forEach(f => {
      rows.push({ label: f.label, value: displayFieldValue(f) });
    });
  }
  return rows;
}
 
/** 列表行增强(保留原始字段供详情/编辑) */
export function mapInstanceListRow(row, listFields = []) {
  if (!row) return {};
  const displayRows = buildFormDisplayRows(row, listFields);
  const extra = {};
  const { fields, formPayload } = resolveInstanceFormPayload(row);
  (listFields || []).forEach(def => {
    if (!def?.prop) return;
    const hit = fields.find(f => f.key === def.prop);
    const raw = resolveListFieldRawValue(def.prop, row, fields, formPayload);
    extra[def.prop] = formatListFieldDisplay(def.prop, raw, hit);
  });
  return {
    ...row,
    approvalStatus: normalizeApprovalStatusKey(row.status),
    summary: row.title || row.templateName || "",
    createTime: formatDateTime(row.applyTime || row.createTime),
    displayRows,
    formPayload,
    formFieldDefs: fields,
    ...extra,
  };
}