问题:店管家 /NewOrder/List 只支持按时间窗查询,不提供发货状态过滤, 原实现仅按 UpFxUserId 归属就整批入库,导致 sended/success/close 的订单 全部堆进「待审核」(实测店铺 19 近 7 天平台返回 385 单,真正待发货只有 11 单)。 - dgj 新增 isOrderFinished():ErpState∈sended/success/close(及各平台等价状态)、 有 OnlineSendTime、面单 IsSended=true、LogisticStatus>0 任一命中即视为已处理; PrintState/ExpressPrintTime 仅表示已打印面单,不作为判定依据 - listOrders 过滤未发货订单,返回 skipped 与 finished 列表 - externalPrintOrder.syncOrders 新增 voidFinishedOrders():本地仍处于待审核、 平台侧已发货的副本自动作废(写 void_at/void_reason + 操作日志),已审核订单不动 - orderStatus.voidOrder 支持 principalId(定时任务无登录态) - 手动/自动拉单消息、接口返回、接口契约、前端提示补充「跳过已发货 / 自动作废」统计 - 新增 scripts/cleanup-shipped-pending-orders.js(npm run orders:cleanup-shipped): 按同一口径清理历史脏单,默认 dry-run,--apply 才删除 - 新增 isOrderFinished 单测(7 项) 已清理历史脏单 103 条(备份见会话 files/backup-shipped-pending-orders.json), 清理后待审核仅剩 2 单真实的平台待发货订单。 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
107 lines
3.9 KiB
JavaScript
107 lines
3.9 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 清理「平台侧已发货/已终结、但本地仍停留在待审核」的外部打单平台订单。
|
||
*
|
||
* 背景:早期拉单只按时间窗 + 商家归属过滤,没有看平台发货状态,
|
||
* 把 sended(已发货)/ success(交易成功)/ close(已关闭)的订单也写进了待审核。
|
||
* 现在拉单已按 isOrderFinished 过滤,本脚本用于清理历史遗留的脏单。
|
||
*
|
||
* 判定口径与 app/lib/printPlatform/dgj.js 的 isOrderFinished 完全一致(直接复用该函数),
|
||
* 只处理 current_status = pending 的订单:已审核流转过的订单涉及库存与仓库动作,不在本脚本范围。
|
||
*
|
||
* 用法:
|
||
* node scripts/cleanup-shipped-pending-orders.js # 只统计(dry-run)
|
||
* node scripts/cleanup-shipped-pending-orders.js --apply # 真正删除
|
||
* node scripts/cleanup-shipped-pending-orders.js --apply --principal 5
|
||
*/
|
||
const mm = require('egg-mock');
|
||
const DgjService = require('../app/lib/printPlatform/dgj');
|
||
|
||
(async () => {
|
||
const apply = process.argv.includes('--apply');
|
||
const principalIndex = process.argv.indexOf('--principal');
|
||
const principalId = principalIndex > 0 ? Number(process.argv[principalIndex + 1]) : null;
|
||
|
||
const app = mm.app();
|
||
await app.ready();
|
||
const ctx = app.mockContext();
|
||
|
||
const where = { current_status: 'pending' };
|
||
if (principalId) where.principal_id = principalId;
|
||
const indexRows = await ctx.model.OrderIndex.findAll({
|
||
where,
|
||
attributes: [ 'id', 'order_no', 'parent_order_no', 'principal_id' ],
|
||
raw: true,
|
||
});
|
||
console.log(`待审核订单 ${indexRows.length} 条,开始按平台原始结构判定…`);
|
||
|
||
const targets = [];
|
||
for (const row of indexRows) {
|
||
const order = await ctx.model.Order.findOne({
|
||
where: { principal_id: row.principal_id, external_order_id: row.parent_order_no },
|
||
attributes: [ 'id', 'order_no', 'external_data', 'platform_status' ],
|
||
raw: true,
|
||
});
|
||
if (!order) continue;
|
||
const raw = typeof order.external_data === 'string'
|
||
? safeJson(order.external_data)
|
||
: (order.external_data || {});
|
||
if (!DgjService.isOrderFinished(raw)) continue;
|
||
targets.push({
|
||
index_id: Number(row.id),
|
||
order_id: Number(order.id),
|
||
principal_id: Number(row.principal_id),
|
||
order_no: row.order_no,
|
||
platform_status: order.platform_status || raw.ErpState || raw.Status || '',
|
||
});
|
||
}
|
||
|
||
for (const item of targets) {
|
||
console.log(` ${item.order_no} 主体 ${item.principal_id} 平台状态 ${item.platform_status}`);
|
||
}
|
||
console.log(`命中「平台已发货/已终结但仍在待审核」${targets.length} 条`);
|
||
|
||
if (!targets.length || !apply) {
|
||
if (targets.length) console.log('[dry-run] 未写库,确认后加 --apply 执行删除');
|
||
await app.close();
|
||
process.exit(0);
|
||
}
|
||
|
||
const transaction = await ctx.model.transaction();
|
||
try {
|
||
const ids = targets.map(item => item.index_id);
|
||
const orderIds = targets.map(item => item.order_id);
|
||
await ctx.model.OrderStatusItem.destroy({ where: { order_id: ids }, transaction });
|
||
await ctx.model.OrderStatusPending.destroy({ where: { id: ids }, transaction });
|
||
await ctx.model.OrderIndex.destroy({ where: { id: ids }, transaction });
|
||
await ctx.model.Order.destroy({ where: { id: orderIds }, transaction });
|
||
await transaction.commit();
|
||
} catch (err) {
|
||
await transaction.rollback();
|
||
console.error(`清理失败,已回滚:${err.message}`);
|
||
await app.close();
|
||
process.exit(1);
|
||
}
|
||
|
||
console.log(`已删除 ${targets.length} 条待审核脏单(含明细与索引)`);
|
||
await app.close();
|
||
process.exit(0);
|
||
})().catch(err => {
|
||
console.error(err);
|
||
process.exit(1);
|
||
});
|
||
|
||
/**
|
||
* 宽松解析 JSON:脏数据解析失败时返回空对象(视为未发货,避免误删)
|
||
* @param {string} text JSON 文本
|
||
* @return {Object} 解析结果
|
||
*/
|
||
function safeJson(text) {
|
||
try {
|
||
return JSON.parse(text) || {};
|
||
} catch {
|
||
return {};
|
||
}
|
||
}
|