feat(storeWarehouse): 新增开始自动分配时间 allocate_start_time,支持跨天自动分配窗口,截单时间下限放开到 00:00,开始时间等于截单时间时保存拒绝

This commit is contained in:
liufumingren 2026-09-03 17:00:46 +08:00
parent 25442ba215
commit 66ea0c4c36
5 changed files with 181 additions and 13 deletions

View File

@ -39,8 +39,11 @@ module.exports = app => {
});
const DEFAULT_CUTOFF_ORDER_TIME = '18:00';
const CUTOFF_ORDER_TIME_MIN = '12:00';
const CUTOFF_ORDER_TIME_MIN = '00:00';
const CUTOFF_ORDER_TIME_MAX = '23:59';
const DEFAULT_ALLOCATE_START_TIME = '08:00';
const ALLOCATE_START_TIME_MIN = '00:00';
const ALLOCATE_START_TIME_MAX = '23:59';
const DEFAULT_SUBJECT_CONFIG = Object.freeze({
max_notice_webhook_count: DEFAULT_MAX_NOTICE_WEBHOOK_COUNT,
@ -67,6 +70,7 @@ module.exports = app => {
auto_outbound_rules: DEFAULT_AUTO_OUTBOUND_RULES,
cutoff_order_enabled: 1,
cutoff_order_time: DEFAULT_CUTOFF_ORDER_TIME,
allocate_start_time: DEFAULT_ALLOCATE_START_TIME,
});
const normalizeSubjectConfigText = (value, defaultValue) => {
@ -369,6 +373,28 @@ module.exports = app => {
}
}
if (Object.prototype.hasOwnProperty.call(parsed, 'allocate_start_time')) {
const allocateStartTime = normalizeSubjectConfigText(parsed.allocate_start_time, DEFAULT_ALLOCATE_START_TIME);
if (!/^([01]\d|2[0-3]):[0-5]\d$/.test(allocateStartTime)) {
throw new Error('subject_config.allocate_start_time 格式必须为 HH:mm(如 08:00)');
}
if (allocateStartTime < ALLOCATE_START_TIME_MIN || allocateStartTime > ALLOCATE_START_TIME_MAX) {
normalized.allocate_start_time = DEFAULT_ALLOCATE_START_TIME;
} else {
normalized.allocate_start_time = allocateStartTime;
}
} else {
// 旧数据补齐默认开始自动分配时间
normalized.allocate_start_time = DEFAULT_ALLOCATE_START_TIME;
}
// 跨字段校验:仅当显式同时提交两个时间字段且相等时拒绝(自动分配窗口为空)
if (Object.prototype.hasOwnProperty.call(parsed, 'allocate_start_time')
&& Object.prototype.hasOwnProperty.call(parsed, 'cutoff_order_time')
&& normalized.allocate_start_time === normalized.cutoff_order_time) {
throw new Error('subject_config.allocate_start_time 不能等于 subject_config.cutoff_order_time(自动分配窗口为空)');
}
return normalized;
};

View File

@ -219,7 +219,18 @@ module.exports = app => {
type: 'flag',
default: 1,
required: false,
description: '启用后,当天订单创建时间超过截单时间,不再自动分配快递和储位,不再自动加团',
description: '启用后,订单创建时间早于开始自动分配时间或超过截单时间(开始时间晚于截单时间时截单时间视为次日),不再自动分配快递和储位,不再自动加团',
}),
Object.freeze({
key: 'allocate_start_time',
name: '开始自动分配时间',
type: 'string',
default: '08:00',
required: false,
maxLength: 5,
min: '00:00',
max: '23:59',
description: '开始自动分配时间,格式 HH:mm,如 08:00;早于该时间的订单与超过截单时间行为一致,不自动分配快递和储位,不自动加团;晚于截单时间时截单时间视为次日',
}),
Object.freeze({
key: 'cutoff_order_time',
@ -228,7 +239,7 @@ module.exports = app => {
default: '18:00',
required: false,
maxLength: 5,
min: '12:00',
min: '00:00',
max: '23:59',
description: '截单时间,格式 HH:mm,如 16:00',
}),
@ -437,6 +448,21 @@ module.exports = app => {
}
}
// 跨字段校验:开始自动分配时间与截单时间相同会导致自动分配窗口为空,保存时拒绝
if (!sanitizeOnly) {
const startTimeField = POLICY_CONFIG_FIELD_MAP.allocate_start_time;
const cutoffTimeField = POLICY_CONFIG_FIELD_MAP.cutoff_order_time;
const startTimeForCheck = Object.prototype.hasOwnProperty.call(normalized, 'allocate_start_time')
? String(normalized.allocate_start_time || '').trim()
: String(startTimeField && startTimeField.default || '08:00');
const cutoffTimeForCheck = Object.prototype.hasOwnProperty.call(normalized, 'cutoff_order_time')
? String(normalized.cutoff_order_time || '').trim()
: String(cutoffTimeField && cutoffTimeField.default || '18:00');
if (startTimeForCheck && cutoffTimeForCheck && startTimeForCheck === cutoffTimeForCheck) {
throw new Error(`${fieldName}.allocate_start_time 不能等于 ${fieldName}.cutoff_order_time(自动分配窗口为空)`);
}
}
return Object.keys(normalized).length ? normalized : null;
};

