后端能力: - 接入店管家订单接口(/NewOrder/List),明细归一化后按店铺商品匹配系统商品/组合商品 - 新增 service/externalPrintOrder:手动与定时拉单共用入库逻辑,同时写入待审核状态分表 - 新增 schedule/fetchExternalShopOrders:按店铺配置的间隔自动拉单(最小 3 分钟,起点重叠 10 分钟) - 新增 lib/orderItemExpand + service/orderItemResolve:平台行与最小单位行互转; 绑定组合商品时按「数量 = 平台数量 x 组合内数量、单价取子商品零售价」拆成子商品行,解绑可还原 - 明细新增 platform_snapshot,保留平台原样(编码/名称/规格/图片/原行号/原数量/原单价) - 店铺商品列表支持 has_unbound_order 筛选,订单列表支持 exclude_unbound(均已同步导出定义) - 店铺商品只读化:移除新增/编辑/删除动作,仅保留查询与绑定; 同步商品时平台已不存在的商品自动清理(未绑定且未被订单引用直接删除,已绑定或被引用仅下架) - 新增 scripts/rebuild-order-items 存量明细重建脚本,新增 orderItemExpand 单测(8 项) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
110 lines
3.8 KiB
JavaScript
110 lines
3.8 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 定时拉取外部打单平台订单。
|
||
*
|
||
* 每分钟扫描开启「自动拉取订单」的外部打单平台店铺(type=2),
|
||
* 按各店铺配置的间隔(auto_fetch_interval,最小 3 分钟)判断是否到期:
|
||
* - 上次拉取时间 + 间隔 > 现在 → 跳过
|
||
* - 否则以「上次拉取时间向前重叠 10 分钟」为起点拉取,避免边界订单漏单;
|
||
* 从未拉取过时按最近一个间隔拉取
|
||
*
|
||
* 拉取逻辑与手动拉取共用 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;
|
||
|
||
/** 防止上一轮未结束时重复触发(单进程内串行) */
|
||
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 shops = await ctx.model.Shop.findAll({
|
||
where: {
|
||
type: 2,
|
||
status: 1,
|
||
auto_fetch_order: 1,
|
||
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() - interval);
|
||
|
||
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} 条`
|
||
);
|
||
fetched++;
|
||
ctx.logger.info(
|
||
`[external-order-fetch] 店铺 ${shop.id} 拉单成功:订单 ${result.orders},明细 ${result.items}`
|
||
);
|
||
} 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;
|
||
}
|
||
},
|
||
};
|