'use strict'; /** * 外部打单(店铺 / 账号 / 平台)配置读取 * * 推送与取号都是逐单处理,直接查库会放大成 N 倍数据库压力,这里统一走缓存服务: * 命中返回缓存,未命中查库并回写;缓存服务不可用(如单元测试直接 new Service)时降级直查。 */ /** * 读取店铺基础资料(按主体隔离校验) * @param {Object} ctx egg context * @param {Number} shopId 店铺ID * @param {Number} [principalId] 主体ID,传入时校验归属 * @return {Promise} 店铺基础资料(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) * @param {Object} ctx egg context * @param {Number} accountId 外部打单账号ID * @param {Number} [principalId] 主体ID,传入时校验归属 * @return {Promise} 账号与平台配置(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' ], }, ], }); 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, };