'use strict'; /** * 通知拦截时间(wms_order_delivery.intercept_time)字段口径工具(纯函数,无 ctx 依赖,可单测)。 * * 背景:`intercept_time` 语义原为「进入拦截流程/库内拦截确认时写入」, * 确认拦截会覆盖发起时刻,导致「通知拦截时间」丢失、且无法与「库内确定拦截时刻」区分。 * * 自 2026-09-10 起统一为「最近一次发起拦截的通知时刻」: * 1. 发起/通知拦截:**首次写入**(当前时间);重复发起**不覆盖**已有值; * 2. 库内拦截确认(confirmIntercept / 拦截扫描 / 自动确认)、退回入库(returned):**不写该字段**(不覆盖); * 3. 取消/解除拦截(batchUnintercept)与订单克隆重置:**置空 null**; * 4. 因此「库内确定拦截时刻」不再使用本字段,改用配送表 `dispath_time`(订单迁移进配送表的时间, * 库内确定拦截后基本都会走迁移)。 */ /** 字段名常量 */ const INTERCEPT_TIME_FIELD = 'intercept_time'; /** * 判断通知拦截时间是否为空(null/undefined/空串)。 * @param {*} value 字段值 * @return {boolean} 是否为空 */ function isEmptyInterceptTime(value) { return value === null || value === undefined || value === ''; } /** * 构造发起拦截时的更新片段:仅当当前值为空时才写入通知拦截时间(首次写入,不覆盖)。 * * 用法(实例更新,实例行需能取到 intercept_time): * ```js * await order.update({ intercept_status: 'requested', ...buildNoticeTimePatch(order.intercept_time), updated_at: now }); * ``` * * @param {*} currentValue 订单当前 intercept_time * @param {Date} [now=new Date()] 本次通知时刻 * @return {Object} 需要写入的字段片段;已有值时返回空对象(不覆盖) */ function buildNoticeTimePatch(currentValue, now = new Date()) { return isEmptyInterceptTime(currentValue) ? { [INTERCEPT_TIME_FIELD]: now } : {}; } /** * 从订单行中筛出「尚未写入通知拦截时间」的订单ID(批量写入用)。 * * 用法(批量更新):主更新不写 intercept_time,随后仅对返回的订单ID写一次通知时刻: * ```js * const fillIds = pickNoticeTimeOrderIds(orderRows); * await model.update({ intercept_status: 'requested', updated_at: now }, { where: { id: { [Op.in]: ids } } }); * if (fillIds.length > 0) { * await model.update({ intercept_time: now }, { where: { id: { [Op.in]: fillIds } } }); * } * ``` * * @param {Array} rows 订单行(需含 id 与 intercept_time) * @return {Array} 需要写入通知拦截时间的订单ID(正整数,保序去重) */ function pickNoticeTimeOrderIds(rows = []) { const seen = new Set(); const orderIds = []; for (const row of (Array.isArray(rows) ? rows : [])) { if (!row || !isEmptyInterceptTime(row[INTERCEPT_TIME_FIELD])) { continue; } const orderId = Number(row.id || 0); if (!Number.isInteger(orderId) || orderId <= 0 || seen.has(orderId)) { continue; } seen.add(orderId); orderIds.push(orderId); } return orderIds; } module.exports = { INTERCEPT_TIME_FIELD, isEmptyInterceptTime, buildNoticeTimePatch, pickNoticeTimeOrderIds, };