- Promise.all 并行调用 index.getShopExpressList 获取店铺快递签约数据 - 三层匹配: sys_express_code → site_code/branch_code → shipperno/customer_code_list - 无店铺或无数据时四字段均为 0 - 补全 response 全部 42 个子字段文档
486 lines
16 KiB
JavaScript
486 lines
16 KiB
JavaScript
'use strict';
|
||
|
||
const MainService = require('../../main');
|
||
|
||
class expressInterfaceService extends MainService {
|
||
/**
|
||
* 初始化快递接口管理模块的权限配置。
|
||
*
|
||
* @param {Egg.Context} ctx 上下文对象
|
||
*/
|
||
constructor(ctx) {
|
||
super(ctx);
|
||
const _noNeedLogin = '';
|
||
const _noNeedRight = 'list,listByWarehousePlatformCode,create,update,delete';
|
||
const _noNeedLog = '*';
|
||
const _noNeedAuth = 'list,listByWarehousePlatformCode,create,update,delete';
|
||
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
|
||
}
|
||
|
||
/**
|
||
* 调用快递接口系统服务。
|
||
*
|
||
* @param {string} methodName 系统服务方法名
|
||
* @param {Object} data 请求参数
|
||
* @param {string} defaultMessage 默认错误信息
|
||
* @return {Promise<Object>} 统一返回结果
|
||
*/
|
||
async callSystemExpressInterface(methodName, data, defaultMessage) {
|
||
const { service } = this;
|
||
|
||
try {
|
||
const result = await service.systems.setting.expressInterface[methodName](data);
|
||
return this.success('ok', result);
|
||
} catch (error) {
|
||
return this.fail(error.message || defaultMessage, {
|
||
code: Number(error.code) || 500,
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取快递接口列表。
|
||
*
|
||
* @param {Object} data 请求参数
|
||
* @return {Promise<Object>} 列表结果
|
||
*/
|
||
async list(data = {}) {
|
||
const {
|
||
where = {},
|
||
pagination: inputPagination,
|
||
page,
|
||
page_size: pageSize,
|
||
pageSize: camelPageSize,
|
||
...rest
|
||
} = data || {};
|
||
|
||
const pagination = {
|
||
...(inputPagination && typeof inputPagination === 'object' ? inputPagination : {}),
|
||
};
|
||
if (page !== undefined) {
|
||
pagination.page = page;
|
||
}
|
||
|
||
const resolvedPageSize = pageSize !== undefined ? pageSize : camelPageSize;
|
||
if (resolvedPageSize !== undefined) {
|
||
pagination.pageSize = resolvedPageSize;
|
||
}
|
||
|
||
const payload = {
|
||
...rest,
|
||
where,
|
||
pagination,
|
||
};
|
||
|
||
this.prepareWhereContext(payload, {
|
||
specialFieldMap: {
|
||
id: {
|
||
field: 'id',
|
||
},
|
||
name: {
|
||
field: 'name',
|
||
},
|
||
express_code: {
|
||
field: 'express_code',
|
||
},
|
||
platform_code: {
|
||
field: 'platform_code',
|
||
},
|
||
sender_name: {
|
||
field: 'sender_name',
|
||
},
|
||
sender_tel: {
|
||
field: 'sender_tel',
|
||
},
|
||
viewer: {
|
||
field: 'viewer',
|
||
},
|
||
client_id: {
|
||
field: 'client_id',
|
||
},
|
||
share_id: {
|
||
field: 'share_id',
|
||
},
|
||
site_code: {
|
||
field: 'site_code',
|
||
},
|
||
product_type: {
|
||
field: 'product_type',
|
||
},
|
||
shop_name: {
|
||
field: 'shop_name',
|
||
},
|
||
is_enabled: {
|
||
field: 'is_enabled',
|
||
},
|
||
quickWhere: {
|
||
bucket: 'quick',
|
||
},
|
||
},
|
||
});
|
||
|
||
const result = await this.callSystemExpressInterface('list', payload, '获取快递接口列表失败');
|
||
if (result.flag !== 'success') {
|
||
return result;
|
||
}
|
||
|
||
const list = result.data?.list || result.list || [];
|
||
if (list.length === 0) {
|
||
return result;
|
||
}
|
||
|
||
// 收集唯一 shop_id(去重)
|
||
const shopIds = [...new Set(
|
||
list
|
||
.map(item => item.shop_id)
|
||
.filter(id => id !== null && id !== undefined && Number(id) !== 0)
|
||
)];
|
||
|
||
if (shopIds.length === 0) {
|
||
const defaultList = list.map(item => ({ ...item, quantity: 0, recycled_quantity: 0, cancel_quantity: 0, allocated_quantity: 0 }));
|
||
if (result.data) {
|
||
result.data.list = defaultList;
|
||
} else {
|
||
result.list = defaultList;
|
||
}
|
||
return result;
|
||
}
|
||
|
||
const { ctx } = this;
|
||
|
||
// 并行调用 getShopExpressList,构建 Map<shopId, expressList>
|
||
const shopExpressMap = new Map();
|
||
const shopResults = await Promise.all(
|
||
shopIds.map(async (shopId) => {
|
||
try {
|
||
const shopExpressResult = await ctx.service.systemMethods.index.getShopExpressList({
|
||
shop_id: shopId,
|
||
});
|
||
return { shopId: Number(shopId), result: shopExpressResult };
|
||
} catch (err) {
|
||
ctx.logger.warn(
|
||
`[expressInterface.list] 获取店铺 ${shopId} 快递签约数据异常: ${err.message}`
|
||
);
|
||
return { shopId: Number(shopId), result: null };
|
||
}
|
||
})
|
||
);
|
||
|
||
for (const { shopId, result: shopResult } of shopResults) {
|
||
ctx.logger.info('[expressInterface.list] shopId=' + shopId + ' flag=' + (shopResult ? shopResult.flag : 'null') + ' msg=' + (shopResult ? shopResult.message : 'null'));
|
||
if (shopResult && shopResult.flag === 'success') {
|
||
const shopExpressList = shopResult.data?.list || shopResult.list || [];
|
||
if (shopExpressList.length > 0) {
|
||
shopExpressMap.set(shopId, shopExpressList);
|
||
}
|
||
}
|
||
}
|
||
|
||
// 为每条记录匹配并汇总 quantity
|
||
const enrichedList = list.map(item => {
|
||
const shopId = item.shop_id;
|
||
if (shopId === null || shopId === undefined) {
|
||
return { ...item, quantity: 0, recycled_quantity: 0, cancel_quantity: 0, allocated_quantity: 0 };
|
||
}
|
||
|
||
const shopExpressList = shopExpressMap.get(Number(shopId));
|
||
if (!shopExpressList || shopExpressList.length === 0) {
|
||
return { ...item, quantity: 0, recycled_quantity: 0, cancel_quantity: 0, allocated_quantity: 0 };
|
||
}
|
||
|
||
const sysExpressCode = (item.sys_express_code || '').trim().toUpperCase();
|
||
const expressCode = (item.express_code || '').trim().toUpperCase();
|
||
|
||
// 按 sys_express_code 或 express_code(wp_code) 匹配
|
||
const matchedExpress = shopExpressList.find(expressItem => {
|
||
const itemSysCode = (expressItem.sys_express_code || '').trim().toUpperCase();
|
||
const itemWpCode = (expressItem.wp_code || '').trim().toUpperCase();
|
||
return itemSysCode === sysExpressCode
|
||
|| itemWpCode === expressCode
|
||
|| itemSysCode === expressCode;
|
||
});
|
||
|
||
if (!matchedExpress) {
|
||
return { ...item, quantity: 0, recycled_quantity: 0, cancel_quantity: 0, allocated_quantity: 0 };
|
||
}
|
||
|
||
let accounts = Array.isArray(matchedExpress.accounts) ? matchedExpress.accounts : [];
|
||
|
||
// 若有 site_code,进一步按 branch_code 筛选匹配的网点账号
|
||
const siteCode = (item.site_code || '').trim().toUpperCase();
|
||
if (siteCode) {
|
||
const branchMatched = accounts.filter(acc =>
|
||
(acc.branch_code || '').trim().toUpperCase() === siteCode
|
||
);
|
||
if (branchMatched.length > 0) {
|
||
accounts = branchMatched;
|
||
}
|
||
}
|
||
|
||
// 若有 shipperno,进一步按 customer_code_list 筛选匹配的账号
|
||
const shipperNo = (item.shipperno || '').trim();
|
||
if (shipperNo && accounts.length > 0) {
|
||
const shipperMatched = accounts.filter(acc =>
|
||
Array.isArray(acc.customer_code_list) && acc.customer_code_list.includes(shipperNo)
|
||
);
|
||
if (shipperMatched.length > 0) {
|
||
accounts = shipperMatched;
|
||
}
|
||
}
|
||
|
||
return { ...item, quantity: accounts.reduce((sum, acc) => sum + (Number(acc.quantity) || 0), 0), recycled_quantity: accounts.reduce((sum, acc) => sum + (Number(acc.recycled_quantity) || 0), 0), cancel_quantity: accounts.reduce((sum, acc) => sum + (Number(acc.cancel_quantity) || 0), 0), allocated_quantity: accounts.reduce((sum, acc) => sum + (Number(acc.allocated_quantity) || 0), 0) };
|
||
});
|
||
|
||
if (result.data) {
|
||
result.data.list = enrichedList;
|
||
} else {
|
||
result.list = enrichedList;
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
/**
|
||
* 按仓库与平台代码查询快递接口全量记录。
|
||
*
|
||
* @param {Object} data 请求参数
|
||
* @return {Promise<Object>} 查询结果
|
||
*/
|
||
async listByWarehousePlatformCode(data = {}) {
|
||
return await this.callSystemExpressInterface('listByWarehousePlatformCode', data, '获取快递接口查询失败');
|
||
}
|
||
|
||
/**
|
||
* 创建快递接口。
|
||
*
|
||
* @param {Object} data 请求参数
|
||
* @return {Promise<Object>} 创建结果
|
||
*/
|
||
async create(data = {}) {
|
||
const {
|
||
name,
|
||
express_code: expressCode,
|
||
expressCode: camelExpressCode,
|
||
sys_express_code: sysExpressCode,
|
||
sysExpressCode: camelSysExpressCode,
|
||
platform_code: platformCode,
|
||
platformCode: camelPlatformCode,
|
||
weight_type: weightType,
|
||
weightType: camelWeightType,
|
||
address,
|
||
town_name: townName,
|
||
townName: camelTownName,
|
||
ispickup: isPickup,
|
||
isPickup: camelIsPickup,
|
||
shipperno: shipperNo,
|
||
shipperNo: camelShipperNo,
|
||
template_url: templateUrl,
|
||
templateUrl: camelTemplateUrl,
|
||
ext_data: extData,
|
||
extData: camelExtData,
|
||
template_id: templateId,
|
||
templateId: camelTemplateId,
|
||
paymode,
|
||
payMode,
|
||
brandno,
|
||
brandNo,
|
||
providertype,
|
||
providerType,
|
||
providerid,
|
||
providerId,
|
||
...rest
|
||
} = data || {};
|
||
|
||
const payload = {
|
||
...rest,
|
||
};
|
||
|
||
if (name !== undefined) {
|
||
payload.name = name;
|
||
}
|
||
if (expressCode !== undefined || camelExpressCode !== undefined) {
|
||
payload.express_code = expressCode !== undefined ? expressCode : camelExpressCode;
|
||
}
|
||
if (platformCode !== undefined || camelPlatformCode !== undefined) {
|
||
payload.platform_code = platformCode !== undefined ? platformCode : camelPlatformCode;
|
||
}
|
||
if (sysExpressCode !== undefined || camelSysExpressCode !== undefined) {
|
||
payload.sys_express_code = sysExpressCode !== undefined ? sysExpressCode : camelSysExpressCode;
|
||
}
|
||
if (weightType !== undefined || camelWeightType !== undefined) {
|
||
payload.weight_type = weightType !== undefined ? weightType : camelWeightType;
|
||
}
|
||
if (address !== undefined) {
|
||
payload.address = address;
|
||
}
|
||
if (townName !== undefined || camelTownName !== undefined) {
|
||
payload.town_name = townName !== undefined ? townName : camelTownName;
|
||
}
|
||
if (isPickup !== undefined || camelIsPickup !== undefined) {
|
||
payload.ispickup = isPickup !== undefined ? isPickup : camelIsPickup;
|
||
}
|
||
if (shipperNo !== undefined || camelShipperNo !== undefined) {
|
||
payload.shipperno = shipperNo !== undefined ? shipperNo : camelShipperNo;
|
||
}
|
||
if (templateUrl !== undefined || camelTemplateUrl !== undefined) {
|
||
payload.template_url = templateUrl !== undefined ? templateUrl : camelTemplateUrl;
|
||
}
|
||
if (extData !== undefined || camelExtData !== undefined) {
|
||
payload.ext_data = extData !== undefined ? extData : camelExtData;
|
||
}
|
||
if (templateId !== undefined || camelTemplateId !== undefined) {
|
||
payload.template_id = templateId !== undefined ? templateId : camelTemplateId;
|
||
}
|
||
if (paymode !== undefined || payMode !== undefined) {
|
||
payload.paymode = paymode !== undefined ? paymode : payMode;
|
||
}
|
||
if (brandno !== undefined || brandNo !== undefined) {
|
||
payload.brandno = brandno !== undefined ? brandno : brandNo;
|
||
}
|
||
if (providertype !== undefined || providerType !== undefined) {
|
||
payload.providertype = providertype !== undefined ? providertype : providerType;
|
||
}
|
||
if (providerid !== undefined || providerId !== undefined) {
|
||
payload.providerid = providerid !== undefined ? providerid : providerId;
|
||
}
|
||
|
||
return await this.callSystemExpressInterface('create', payload, '创建快递接口失败');
|
||
}
|
||
|
||
/**
|
||
* 修改快递接口。
|
||
*
|
||
* @param {Object} data 请求参数
|
||
* @return {Promise<Object>} 修改结果
|
||
*/
|
||
async update(data = {}) {
|
||
const {
|
||
id,
|
||
interface_id: interfaceId,
|
||
interfaceId: camelInterfaceId,
|
||
name,
|
||
express_code: expressCode,
|
||
expressCode: camelExpressCode,
|
||
sys_express_code: sysExpressCode,
|
||
sysExpressCode: camelSysExpressCode,
|
||
platform_code: platformCode,
|
||
platformCode: camelPlatformCode,
|
||
weight_type: weightType,
|
||
weightType: camelWeightType,
|
||
address,
|
||
town_name: townName,
|
||
townName: camelTownName,
|
||
ispickup: isPickup,
|
||
isPickup: camelIsPickup,
|
||
shipperno: shipperNo,
|
||
shipperNo: camelShipperNo,
|
||
template_url: templateUrl,
|
||
templateUrl: camelTemplateUrl,
|
||
ext_data: extData,
|
||
extData: camelExtData,
|
||
platform_shop_id: platformShopId,
|
||
platformShopId: camelPlatformShopId,
|
||
template_id: templateId,
|
||
templateId: camelTemplateId,
|
||
paymode,
|
||
payMode,
|
||
brandno,
|
||
brandNo,
|
||
providertype,
|
||
providerType,
|
||
providerid,
|
||
providerId,
|
||
is_enabled: isEnabledSnake,
|
||
isEnabled: isEnabledCamel,
|
||
...rest
|
||
} = data || {};
|
||
|
||
const payload = {
|
||
...rest,
|
||
};
|
||
|
||
payload.id = id !== undefined ? id : (interfaceId !== undefined ? interfaceId : camelInterfaceId);
|
||
if (name !== undefined) {
|
||
payload.name = name;
|
||
}
|
||
if (expressCode !== undefined || camelExpressCode !== undefined) {
|
||
payload.express_code = expressCode !== undefined ? expressCode : camelExpressCode;
|
||
}
|
||
if (platformCode !== undefined || camelPlatformCode !== undefined) {
|
||
payload.platform_code = platformCode !== undefined ? platformCode : camelPlatformCode;
|
||
}
|
||
if (sysExpressCode !== undefined || camelSysExpressCode !== undefined) {
|
||
payload.sys_express_code = sysExpressCode !== undefined ? sysExpressCode : camelSysExpressCode;
|
||
}
|
||
if (weightType !== undefined || camelWeightType !== undefined) {
|
||
payload.weight_type = weightType !== undefined ? weightType : camelWeightType;
|
||
}
|
||
if (address !== undefined) {
|
||
payload.address = address;
|
||
}
|
||
if (townName !== undefined || camelTownName !== undefined) {
|
||
payload.town_name = townName !== undefined ? townName : camelTownName;
|
||
}
|
||
if (isPickup !== undefined || camelIsPickup !== undefined) {
|
||
payload.ispickup = isPickup !== undefined ? isPickup : camelIsPickup;
|
||
}
|
||
if (shipperNo !== undefined || camelShipperNo !== undefined) {
|
||
payload.shipperno = shipperNo !== undefined ? shipperNo : camelShipperNo;
|
||
}
|
||
if (templateUrl !== undefined || camelTemplateUrl !== undefined) {
|
||
payload.template_url = templateUrl !== undefined ? templateUrl : camelTemplateUrl;
|
||
}
|
||
if (extData !== undefined || camelExtData !== undefined) {
|
||
payload.ext_data = extData !== undefined ? extData : camelExtData;
|
||
}
|
||
if (platformShopId !== undefined || camelPlatformShopId !== undefined) {
|
||
payload.platform_shop_id = platformShopId !== undefined ? platformShopId : camelPlatformShopId;
|
||
}
|
||
if (templateId !== undefined || camelTemplateId !== undefined) {
|
||
payload.template_id = templateId !== undefined ? templateId : camelTemplateId;
|
||
}
|
||
if (paymode !== undefined || payMode !== undefined) {
|
||
payload.paymode = paymode !== undefined ? paymode : payMode;
|
||
}
|
||
if (brandno !== undefined || brandNo !== undefined) {
|
||
payload.brandno = brandno !== undefined ? brandno : brandNo;
|
||
}
|
||
if (providertype !== undefined || providerType !== undefined) {
|
||
payload.providertype = providertype !== undefined ? providertype : providerType;
|
||
}
|
||
if (providerid !== undefined || providerId !== undefined) {
|
||
payload.providerid = providerid !== undefined ? providerid : providerId;
|
||
}
|
||
if (isEnabledSnake !== undefined || isEnabledCamel !== undefined) {
|
||
payload.is_enabled = isEnabledSnake !== undefined ? isEnabledSnake : isEnabledCamel;
|
||
}
|
||
|
||
return await this.callSystemExpressInterface('update', payload, '更新快递接口失败');
|
||
}
|
||
|
||
/**
|
||
* 删除快递接口。
|
||
*
|
||
* @param {Object} data 请求参数
|
||
* @return {Promise<Object>} 删除结果
|
||
*/
|
||
async delete(data = {}) {
|
||
const {
|
||
id,
|
||
interface_id: interfaceId,
|
||
interfaceId: camelInterfaceId,
|
||
...rest
|
||
} = data || {};
|
||
|
||
const payload = {
|
||
...rest,
|
||
};
|
||
|
||
payload.id = id !== undefined ? id : (interfaceId !== undefined ? interfaceId : camelInterfaceId);
|
||
|
||
return await this.callSystemExpressInterface('delete', payload, '删除快递接口失败');
|
||
}
|
||
}
|
||
|
||
module.exports = expressInterfaceService;
|