zksu
/
API
forked from yoone/API
1
0
Fork 0

Compare commits

...

7 Commits

Author SHA1 Message Date
tikkhun f4a46bcf72 fix(订单服务): 修复产品分类可能为空的错误并优化产品查询
修复订单服务中产品分类可能为空导致的错误,改为可选链操作
优化产品服务中组件查询逻辑,使用sku查询并添加关联关系
移除产品服务中不必要的组件查询逻辑
2026-01-29 09:20:57 +08:00
tikkhun 534cc3f2c7 refactor(订单服务): 移除getComponentDetailFromSiteSku中不必要的quantity参数
简化组件详情查询逻辑,默认数量设为1,不再需要外部传入quantity参数
2026-01-29 09:20:57 +08:00
tikkhun d8a3422015 fix(product): 修复产品属性重复检查逻辑并添加日志记录
修复产品属性重复检查时未考虑属性数量的问题,确保只有当属性数量完全匹配时才认为重复
添加导入结果的调试日志记录
2026-01-29 09:20:57 +08:00
tikkhun 9d69af03b2 fix: 修复父产品ID可能为undefined时的处理 2026-01-29 09:20:57 +08:00
zhuotianyuan 1657b96694 fix(logistics): 修复物流订单状态检查和结果合并问题
修复 resShipmentOrder 状态检查时的可选链操作问题
修正 partnerOrderNumber 的拼接格式
调整查询结果合并方式,避免直接 push 操作
优化错误处理,使用 Error 对象抛出异常
2026-01-27 19:27:18 +08:00
zhuotianyuan 0a14a7c1ae fix(logistics): 修复物流服务中的错误处理和快递公司字段
修复freightwaves服务中的错误响应处理,增加错误码检查
添加courierCompany字段到物流DTO以支持不同快递公司
移除订单服务中注释掉的saveOrderSale调用
更新物流服务中使用courierCompany代替硬编码的shipCompany
2026-01-27 19:27:18 +08:00
zhuotianyuan 51b59ca176 Merge pull request 'feat(产品服务): 重构产品查询逻辑并添加价格字段' (#66) from zksu/API:main into main
Reviewed-on: yoone/API#66
2026-01-27 11:19:15 +00:00
5 changed files with 55 additions and 32 deletions

View File

@ -23,6 +23,10 @@ export class ShipmentBookDTO {
@ApiProperty() @ApiProperty()
@Rule(RuleType.string()) @Rule(RuleType.string())
shipmentPlatform: string; shipmentPlatform: string;
@ApiProperty()
@Rule(RuleType.string())
courierCompany: string;
} }
export class ShipmentFeeBookDTO { export class ShipmentFeeBookDTO {
@ -30,6 +34,8 @@ export class ShipmentFeeBookDTO {
@ApiProperty() @ApiProperty()
shipmentPlatform: string; shipmentPlatform: string;
@ApiProperty() @ApiProperty()
courierCompany: string;
@ApiProperty()
stockPointId: number; stockPointId: number;
@ApiProperty() @ApiProperty()
sender: string; sender: string;

View File

@ -247,6 +247,9 @@ export class FreightwavesService {
}; };
const response = await this.sendRequest<RateTryResponseData>('/shipService/order/rateTry', requestData); const response = await this.sendRequest<RateTryResponseData>('/shipService/order/rateTry', requestData);
if (response.code !== '00000200') {
throw new Error(response.msg);
}
return response.data; return response.data;
} }
@ -262,7 +265,10 @@ export class FreightwavesService {
}; };
const response = await this.sendRequest<CreateOrderResponseData>('/shipService/order/createOrder', requestData); const response = await this.sendRequest<CreateOrderResponseData>('/shipService/order/createOrder', requestData);
return response; if (response.code !== '00000200') {
throw new Error(response.msg);
}
return response.data;
} }
/** /**
@ -295,6 +301,9 @@ export class FreightwavesService {
}; };
const response = await this.sendRequest<ModifyOrderResponseData>('/shipService/order/modifyOrder', requestData); const response = await this.sendRequest<ModifyOrderResponseData>('/shipService/order/modifyOrder', requestData);
if (response.code !== '00000200') {
throw new Error(response.msg);
}
return response.data; return response.data;
} }
@ -309,6 +318,9 @@ export class FreightwavesService {
partner: this.config.partner, partner: this.config.partner,
}; };
const response = await this.sendRequest<RefundOrderResponseData>('/shipService/order/refundOrder', requestData); const response = await this.sendRequest<RefundOrderResponseData>('/shipService/order/refundOrder', requestData);
if (response.code !== '00000200') {
throw new Error(response.msg);
}
return response.data; return response.data;
} }

View File

@ -464,7 +464,7 @@ export class LogisticsService {
} }
}; };
} catch (error) { } catch (error) {
if (resShipmentOrder.status === 'SUCCESS') { if (resShipmentOrder?.status === 'SUCCESS') {
await this.uniExpressService.deleteShipment(resShipmentOrder.data.tno); await this.uniExpressService.deleteShipment(resShipmentOrder.data.tno);
} }
throw new Error(`上游请求错误:${error}`); throw new Error(`上游请求错误:${error}`);
@ -733,8 +733,9 @@ export class LogisticsService {
if (data.shipmentPlatform === 'freightwaves') { if (data.shipmentPlatform === 'freightwaves') {
// 根据TMS系统对接说明文档格式化参数 // 根据TMS系统对接说明文档格式化参数
const reqBody: any = { const reqBody: any = {
shipCompany: 'UPSYYZ7000NEW', // shipCompany: 'UPSYYZ7000NEW',
partnerOrderNumber: order.siteId + '-' + order.externalOrderId, shipCompany: data.courierCompany || "",
partnerOrderNumber: order.siteId + '-1-' + order.externalOrderId,
warehouseId: '25072621030107400060', warehouseId: '25072621030107400060',
shipper: { shipper: {
name: data.details.origin.contact_name, // 姓名 name: data.details.origin.contact_name, // 姓名
@ -798,15 +799,20 @@ export class LogisticsService {
}; };
resShipmentOrder = await this.freightwavesService.createOrder(reqBody); // 创建订单 resShipmentOrder = await this.freightwavesService.createOrder(reqBody); // 创建订单
//tms只返回了物流订单号需要查询一次来获取完整的物流信息 //tms只返回了物流订单号需要查询一次来获取完整的物流信息
const queryRes = await this.freightwavesService.queryOrder({ shipOrderId: resShipmentOrder.shipOrderId }); // 查询订单 const queryRes = await this.freightwavesService.queryOrder({ shipOrderId: resShipmentOrder.shipOrderId }); // 查询订单
resShipmentOrder.push(queryRes); return {
...resShipmentOrder,
...queryRes
}
} }
return resShipmentOrder; return resShipmentOrder;
} catch (error) { } catch (error) {
console.log('物流订单处理失败:', error); // 使用console.log代替this.log // 处理错误,例如记录日志或抛出异常
throw error; throw new Error(`物流订单处理失败: ${error}`);
} }
} }
@ -826,7 +832,8 @@ export class LogisticsService {
const address = shipments?.address; const address = shipments?.address;
// 转换为RateTryRequest格式 // 转换为RateTryRequest格式
const r = { const r = {
shipCompany: 'UPSYYZ7000NEW', // 必填但ShipmentFeeBookDTO中缺少 //shipCompany: 'UPSYYZ7000NEW', // 必填但ShipmentFeeBookDTO中缺少
shipCompany: data.courierCompany || "",
partnerOrderNumber: `order-${Date.now()}`, // 必填,使用时间戳生成 partnerOrderNumber: `order-${Date.now()}`, // 必填,使用时间戳生成
warehouseId: '25072621030107400060', // 可选使用stockPointId转换 warehouseId: '25072621030107400060', // 可选使用stockPointId转换
shipper: { shipper: {

View File

@ -733,19 +733,20 @@ export class OrderService {
if (!orderItem.sku) return; if (!orderItem.sku) return;
// 从数据库查询产品,关联查询组件 // 从数据库查询产品,关联查询组件
const componentDetails = await this.productService.getComponentDetailFromSiteSku({ sku: orderItem.sku, name: orderItem.name },orderItem.quantity,site); const componentDetails = await this.productService.getComponentDetailFromSiteSku({ sku: orderItem.sku, name: orderItem.name }, site);
if(!componentDetails?.length){ if(!componentDetails?.length){
return return
} }
const orderSales: OrderSale[] = componentDetails.map(({product, parentProduct, quantity}) => { const orderSales: OrderSale[] = componentDetails.map(({product, parentProduct, quantity}) => {
if (!product) return null if (!product) return null
console.log('product',product)
const attrsObj = this.productService.getAttributesObject(product.attributes) const attrsObj = this.productService.getAttributesObject(product.attributes)
const orderSale = plainToClass(OrderSale, { const orderSale = plainToClass(OrderSale, {
orderId: orderItem.orderId, orderId: orderItem.orderId,
siteId: orderItem.siteId, siteId: orderItem.siteId,
externalOrderItemId: orderItem.externalOrderItemId,// 原始 itemId externalOrderItemId: orderItem.externalOrderItemId,// 原始 itemId
parentProductId: parentProduct.id, // 父产品 ID 用于统计套餐 如果是单品则不记录 parentProductId: parentProduct?.id, // 父产品 ID 用于统计套餐 如果是单品则不记录
productId: product.id, productId: product.id,
isPackage: product.type === 'bundle',// 这里是否是套餐取决于父产品 isPackage: product.type === 'bundle',// 这里是否是套餐取决于父产品
name: product.name, name: product.name,
@ -758,7 +759,7 @@ export class OrderService {
flavor: attrsObj?.['flavor']?.name, flavor: attrsObj?.['flavor']?.name,
humidity: attrsObj?.['humidity']?.name, humidity: attrsObj?.['humidity']?.name,
size: attrsObj?.['size']?.name, size: attrsObj?.['size']?.name,
category: product.category.name, category: product.category?.name,
}); });
return orderSale return orderSale
}).filter(v => v !== null) }).filter(v => v !== null)

View File

@ -1,4 +1,4 @@
import { Inject, Provide } from '@midwayjs/core'; import { ILogger, Inject, Logger, Provide } from '@midwayjs/core';
import { Context } from '@midwayjs/koa'; import { Context } from '@midwayjs/koa';
import { InjectEntityModel } from '@midwayjs/typeorm'; import { InjectEntityModel } from '@midwayjs/typeorm';
import * as fs from 'fs'; import * as fs from 'fs';
@ -38,6 +38,9 @@ import { Site } from '../entity/site.entity';
@Provide() @Provide()
export class ProductService { export class ProductService {
@Logger()
logger: ILogger; // 注入 Logger 实例
@Inject() @Inject()
ctx: Context; ctx: Context;
@ -856,7 +859,7 @@ export class ProductService {
// 检查完全相同属性组合是否已存在(避免重复) // 检查完全相同属性组合是否已存在(避免重复)
// 仅当产品类型为 'single' 且有属性时才检查重复 // 仅当产品类型为 'single' 且有属性时才检查重复
if (type === 'single' && resolvedAttributes.length > 0) { if (type === 'single' && resolvedAttributes.length > 0) {
const qb = this.productModel.createQueryBuilder('product'); const qb = this.productModel.createQueryBuilder('product')
resolvedAttributes.forEach((attr, index) => { resolvedAttributes.forEach((attr, index) => {
qb.innerJoin( qb.innerJoin(
'product.attributes', 'product.attributes',
@ -865,8 +868,12 @@ export class ProductService {
{ [`attrId${index}`]: attr.id } { [`attrId${index}`]: attr.id }
); );
}); });
// 添加属性数量的判断,确保产品的属性数量与指定的属性数量相同
qb.andWhere('(SELECT COUNT(*) FROM product_attributes_dict_item pad WHERE pad.productId = product.id) = :attrCount', {
attrCount: resolvedAttributes.length
});
const isExist = await qb.getOne(); const isExist = await qb.getOne();
if (isExist) throw new Error('相同产品属性的产品已存在'); if (isExist) throw new Error(`相同产品属性的产品已存在,sku 为${isExist?.sku}`);
} }
// 创建新产品实例(绑定属性与基础字段) // 创建新产品实例(绑定属性与基础字段)
@ -1778,14 +1785,8 @@ export class ProductService {
attributes: attributes.length > 0 ? attributes : undefined, attributes: attributes.length > 0 ? attributes : undefined,
} }
} }
isMixedSku(sku: string) { // 获取库存单品列表
const splitSKu = sku.split('-') async getComponentDetailFromSiteSku(siteProduct: { sku: string, name?: string }, site: Site): Promise<{ product: Product,parentProduct?: Product, quantity: number }[]> {
const last = splitSKu[splitSKu.length - 1]
const second = splitSKu[splitSKu.length - 2]
// 这里判断 second 是否是数字
return sku.includes('-MX-') || sku.includes('-Mixed-') || /^\d+$/.test(second) && /^\d+$/.test(last)
}
async getComponentDetailFromSiteSku(siteProduct: { sku: string, name: string }, quantity: number = 1, site: Site): Promise<{ product: Product,parentProduct?: Product, quantity: number }[]> {
if (!siteProduct.sku) { if (!siteProduct.sku) {
throw new Error('siteSku 不能为空') throw new Error('siteSku 不能为空')
} }
@ -1797,17 +1798,18 @@ export class ProductService {
if(!product?.components?.length){ if(!product?.components?.length){
return [{ return [{
product, product,
quantity quantity:1
}] }]
} }
return await Promise.all(product.components.map(async comp => { return await Promise.all(product.components.map(async comp => {
return { return {
product: await this.productModel.findOne({ product: await this.productModel.findOne({
where: { id: comp.productId }, where: { sku: comp.sku },
relations: ['category', 'attributes', 'attributes.dict', 'components']
}), }),
parentProduct: product, // 这里得记录一下他的爸爸用来记录 parentProduct: product, // 这里得记录一下他的爸爸用来记录
quantity: comp.quantity * quantity, quantity: comp.quantity,
} }
})) }))
} }
@ -2074,7 +2076,7 @@ export class ProductService {
errors.push({ identifier: '' + rec.sku, error: `产品${rec?.sku}导入失败:${e?.message || String(e)}` }); errors.push({ identifier: '' + rec.sku, error: `产品${rec?.sku}导入失败:${e?.message || String(e)}` });
} }
} }
this.logger.debug(`导入 ${records.length} 条记录,成功创建 ${created} 条,更新 ${updated} 条,失败 ${errors.length} 条,错误详情:${JSON.stringify(errors)}`);
return { total: records.length, processed: records.length - errors.length, created, updated, errors }; return { total: records.length, processed: records.length - errors.length, created, updated, errors };
} }
@ -2132,11 +2134,6 @@ export class ProductService {
component.sku = product.sku; component.sku = product.sku;
component.quantity = 1; component.quantity = 1;
product.components = [component]; product.components = [component];
} else {
// 混装商品返回持久化的 SKU 组成
product.components = await this.productStockComponentModel.find({
where: { productId: product.id },
});
} }
return product; return product;