- 新增 shopAreaSync:按账号拉一次商家列表(同账号多店铺共享,PageSize=200 翻页),
把最新商家结构(FxUserId、分区声明 DbAreas)写回 shops.external_data;
商家已不在合作列表时自动把店铺置为禁用,并写明原因
- 手动刷新入口:POST /api/shops/{id}/refresh-area(货主权限)、
POST /api/external-print-accounts/{id}/refresh-shops-area(集团权限)
- 新增定时任务 refreshExternalShopAreas:每小时巡检,按账号 6 小时刷新一次
(DGJ_AREA_REFRESH_HOURS 可调),自动禁用店铺后不再参与自动拉单与一键拉单
- 新增 shopOrderGuard:店铺禁用立即作废该店铺待审核订单
(void_reason 前缀「店铺已禁用,暂停处理」),重新启用时只恢复因此暂停的订单;
禁用店铺再拉单直接拒绝,避免又拉回待审核单
- externalPrintOrder 待审核单核对改用 LogicOrderId 批量回读(50 单/批,最多 300 单)
- orderStatus.restore 支持显式 principalId,供无登录态的系统任务调用
- 前端:店铺列表新增「所在区」列与行内/详情「刷新所在区」按钮,
系统自动禁用的店铺状态带原因提示,启停与刷新后提示作废/恢复了多少订单
- 文档:新增 §4.2.2「所在区」刷新、§4.2.3 店铺禁用/启用与待审核订单
(原 LastWaybillCode 小节顺延为 §4.2.4,WMS 对接文档引用同步)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
106 lines
4.0 KiB
JavaScript
106 lines
4.0 KiB
JavaScript
'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' ],
|
||
},
|
||
],
|
||
});
|
||
|
||
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;
|
||
}
|
||
},
|
||
};
|