fix: 将origin_id类型从number改为string并重构客户创建逻辑

修改customer.dto.ts中的origin_id类型为string,以保持数据类型一致性
重构customer.service.ts中的客户数据映射逻辑,移除冗余的origin_id转换
在order.service.ts中使用customerService处理客户创建,替代直接操作model
This commit is contained in:
tikkhun 2026-01-05 16:30:48 +08:00
parent 70948ef977
commit 99bd7009cc
3 changed files with 34 additions and 20 deletions

View File

@ -73,7 +73,7 @@ export class CreateCustomerDTO {
site_id: number; site_id: number;
@ApiProperty({ description: '原始ID', required: false }) @ApiProperty({ description: '原始ID', required: false })
origin_id?: number; origin_id?: string;
@ApiProperty({ description: '邮箱' }) @ApiProperty({ description: '邮箱' })
email: string; email: string;

View File

@ -39,7 +39,7 @@ export class CustomerService {
site_id: siteId, // 使用站点ID而不是客户ID site_id: siteId, // 使用站点ID而不是客户ID
site_created_at: this.parseDate(siteCustomer.date_created), site_created_at: this.parseDate(siteCustomer.date_created),
site_updated_at: this.parseDate(siteCustomer.date_modified), site_updated_at: this.parseDate(siteCustomer.date_modified),
origin_id: Number(siteCustomer.id), origin_id: String(siteCustomer.id),
email: siteCustomer.email, email: siteCustomer.email,
first_name: siteCustomer.first_name, first_name: siteCustomer.first_name,
last_name: siteCustomer.last_name, last_name: siteCustomer.last_name,
@ -171,10 +171,7 @@ export class CustomerService {
// 第二步:将站点客户数据转换为客户实体数据 // 第二步:将站点客户数据转换为客户实体数据
const customersData = siteCustomers.map(siteCustomer => { const customersData = siteCustomers.map(siteCustomer => {
return this.mapSiteCustomerToCustomer(siteCustomer, siteId); return this.mapSiteCustomerToCustomer(siteCustomer, siteId);
}).map(customer => ({ })
...customer,
origin_id: String(customer.origin_id),
}));
// 第三步批量upsert客户数据 // 第三步批量upsert客户数据
const upsertResult = await this.upsertManyCustomers(customersData); const upsertResult = await this.upsertManyCustomers(customersData);

View File

@ -37,6 +37,7 @@ import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import * as os from 'os'; import * as os from 'os';
import { UnifiedOrderDTO } from '../dto/site-api.dto'; import { UnifiedOrderDTO } from '../dto/site-api.dto';
import { CustomerService } from './customer.service';
@Provide() @Provide()
export class OrderService { export class OrderService {
@ -97,9 +98,12 @@ export class OrderService {
@Inject() @Inject()
siteService: SiteService; siteService: SiteService;
@Inject() @Inject()
siteApiService: SiteApiService; siteApiService: SiteApiService;
@Inject()
customerService: CustomerService;
@Logger() @Logger()
logger; // 注入 Logger 实例 logger; // 注入 Logger 实例
@ -343,20 +347,33 @@ export class OrderService {
return entity; return entity;
} }
entity.orderStatus = this.mapOrderStatus(entity.status); entity.orderStatus = this.mapOrderStatus(entity.status);
const customer = await this.customerModel.findOne({ await this.customerService.upsertCustomer({
where: { email: order.customer_email }, email: order.customer_email,
site_id: siteId,
origin_id: String(order.customer_id),
billing: order.billing,
shipping: order.shipping,
first_name: order?.billing?.first_name || order?.shipping?.first_name,
last_name: order?.billing?.last_name || order?.shipping?.last_name,
fullname: order?.billing?.fullname || order?.shipping?.fullname,
phone: order?.billing?.phone || order?.shipping?.phone,
// tags:['fromOrder']
}); });
if (!customer) { // const customer = await this.customerModel.findOne({
// 这里用 customer create // where: { email: order.customer_email },
await this.customerModel.save({ // });
email: order.customer_email, // if (!customer) {
site_id: siteId, // // 这里用 customer create
origin_id: String(order.customer_id), // await this.customerModel.save({
billing: order.billing, // email: order.customer_email,
shipping: order.shipping, // site_id: siteId,
phone: order?.billing?.phone || order?.shipping?.phone, // origin_id: String(order.customer_id),
}); // billing: order.billing,
} // shipping: order.shipping,
// phone: order?.billing?.phone || order?.shipping?.phone,
// });
// }
return await this.orderModel.save(entity); return await this.orderModel.save(entity);
} }