forked from yoone/API
1
0
Fork 0

fix: track查询更换后产品

This commit is contained in:
cll 2025-09-01 11:53:44 +08:00
parent c55eefa3de
commit 96eb7768f3
2 changed files with 28 additions and 68 deletions

View File

@ -247,11 +247,11 @@ export class LogisticsController {
}
@ApiOkResponse()
@Post('/getTrackingNumber')
async getTrackingNumber(@Query('number') number: string) {
@Post('/getOrderList')
async getOrderList(@Query('number') number: string) {
try {
return successResponse(
await this.logisticsService.getTrackingNumber(number)
await this.logisticsService.getOrderList(number)
);
} catch (error) {
return errorResponse(error?.message || '获取失败');
@ -259,11 +259,11 @@ export class LogisticsController {
}
@ApiOkResponse()
@Post('/getListByTrackingId')
async getListByTrackingId(@Query('shipment_id') shipment_id: number) {
@Post('/getListByOrderId')
async getListByOrderId(@Query('id') orderId: number) {
try {
return successResponse(
await this.logisticsService.getListByTrackingId(shipment_id)
await this.logisticsService.getListByOrderId(orderId)
);
} catch (error) {
return errorResponse(error?.message || '获取失败');

View File

@ -142,7 +142,7 @@ export class LogisticsService {
}
async updateShipmentStateById(id: number) {
const shipment:Shipment = await this.shipmentModel.findOneBy({ id : id });
const shipment: Shipment = await this.shipmentModel.findOneBy({ id: id });
return this.updateShipmentState(shipment);
}
@ -219,7 +219,7 @@ export class LogisticsService {
async getShipmentLabel(shipmentId) {
try {
const shipment:Shipment = await this.shipmentModel.findOneBy({id: shipmentId});
const shipment: Shipment = await this.shipmentModel.findOneBy({ id: shipmentId });
if (!shipment) {
throw new Error('运单不存在');
}
@ -231,11 +231,11 @@ export class LogisticsService {
async removeShipment(shipmentId: number) {
try {
const shipment:Shipment = await this.shipmentModel.findOneBy({id: shipmentId});
const shipment: Shipment = await this.shipmentModel.findOneBy({ id: shipmentId });
if (shipment.state !== '190') { // todo写常数
throw new Error('订单当前状态无法删除');
}
const order:Order = await this.orderModel.findOneBy({id: shipment.order_id});
const order: Order = await this.orderModel.findOneBy({ id: shipment.order_id });
const dataSource = this.dataSourceManager.getDataSource('default');
let transactionError = undefined;
await dataSource.transaction(async manager => {
@ -287,7 +287,7 @@ export class LogisticsService {
async getShipmentFee(data: ShipmentFeeBookDTO) {
try {
const stock_point = await this.stockPointModel.findOneBy({ id: data.stockPointId});
const stock_point = await this.stockPointModel.findOneBy({ id: data.stockPointId });
const reqBody = {
...convertKeysFromCamelToSnake(data),
pickup_warehouse: stock_point.upStreamStockPointId,
@ -319,7 +319,7 @@ export class LogisticsService {
let resShipmentOrder;
try {
const stock_point = await this.stockPointModel.findOneBy({ id: data.stockPointId});
const stock_point = await this.stockPointModel.findOneBy({ id: data.stockPointId });
const reqBody = {
sender: data.details.origin.contact_name,
start_phone: data.details.origin.phone_number,
@ -409,10 +409,12 @@ export class LogisticsService {
// 更新产品发货信息
this.orderService.updateOrderSales(order.id, sales);
return { data: {
shipmentId
} };
} catch(error) {
return {
data: {
shipmentId
}
};
} catch (error) {
if (resShipmentOrder.status === 'SUCCESS') {
await this.uniExpressService.deleteShipment(resShipmentOrder.data.tno);
}
@ -469,7 +471,7 @@ export class LogisticsService {
}
async getShipment(id: number) {
const orderShipments:OrderShipment[] = await this.orderShipmentModel.find({
const orderShipments: OrderShipment[] = await this.orderShipmentModel.find({
where: { shipment_id: id },
});
if (!orderShipments || orderShipments.length === 0) return;
@ -553,7 +555,7 @@ export class LogisticsService {
});
}
async getTrackingNumber(number: string) {
async getOrderList(number: string) {
const orders = await this.orderModel.find({
where: {
externalOrderId: Like(`%${number}%`),
@ -568,56 +570,14 @@ export class LogisticsService {
}));
}
async getListByTrackingId(id: number) {
const qb = `
SELECT
oi.name,
oi.quantity,
CASE
WHEN oi.externalVariationId != 0 THEN v.constitution
ELSE p.constitution
END AS constitution
FROM order_item oi
LEFT JOIN wp_product p ON oi.siteId=p.siteId AND oi.externalProductId=p.externalProductId
LEFT JOIN variation v ON oi.siteId=v.siteId AND oi.externalVariationId=v.externalVariationId
WHERE oi.orderId=?
`;
const saleItem = await this.orderSaleModel.query(qb, [id]);
const allSkus = new Set<string>();
for (const item of saleItem) {
if (!item.constitution) continue;
try {
item.constitution.forEach(c => allSkus.add(c.sku));
} catch (e) {
console.warn('Invalid constitution JSON:', item.constitution);
}
}
console.log(allSkus);
const skuList = Array.from(allSkus);
let skuNameMap = new Map<string, string>();
async getListByOrderId(id: number) {
const item = await this.orderItem.find({ where: { orderId: id } });
const saleItem = await this.orderSaleModel.find({ where: { orderId: id } });
if (skuList.length > 0) {
const placeholders = skuList.map(() => '?').join(', ');
const productRows = await this.orderSaleModel.query(
`SELECT sku, name FROM product WHERE sku IN (${placeholders})`,
skuList
);
skuNameMap = new Map(productRows.map(p => [p.sku, p.name]));
}
for (const item of saleItem) {
if (!item.constitution) continue;
try {
item.constitution = item.constitution.map(c => ({
...c,
name: skuNameMap.get(c.sku) || null,
}));
} catch (e) {
item.constitution = [];
}
}
return saleItem;
return {
item,
saleItem
};
}
async getList(param: Record<string, any>) {