normalizeReconciliationRuleForCompare.js
6.34 KB
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
import { deepClone } from "@/common/util"
const TOP_EMPTY_ARRAY_KEYS = [
"upgradeGoods",
"packageGoods",
"goodsTypeGroupRelations"
]
/** 与 getBackendFormat / 提交体对齐的顶层字段;接口其余字段不参与「是否改动」比对 */
const ALLOWED_TOP_KEYS = new Set([
"groupType",
"categoryType",
"expenseCategoryRules",
"goodsSource",
"upgradeGoods",
"packageGoods",
"goodsTypeGroupRelations",
"goodsTypes",
"id",
"groupDivisionRuleId",
"formType"
])
/** 费用行可比字段(排除接口审计、扩展字段) */
const ALLOWED_EXPENSE_KEYS = new Set([
"targetType",
"expenseCategoryId",
"expenseCategoryName",
"expenseCategoryCalculateType",
"expenseCategoryValue",
"targetMerchantName",
"targetMerchantId",
"calculateMode",
"formType",
"nodeType",
"children",
"categoryType"
])
function omitUndefinedShallow(obj) {
if (!obj || typeof obj !== "object") return
Object.keys(obj).forEach((k) => {
if (obj[k] === undefined) delete obj[k]
})
}
function omitUndefinedDeep(val, maxDepth) {
if (maxDepth <= 0 || val == null || typeof val !== "object") return
if (Array.isArray(val)) {
for (let i = 0; i < val.length; i++) {
omitUndefinedDeep(val[i], maxDepth - 1)
}
return
}
const keys = Object.keys(val)
for (let i = 0; i < keys.length; i++) {
const k = keys[i]
const v = val[k]
if (v === undefined) {
delete val[k]
} else {
omitUndefinedDeep(v, maxDepth - 1)
}
}
}
/** 比对时统一主键:仅 id、或 id 与 groupDivisionRuleId 同值、或仅有 groupDivisionRuleId 时迁到 id */
function normalizePrimaryRuleId(o) {
if (!o || typeof o !== "object") return
const gid = o.groupDivisionRuleId
const rid = o.id
const gStr = gid != null && String(gid).trim() !== "" ? String(gid).trim() : ""
const rStr = rid != null && String(rid).trim() !== "" ? String(rid).trim() : ""
if (gStr && rStr && gStr === rStr) {
delete o.groupDivisionRuleId
return
}
if (gStr && !rStr) {
o.id = gid
delete o.groupDivisionRuleId
}
}
function pruneTargetMerchantEmpties(row) {
if (!row || typeof row !== "object") return
const id = row.targetMerchantId
const name = row.targetMerchantName
if (id === "" || id == null) delete row.targetMerchantId
if (name === "" || name == null) delete row.targetMerchantName
}
/**
* 仅用于对账基线比对:接口返回与 getBackendFormat(流量+服务+平台拼接)行顺序可能不同,
* financialRulePayloadDeepEqual 保留数组顺序,故在此按稳定键排序后再比。
*/
function expenseRowCompareKey(row) {
if (!row || typeof row !== "object") {
return [1e9, 1e9, 1e9, 1e9]
}
const tt = Number(row.targetType)
const eid = Number(row.expenseCategoryId)
const nt = Number(row.nodeType)
const ft = Number(row.formType)
return [
Number.isFinite(tt) ? tt : 1e9,
Number.isFinite(eid) ? eid : 1e9,
Number.isFinite(nt) ? nt : 1e9,
Number.isFinite(ft) ? ft : 1e9
]
}
function compareExpenseRowsForCompare(a, b) {
const ka = expenseRowCompareKey(a)
const kb = expenseRowCompareKey(b)
for (let i = 0; i < ka.length; i++) {
if (ka[i] !== kb[i]) return ka[i] - kb[i]
}
const na = a && a.expenseCategoryName != null ? String(a.expenseCategoryName) : ""
const nb = b && b.expenseCategoryName != null ? String(b.expenseCategoryName) : ""
if (na < nb) return -1
if (na > nb) return 1
return 0
}
function sortExpenseRulesInPlace(arr) {
if (!Array.isArray(arr) || arr.length < 2) return
arr.sort(compareExpenseRowsForCompare)
}
function normalizeExpenseCategoryRowForCompare(row, sortForCompare = true) {
if (!row || typeof row !== "object") return row
const copy = { ...row }
delete copy.merchant
if (copy.categoryType === "" || copy.categoryType == null) {
delete copy.categoryType
}
pruneTargetMerchantEmpties(copy)
let childrenOut
if (Array.isArray(copy.children) && copy.children.length > 0) {
childrenOut = copy.children.map((ch) =>
normalizeExpenseCategoryRowForCompare(ch, sortForCompare)
)
} else if (Array.isArray(copy.children) && copy.children.length === 0) {
delete copy.children
}
const out = {}
ALLOWED_EXPENSE_KEYS.forEach((k) => {
if (k === "children") {
if (childrenOut && childrenOut.length) {
if (sortForCompare) sortExpenseRulesInPlace(childrenOut)
out.children = childrenOut
}
return
}
if (
Object.prototype.hasOwnProperty.call(copy, k)
&& copy[k] !== undefined
) {
out[k] = copy[k]
}
})
omitUndefinedShallow(out)
return out
}
function pickComparableTopRule(o) {
const next = {}
ALLOWED_TOP_KEYS.forEach((k) => {
if (Object.prototype.hasOwnProperty.call(o, k) && o[k] !== undefined) {
next[k] = o[k]
}
})
return next
}
/**
* 对账规则与基线 / 快照比对前归一化(仅用于 financialRulePayloadDeepEqual):
* - 去掉接口多出的顶层/费用行字段,与 getBackendFormat 语义对齐
* - expenseCategoryRules:去掉 merchant;空 categoryType;空 children;空 targetMerchant*
* - 顶层:空 categoryType;空 upgradeGoods/packageGoods/goodsTypeGroupRelations 整键删除
* - groupDivisionRuleId 与 id 相同时去掉重复字段
* - expenseCategoryRules(及行内 children):按 targetType、expenseCategoryId、nodeType、formType、name 稳定排序(默认仅比对;提交序列化可传 { forCompare: false } 保留表单顺序)
*
* @param {object} rule
* @param {{ forCompare?: boolean }} [options]
* @returns {object}
*/
export function normalizeReconciliationRuleForCompare(rule, options = {}) {
const forCompare = options.forCompare !== false
if (!rule || typeof rule !== "object") return rule
const o = deepClone(rule)
if (o.categoryType === "" || o.categoryType == null) {
delete o.categoryType
}
normalizePrimaryRuleId(o)
for (let i = 0; i < TOP_EMPTY_ARRAY_KEYS.length; i++) {
const k = TOP_EMPTY_ARRAY_KEYS[i]
if (Array.isArray(o[k]) && o[k].length === 0) {
delete o[k]
}
}
if (Array.isArray(o.expenseCategoryRules)) {
o.expenseCategoryRules = o.expenseCategoryRules.map((r) =>
normalizeExpenseCategoryRowForCompare(r, forCompare)
)
if (forCompare) sortExpenseRulesInPlace(o.expenseCategoryRules)
}
omitUndefinedShallow(o)
omitUndefinedDeep(o, 24)
const picked = pickComparableTopRule(o)
return picked
}