fix(workbench): 团购波次打包拦截订单静默跳过且日志仅写非拦截订单
- _packOrder 团购波次拦截订单不再拒绝,返回完成结构静默跳过,不创建打包任务 - _getWaveOrderIds 默认排除拦截订单,团购打包操作日志仅写给非拦截订单 - 散单拦截订单仍拒绝打包,保持原行为
This commit is contained in:
parent
c9fc06ebe6
commit
68c279b896
@ -56,9 +56,6 @@ class WorkbenchService extends Service {
|
||||
if (where.name) {
|
||||
queryWhere.name = { [Op.like]: `%${where.name}%` };
|
||||
}
|
||||
if (where.barcode) {
|
||||
queryWhere.barcode = { [Op.like]: `%${where.barcode}%` };
|
||||
}
|
||||
if (where.status !== undefined && where.status !== null && where.status !== '') {
|
||||
queryWhere.status = parseInt(where.status);
|
||||
}
|
||||
@ -107,7 +104,7 @@ class WorkbenchService extends Service {
|
||||
|
||||
/**
|
||||
* 创建工作台
|
||||
* @param {Object} data - { name, barcode, status, remark }
|
||||
* @param {Object} data - { name, type, status, device_ids, remark }
|
||||
* @returns {Promise<Object>}
|
||||
*/
|
||||
async create(data) {
|
||||
@ -121,7 +118,6 @@ class WorkbenchService extends Service {
|
||||
const record = await ctx.model.Workbench.create({
|
||||
warehouse_id: warehouseId,
|
||||
name: String(data.name).trim(),
|
||||
barcode: data.barcode ? String(data.barcode).trim() : null,
|
||||
type: data.type || 'personal',
|
||||
status: data.status !== undefined ? parseInt(data.status) : 1,
|
||||
device_ids: data.device_ids || null,
|
||||
@ -135,7 +131,7 @@ class WorkbenchService extends Service {
|
||||
/**
|
||||
* 更新工作台
|
||||
* @param {number} id - 工作台 ID
|
||||
* @param {Object} data - 要更新的字段 { name, barcode, status, remark }
|
||||
* @param {Object} data - 要更新的字段 { name, type, status, device_ids, remark }
|
||||
* @returns {Promise<Object>}
|
||||
*/
|
||||
async update(id, data) {
|
||||
@ -152,7 +148,6 @@ class WorkbenchService extends Service {
|
||||
|
||||
const updateFields = {};
|
||||
if (data.name !== undefined) updateFields.name = String(data.name).trim();
|
||||
if (data.barcode !== undefined) updateFields.barcode = data.barcode ? String(data.barcode).trim() : null;
|
||||
if (data.type !== undefined) updateFields.type = data.type;
|
||||
if (data.status !== undefined) updateFields.status = parseInt(data.status);
|
||||
if (data.device_ids !== undefined) updateFields.device_ids = data.device_ids;
|
||||
@ -316,12 +311,15 @@ class WorkbenchService extends Service {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取波次下所有订单ID(遍历 current + dispatch 分表)
|
||||
* 获取波次下订单ID(遍历 current + dispatch 分表)。
|
||||
* 默认排除拦截订单(intercept_status 空值按 none 处理),
|
||||
* 团购打包日志只写给非拦截订单,拦截订单跳过。
|
||||
* @param {number} warehouseId
|
||||
* @param {number} waveId
|
||||
* @param {boolean} [excludeIntercepted=true] 是否排除拦截订单
|
||||
* @returns {Promise<number[]>}
|
||||
*/
|
||||
async _getWaveOrderIds(warehouseId, waveId) {
|
||||
async _getWaveOrderIds(warehouseId, waveId, excludeIntercepted = true) {
|
||||
const { ctx } = this;
|
||||
const orderDeliveryFactory = ctx.model.OrderDeliveryFactory;
|
||||
const tableOrders = [
|
||||
@ -333,8 +331,12 @@ class WorkbenchService extends Service {
|
||||
try {
|
||||
const model = await entry.getModel();
|
||||
if (!model) continue;
|
||||
const where = { wave_id: waveId, warehouse_id: warehouseId };
|
||||
if (excludeIntercepted) {
|
||||
where.intercept_status = { [ctx.app.Sequelize.Op.in]: [ 'none', null ] };
|
||||
}
|
||||
const rows = await model.findAll({
|
||||
where: { wave_id: waveId, warehouse_id: warehouseId },
|
||||
where,
|
||||
attributes: [ 'id' ],
|
||||
raw: true,
|
||||
});
|
||||
@ -448,7 +450,7 @@ class WorkbenchService extends Service {
|
||||
}]);
|
||||
ctx.logger.info('[Workbench._writePackOperationLog] 散单操作日志已写入 orderId=%d', orderId);
|
||||
} else {
|
||||
// 团购:批量写入波次下所有订单的操作日志
|
||||
// 团购:批量写入波次下所有非拦截订单的操作日志(拦截订单跳过,不写日志)
|
||||
if (!waveId || waveId <= 0) {
|
||||
// 无波次ID时按散单处理
|
||||
await operationLogService.createOperationLogs([{
|
||||
@ -626,11 +628,6 @@ class WorkbenchService extends Service {
|
||||
|
||||
if (!orderRow) throw new Error(`未找到发货单 [${deliveryOrderCode}]`);
|
||||
|
||||
// 拦截流程中的订单不允许打包
|
||||
if (isInterceptedOrderStatus(orderRow.intercept_status)) {
|
||||
throw new Error(`订单已被拦截,无法打包 [${deliveryOrderCode}]`);
|
||||
}
|
||||
|
||||
const orderId = orderRow.id;
|
||||
const waveId = orderRow.wave_id || 0;
|
||||
const actualWaveNo = waveNo || orderRow.wave_no || '';
|
||||
@ -655,6 +652,27 @@ class WorkbenchService extends Service {
|
||||
} catch (_err) { /* ignore */ }
|
||||
}
|
||||
|
||||
// 拦截流程中的订单不允许打包:
|
||||
// - 散单:直接拒绝(保持原行为);
|
||||
// - 团购波次:静默跳过并视为完成(返回完成结构,不带任何提示字段),
|
||||
// 不创建打包任务、不写操作日志,不阻断波次整体提交完成。
|
||||
if (isInterceptedOrderStatus(orderRow.intercept_status)) {
|
||||
if (isSandan === 0) {
|
||||
ctx.logger.info('[Workbench._packOrder] 团购波次拦截订单静默跳过 waveId=%d waveNo=%s orderId=%d deliveryOrderCode=%s',
|
||||
waveId, actualWaveNo, orderId, deliveryOrderCode);
|
||||
return {
|
||||
order_id: orderId,
|
||||
delivery_order_code: deliveryOrderCode,
|
||||
wave_no: actualWaveNo,
|
||||
isSandan: 0,
|
||||
pack_mode: 2,
|
||||
status: 2,
|
||||
batch: { wave_id: waveId, wave_no: actualWaveNo, wave_type: waveType, total_orders: totalOrders },
|
||||
};
|
||||
}
|
||||
throw new Error(`订单已被拦截,无法打包 [${deliveryOrderCode}]`);
|
||||
}
|
||||
|
||||
// 步骤3:获取工作台 device_ids
|
||||
let deviceIds = null;
|
||||
if (validWorkbenchId) {
|
||||
@ -1610,31 +1628,6 @@ class WorkbenchService extends Service {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查条码在仓库内是否已存在(排除指定ID)
|
||||
* @param {string} barcode - 条码
|
||||
* @param {number} [excludeId] - 排除的工作台ID(更新时使用)
|
||||
* @returns {Promise<boolean>}
|
||||
*/
|
||||
async checkBarcodeExists(barcode, excludeId) {
|
||||
const { ctx } = this;
|
||||
const warehouseId = ctx.user.store_id;
|
||||
|
||||
if (!barcode) return false;
|
||||
|
||||
const where = {
|
||||
warehouse_id: warehouseId,
|
||||
barcode: String(barcode).trim(),
|
||||
};
|
||||
if (excludeId) {
|
||||
const { Op } = ctx.app.Sequelize;
|
||||
where.id = { [Op.ne]: excludeId };
|
||||
}
|
||||
|
||||
const count = await ctx.model.Workbench.count({ where });
|
||||
return count > 0;
|
||||
}
|
||||
|
||||
// ==================== 绑定码 / 登录码 / 令牌 ====================
|
||||
|
||||
/**
|
||||
@ -1825,11 +1818,21 @@ class WorkbenchService extends Service {
|
||||
}
|
||||
|
||||
/**
|
||||
* 为已有 client_code 重新生成绑定码并更新缓存。
|
||||
* 为已有 client_code 重新生成绑定码并更新缓存(旧绑定码缓存同步失效)。
|
||||
* @param {string} clientCode 客户端编码
|
||||
* @param {Object|null} [previousState] 旧会话状态,含旧绑定码时删除旧 code 维度缓存
|
||||
* @return {Promise<{code: string, state: Object}>} 新绑定码与会话状态
|
||||
*/
|
||||
async refreshWorkbenchBindCode(clientCode) {
|
||||
async refreshWorkbenchBindCode(clientCode, previousState = null) {
|
||||
const oldCode = previousState && typeof previousState.code === 'string' ? previousState.code : '';
|
||||
if (oldCode) {
|
||||
try {
|
||||
await this.app.delCacheAsync(this.getWorkbenchBindCodeCacheKey(oldCode));
|
||||
} catch (delErr) {
|
||||
this.app.coreLogger.warn(`删除旧工作台绑定码缓存失败: ${delErr.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
const code = await this.createUniqueWorkbenchBindCode();
|
||||
const state = this.buildWorkbenchBindState(clientCode, code);
|
||||
await this.setWorkbenchBindState(state, WORKBENCH_BIND_CACHE_TTL_SECONDS);
|
||||
@ -1956,8 +1959,23 @@ class WorkbenchService extends Service {
|
||||
};
|
||||
}
|
||||
|
||||
// 超时:重新生成绑定码后才返回数据。
|
||||
const refreshed = await this.refreshWorkbenchBindCode(clientCode);
|
||||
// 超时兜底:重读一次缓存,避免刷新瞬间恰好绑定完成而覆盖结果。
|
||||
try {
|
||||
cachedState = await app.getCacheAsync(clientCacheKey);
|
||||
} catch (cacheErr) {
|
||||
app.coreLogger.warn(`读取工作台绑定码缓存失败: ${cacheErr.message}`);
|
||||
}
|
||||
if (cachedState && cachedState.status === 'bound' && cachedState.token) {
|
||||
return {
|
||||
success: true,
|
||||
code: 200,
|
||||
data: this.formatWorkbenchBindData(cachedState),
|
||||
message: '绑定成功',
|
||||
};
|
||||
}
|
||||
|
||||
// 超时:重新生成绑定码后才返回数据(旧绑定码缓存同步失效)。
|
||||
const refreshed = await this.refreshWorkbenchBindCode(clientCode, cachedState);
|
||||
return {
|
||||
success: true,
|
||||
code: 200,
|
||||
@ -1988,7 +2006,6 @@ class WorkbenchService extends Service {
|
||||
id: plain.id,
|
||||
warehouse_id: plain.warehouse_id,
|
||||
name: plain.name,
|
||||
barcode: plain.barcode,
|
||||
status: plain.status,
|
||||
type: plain.type,
|
||||
remark: plain.remark,
|
||||
@ -2266,6 +2283,230 @@ class WorkbenchService extends Service {
|
||||
|
||||
return { id: record.id, login_code: loginCode };
|
||||
}
|
||||
|
||||
// ==================== 工作台成员(用户登录副表) ====================
|
||||
|
||||
/**
|
||||
* 通过 token 解析工作台记录(缓存优先,未命中查库并回填缓存)。
|
||||
* @param {string} token 工作台令牌
|
||||
* @return {Promise<Object|null>} 工作台记录(模型实例),未命中返回 null
|
||||
*/
|
||||
async resolveWorkbenchByToken(token) {
|
||||
const { ctx, app } = this;
|
||||
const tokenCacheKey = this.getWorkbenchTokenCacheKey(token);
|
||||
try {
|
||||
const cached = await app.getCacheAsync(tokenCacheKey);
|
||||
if (cached && cached.id) {
|
||||
return await ctx.model.Workbench.findOne({
|
||||
where: { id: cached.id },
|
||||
});
|
||||
}
|
||||
} catch (cacheErr) {
|
||||
app.coreLogger.warn(`读取工作台 token 缓存失败: ${cacheErr.message}`);
|
||||
}
|
||||
|
||||
const record = await ctx.model.Workbench.findOne({
|
||||
where: { token },
|
||||
});
|
||||
if (record) {
|
||||
await app.setCacheAsync(
|
||||
tokenCacheKey,
|
||||
this.formatWorkbenchTokenInfo(record),
|
||||
WORKBENCH_TOKEN_CACHE_TTL_SECONDS
|
||||
);
|
||||
}
|
||||
return record;
|
||||
}
|
||||
|
||||
/**
|
||||
* 标准化成员行 ID 列表:支持数组、单值、逗号分隔字符串,去重后仅保留正整数。
|
||||
* @param {*} ids 原始行 ID 入参
|
||||
* @return {number[]} 标准化后的行 ID 列表
|
||||
*/
|
||||
normalizeWorkbenchMemberIds(ids) {
|
||||
const source = Array.isArray(ids) ? ids : (ids === undefined || ids === null ? [] : [ ids ]);
|
||||
const values = [];
|
||||
source.forEach(item => {
|
||||
if (typeof item === 'string' && item.includes(',')) {
|
||||
item.split(',').forEach(part => values.push(part.trim()));
|
||||
} else {
|
||||
values.push(item);
|
||||
}
|
||||
});
|
||||
return [ ...new Set(values
|
||||
.map(item => Number(item))
|
||||
.filter(n => Number.isInteger(n) && n > 0)) ];
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除工作台成员(供 workbench.removeMember,免登录免鉴权,以工作台 token 鉴权)。
|
||||
* 按 token 解析工作台后,仅删除归属于该工作台的成员行(软删除)。
|
||||
* @param {Object} [data={}] - { ids, token }
|
||||
* @return {Promise<Object>} { success, code, data, message }
|
||||
*/
|
||||
async removeWorkbenchMember(data = {}) {
|
||||
const { ctx, app } = this;
|
||||
try {
|
||||
const rawToken = typeof data.token === 'string' ? data.token.trim() : '';
|
||||
if (!rawToken) {
|
||||
return {
|
||||
success: false,
|
||||
code: 0,
|
||||
statusCode: 400,
|
||||
data: null,
|
||||
message: '工作台 token 不能为空',
|
||||
};
|
||||
}
|
||||
|
||||
const memberIds = this.normalizeWorkbenchMemberIds(data.ids ?? data.memberIds ?? data.member_ids);
|
||||
if (memberIds.length === 0) {
|
||||
return {
|
||||
success: false,
|
||||
code: 0,
|
||||
statusCode: 400,
|
||||
data: null,
|
||||
message: '成员行ID不能为空或无效',
|
||||
};
|
||||
}
|
||||
|
||||
const workbench = await this.resolveWorkbenchByToken(rawToken);
|
||||
if (!workbench) {
|
||||
return {
|
||||
success: false,
|
||||
code: 0,
|
||||
statusCode: 403,
|
||||
data: null,
|
||||
message: '工作台不存在或 token 无效',
|
||||
};
|
||||
}
|
||||
|
||||
const rows = await ctx.model.WorkbenchMember.findAll({
|
||||
where: { id: memberIds, workbench_id: workbench.id },
|
||||
attributes: [ 'id' ],
|
||||
});
|
||||
const matchedIds = rows.map(row => row.id);
|
||||
if (matchedIds.length === 0) {
|
||||
return {
|
||||
success: false,
|
||||
code: 0,
|
||||
statusCode: 404,
|
||||
data: null,
|
||||
message: '未找到可移除的成员',
|
||||
};
|
||||
}
|
||||
|
||||
await ctx.model.WorkbenchMember.destroy({
|
||||
where: { id: matchedIds, workbench_id: workbench.id },
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
code: 200,
|
||||
data: {
|
||||
removed: matchedIds.length,
|
||||
ids: matchedIds,
|
||||
},
|
||||
message: '移除成功',
|
||||
};
|
||||
} catch (error) {
|
||||
app.coreLogger.error(`移除工作台成员失败: ${error.message}`);
|
||||
return {
|
||||
success: false,
|
||||
code: 0,
|
||||
data: null,
|
||||
message: '移除工作台成员失败,请稍后重试',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定工作台成员(供 workbench.bindMember,免登录免鉴权,以工作台 token 鉴权)。
|
||||
* 入参用户ID(可空,兼容非系统账号用户)+ 姓名(必填)+ 工作台 token。
|
||||
* @param {Object} [data={}] - { userId | user_id, userName | user_name | name, token }
|
||||
* @return {Promise<Object>} { success, code, data, message }
|
||||
*/
|
||||
async bindWorkbenchMember(data = {}) {
|
||||
const { ctx, app } = this;
|
||||
try {
|
||||
const rawToken = typeof data.token === 'string' ? data.token.trim() : '';
|
||||
if (!rawToken) {
|
||||
return {
|
||||
success: false,
|
||||
code: 0,
|
||||
statusCode: 400,
|
||||
data: null,
|
||||
message: '工作台 token 不能为空',
|
||||
};
|
||||
}
|
||||
|
||||
const rawUserName = typeof data.userName === 'string'
|
||||
? data.userName
|
||||
: (typeof data.user_name === 'string'
|
||||
? data.user_name
|
||||
: (typeof data.name === 'string' ? data.name : ''));
|
||||
const userName = rawUserName.trim();
|
||||
if (!userName) {
|
||||
return {
|
||||
success: false,
|
||||
code: 0,
|
||||
statusCode: 400,
|
||||
data: null,
|
||||
message: '姓名不能为空',
|
||||
};
|
||||
}
|
||||
|
||||
const rawUserId = data.userId ?? data.user_id;
|
||||
const userId = rawUserId === null || rawUserId === undefined || rawUserId === ''
|
||||
? null
|
||||
: Number(rawUserId);
|
||||
if (userId !== null && (!Number.isInteger(userId) || userId <= 0)) {
|
||||
return {
|
||||
success: false,
|
||||
code: 0,
|
||||
statusCode: 400,
|
||||
data: null,
|
||||
message: '用户ID无效',
|
||||
};
|
||||
}
|
||||
|
||||
const workbench = await this.resolveWorkbenchByToken(rawToken);
|
||||
if (!workbench) {
|
||||
return {
|
||||
success: false,
|
||||
code: 0,
|
||||
statusCode: 403,
|
||||
data: null,
|
||||
message: '工作台不存在或 token 无效',
|
||||
};
|
||||
}
|
||||
|
||||
const member = await ctx.model.WorkbenchMember.create({
|
||||
workbench_id: workbench.id,
|
||||
user_id: userId,
|
||||
user_name: userName,
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
code: 200,
|
||||
data: {
|
||||
id: member.id,
|
||||
workbench_id: member.workbench_id,
|
||||
user_id: member.user_id,
|
||||
user_name: member.user_name,
|
||||
},
|
||||
message: '绑定成功',
|
||||
};
|
||||
} catch (error) {
|
||||
app.coreLogger.error(`绑定工作台成员失败: ${error.message}`);
|
||||
return {
|
||||
success: false,
|
||||
code: 0,
|
||||
data: null,
|
||||
message: '绑定工作台成员失败,请稍后重试',
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = WorkbenchService;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user