forked from yoone/API
1
0
Fork 0

2024-0928-zty-wp产品列表逻辑删除实现

This commit is contained in:
zhuotianyuan 2025-09-28 17:19:17 +08:00
parent 20b84637d3
commit 33e5baa083
4 changed files with 62 additions and 6 deletions

View File

@ -75,7 +75,6 @@ export class WebhookController {
switch (topic) {
case 'product.created':
case 'product.updated':
const site = await this.wpProductService.getSite(siteId);
// 变体更新
if (body.type === 'variation') {
const variation = await this.wpApiService.getVariation(

View File

@ -80,6 +80,10 @@ export class Variation {
@Column({ nullable: true, type: Boolean })
on_sale: boolean; // 是否促销中
@ApiProperty({ description: '是否删除', type: Boolean })
@Column({ nullable: true, type: Boolean , default: false })
on_delete: boolean; // 是否删除
@Column({ type: 'json', nullable: true })
attributes: Record<string, any>; // 变体的属性

View File

@ -77,6 +77,11 @@ export class WpProduct {
@Column({ nullable: true, type: Boolean })
on_sale: boolean; // 是否促销中
@ApiProperty({ description: '是否删除', type: Boolean })
@Column({ nullable: true, type: Boolean , default: false })
on_delete: boolean; // 是否删除
@ApiProperty({
description: '产品类型',
enum: ProductType,
@ -84,6 +89,7 @@ export class WpProduct {
@Column({ type: 'enum', enum: ProductType })
type: ProductType;
@Column({ type: 'json', nullable: true })
metadata: Record<string, any>; // 产品的其他扩展字段

View File

@ -1,3 +1,4 @@
import { Product } from './../entity/product.entty';
import { Config, Inject, Provide } from '@midwayjs/core';
import { WPService } from './wp.service';
import { WpSite } from '../interface';
@ -10,7 +11,6 @@ import {
UpdateVariationDTO,
UpdateWpProductDTO,
} from '../dto/wp_product.dto';
import { Product } from '../entity/product.entty';
import { ProductStatus, ProductStockStatus } from '../enums/base.enum';
@Provide()
@ -47,14 +47,45 @@ export class WpProductService {
async syncSite(siteId: string) {
const site = this.getSite(siteId);
const externalProductIds = this.wpProductModel.createQueryBuilder('wp_product')
.select([
'wp_product.id ',
'wp_product.externalProductId ',
])
.where('wp_product.siteId = :siteIds ', {
siteIds: siteId,
})
const rawResult = await externalProductIds.getRawMany();
const externalIds = rawResult.map(item => item.externalProductId);
const excludeValues = [];
const products = await this.wpApiService.getProducts(site);
for (const product of products) {
const variations =
excludeValues.push(String(product.id));
const variations =
product.type === 'variable'
? await this.wpApiService.getVariations(site, product.id)
: [];
await this.syncProductAndVariations(site.id, product, variations);
}
const filteredIds = externalIds.filter(id => !excludeValues.includes(id));
if(filteredIds.length!=0){
await this.variationModel.createQueryBuilder('variation')
.update()
.set({ on_delete: true })
.where(" variation.externalProductId in (:...filteredId) ",{filteredId:filteredIds})
.execute();
this.wpProductModel.createQueryBuilder('wp_product')
.update()
.set({ on_delete: true })
.where(" wp_product.externalProductId in (:...filteredId) ",{filteredId:filteredIds})
.execute();
}
}
// 控制产品上下架
@ -279,6 +310,8 @@ export class WpProductService {
if (status) {
where.status = status;
}
where.on_delete = false;
const products = await this.wpProductModel.find({
where,
skip: (current - 1) * pageSize,
@ -325,7 +358,7 @@ export class WpProductService {
'product.name as product_name', // 关联查询返回 product.name
'variation_product.name as variation_product_name', // 关联查询返回 variation 的产品 name
])
.where('wp_product.id IN (:...ids)', {
.where('wp_product.id IN (:...ids) AND wp_product.on_delete = false ', {
ids: products.map(product => product.id),
});
@ -457,7 +490,21 @@ export class WpProductService {
where: { siteId, externalProductId: productId },
});
if (!product) throw new Error('未找到该商品');
await this.variationModel.delete({ siteId, externalProductId: productId });
await this.wpProductModel.delete({ siteId, externalProductId: productId });
await this.variationModel.createQueryBuilder('variation')
.update()
.set({ on_delete: true })
.where(" variation.externalProductId = :externalProductId ",{externalProductId:productId})
.execute();
const sums= await this.wpProductModel.createQueryBuilder('wp_product')
.update()
.set({ on_delete: true })
.where(" wp_product.externalProductId = :externalProductId ",{externalProductId:productId})
.execute();
console.log(sums);
//await this.variationModel.delete({ siteId, externalProductId: productId });
//await this.wpProductModel.delete({ siteId, externalProductId: productId });
}
}