main #22
|
|
@ -75,7 +75,6 @@ export class WebhookController {
|
||||||
switch (topic) {
|
switch (topic) {
|
||||||
case 'product.created':
|
case 'product.created':
|
||||||
case 'product.updated':
|
case 'product.updated':
|
||||||
const site = await this.wpProductService.getSite(siteId);
|
|
||||||
// 变体更新
|
// 变体更新
|
||||||
if (body.type === 'variation') {
|
if (body.type === 'variation') {
|
||||||
const variation = await this.wpApiService.getVariation(
|
const variation = await this.wpApiService.getVariation(
|
||||||
|
|
|
||||||
|
|
@ -249,6 +249,16 @@ export class Order {
|
||||||
@Expose()
|
@Expose()
|
||||||
utm_source: string;
|
utm_source: string;
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
@Column({ default: '' })
|
||||||
|
@Expose()
|
||||||
|
is_exchange: string;
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
@Column({ default: '' })
|
||||||
|
@Expose()
|
||||||
|
exchange_frequency: string;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
example: '2022-12-12 11:11:11',
|
example: '2022-12-12 11:11:11',
|
||||||
description: '创建时间',
|
description: '创建时间',
|
||||||
|
|
|
||||||
|
|
@ -77,3 +77,6 @@ export class OrderSaleOriginal {
|
||||||
@Expose()
|
@Expose()
|
||||||
updatedAt?: Date;
|
updatedAt?: Date;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -80,6 +80,10 @@ export class Variation {
|
||||||
@Column({ nullable: true, type: Boolean })
|
@Column({ nullable: true, type: Boolean })
|
||||||
on_sale: boolean; // 是否促销中
|
on_sale: boolean; // 是否促销中
|
||||||
|
|
||||||
|
@ApiProperty({ description: '是否删除', type: Boolean })
|
||||||
|
@Column({ nullable: true, type: Boolean , default: false })
|
||||||
|
on_delete: boolean; // 是否删除
|
||||||
|
|
||||||
@Column({ type: 'json', nullable: true })
|
@Column({ type: 'json', nullable: true })
|
||||||
attributes: Record<string, any>; // 变体的属性
|
attributes: Record<string, any>; // 变体的属性
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -77,6 +77,11 @@ export class WpProduct {
|
||||||
@Column({ nullable: true, type: Boolean })
|
@Column({ nullable: true, type: Boolean })
|
||||||
on_sale: boolean; // 是否促销中
|
on_sale: boolean; // 是否促销中
|
||||||
|
|
||||||
|
@ApiProperty({ description: '是否删除', type: Boolean })
|
||||||
|
@Column({ nullable: true, type: Boolean , default: false })
|
||||||
|
on_delete: boolean; // 是否删除
|
||||||
|
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: '产品类型',
|
description: '产品类型',
|
||||||
enum: ProductType,
|
enum: ProductType,
|
||||||
|
|
@ -84,6 +89,7 @@ export class WpProduct {
|
||||||
@Column({ type: 'enum', enum: ProductType })
|
@Column({ type: 'enum', enum: ProductType })
|
||||||
type: ProductType;
|
type: ProductType;
|
||||||
|
|
||||||
|
|
||||||
@Column({ type: 'json', nullable: true })
|
@Column({ type: 'json', nullable: true })
|
||||||
metadata: Record<string, any>; // 产品的其他扩展字段
|
metadata: Record<string, any>; // 产品的其他扩展字段
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import { In, Like, Repository } from 'typeorm';
|
||||||
import { InjectEntityModel, TypeORMDataSourceManager } from '@midwayjs/typeorm';
|
import { InjectEntityModel, TypeORMDataSourceManager } from '@midwayjs/typeorm';
|
||||||
import { plainToClass } from 'class-transformer';
|
import { plainToClass } from 'class-transformer';
|
||||||
import { OrderItem } from '../entity/order_item.entity';
|
import { OrderItem } from '../entity/order_item.entity';
|
||||||
|
import { OrderItemOriginal } from '../entity/order_items_original.entity';
|
||||||
import { OrderSale } from '../entity/order_sale.entity';
|
import { OrderSale } from '../entity/order_sale.entity';
|
||||||
import { WpProduct } from '../entity/wp_product.entity';
|
import { WpProduct } from '../entity/wp_product.entity';
|
||||||
import { Product } from '../entity/product.entty';
|
import { Product } from '../entity/product.entty';
|
||||||
|
|
@ -51,6 +52,9 @@ export class OrderService {
|
||||||
@InjectEntityModel(OrderItem)
|
@InjectEntityModel(OrderItem)
|
||||||
orderItemModel: Repository<OrderItem>;
|
orderItemModel: Repository<OrderItem>;
|
||||||
|
|
||||||
|
@InjectEntityModel(OrderItem)
|
||||||
|
orderItemOriginalModel: Repository<OrderItemOriginal>;
|
||||||
|
|
||||||
@InjectEntityModel(OrderSale)
|
@InjectEntityModel(OrderSale)
|
||||||
orderSaleModel: Repository<OrderSale>;
|
orderSaleModel: Repository<OrderSale>;
|
||||||
|
|
||||||
|
|
@ -209,7 +213,7 @@ export class OrderService {
|
||||||
el => el.key === '_wc_order_attribution_utm_source'
|
el => el.key === '_wc_order_attribution_utm_source'
|
||||||
)?.value || '';
|
)?.value || '';
|
||||||
order.customer_email = order?.billing?.email || order?.shipping?.email;
|
order.customer_email = order?.billing?.email || order?.shipping?.email;
|
||||||
order.billing_phone = order?.billing?.phone || order?.shipping?.phone;
|
order.billing_phone = order?.billing?.phone || order?.shipping?.phone;
|
||||||
const entity = plainToClass(Order, order);
|
const entity = plainToClass(Order, order);
|
||||||
const existingOrder = await this.orderModel.findOne({
|
const existingOrder = await this.orderModel.findOne({
|
||||||
where: { externalOrderId: order.externalOrderId, siteId: siteId },
|
where: { externalOrderId: order.externalOrderId, siteId: siteId },
|
||||||
|
|
@ -323,6 +327,28 @@ export class OrderService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async saveOrderItemsG(orderItem: OrderItem) {
|
||||||
|
const existingOrderItem = await this.orderItemModel.findOne({
|
||||||
|
where: {
|
||||||
|
externalOrderId: orderItem.externalOrderId,
|
||||||
|
siteId: orderItem.siteId,
|
||||||
|
externalOrderItemId: orderItem.externalOrderItemId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (
|
||||||
|
existingOrderItem &&
|
||||||
|
existingOrderItem.quantity === orderItem.quantity
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (existingOrderItem) {
|
||||||
|
await this.orderItemModel.update(existingOrderItem.id, orderItem);
|
||||||
|
} else {
|
||||||
|
await this.orderItemModel.save(orderItem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async saveOrderSale(orderItem: OrderItem) {
|
async saveOrderSale(orderItem: OrderItem) {
|
||||||
const currentOrderSale = await this.orderSaleModel.find({
|
const currentOrderSale = await this.orderSaleModel.find({
|
||||||
where: {
|
where: {
|
||||||
|
|
@ -565,7 +591,7 @@ export class OrderService {
|
||||||
o.total as total,
|
o.total as total,
|
||||||
o.date_created as date_created,
|
o.date_created as date_created,
|
||||||
o.customer_email as customer_email,
|
o.customer_email as customer_email,
|
||||||
o.billing_phone as billing_phone,
|
|
||||||
o.transaction_id as transaction_id,
|
o.transaction_id as transaction_id,
|
||||||
o.orderStatus as orderStatus,
|
o.orderStatus as orderStatus,
|
||||||
o.customer_ip_address as customer_ip_address,
|
o.customer_ip_address as customer_ip_address,
|
||||||
|
|
@ -1116,7 +1142,6 @@ export class OrderService {
|
||||||
}
|
}
|
||||||
|
|
||||||
// update order_sale_origin if not exist
|
// update order_sale_origin if not exist
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const order_sale_origin_count = await this.orderSaleOriginalModel.countBy({ orderId: id });
|
const order_sale_origin_count = await this.orderSaleOriginalModel.countBy({ orderId: id });
|
||||||
if (order_sale_origin_count === 0) {
|
if (order_sale_origin_count === 0) {
|
||||||
|
|
@ -1125,6 +1150,15 @@ export class OrderService {
|
||||||
await this.orderSaleOriginalModel.save(saleData);
|
await this.orderSaleOriginalModel.save(saleData);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// update order_item_origin if not exist
|
||||||
|
const order_item_origin_count = await this.orderItemOriginalModel.countBy({ orderId: id });
|
||||||
|
if (order_item_origin_count === 0 && items.length!=0) {
|
||||||
|
items.forEach(async sale => {
|
||||||
|
const { id: saleId, ...saleData } = sale;
|
||||||
|
await this.orderItemOriginalModel.save(saleData);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log('create order sale origin error: ', error.message);
|
console.log('create order sale origin error: ', error.message);
|
||||||
|
|
@ -1342,6 +1376,9 @@ export class OrderService {
|
||||||
// externalOrderItemId:
|
// externalOrderItemId:
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//await orderRepo.save();
|
||||||
|
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
transactionError = error;
|
transactionError = error;
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { Product } from './../entity/product.entty';
|
||||||
import { Config, Inject, Provide } from '@midwayjs/core';
|
import { Config, Inject, Provide } from '@midwayjs/core';
|
||||||
import { WPService } from './wp.service';
|
import { WPService } from './wp.service';
|
||||||
import { WpSite } from '../interface';
|
import { WpSite } from '../interface';
|
||||||
|
|
@ -10,7 +11,6 @@ import {
|
||||||
UpdateVariationDTO,
|
UpdateVariationDTO,
|
||||||
UpdateWpProductDTO,
|
UpdateWpProductDTO,
|
||||||
} from '../dto/wp_product.dto';
|
} from '../dto/wp_product.dto';
|
||||||
import { Product } from '../entity/product.entty';
|
|
||||||
import { ProductStatus, ProductStockStatus } from '../enums/base.enum';
|
import { ProductStatus, ProductStockStatus } from '../enums/base.enum';
|
||||||
|
|
||||||
@Provide()
|
@Provide()
|
||||||
|
|
@ -47,14 +47,45 @@ export class WpProductService {
|
||||||
|
|
||||||
async syncSite(siteId: string) {
|
async syncSite(siteId: string) {
|
||||||
const site = this.getSite(siteId);
|
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);
|
const products = await this.wpApiService.getProducts(site);
|
||||||
for (const product of products) {
|
for (const product of products) {
|
||||||
const variations =
|
excludeValues.push(String(product.id));
|
||||||
|
const variations =
|
||||||
product.type === 'variable'
|
product.type === 'variable'
|
||||||
? await this.wpApiService.getVariations(site, product.id)
|
? await this.wpApiService.getVariations(site, product.id)
|
||||||
: [];
|
: [];
|
||||||
|
|
||||||
await this.syncProductAndVariations(site.id, product, variations);
|
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) {
|
if (status) {
|
||||||
where.status = status;
|
where.status = status;
|
||||||
}
|
}
|
||||||
|
where.on_delete = false;
|
||||||
|
|
||||||
const products = await this.wpProductModel.find({
|
const products = await this.wpProductModel.find({
|
||||||
where,
|
where,
|
||||||
skip: (current - 1) * pageSize,
|
skip: (current - 1) * pageSize,
|
||||||
|
|
@ -325,7 +358,7 @@ export class WpProductService {
|
||||||
'product.name as product_name', // 关联查询返回 product.name
|
'product.name as product_name', // 关联查询返回 product.name
|
||||||
'variation_product.name as variation_product_name', // 关联查询返回 variation 的产品 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),
|
ids: products.map(product => product.id),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -457,7 +490,21 @@ export class WpProductService {
|
||||||
where: { siteId, externalProductId: productId },
|
where: { siteId, externalProductId: productId },
|
||||||
});
|
});
|
||||||
if (!product) throw new Error('未找到该商品');
|
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 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue