diff --git a/src/dto/order.dto.ts b/src/dto/order.dto.ts index 0ae7cdf..428c58d 100644 --- a/src/dto/order.dto.ts +++ b/src/dto/order.dto.ts @@ -90,6 +90,10 @@ export class QueryOrderSalesDTO { @Rule(RuleType.bool().default(false)) isSource: boolean; + @ApiProperty() + @Rule(RuleType.bool().default(false)) + exceptPackage: boolean; + @ApiProperty({ example: '1', description: '页码' }) @Rule(RuleType.number()) current: number; diff --git a/src/entity/order_item.entity.ts b/src/entity/order_item.entity.ts index 9ebf04e..d41eef2 100644 --- a/src/entity/order_item.entity.ts +++ b/src/entity/order_item.entity.ts @@ -37,7 +37,7 @@ export class OrderItem { externalOrderId: string; // WooCommerce 订单 ID @ApiProperty() - @Column() + @Column({ nullable: true }) @Expose() externalOrderItemId: string; // WooCommerce 订单item ID diff --git a/src/entity/order_item_original.entity.ts b/src/entity/order_item_original.entity.ts index 195e866..c457c6e 100644 --- a/src/entity/order_item_original.entity.ts +++ b/src/entity/order_item_original.entity.ts @@ -22,6 +22,7 @@ export class OrderSaleOriginal { @ApiProperty() @ManyToOne(() => Order) @JoinColumn({ name: 'order_id' }) + @Column({ name: 'order_id' }) @Expose() orderId: number; // 订单 ID @@ -31,7 +32,7 @@ export class OrderSaleOriginal { siteId: string; // 来源站点唯一标识 @ApiProperty() - @Column() + @Column({ nullable: true }) @Expose() externalOrderItemId: string; // WooCommerce 订单item ID diff --git a/src/service/logistics.service.ts b/src/service/logistics.service.ts index f2ef29d..721ce90 100644 --- a/src/service/logistics.service.ts +++ b/src/service/logistics.service.ts @@ -129,6 +129,7 @@ export class LogisticsService { async updateShipmentState(shipment: Shipment) { try { const data = await this.uniExpressService.getOrderStatus(shipment.return_tracking_number); + console.log('updateShipmentState data:', data); shipment.state = data.data[0].state; if (shipment.state in [203, 215, 216, 230]) { // todo,写常数 shipment.finished = true; diff --git a/src/service/order.service.ts b/src/service/order.service.ts index a09ef32..162a03a 100644 --- a/src/service/order.service.ts +++ b/src/service/order.service.ts @@ -749,7 +749,7 @@ export class OrderService { return await query.getRawMany(); } - async getOrderSales({ siteId, startDate, endDate, current, pageSize, name }: QueryOrderSalesDTO) { + async getOrderSales({ siteId, startDate, endDate, current, pageSize, name, exceptPackage }: QueryOrderSalesDTO) { const nameKeywords = name ? name.split(' ').filter(Boolean) : []; const offset = (current - 1) * pageSize; @@ -796,6 +796,16 @@ export class OrderService { itemSql += ' AND os.siteId = ?'; itemParams.push(siteId); } + if (exceptPackage) { + itemSql += ` + AND os.orderId IN ( + SELECT orderId + FROM order_sale + GROUP BY orderId + HAVING COUNT(*) = 1 + ) + `; + } itemSql += nameCondition; itemSql += ` GROUP BY os.productId, os.name @@ -813,17 +823,17 @@ export class OrderService { const pcParams: any[] = [...productIds, startDate, endDate]; if (siteId) pcParams.push(siteId); - const pcSql = ` + let pcSql = ` SELECT os.productId, SUM(CASE WHEN t.purchaseIndex = 1 THEN os.quantity ELSE 0 END) AS firstOrderYOONEBoxCount, - COUNT(CASE WHEN t.purchaseIndex = 1 THEN 1 END) AS firstOrderCount, + COUNT(DISTINCT CASE WHEN t.purchaseIndex = 1 THEN os.orderId END) AS firstOrderCount, SUM(CASE WHEN t.purchaseIndex = 2 THEN os.quantity ELSE 0 END) AS secondOrderYOONEBoxCount, - COUNT(CASE WHEN t.purchaseIndex = 2 THEN 1 END) AS secondOrderCount, + COUNT(DISTINCT CASE WHEN t.purchaseIndex = 2 THEN os.orderId END) AS secondOrderCount, SUM(CASE WHEN t.purchaseIndex = 3 THEN os.quantity ELSE 0 END) AS thirdOrderYOONEBoxCount, - COUNT(CASE WHEN t.purchaseIndex = 3 THEN 1 END) AS thirdOrderCount, + COUNT(DISTINCT CASE WHEN t.purchaseIndex = 3 THEN os.orderId END) AS thirdOrderCount, SUM(CASE WHEN t.purchaseIndex > 3 THEN os.quantity ELSE 0 END) AS moreThirdOrderYOONEBoxCount, - COUNT(CASE WHEN t.purchaseIndex > 3 THEN 1 END) AS moreThirdOrderCount + COUNT(DISTINCT CASE WHEN t.purchaseIndex > 3 THEN os.orderId END) AS moreThirdOrderCount FROM order_sale os INNER JOIN ( SELECT o2.id AS orderId, @@ -840,9 +850,22 @@ export class OrderService { WHERE date_paid BETWEEN ? AND ? ${siteId ? 'AND siteId = ?' : ''} ) + `; + if (exceptPackage) { + pcSql += ` + AND os.orderId IN ( + SELECT orderId + FROM order_sale + GROUP BY orderId + HAVING COUNT(*) = 1 + ) + `; + } + pcSql += ` GROUP BY os.productId `; + console.log('------3.5-----', pcSql, pcParams, exceptPackage); const pcResults = await this.orderSaleModel.query(pcSql, pcParams); const pcMap = new Map(); @@ -1297,7 +1320,8 @@ export class OrderService { productId: product.id, name: product.name, sku: sale.sku, - quantity: sale.quantity + quantity: sale.quantity, + // externalOrderItemId: }); }; }).catch(error => {