forked from yoone/API
Compare commits
No commits in common. "d04841c7afad44fc99d630d03c406dd10fc6bf9c" and "9a26c32ae08978ab3dbd3b5e1ddb76aa22e47d50" have entirely different histories.
d04841c7af
...
9a26c32ae0
|
|
@ -17,7 +17,7 @@ export default {
|
||||||
default: {
|
default: {
|
||||||
host: 'localhost',
|
host: 'localhost',
|
||||||
username: 'root',
|
username: 'root',
|
||||||
password: '12345678',
|
password: '123456',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -115,10 +115,10 @@ export class OrderController {
|
||||||
@Post('/updateOrderItems/:orderId')
|
@Post('/updateOrderItems/:orderId')
|
||||||
async updateOrderItems(
|
async updateOrderItems(
|
||||||
@Param('orderId') orderId: number,
|
@Param('orderId') orderId: number,
|
||||||
@Body() data: any,
|
@Body() data: any
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
const res = await this.orderService.updateExchangeOrder(orderId, data);
|
const res = await this.orderService.updateOrderSales(orderId, data);
|
||||||
return successResponse(res);
|
return successResponse(res);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return errorResponse(error?.message || '更新失败');
|
return errorResponse(error?.message || '更新失败');
|
||||||
|
|
|
||||||
|
|
@ -21,9 +21,7 @@ import {
|
||||||
} from '../dto/wp_product.dto';
|
} from '../dto/wp_product.dto';
|
||||||
import { WPService } from '../service/wp.service';
|
import { WPService } from '../service/wp.service';
|
||||||
import { WpSite } from '../interface';
|
import { WpSite } from '../interface';
|
||||||
import {
|
|
||||||
ProductsRes,
|
|
||||||
} from '../dto/reponse.dto';
|
|
||||||
@Controller('/wp_product')
|
@Controller('/wp_product')
|
||||||
export class WpProductController {
|
export class WpProductController {
|
||||||
@Inject()
|
@Inject()
|
||||||
|
|
@ -189,19 +187,4 @@ export class WpProductController {
|
||||||
return errorResponse(error.message || '产品变体更新失败');
|
return errorResponse(error.message || '产品变体更新失败');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiOkResponse({
|
|
||||||
description: '通过name搜索产品/订单',
|
|
||||||
type: ProductsRes,
|
|
||||||
})
|
|
||||||
@Get('/search')
|
|
||||||
async searchProducts(@Query('name') name: string) {
|
|
||||||
try {
|
|
||||||
// 调用服务获取产品数据
|
|
||||||
const products = await this.wpProductService.findProductsByName(name);
|
|
||||||
return successResponse(products);
|
|
||||||
} catch (error) {
|
|
||||||
return errorResponse(error.message || '获取数据失败');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -244,15 +244,14 @@ export class Order {
|
||||||
utm_source: string;
|
utm_source: string;
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@Column({ default: false })
|
@Column({ default: '' })
|
||||||
@Expose()
|
@Expose()
|
||||||
is_exchange: boolean;
|
is_exchange: string;
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@Column('decimal', { precision: 10, scale: 0, default: 0 })
|
@Column({ default: '' })
|
||||||
@Expose()
|
@Expose()
|
||||||
exchange_frequency: number;
|
exchange_frequency: string;
|
||||||
|
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
example: '2022-12-12 11:11:11',
|
example: '2022-12-12 11:11:11',
|
||||||
|
|
|
||||||
|
|
@ -41,9 +41,6 @@ export enum OrderStatus {
|
||||||
REFUNDED = 'refunded',
|
REFUNDED = 'refunded',
|
||||||
FAILED = 'failed',
|
FAILED = 'failed',
|
||||||
DRAFT = 'draft',
|
DRAFT = 'draft',
|
||||||
REFUND_REQUESTED = 'refund_requested', // 已申请退款
|
|
||||||
REFUND_APPROVED = 'refund_approved', // 退款申请已通过
|
|
||||||
REFUND_CANCELLED = 'refund_cancelled', // 已取消退款
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum ErpOrderStatus {
|
export enum ErpOrderStatus {
|
||||||
|
|
@ -53,12 +50,10 @@ export enum ErpOrderStatus {
|
||||||
CANCEL = 'cancelled', //已取消
|
CANCEL = 'cancelled', //已取消
|
||||||
REFUNDED = 'refunded', //已退款
|
REFUNDED = 'refunded', //已退款
|
||||||
FAILED = 'failed', //失败
|
FAILED = 'failed', //失败
|
||||||
|
|
||||||
AFTER_SALE_PROCESSING = 'after_sale_pending', // 售后处理中
|
AFTER_SALE_PROCESSING = 'after_sale_pending', // 售后处理中
|
||||||
PENDING_RESHIPMENT = 'pending_reshipment', // 待补发
|
PENDING_RESHIPMENT = 'pending_reshipment', // 待补发
|
||||||
PENDING_REFUND = 'pending_refund', // 待退款
|
PENDING_REFUND = 'pending_refund', // 待退款
|
||||||
REFUND_REQUESTED = 'refund_requested', // 已申请退款
|
|
||||||
REFUND_APPROVED = 'refund_approved', // 退款申请已通过
|
|
||||||
REFUND_CANCELLED = 'refund_cancelled', // 已取消退款
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum ShipmentType {
|
export enum ShipmentType {
|
||||||
|
|
|
||||||
|
|
@ -263,12 +263,6 @@ export class OrderService {
|
||||||
return ErpOrderStatus.REFUNDED;
|
return ErpOrderStatus.REFUNDED;
|
||||||
case OrderStatus.FAILED:
|
case OrderStatus.FAILED:
|
||||||
return ErpOrderStatus.FAILED;
|
return ErpOrderStatus.FAILED;
|
||||||
case OrderStatus.REFUND_REQUESTED:
|
|
||||||
return ErpOrderStatus.REFUND_REQUESTED;
|
|
||||||
case OrderStatus.REFUND_APPROVED:
|
|
||||||
return ErpOrderStatus.REFUND_APPROVED;
|
|
||||||
case OrderStatus.REFUND_CANCELLED:
|
|
||||||
return ErpOrderStatus.REFUND_CANCELLED;
|
|
||||||
default:
|
default:
|
||||||
return ErpOrderStatus.PENDING;
|
return ErpOrderStatus.PENDING;
|
||||||
}
|
}
|
||||||
|
|
@ -597,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.exchange_frequency as exchange_frequency,
|
|
||||||
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,
|
||||||
|
|
@ -1383,97 +1377,7 @@ export class OrderService {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//await orderRepo.save();
|
||||||
}).catch(error => {
|
|
||||||
transactionError = error;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (transactionError !== undefined) {
|
|
||||||
throw new Error(`更新物流信息错误:${transactionError.message}`);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
} catch (error) {
|
|
||||||
throw new Error(`更新发货产品失败:${error.message}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//换货确认按钮改成调用这个方法
|
|
||||||
//换货功能更新OrderSale和Orderitem数据
|
|
||||||
async updateExchangeOrder(orderId: number, data: any) {
|
|
||||||
try {
|
|
||||||
const dataSource = this.dataSourceManager.getDataSource('default');
|
|
||||||
let transactionError = undefined;
|
|
||||||
|
|
||||||
await dataSource.transaction(async manager => {
|
|
||||||
const orderRepo = manager.getRepository(Order);
|
|
||||||
const orderSaleRepo = manager.getRepository(OrderSale);
|
|
||||||
const orderItemRepo = manager.getRepository(OrderItem);
|
|
||||||
const OrderItemOriginalRepo = manager.getRepository(OrderItemOriginal);
|
|
||||||
|
|
||||||
const productRepo = manager.getRepository(Product);
|
|
||||||
const WpProductRepo = manager.getRepository(WpProduct);
|
|
||||||
|
|
||||||
const order = await orderRepo.findOneBy({ id: orderId });
|
|
||||||
let product: Product;
|
|
||||||
let wpProduct: WpProduct;
|
|
||||||
let wpOrderItemOriginal: OrderItemOriginal;
|
|
||||||
await orderSaleRepo.delete({ orderId });
|
|
||||||
await orderItemRepo.delete({ orderId });
|
|
||||||
for (const sale of data['sales']) {
|
|
||||||
product = await productRepo.findOneBy({ sku: sale['sku'] });
|
|
||||||
await orderSaleRepo.save({
|
|
||||||
orderId,
|
|
||||||
siteId: order.siteId,
|
|
||||||
productId: product.id,
|
|
||||||
name: product.name,
|
|
||||||
sku: sale['sku'],
|
|
||||||
quantity: sale['quantity'],
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
for (const item of data['items']) {
|
|
||||||
wpProduct = await WpProductRepo.findOneBy({ sku: item['sku'] });
|
|
||||||
wpOrderItemOriginal = await OrderItemOriginalRepo.findOneBy({ sku: item['sku'] });
|
|
||||||
let externalVariationId = wpOrderItemOriginal?.externalVariationId;
|
|
||||||
let price = wpOrderItemOriginal?.price;
|
|
||||||
if (wpOrderItemOriginal == null) {
|
|
||||||
externalVariationId = '0';
|
|
||||||
price = 0;
|
|
||||||
}
|
|
||||||
await orderItemRepo.save({
|
|
||||||
orderId,
|
|
||||||
siteId: order.siteId,
|
|
||||||
productId: wpProduct.id,
|
|
||||||
name: wpProduct.name,
|
|
||||||
externalOrderId: order.externalOrderId,
|
|
||||||
externalProductId: wpProduct.externalProductId,
|
|
||||||
externalVariationId: externalVariationId,
|
|
||||||
price: price,
|
|
||||||
sku: item['sku'],
|
|
||||||
quantity: item['quantity'],
|
|
||||||
});
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
//将是否换货状态改为true
|
|
||||||
await orderRepo.update(
|
|
||||||
order.id
|
|
||||||
, {
|
|
||||||
is_exchange: true
|
|
||||||
});
|
|
||||||
|
|
||||||
//查询这个用户换过多少次货
|
|
||||||
const counts = await orderRepo.countBy({
|
|
||||||
is_editable: true,
|
|
||||||
customer_email: order.customer_email,
|
|
||||||
});
|
|
||||||
|
|
||||||
//批量更新当前用户换货次数
|
|
||||||
await orderRepo.update({
|
|
||||||
customer_email: order.customer_email
|
|
||||||
}, {
|
|
||||||
exchange_frequency: counts
|
|
||||||
});
|
|
||||||
|
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
transactionError = error;
|
transactionError = error;
|
||||||
|
|
|
||||||
|
|
@ -507,42 +507,4 @@ const excludeValues = [];
|
||||||
//await this.variationModel.delete({ siteId, externalProductId: productId });
|
//await this.variationModel.delete({ siteId, externalProductId: productId });
|
||||||
//await this.wpProductModel.delete({ siteId, externalProductId: productId });
|
//await this.wpProductModel.delete({ siteId, externalProductId: productId });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async findProductsByName(name: string): Promise<WpProduct[]> {
|
|
||||||
const nameFilter = name ? name.split(' ').filter(Boolean) : [];
|
|
||||||
const query = this.wpProductModel.createQueryBuilder('product');
|
|
||||||
|
|
||||||
// 保证 sku 不为空
|
|
||||||
query.where('product.sku IS NOT NULL AND product.on_delete = false');
|
|
||||||
|
|
||||||
if (nameFilter.length > 0 || name) {
|
|
||||||
const params: Record<string, string> = {};
|
|
||||||
const conditions: string[] = [];
|
|
||||||
|
|
||||||
// 英文名关键词全部匹配(AND)
|
|
||||||
if (nameFilter.length > 0) {
|
|
||||||
const nameConds = nameFilter.map((word, index) => {
|
|
||||||
const key = `name${index}`;
|
|
||||||
params[key] = `%${word}%`;
|
|
||||||
return `product.name LIKE :${key}`;
|
|
||||||
});
|
|
||||||
conditions.push(`(${nameConds.join(' AND ')})`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 中文名模糊匹配
|
|
||||||
if (name) {
|
|
||||||
params['nameCn'] = `%${name}%`;
|
|
||||||
conditions.push(`product.nameCn LIKE :nameCn`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 英文名关键词匹配 OR 中文名匹配
|
|
||||||
query.andWhere(`(${conditions.join(' OR ')})`, params);
|
|
||||||
}
|
|
||||||
|
|
||||||
query.take(50);
|
|
||||||
|
|
||||||
return await query.getMany();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue