oms/backend/app/lib/externalPrintConfig.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

71 lines
2.4 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';
/**
* 外部打单(店铺 / 账号 / 平台)配置读取
*
* 推送与取号都是逐单处理,直接查库会放大成 N 倍数据库压力,这里统一走缓存服务:
* 命中返回缓存,未命中查库并回写;缓存服务不可用(如单元测试直接 new Service)时降级直查。
*/
/**
* 读取店铺基础资料(按主体隔离校验)
* @param {Object} ctx egg context
* @param {Number} shopId 店铺ID
* @param {Number} [principalId] 主体ID,传入时校验归属
* @return {Promise<Object|null>} 店铺基础资料(plain),不存在或跨主体为 null
*/
async function getShopBasic(ctx, shopId, principalId) {
if (!shopId) return null;
const cache = ctx.service && ctx.service.cache;
let shop = null;
if (cache && typeof cache.getShopBasic === 'function') {
shop = await cache.getShopBasic(shopId);
} else {
const row = await ctx.model.Shop.findByPk(shopId, {
attributes: [ 'id', 'principal_id', 'name', 'type', 'external_print_account_id' ],
});
shop = row ? row.toJSON() : null;
}
if (!shop) return null;
if (principalId != null && Number(shop.principal_id) !== Number(principalId)) return null;
return shop;
}
/**
* 读取外部打单账号及其平台配置(含 fetch_waybill_no / has_own_waybill)
* @param {Object} ctx egg context
* @param {Number} accountId 外部打单账号ID
* @param {Number} [principalId] 主体ID,传入时校验归属
* @return {Promise<Object|null>} 账号与平台配置(plain),不存在或跨主体为 null
*/
async function getAccountWithPlatform(ctx, accountId, principalId) {
if (!accountId) return null;
const cache = ctx.service && ctx.service.cache;
let account = null;
if (cache && typeof cache.getExternalPrintAccount === 'function') {
account = await cache.getExternalPrintAccount(accountId);
} else {
const row = await ctx.model.ExternalPrintAccount.findByPk(accountId, {
attributes: [ 'id', 'principal_id', 'platform_id' ],
include: [
{
model: ctx.model.ExternalPrintPlatform,
as: 'platform',
attributes: [ 'id', 'code', 'name', 'url', 'fetch_waybill_no', 'has_own_waybill' ],
},
],
});
account = row ? row.toJSON() : null;
}
if (!account) return null;
if (principalId != null && Number(account.principal_id) !== Number(principalId)) return null;
return account;
}
module.exports = {
getShopBasic,
getAccountWithPlatform,
};