抖音平台订单拉不到排查结论:平台侧数据本身能拉到(意洁的 1688→抖店分销单 11-07 16:27 已入库 6 单 DYXD),问题出在「店铺声明的商家 FxUserId 与平台不一致」时 只回 0 单、看不出原因。本次让这种情况自己说话: - dgj.listOrders:全量兜底扫描时记录平台实际出现的商家(UpFxUserId → 单量/店铺名), 返回 merchantSeen / otherMerchants - externalPrintOrder:新增 buildZeroOrderHint —— 0 单且本店商家没出现过时, 输出「近 N 天没有出现 FxUserId=xxx(分区…),实际出现的是 意洁(953516,450 单)…」, 并提示刷新店铺所在区/商家 - 自动拉取的 last_order_sync_msg、手动拉取响应 hint / 前端提示都带上该说明 - 单测 pullOrderHint.test.js(2 例:有提示/无误报,含分区去重) 排查记录(本地账号 1 实测): - 意洁(953516):15 天待处理 10 单 = DYXD 6 + TB 3 + PDD 1,抖音分销单正常 - 抖店分区(fxdd) 近 90 天 1099+154 单全部已发货/关闭,0 条待发货 → 平台侧确实没得拉 - 两家「奇迹商贸」店铺绑的商家 1921984 / 1844767 在全部分区 90 天内 0 单; 平台上真正有单的奇迹商贸商家是 401499 → 需要在 OMS 重新同步店铺 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
125 lines
4.8 KiB
JavaScript
125 lines
4.8 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 定时拉取外部打单平台订单。
|
||
*
|
||
* 每分钟扫描开启「自动拉取订单」的外部打单平台店铺(type=2),
|
||
* 按各店铺配置的间隔(auto_fetch_interval,最小 3 分钟)判断是否到期:
|
||
* - 上次拉取时间 + 间隔 > 现在 → 跳过
|
||
* - 否则以「上次拉取时间向前重叠 10 分钟」为起点拉取,避免边界订单漏单;
|
||
* 从未拉取过时回看 15 天(与店管家「待发货订单」页的默认下单时间范围一致),
|
||
* 把店铺的历史待处理订单一次性拉进来
|
||
* (只按一个间隔回看会漏掉上架前就积压的未发货订单)
|
||
*
|
||
* 拉取逻辑与手动拉取共用 service(externalPrintOrder),保证入库口径一致;
|
||
* 每次执行后记录 last_order_sync_at / last_order_sync_msg,失败不影响其它店铺。
|
||
*/
|
||
const OVERLAP_MS = 10 * 60 * 1000;
|
||
const MIN_INTERVAL_MINUTES = 3;
|
||
const DEFAULT_INTERVAL_MINUTES = 3;
|
||
|
||
/** 首次拉取(从未同步过)时回看的天数:取平台「待发货订单」页的默认范围 15 天 */
|
||
const INITIAL_LOOKBACK_DAYS = 15;
|
||
|
||
/** 防止上一轮未结束时重复触发(单进程内串行) */
|
||
let running = false;
|
||
|
||
module.exports = {
|
||
schedule: {
|
||
cron: '0 * * * * *',
|
||
type: 'worker',
|
||
disable: false,
|
||
},
|
||
|
||
async task(ctx) {
|
||
if (running) {
|
||
ctx.logger.info('[external-order-fetch] 上一轮仍在执行,跳过本次');
|
||
return { skipped: true };
|
||
}
|
||
|
||
running = true;
|
||
try {
|
||
const Op = ctx.app.Sequelize.Op;
|
||
// 主体停用后不再自动拉单
|
||
const enabledPrincipalIds = await ctx.service.cache.getEnabledPrincipalIds();
|
||
if (!enabledPrincipalIds.length) return { shops: 0 };
|
||
const shops = await ctx.model.Shop.findAll({
|
||
where: {
|
||
type: 2,
|
||
status: 1,
|
||
auto_fetch_order: 1,
|
||
principal_id: { [Op.in]: enabledPrincipalIds },
|
||
external_print_account_id: { [Op.ne]: null },
|
||
},
|
||
order: [[ 'id', 'ASC' ]],
|
||
});
|
||
if (!shops.length) return { shops: 0 };
|
||
|
||
const accountIds = Array.from(new Set(shops.map(shop => Number(shop.external_print_account_id))));
|
||
const accounts = await ctx.model.ExternalPrintAccount.findAll({
|
||
where: { id: { [Op.in]: accountIds } },
|
||
include: [
|
||
{
|
||
model: ctx.model.ExternalPrintPlatform,
|
||
as: 'platform',
|
||
attributes: [ 'id', 'code', 'name', 'url', 'fetch_waybill_no' ],
|
||
},
|
||
],
|
||
});
|
||
const accountMap = new Map(accounts.map(account => [ Number(account.id), account ]));
|
||
|
||
const now = new Date();
|
||
let fetched = 0;
|
||
let failed = 0;
|
||
|
||
for (const shop of shops) {
|
||
const interval = Math.max(
|
||
MIN_INTERVAL_MINUTES,
|
||
Number(shop.auto_fetch_interval) || DEFAULT_INTERVAL_MINUTES
|
||
) * 60 * 1000;
|
||
const last = shop.last_order_sync_at ? new Date(shop.last_order_sync_at) : null;
|
||
if (last && now.getTime() - last.getTime() < interval) continue;
|
||
|
||
const account = accountMap.get(Number(shop.external_print_account_id));
|
||
if (!account) {
|
||
await ctx.service.externalPrintOrder.markShopSynced(shop, now, '拉单失败:店铺关联的打单账号不存在');
|
||
failed++;
|
||
continue;
|
||
}
|
||
|
||
const startTime = last
|
||
? new Date(last.getTime() - OVERLAP_MS)
|
||
: new Date(now.getTime() - INITIAL_LOOKBACK_DAYS * 24 * 3600 * 1000);
|
||
|
||
try {
|
||
const result = await ctx.service.externalPrintOrder.syncOrders(shop, account, {
|
||
startTime,
|
||
endTime: now,
|
||
});
|
||
await ctx.service.externalPrintOrder.markShopSynced(
|
||
shop,
|
||
now,
|
||
`自动拉取成功:${result.orders} 单(新增 ${result.created},更新 ${result.updated}),明细 ${result.items} 条` +
|
||
`,跳过平台已发货 ${result.skipped || 0} 单,自动作废 ${result.voided || 0} 单` +
|
||
(result.voidedMissing ? `(其中平台已查不到 ${result.voidedMissing} 单)` : '') +
|
||
(result.hint ? `;${result.hint}` : '')
|
||
);
|
||
fetched++;
|
||
ctx.logger.info(
|
||
`[external-order-fetch] 店铺 ${shop.id} 拉单成功:订单 ${result.orders},明细 ${result.items},跳过已发货 ${result.skipped || 0},` +
|
||
`作废 ${result.voided || 0}(已发货 ${result.voidedShipped || 0}、查不到 ${result.voidedMissing || 0})`
|
||
);
|
||
} catch (err) {
|
||
await ctx.service.externalPrintOrder.markShopSynced(shop, now, `自动拉取失败:${err.message}`);
|
||
failed++;
|
||
ctx.logger.warn(`[external-order-fetch] 店铺 ${shop.id} 拉单失败:${err.message}`);
|
||
}
|
||
}
|
||
|
||
return { shops: shops.length, fetched, failed };
|
||
} finally {
|
||
running = false;
|
||
}
|
||
},
|
||
};
|