diff --git a/app/service/systemMethods/index.js b/app/service/systemMethods/index.js index 0702e88..5af9d95 100644 --- a/app/service/systemMethods/index.js +++ b/app/service/systemMethods/index.js @@ -425,6 +425,39 @@ function resolveExpressMapping(expressNameMap, candidateKeys = []) { return {}; } +/** + * 按系统快递编码分组构建快递产品类型映射。 + * + * @param {Array} productTypeList 快递产品类型列表 + * @return {Object} 系统快递编码(大写)到产品类型列表([{ name, code }])的映射 + */ +function buildExpressProductTypeMap(productTypeList) { + return normalizeArray(productTypeList).reduce((result, productTypeItem) => { + const currentItem = productTypeItem && typeof productTypeItem === 'object' ? productTypeItem : {}; + const expressCode = normalizeText(currentItem.express_code).trim().toUpperCase(); + const code = normalizeText(currentItem.code).trim(); + + if (!expressCode || !code) { + return result; + } + + if (!Array.isArray(result[expressCode])) { + result[expressCode] = []; + } + + if (result[expressCode].some(item => item.code === code)) { + return result; + } + + result[expressCode].push({ + name: normalizeText(currentItem.name).trim() || code, + code, + }); + + return result; + }, {}); +} + /** * 解析拼多多电子面单网点查询响应,收敛为前端易消费的结构。 * @@ -2226,6 +2259,9 @@ class indexService extends MainService { /** * 获取指定店铺的快递签约列表。 * + * 每个列表项会按 sys_express_code 匹配 wms_express_product_type, + * 补充 product_types: [{ name, code }],未匹配到时返回空数组;PDD 平台不补充该字段。 + * * @param {Object} data 请求参数 * @param {string|number} [data.shop_id] 店铺 ID * @param {string|number} [data.shopId] 店铺 ID,兼容驼峰字段 @@ -2553,16 +2589,57 @@ class indexService extends MainService { break; } + const listWithProductTypes = await this.attachExpressProductTypes(list, platformCode); + return this.success('ok', { data: { shop_id: shopPlain.id, platform_code: platformCode, - list, + list: listWithProductTypes, }, code: 200, }); } + /** + * 按系统快递编码为店铺快递列表补充快递产品类型。 + * + * 产品类型来源于 wms_express_product_type,读取走系统快递服务缓存。 + * PDD 分支不补充该字段。 + * + * @param {Array} list 店铺快递列表 + * @param {string} [platformCode=''] 店铺所属平台编码 + * @return {Promise} 补充 product_types 后的列表 + */ + async attachExpressProductTypes(list, platformCode = '') { + const { ctx, service } = this; + const normalizedList = normalizeArray(list); + const normalizedPlatformCode = normalizeText(platformCode).trim().toUpperCase(); + + if (normalizedList.length === 0 || normalizedPlatformCode === 'PDD') { + return normalizedList; + } + + let productTypeMap = {}; + + try { + const productTypeList = await service.systems.express.getExpressProductTypeList(); + productTypeMap = buildExpressProductTypeMap(productTypeList); + } catch (error) { + ctx.logger.warn('[SystemMethods.attachExpressProductTypes] 获取快递产品类型失败: %s', error.message); + } + + return normalizedList.map(item => { + const currentItem = item && typeof item === 'object' ? item : {}; + const expressCode = normalizeText(currentItem.sys_express_code).trim().toUpperCase(); + + return { + ...currentItem, + product_types: normalizeArray(productTypeMap[expressCode]), + }; + }); + } + /** * 获取淘宝菜鸟 STF 模板列表。 * diff --git a/test/app/service/systemMethods/index.test.js b/test/app/service/systemMethods/index.test.js index 200b010..75ccbd5 100644 --- a/test/app/service/systemMethods/index.test.js +++ b/test/app/service/systemMethods/index.test.js @@ -338,6 +338,8 @@ describe('test/app/service/systemMethods/index.test.js', () => { assert.equal(result.data.list[1].sys_express_code, ''); assert.equal(calls.syncPddSfTimedDeliveryExpressProductTypes, 0); assert.equal(calls.getExpressProductTypeList, 0); + assert.equal(result.data.list[0].product_types, undefined); + assert.equal(result.data.list[1].product_types, undefined); }); it('getShopExpressList 在 TB 下 SF 且无 TIMED-DELIVERY 时也应补齐时效服务', async () => { @@ -371,7 +373,7 @@ describe('test/app/service/systemMethods/index.test.js', () => { calls.getExpressProductTypeList += 1; calls.getExpressProductTypeListPayload = expressCode; return [ - { code: 'T6', name: '特快' }, + { express_code: 'SF', code: 'T6', name: '特快' }, ]; }; service.requestTbWaybillSearch = async payload => { @@ -446,10 +448,77 @@ describe('test/app/service/systemMethods/index.test.js', () => { ], }, ]); - assert.equal(calls.getExpressProductTypeList, 0); + assert.equal(calls.getExpressProductTypeList, 1); + assert.deepEqual(result.data.list[0].product_types, [ + { name: '特快', code: 'T6' }, + ]); assert.equal(calls.syncPddSfTimedDeliveryExpressProductTypes, 0); }); + it('getShopExpressList 在产品类型读取异常时应降级为空数组', async () => { + const { service, ctx, calls } = createHarness(); + + ctx.model.Shop.findOne = async () => ({ + get() { + return { + id: 26, + warehouse_id: 100000, + platform_code: 'CAINIAO', + access_token: 'TB_ACCESS_TOKEN', + printPlatform: { + platform_code: 'CAINIAO', + platform_name: '菜鸟电子面单', + }, + }; + }, + }); + service.service.systems.express.getExpressList = async () => ([ + { + code: 'YTO_SYS', + name: '圆通速递', + CAINIAO: 'YTO', + }, + ]); + service.service.systems.express.getExpressProductTypeList = async () => { + calls.getExpressProductTypeList += 1; + throw new Error('产品类型缓存不可用'); + }; + service.requestTbWaybillSearch = async () => ({ + cainiao_waybill_ii_search_response: { + request_id: 'TB-REQ-026', + modules: { + waybill_apply_subscription_cols: { + waybill_apply_subscription_info: { + cp_code: 'YTO', + cp_name: '圆通速递', + waybill_type: 'STANDARD', + branch_account_cols: { + waybill_branch_account: { + branch_name: '圆通转运中心', + customer_code_list: { + string: 'TB-CUST-026', + }, + }, + }, + }, + }, + }, + }, + }); + + const result = await service.getShopExpressList({ + shop_id: 26, + cp_code: 'YTO', + }); + + assert.equal(result.flag, 'success'); + assert.equal(result.code, 200); + assert.equal(result.data.platform_code, 'CAINIAO'); + assert.equal(result.data.list[0].sys_express_code, 'YTO_SYS'); + assert.equal(calls.getExpressProductTypeList, 1); + assert.deepEqual(result.data.list[0].product_types, []); + }); + it('getShopExpressList 在 SF 且已有 TIMED-DELIVERY 时不应重复追加', async () => { const { service, ctx, calls } = createHarness(); @@ -512,6 +581,7 @@ describe('test/app/service/systemMethods/index.test.js', () => { assert.equal(result.data.list[0].accounts[0].services.length, 1); assert.equal(result.data.list[0].accounts[0].services[0].service_code, 'TIMED-DELIVERY'); assert.equal(calls.getExpressProductTypeList, 0); + assert.equal(result.data.list[0].product_types, undefined); }); it('getShopExpressList 在 DY 下应编排为与 PDD 一致的统一结构', async () => { @@ -612,6 +682,7 @@ describe('test/app/service/systemMethods/index.test.js', () => { wp_name: '圆通速递', sys_express_code: 'YTO_SYS', wp_type: 'STANDARD', + product_types: [], accounts: [ { branch_code: 'DY-BRANCH-001', @@ -1040,6 +1111,7 @@ describe('test/app/service/systemMethods/index.test.js', () => { assert.equal(result.flag, 'success'); assert.deepEqual(result.data.list[0].accounts[0].services, []); assert.equal(calls.getExpressProductTypeList, 0); + assert.equal(result.data.list[0].product_types, undefined); }); it('getShopExpressList 应仅在拼多多顺丰定时派送场景触发产品类型同步', async () => { @@ -1200,6 +1272,7 @@ describe('test/app/service/systemMethods/index.test.js', () => { wp_name: '圆通速递', sys_express_code: 'YTO_SYS', wp_type: 'STANDARD', + product_types: [], accounts: [ { branch_code: 'CUST-001',