Improvement: 销售统计的筛选中增加了排除套装购买,并修复了数据不准确的bug #14

Merged
longbot merged 1 commits from longbot/API:Improvement-add-except-package into main 2025-09-12 12:28:44 +00:00
5 changed files with 39 additions and 9 deletions
Showing only changes of commit ca1a15c75d - Show all commits

View File

@ -90,6 +90,10 @@ export class QueryOrderSalesDTO {
@Rule(RuleType.bool().default(false)) @Rule(RuleType.bool().default(false))
isSource: boolean; isSource: boolean;
@ApiProperty()
@Rule(RuleType.bool().default(false))
exceptPackage: boolean;
@ApiProperty({ example: '1', description: '页码' }) @ApiProperty({ example: '1', description: '页码' })
@Rule(RuleType.number()) @Rule(RuleType.number())
current: number; current: number;

View File

@ -37,7 +37,7 @@ export class OrderItem {
externalOrderId: string; // WooCommerce 订单 ID externalOrderId: string; // WooCommerce 订单 ID
@ApiProperty() @ApiProperty()
@Column() @Column({ nullable: true })
@Expose() @Expose()
externalOrderItemId: string; // WooCommerce 订单item ID externalOrderItemId: string; // WooCommerce 订单item ID

View File

@ -22,6 +22,7 @@ export class OrderSaleOriginal {
@ApiProperty() @ApiProperty()
@ManyToOne(() => Order) @ManyToOne(() => Order)
@JoinColumn({ name: 'order_id' }) @JoinColumn({ name: 'order_id' })
@Column({ name: 'order_id' })
@Expose() @Expose()
orderId: number; // 订单 ID orderId: number; // 订单 ID
@ -31,7 +32,7 @@ export class OrderSaleOriginal {
siteId: string; // 来源站点唯一标识 siteId: string; // 来源站点唯一标识
@ApiProperty() @ApiProperty()
@Column() @Column({ nullable: true })
@Expose() @Expose()
externalOrderItemId: string; // WooCommerce 订单item ID externalOrderItemId: string; // WooCommerce 订单item ID

View File

@ -129,6 +129,7 @@ export class LogisticsService {
async updateShipmentState(shipment: Shipment) { async updateShipmentState(shipment: Shipment) {
try { try {
const data = await this.uniExpressService.getOrderStatus(shipment.return_tracking_number); const data = await this.uniExpressService.getOrderStatus(shipment.return_tracking_number);
console.log('updateShipmentState data:', data);
shipment.state = data.data[0].state; shipment.state = data.data[0].state;
if (shipment.state in [203, 215, 216, 230]) { // todo,写常数 if (shipment.state in [203, 215, 216, 230]) { // todo,写常数
shipment.finished = true; shipment.finished = true;

View File

@ -749,7 +749,7 @@ export class OrderService {
return await query.getRawMany(); 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 nameKeywords = name ? name.split(' ').filter(Boolean) : [];
const offset = (current - 1) * pageSize; const offset = (current - 1) * pageSize;
@ -796,6 +796,16 @@ export class OrderService {
itemSql += ' AND os.siteId = ?'; itemSql += ' AND os.siteId = ?';
itemParams.push(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 += nameCondition;
itemSql += ` itemSql += `
GROUP BY os.productId, os.name GROUP BY os.productId, os.name
@ -813,17 +823,17 @@ export class OrderService {
const pcParams: any[] = [...productIds, startDate, endDate]; const pcParams: any[] = [...productIds, startDate, endDate];
if (siteId) pcParams.push(siteId); if (siteId) pcParams.push(siteId);
const pcSql = ` let pcSql = `
SELECT SELECT
os.productId, os.productId,
SUM(CASE WHEN t.purchaseIndex = 1 THEN os.quantity ELSE 0 END) AS firstOrderYOONEBoxCount, 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, 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, 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, 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 FROM order_sale os
INNER JOIN ( INNER JOIN (
SELECT o2.id AS orderId, SELECT o2.id AS orderId,
@ -840,9 +850,22 @@ export class OrderService {
WHERE date_paid BETWEEN ? AND ? WHERE date_paid BETWEEN ? AND ?
${siteId ? 'AND siteId = ?' : ''} ${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 GROUP BY os.productId
`; `;
console.log('------3.5-----', pcSql, pcParams, exceptPackage);
const pcResults = await this.orderSaleModel.query(pcSql, pcParams); const pcResults = await this.orderSaleModel.query(pcSql, pcParams);
const pcMap = new Map<number, any>(); const pcMap = new Map<number, any>();
@ -1297,7 +1320,8 @@ export class OrderService {
productId: product.id, productId: product.id,
name: product.name, name: product.name,
sku: sale.sku, sku: sale.sku,
quantity: sale.quantity quantity: sale.quantity,
// externalOrderItemId:
}); });
}; };
}).catch(error => { }).catch(error => {