diff --git a/src/dto/customer.dto.ts b/src/dto/customer.dto.ts index d7ed2e4..99a343d 100644 --- a/src/dto/customer.dto.ts +++ b/src/dto/customer.dto.ts @@ -73,7 +73,7 @@ export class CreateCustomerDTO { site_id: number; @ApiProperty({ description: '原始ID', required: false }) - origin_id?: number; + origin_id?: string; @ApiProperty({ description: '邮箱' }) email: string; diff --git a/src/service/customer.service.ts b/src/service/customer.service.ts index 56b3b01..bd84592 100644 --- a/src/service/customer.service.ts +++ b/src/service/customer.service.ts @@ -39,7 +39,7 @@ export class CustomerService { site_id: siteId, // 使用站点ID而不是客户ID site_created_at: this.parseDate(siteCustomer.date_created), site_updated_at: this.parseDate(siteCustomer.date_modified), - origin_id: Number(siteCustomer.id), + origin_id: String(siteCustomer.id), email: siteCustomer.email, first_name: siteCustomer.first_name, last_name: siteCustomer.last_name, @@ -171,10 +171,7 @@ export class CustomerService { // 第二步:将站点客户数据转换为客户实体数据 const customersData = siteCustomers.map(siteCustomer => { return this.mapSiteCustomerToCustomer(siteCustomer, siteId); - }).map(customer => ({ - ...customer, - origin_id: String(customer.origin_id), - })); + }) // 第三步:批量upsert客户数据 const upsertResult = await this.upsertManyCustomers(customersData); diff --git a/src/service/order.service.ts b/src/service/order.service.ts index ba75691..998b260 100644 --- a/src/service/order.service.ts +++ b/src/service/order.service.ts @@ -37,6 +37,7 @@ import * as fs from 'fs'; import * as path from 'path'; import * as os from 'os'; import { UnifiedOrderDTO } from '../dto/site-api.dto'; +import { CustomerService } from './customer.service'; @Provide() export class OrderService { @@ -97,9 +98,12 @@ export class OrderService { @Inject() siteService: SiteService; - @Inject() + @Inject() siteApiService: SiteApiService; + @Inject() + customerService: CustomerService; + @Logger() logger; // 注入 Logger 实例 @@ -343,20 +347,33 @@ export class OrderService { return entity; } entity.orderStatus = this.mapOrderStatus(entity.status); - const customer = await this.customerModel.findOne({ - where: { email: order.customer_email }, + await this.customerService.upsertCustomer({ + 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) { - // 这里用 customer create - await this.customerModel.save({ - email: order.customer_email, - site_id: siteId, - origin_id: String(order.customer_id), - billing: order.billing, - shipping: order.shipping, - phone: order?.billing?.phone || order?.shipping?.phone, - }); - } + // const customer = await this.customerModel.findOne({ + // where: { email: order.customer_email }, + // }); + // if (!customer) { + // // 这里用 customer create + // await this.customerModel.save({ + // email: order.customer_email, + // site_id: siteId, + // 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); }