From e83745db4fcdf94ea661143641873a2dd0cb6294 Mon Sep 17 00:00:00 2001 From: cll <931958862@qq.com> Date: Fri, 23 May 2025 14:45:32 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A2=E5=8D=95=E5=8F=B7=E6=9F=A5=E8=AE=A2?= =?UTF-8?q?=E5=8D=95=E8=AF=A6=E6=83=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- src/service/logistics.service.ts | 85 +++++++++++++++++++++++--------- 2 files changed, 65 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index c492891..ff2ba74 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,5 @@ run/ .tsbuildinfo .tsbuildinfo.* yarn.lock -**/config.prod.ts \ No newline at end of file +**/config.prod.ts +**/config.local.ts \ No newline at end of file diff --git a/src/service/logistics.service.ts b/src/service/logistics.service.ts index 364a7f4..4da40b0 100644 --- a/src/service/logistics.service.ts +++ b/src/service/logistics.service.ts @@ -26,6 +26,7 @@ import { Product } from '../entity/product.entty'; import { ShippingDetailsDTO } from '../dto/freightcom.dto'; import { CanadaPostService } from './canadaPost.service'; import { OrderItem } from '../entity/order_item.entity'; +import { OrderSale } from '../entity/order_sale.entity'; @Provide() export class LogisticsService { @@ -41,6 +42,9 @@ export class LogisticsService { @InjectEntityModel(Order) orderModel: Repository; + @InjectEntityModel(OrderSale) + orderSaleModel: Repository; + @InjectEntityModel(Shipment) shipmentModel: Repository; @@ -467,33 +471,70 @@ export class LogisticsService { } async getTrackingNumber(number: string) { - return await this.shipmentModel.find({ + const orders = await this.orderModel.find({ where: { - primary_tracking_number: Like(`%${number}%`), + externalOrderId: Like(`%${number}%`), }, }); + + const siteMap = new Map(this.sites.map(site => [site.id, site.siteName])); + + return orders.map(order => ({ + ...order, + siteName: siteMap.get(order.siteId) || '', + })); } - async getListByTrackingId(shipment_id: string) { - const shipmentItem = await this.shipmentItemModel.find({ - where: { - shipment_id, - }, - }); - const orderShipment = await this.orderShipmentModel.find({ - where: { - shipment_id, - }, - }); - const orderItem = await this.orderItem.find({ - where: { - id: In(orderShipment.map(v => v.order_id)), - }, - }); - return { - shipmentItem, - orderItem, - }; + async getListByTrackingId(id: string) { + 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(); + 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(); + + 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; } async getList(param: Record) {