oms/backend/app/schedule/refreshExternalShopAreas.js
liufumingren 2b8fcc2286 外部打单平台增加「是否自带运单」
- external_print_platforms 新增 has_own_waybill(默认 0),同步接口契约与前端类型
- 自带运单的平台取号时复用平台已确认运单号(WaybillCodes),只重新拉取打印数据,不分配新面单
- 平台属性查询补齐 has_own_waybill,更新店管家取号文档并补测试

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-23 21:13:48 +08:00

106 lines
4.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 定时刷新外部打单平台店铺的「所在区」。
*
* 店管家把商家挂在若干分区(`DbAreas[]`,页面上的「所在区」)下,平台迁移分区或解绑时这份声明会变,
* 声明过期会导致拉单漏单/报错,因此每小时检查一次,按账号维度刷新:
* - 同一账号下的店铺共享一次商家列表拉取(只请求平台一次);
* - 账号下最近一次刷新时间(shops.extra.area_synced_at)未超过间隔(默认 6 小时,可用
* DGJ_AREA_REFRESH_HOURS 调整)时跳过;
* - 商家已不在合作列表(解绑/停用)时把店铺自动禁用,自动拉单与一键拉单都会跳过,
* 需人工把店铺状态改回启用才会恢复。
*/
const AREA_REFRESH_INTERVAL_HOURS = Math.max(1, Number(process.env.DGJ_AREA_REFRESH_HOURS) || 6);
/** 防止上一轮未结束时重复触发(单进程内串行) */
let running = false;
module.exports = {
schedule: {
cron: '0 37 * * * *',
type: 'worker',
disable: false,
},
async task(ctx) {
if (running) {
ctx.logger.info('[external-shop-area] 上一轮仍在执行,跳过本次');
return { skipped: true };
}
running = true;
try {
const Op = ctx.app.Sequelize.Op;
// 主体停用后不再刷新
const enabledPrincipalIds = await ctx.service.cache.getEnabledPrincipalIds();
if (!enabledPrincipalIds.length) return { accounts: 0 };
const shops = await ctx.model.Shop.findAll({
where: {
type: 2,
principal_id: { [Op.in]: enabledPrincipalIds },
external_print_account_id: { [Op.ne]: null },
},
attributes: [ 'id', 'external_print_account_id', 'extra' ],
order: [[ 'id', 'ASC' ]],
});
if (!shops.length) return { accounts: 0 };
// 同账号下的店铺共享一次拉取结果:按账号分组,再逐个账号判断是否到期
const groups = new Map();
for (const shop of shops) {
const accountId = Number(shop.external_print_account_id);
if (!groups.has(accountId)) groups.set(accountId, []);
groups.get(accountId).push(shop);
}
const now = new Date();
const intervalMs = AREA_REFRESH_INTERVAL_HOURS * 3600 * 1000;
const dueAccountIds = [];
for (const [ accountId, list ] of groups) {
const last = ctx.service.shopAreaSync.lastSyncedAt(list);
if (!last || now.getTime() - last.getTime() >= intervalMs) dueAccountIds.push(accountId);
}
if (!dueAccountIds.length) return { accounts: groups.size, refreshed: 0 };
const accounts = await ctx.model.ExternalPrintAccount.findAll({
where: { id: { [Op.in]: dueAccountIds } },
include: [
{
model: ctx.model.ExternalPrintPlatform,
as: 'platform',
attributes: [ 'id', 'code', 'name', 'url', 'fetch_waybill_no', 'has_own_waybill' ],
},
],
});
let refreshed = 0;
let failed = 0;
let disabled = 0;
let voided = 0;
for (const account of accounts) {
try {
const result = await ctx.service.shopAreaSync.refreshAccount(account);
refreshed++;
disabled += result.disabled.length;
voided += result.voided;
ctx.logger.info(
`[external-shop-area] 账号 ${account.id} 刷新店铺所在区:商家 ${result.merchants},` +
`本地店铺 ${result.shops},更新 ${result.updated},自动禁用 ${result.disabled.length}` +
(result.voided ? `,作废待审核订单 ${result.voided}` : '') +
(result.missing.length ? `,已不在合作列表 ${result.missing.length}` : '')
);
} catch (err) {
failed++;
ctx.logger.warn(`[external-shop-area] 账号 ${account.id} 刷新店铺所在区失败:${err.message}`);
}
}
return { accounts: groups.size, refreshed, failed, disabled, voided };
} finally {
running = false;
}
},
};