196 lines
6.7 KiB
JavaScript
196 lines
6.7 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* 执行波次策略快速扫描通用流程。
|
|
*
|
|
* @param {Object} [options={}] 扫描参数。
|
|
* @param {Array<Object>} [options.orders] 订单数组。
|
|
* @param {Array<Object>} [options.strategyList] 策略数组。
|
|
* @param {Function} [options.getStructureKey] 获取订单结构 key。
|
|
* @param {Function} [options.getExpressKey] 获取订单快递 key。
|
|
* @param {Function} [options.isOrderMatched] 判断订单是否命中策略。
|
|
* @param {Function} [options.getMatchedStrategyIds] 获取订单命中策略ID数组。
|
|
* @param {Function} [options.onProgress] 异步进度回调。
|
|
* @return {Promise<{totalOrderCount:number, processedOrderCount:number, matchedOrderIds:Array<number>, unmatchedOrderIds:Array<number>, matchedStrategyIdsByOrder:Object}>} 扫描结果。
|
|
*/
|
|
async function runWaveStrategyQuickScanRunner(options = {}) {
|
|
const normalizedOptions = options && typeof options === 'object' ? options : {};
|
|
const normalizedOrders = Array.isArray(normalizedOptions.orders) ? normalizedOptions.orders : [];
|
|
const strategyList = Array.isArray(normalizedOptions.strategyList) ? normalizedOptions.strategyList : [];
|
|
const getStructureKey = typeof normalizedOptions.getStructureKey === 'function'
|
|
? normalizedOptions.getStructureKey
|
|
: () => '';
|
|
const getExpressKey = typeof normalizedOptions.getExpressKey === 'function'
|
|
? normalizedOptions.getExpressKey
|
|
: () => '';
|
|
const isOrderMatched = typeof normalizedOptions.isOrderMatched === 'function'
|
|
? normalizedOptions.isOrderMatched
|
|
: () => false;
|
|
const getMatchedStrategyIds = typeof normalizedOptions.getMatchedStrategyIds === 'function'
|
|
? normalizedOptions.getMatchedStrategyIds
|
|
: null;
|
|
const onProgress = typeof normalizedOptions.onProgress === 'function'
|
|
? normalizedOptions.onProgress
|
|
: null;
|
|
|
|
const structureStoreSetMap = new Map();
|
|
const structureOwnerCountMap = new Map();
|
|
const structureExpressCountMap = new Map();
|
|
|
|
for (const order of normalizedOrders) {
|
|
const structureKey = String(getStructureKey(order) || '').trim();
|
|
if (!structureKey) {
|
|
continue;
|
|
}
|
|
|
|
const storeId = String(order && order.store_id || '').trim().toLowerCase();
|
|
if (!structureStoreSetMap.has(structureKey)) {
|
|
structureStoreSetMap.set(structureKey, new Set());
|
|
}
|
|
if (!structureOwnerCountMap.has(structureKey)) {
|
|
structureOwnerCountMap.set(structureKey, new Map());
|
|
}
|
|
if (!structureExpressCountMap.has(structureKey)) {
|
|
structureExpressCountMap.set(structureKey, new Map());
|
|
}
|
|
if (storeId) {
|
|
structureStoreSetMap.get(structureKey).add(storeId);
|
|
const ownerCountMap = structureOwnerCountMap.get(structureKey);
|
|
ownerCountMap.set(storeId, Number(ownerCountMap.get(storeId) || 0) + 1);
|
|
}
|
|
|
|
const expressKey = String(getExpressKey(order) || '').trim().toLowerCase();
|
|
if (expressKey) {
|
|
const expressCountMap = structureExpressCountMap.get(structureKey);
|
|
expressCountMap.set(expressKey, Number(expressCountMap.get(expressKey) || 0) + 1);
|
|
}
|
|
}
|
|
|
|
const structurePrimaryOwnerMap = new Map();
|
|
for (const [ structureKey, ownerCountMap ] of structureOwnerCountMap.entries()) {
|
|
if (!(ownerCountMap instanceof Map) || ownerCountMap.size === 0) {
|
|
continue;
|
|
}
|
|
|
|
let primaryOwner = '';
|
|
let maxCount = 0;
|
|
for (const [ storeId, count ] of ownerCountMap.entries()) {
|
|
const normalizedStoreId = String(storeId || '').trim().toLowerCase();
|
|
const normalizedCount = Number(count || 0);
|
|
if (!normalizedStoreId || normalizedCount <= 0) {
|
|
continue;
|
|
}
|
|
|
|
if (
|
|
normalizedCount > maxCount
|
|
|| (normalizedCount === maxCount && (!primaryOwner || normalizedStoreId < primaryOwner))
|
|
) {
|
|
maxCount = normalizedCount;
|
|
primaryOwner = normalizedStoreId;
|
|
}
|
|
}
|
|
|
|
if (primaryOwner) {
|
|
structurePrimaryOwnerMap.set(structureKey, primaryOwner);
|
|
}
|
|
}
|
|
|
|
const structurePrimaryExpressMap = new Map();
|
|
for (const [ structureKey, expressCountMap ] of structureExpressCountMap.entries()) {
|
|
if (!(expressCountMap instanceof Map) || expressCountMap.size === 0) {
|
|
continue;
|
|
}
|
|
|
|
let primaryExpressKey = '';
|
|
let maxCount = 0;
|
|
for (const [ expressKey, count ] of expressCountMap.entries()) {
|
|
const normalizedExpressKey = String(expressKey || '').trim().toLowerCase();
|
|
const normalizedCount = Number(count || 0);
|
|
if (!normalizedExpressKey || normalizedCount <= 0) {
|
|
continue;
|
|
}
|
|
|
|
if (
|
|
normalizedCount > maxCount
|
|
|| (normalizedCount === maxCount && (!primaryExpressKey || normalizedExpressKey < primaryExpressKey))
|
|
) {
|
|
maxCount = normalizedCount;
|
|
primaryExpressKey = normalizedExpressKey;
|
|
}
|
|
}
|
|
|
|
if (primaryExpressKey) {
|
|
structurePrimaryExpressMap.set(structureKey, primaryExpressKey);
|
|
}
|
|
}
|
|
|
|
const matchedOrderIds = [];
|
|
const unmatchedOrderIds = [];
|
|
const matchedStrategyIdsByOrder = {};
|
|
const totalOrderCount = normalizedOrders.length;
|
|
let processedOrderCount = 0;
|
|
|
|
for (const order of normalizedOrders) {
|
|
const context = {
|
|
structureStoreSetMap,
|
|
structureOwnerCountMap,
|
|
structurePrimaryOwnerMap,
|
|
structureExpressCountMap,
|
|
structurePrimaryExpressMap,
|
|
};
|
|
const orderId = Number(order && order.id || 0);
|
|
let matched = false;
|
|
|
|
if (getMatchedStrategyIds) {
|
|
const rawMatchedStrategyIds = getMatchedStrategyIds(order, strategyList, context);
|
|
const matchedStrategyIds = [ ...new Set((Array.isArray(rawMatchedStrategyIds) ? rawMatchedStrategyIds : [])
|
|
.map(id => Number(id))
|
|
.filter(id => Number.isInteger(id) && id > 0)) ];
|
|
matched = matchedStrategyIds.length > 0;
|
|
|
|
if (Number.isInteger(orderId) && orderId > 0) {
|
|
matchedStrategyIdsByOrder[orderId] = matchedStrategyIds;
|
|
}
|
|
} else {
|
|
matched = Boolean(isOrderMatched(order, strategyList, context));
|
|
}
|
|
|
|
if (Number.isInteger(orderId) && orderId > 0) {
|
|
if (matched) {
|
|
matchedOrderIds.push(orderId);
|
|
} else {
|
|
unmatchedOrderIds.push(orderId);
|
|
}
|
|
}
|
|
|
|
processedOrderCount += 1;
|
|
if (onProgress && (processedOrderCount % 200 === 0 || processedOrderCount === totalOrderCount)) {
|
|
await onProgress({
|
|
totalOrderCount,
|
|
processedOrderCount,
|
|
matchedOrderCount: matchedOrderIds.length,
|
|
unmatchedOrderCount: unmatchedOrderIds.length,
|
|
});
|
|
}
|
|
}
|
|
|
|
if (onProgress && totalOrderCount === 0) {
|
|
await onProgress({
|
|
totalOrderCount,
|
|
processedOrderCount,
|
|
matchedOrderCount: 0,
|
|
unmatchedOrderCount: 0,
|
|
});
|
|
}
|
|
|
|
return {
|
|
totalOrderCount,
|
|
processedOrderCount,
|
|
matchedOrderIds,
|
|
unmatchedOrderIds,
|
|
matchedStrategyIdsByOrder,
|
|
};
|
|
}
|
|
|
|
module.exports = runWaveStrategyQuickScanRunner;
|