View File

@ -1983,11 +1983,12 @@ class DeliveryOrderService extends Service {
}
/**
* 解析截单配置,货主-仓库级优先,未启用时由仓库级兜底。
* 解析自动分配时间窗口配置,货主-仓库级优先,未启用时由仓库级兜底。
*
* @param {Object} policyConfig 货主-仓库级策略配置。
* @param {Object|null} warehouseSubjectConfig 仓库级主体配置。
* @return {{cutoffEnabled: boolean, cutoffTime: string}} 最终生效的截单开关与截单时间。
* @return {{cutoffEnabled: boolean, cutoffTime: string, allocateStartTime: string}}
* 最终生效的截单开关、截单时间与开始自动分配时间。
*/
resolveCutoffOrderConfig(policyConfig, warehouseSubjectConfig) {
const policyEnabled = this.normalizePolicyFlag(
@ -1996,12 +1997,16 @@ class DeliveryOrderService extends Service {
const policyTime = (policyConfig && typeof policyConfig.cutoff_order_time === 'string'
? policyConfig.cutoff_order_time.trim()
: '') || '18:00';
const policyStartTime = (policyConfig && typeof policyConfig.allocate_start_time === 'string'
? policyConfig.allocate_start_time.trim()
: '') || '08:00';
// 货主-仓库级已启用截单,直接使用
if (policyEnabled) {
return {
cutoffEnabled: true,
cutoffTime: policyTime,
allocateStartTime: policyStartTime,
};
}
@ -2010,15 +2015,20 @@ class DeliveryOrderService extends Service {
const warehouseTime = (typeof warehouseSubjectConfig.cutoff_order_time === 'string'
? warehouseSubjectConfig.cutoff_order_time.trim()
: '') || '18:00';
const warehouseStartTime = (typeof warehouseSubjectConfig.allocate_start_time === 'string'
? warehouseSubjectConfig.allocate_start_time.trim()
: '') || '08:00';
return {
cutoffEnabled: true,
cutoffTime: warehouseTime,
allocateStartTime: warehouseStartTime,
};
}
return {
cutoffEnabled: false,
cutoffTime: '18:00',
allocateStartTime: '08:00',
};
}
@ -2046,6 +2056,54 @@ class DeliveryOrderService extends Service {
return nowTotalMinutes >= cutoffTotalMinutes;
}
/**
* 判断当前时间是否处于自动分配窗口之外。
*
* 同天窗口(开始时间早于截单时间):now < start 或 now >= cutoff 时禁止;
* 跨天窗口(开始时间晚于截单时间,截单时间视为次日):now in [cutoff, start) 时禁止;
* 开始时间与截单时间相同时窗口恒空,防御性全天禁止。
*
* @param {string} allocateStartTimeStr 开始自动分配时间,格式 HH:mm。
* @param {string} cutoffTimeStr 截单时间,格式 HH:mm。
* @param {Date} [nowDate=new Date()] 当前时间(测试可注入)。
* @return {boolean} 是否禁止自动分配。
*/
isAllocationWindowClosed(allocateStartTimeStr, cutoffTimeStr, nowDate = new Date()) {
const parseTime = value => {
if (!value || typeof value !== 'string') {
return null;
}
const trimmed = value.trim();
if (!/^\d{2}:\d{2}$/.test(trimmed)) {
return null;
}
const [ hours, minutes ] = trimmed.split(':').map(Number);
return hours * 60 + minutes;
};
const startMinutes = parseTime(allocateStartTimeStr);
const cutoffMinutes = parseTime(cutoffTimeStr);
const nowMinutes = nowDate.getHours() * 60 + nowDate.getMinutes();
if (startMinutes === null || cutoffMinutes === null) {
return false;
}
if (startMinutes === cutoffMinutes) {
return true; // 窗口为空,防御性全天禁止
}
if (startMinutes < cutoffMinutes) {
// 同天窗口 [start, cutoff)
return nowMinutes < startMinutes || nowMinutes >= cutoffMinutes;
}
// 跨天窗口:截单时间视为次日 [start, 24:00) ∪ [00:00, cutoff)
return nowMinutes >= cutoffMinutes && nowMinutes < startMinutes;
}
/**
* 构建自动加团结构信息。
*
@ -2571,15 +2629,17 @@ class DeliveryOrderService extends Service {
// 截单时间兜底:货主-仓库级未启用时,fallback 到仓库级配置
const warehouseSubjectConfig = await this.getStoreSubjectConfig(context.warehouseId);
const { cutoffEnabled, cutoffTime } = this.resolveCutoffOrderConfig(policyConfig, warehouseSubjectConfig);
const { cutoffEnabled, cutoffTime, allocateStartTime } = this.resolveCutoffOrderConfig(policyConfig, warehouseSubjectConfig);
const isCutoffActive = cutoffEnabled && this.isPastCutoffTime(cutoffTime);
// 早于开始自动分配时间或超过截单时间(开始时间晚于截单时间时截单时间视为次日),均不自动分配
const isAllocationBlocked = cutoffEnabled
&& this.isAllocationWindowClosed(allocateStartTime, cutoffTime);
// 如果需要审核,不触发自动分配快递、储位、加团
const needApproval = Number(policyConfig && policyConfig.need_order_approval) !== 0;
const isAutoAllocateExpressEnabled = !isCutoffActive && !needApproval && this.normalizePolicyFlag(policyConfig.auto_allocate_express);
const isAutoAllocateStorageEnabled = !isCutoffActive && !needApproval && this.normalizePolicyFlag(policyConfig.auto_allocate_storage);
const isAutoAllocateExpressEnabled = !isAllocationBlocked && !needApproval && this.normalizePolicyFlag(policyConfig.auto_allocate_express);
const isAutoAllocateStorageEnabled = !isAllocationBlocked && !needApproval && this.normalizePolicyFlag(policyConfig.auto_allocate_storage);
// 记录开关判定结果,便于排查自动分配快递/储位未触发的问题
this.ctx.logger.info('[DeliveryOrderService.asyncProcessOrderCreated] 自动流程开关判定', {
@ -2587,7 +2647,9 @@ class DeliveryOrderService extends Service {
orderId: context.orderId,
storeId: context.storeId,
cutoffEnabled,
isCutoffActive,
allocateStartTime,
cutoffTime,
isAllocationBlocked,
needApproval,
autoAllocateExpressFlag: policyConfig && policyConfig.auto_allocate_express,
autoAllocateStorageFlag: policyConfig && policyConfig.auto_allocate_storage,
@ -2615,7 +2677,7 @@ class DeliveryOrderService extends Service {
});
}
if (!isCutoffActive && !needApproval) {
if (!isAllocationBlocked && !needApproval) {
await this.runAsyncProcessStep('autoGrouping', async () => {
await this.runAutoGrouping(context);
});

View File

@ -212,7 +212,18 @@ const WAREHOUSE_SETTING_FIELDS = Object.freeze([
type: 'flag',
default: 1,
required: false,
description: '启用后,当天订单创建时间超过截单时间,不再自动分配快递和储位,不再自动加团',
description: '启用后,订单创建时间早于开始自动分配时间或超过截单时间(开始时间晚于截单时间时截单时间视为次日),不再自动分配快递和储位,不再自动加团',
}),
Object.freeze({
key: 'allocate_start_time',
name: '开始自动分配时间',
type: 'string',
default: '08:00',
required: false,
maxLength: 5,
min: '00:00',
max: '23:59',
description: '开始自动分配时间,格式 HH:mm,如 08:00;早于该时间的订单与超过截单时间行为一致,不自动分配快递和储位,不自动加团;晚于截单时间时截单时间视为次日',
}),
Object.freeze({
key: 'cutoff_order_time',
@ -221,7 +232,7 @@ const WAREHOUSE_SETTING_FIELDS = Object.freeze([
default: '18:00',
required: false,
maxLength: 5,
min: '12:00',
min: '00:00',
max: '23:59',
description: '截单时间,格式 HH:mm,如 18:00',
}),

View File

@ -170,4 +170,47 @@ describe('test/app/model/store.test.js', () => {
/subject_config\.inventory_warning_rules\[0\]\.key 不能为空/
);
});
it('should default allocate_start_time to "08:00" and backfill legacy config', () => {
const Store = require('../../../app/model/store')(createApp());
const fromNull = Store.normalizeSubjectConfig(null);
const fromLegacy = Store.normalizeSubjectConfig({ cutoff_order_time: '16:00' });
assert.equal(fromNull.allocate_start_time, '08:00');
assert.equal(fromLegacy.allocate_start_time, '08:00');
});
it('should normalize valid allocate_start_time and allow cross-day window', () => {
const Store = require('../../../app/model/store')(createApp());
const normalized = Store.normalizeSubjectConfig({
allocate_start_time: '20:00',
cutoff_order_time: '06:00',
});
assert.equal(normalized.allocate_start_time, '20:00');
assert.equal(normalized.cutoff_order_time, '06:00');
});
it('should throw for invalid allocate_start_time format', () => {
const Store = require('../../../app/model/store')(createApp());
assert.throws(
() => Store.normalizeSubjectConfig({ allocate_start_time: '8:00' }),
/subject_config\.allocate_start_time 格式必须为 HH:mm(如 08:00)/
);
});
it('should reject allocate_start_time equal to cutoff_order_time', () => {
const Store = require('../../../app/model/store')(createApp());
assert.throws(
() => Store.normalizeSubjectConfig({
allocate_start_time: '09:00',
cutoff_order_time: '09:00',
}),
/subject_config\.allocate_start_time 不能等于 subject_config\.cutoff_order_time/
);
});
});