buildStep2DisplayMerchants.js
4.5 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
import { sortDivisionTestMerchants, isPlatformMerchant } from './sortDivisionTestMerchants'
import { isMerchantCompletedFromDetail } from './validateStep3Amounts'
export const PLATFORM_FIXED_AMOUNT = 0.5
export const ALLOCATABLE_POOL = 3
export const TOTAL_PROFIT_AMOUNT = 3.5
/** distributionMoney 为 0、null、'' 表示已完成分账测试 */
export function isMerchantTestCompleted(item) {
if (!item) return false
if (item.testCompleted === true) return true
const v = item.distributionMoney
if (v == null || v === '') return true
const n = Number(v)
return Number.isFinite(n) && n === 0
}
function roundMoney(n) {
return Math.round(n * 100) / 100
}
function hasDistributionMoneyValue(item) {
const v = item?.distributionMoney
if (v == null || v === '') return false
const n = Number(v)
return Number.isFinite(n) && n !== 0
}
function hasPresetDivisionMoney(item) {
if (hasDistributionMoneyValue(item)) return true
const v = item.divisionMoney ?? item.testDivisionMoney ?? item.allocateDivisionMoney
return v != null && v !== ''
}
/**
* 对未完成测试的非平台商户随机分配 ALLOCATABLE_POOL 元,最后一户承担尾差。
* @param {object[]} pendingMerchants
* @param {() => number} [randomFn] 0-1,便于单测
*/
export function randomAllocatePool(pendingMerchants, randomFn = Math.random) {
const n = pendingMerchants.length
if (n === 0) return []
if (n === 1) return [ALLOCATABLE_POOL]
const weights = []
let sum = 0
for (let i = 0; i < n - 1; i++) {
const w = randomFn() + 0.01
weights.push(w)
sum += w
}
const amounts = []
let allocated = 0
for (let i = 0; i < n - 1; i++) {
const part = roundMoney((weights[i] / sum) * ALLOCATABLE_POOL)
amounts.push(part)
allocated += part
}
amounts.push(roundMoney(ALLOCATABLE_POOL - allocated))
return amounts
}
function resolveDisplayAmount(item, platform, cache, randomAmounts, pendingIdxRef) {
if (hasDistributionMoneyValue(item)) {
return String(item.distributionMoney)
}
if (platform) {
if (hasPresetDivisionMoney(item)) {
return String(
item.distributionMoney
?? item.divisionMoney
?? item.testDivisionMoney
?? PLATFORM_FIXED_AMOUNT
)
}
return String(PLATFORM_FIXED_AMOUNT)
}
if (hasPresetDivisionMoney(item)) {
return String(
item.distributionMoney
?? item.divisionMoney
?? item.testDivisionMoney
?? item.allocateDivisionMoney
)
}
const key = String(item.merchantId ?? item.merchantName ?? '')
if (cache[key] != null && cache[key] !== '') {
return String(cache[key])
}
const amount = randomAmounts[pendingIdxRef.idx] ?? 0
pendingIdxRef.idx += 1
return String(amount)
}
/**
* @param {object[]} testMerchantDivisions
* @param {{ randomFn?: () => number, cachedAllocations?: Record<string, string> }} [options]
* @returns {object[]} 带 displayDivisionMoney、testCompleted 的副本(已排序)
*/
export function buildStep2DisplayMerchants(testMerchantDivisions, options = {}) {
const sorted = sortDivisionTestMerchants(testMerchantDivisions || [])
const randomFn = options.randomFn || Math.random
const cache = options.cachedAllocations || {}
const pending = sorted.filter((m) => !isMerchantTestCompleted(m) && !isPlatformMerchant(m))
const randomAmounts = randomAllocatePool(pending, randomFn)
const pendingIdxRef = { idx: 0 }
return sorted.map((item) => {
const completed = isMerchantTestCompleted(item)
const platform = isPlatformMerchant(item)
const displayDivisionMoney = completed
? ''
: resolveDisplayAmount(item, platform, cache, randomAmounts, pendingIdxRef)
return {
...item,
testCompleted: completed,
displayDivisionMoney
}
})
}
/** 步骤 3:同一商户列表与完成态,保留 confirmDivisionMoney 供填写 */
export function buildStep3VerifyMerchants(testMerchantDivisions) {
const sorted = sortDivisionTestMerchants(testMerchantDivisions || [])
return sorted.map((m) => ({
...m,
testCompleted: isMerchantCompletedFromDetail(m),
confirmDivisionMoney: m.confirmDivisionMoney ?? ''
}))
}
export function sumDisplayDivisionMoney(merchants) {
return (merchants || []).reduce((sum, m) => {
if (m.testCompleted) return sum
const v = parseFloat(m.displayDivisionMoney)
return sum + (Number.isFinite(v) ? v : 0)
}, 0)
}
export function isTotalProfitAmountValid(merchants, total = TOTAL_PROFIT_AMOUNT) {
const sum = roundMoney(sumDisplayDivisionMoney(merchants))
return Math.abs(sum - total) < 0.001
}