forked from yoone/API
Compare commits
4 Commits
ff5d7e81a1
...
8b2aea7038
| Author | SHA1 | Date |
|---|---|---|
|
|
8b2aea7038 | |
|
|
39401aeaa0 | |
|
|
4e1f4e192d | |
|
|
6cb793b3ca |
|
|
@ -41,6 +41,7 @@ import { Category } from '../entity/category.entity';
|
|||
import DictSeeder from '../db/seeds/dict.seeder';
|
||||
import CategorySeeder from '../db/seeds/category.seeder';
|
||||
import CategoryAttributeSeeder from '../db/seeds/category_attribute.seeder';
|
||||
import { SiteSku } from '../entity/site-sku.entity';
|
||||
|
||||
export default {
|
||||
// use for cookie sign key, should change to your own and keep security
|
||||
|
|
@ -50,6 +51,7 @@ export default {
|
|||
entities: [
|
||||
Product,
|
||||
ProductStockComponent,
|
||||
SiteSku,
|
||||
User,
|
||||
PurchaseOrder,
|
||||
PurchaseOrderItem,
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import {
|
|||
Body,
|
||||
Controller,
|
||||
Del,
|
||||
Files,
|
||||
Get,
|
||||
Inject,
|
||||
Param,
|
||||
|
|
@ -27,7 +28,6 @@ import {
|
|||
} from '../dto/order.dto';
|
||||
import { User } from '../decorator/user.decorator';
|
||||
import { ErpOrderStatus } from '../enums/base.enum';
|
||||
|
||||
@Controller('/order')
|
||||
export class OrderController {
|
||||
@Inject()
|
||||
|
|
@ -264,4 +264,21 @@ export class OrderController {
|
|||
return errorResponse(error?.message || '导出失败');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 导入产品(CSV 文件)
|
||||
@ApiOkResponse()
|
||||
@Post('/import')
|
||||
async importWintopay(@Files() files: any) {
|
||||
try {
|
||||
// 条件判断:确保存在文件
|
||||
const file = files?.[0];
|
||||
if (!file) return errorResponse('未接收到上传文件');
|
||||
|
||||
const result = await this.orderService.importWintopayTable(file);
|
||||
return successResponse(result);
|
||||
} catch (error) {
|
||||
return errorResponse(error?.message || error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,77 @@
|
|||
import { Bootstrap } from '@midwayjs/bootstrap';
|
||||
import { Product } from '../../entity/product.entity';
|
||||
import { SiteSku } from '../../entity/site-sku.entity';
|
||||
import { InjectEntityModel } from '@midwayjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Provide } from '@midwayjs/core';
|
||||
|
||||
@Provide()
|
||||
export class MigrateSiteSkus {
|
||||
@InjectEntityModel(Product)
|
||||
productModel: Repository<Product>;
|
||||
|
||||
@InjectEntityModel(SiteSku)
|
||||
siteSkuModel: Repository<SiteSku>;
|
||||
|
||||
async main() {
|
||||
console.log('开始迁移 siteSkus 数据...');
|
||||
|
||||
try {
|
||||
// 获取所有产品
|
||||
const products = await this.productModel.find();
|
||||
|
||||
console.log(`找到 ${products.length} 个产品需要检查 siteSkus 数据`);
|
||||
|
||||
let migratedCount = 0;
|
||||
|
||||
for (const product of products) {
|
||||
// 检查 siteSkus 是否为字符串数组
|
||||
if (Array.isArray(product.siteSkus) && product.siteSkus.length > 0) {
|
||||
for (const siteSku of product.siteSkus) {
|
||||
// 检查是否已存在该 SKU
|
||||
const existingSiteSku = await this.siteSkuModel.findOne({
|
||||
where: { sku: siteSku.sku },
|
||||
});
|
||||
|
||||
if (!existingSiteSku) {
|
||||
// 创建新的 SiteSku 实体
|
||||
const siteSku = new SiteSku();
|
||||
siteSku.sku = siteSku.sku;
|
||||
siteSku.productId = product.id;
|
||||
siteSku.isOld = true;
|
||||
|
||||
await this.siteSkuModel.save(siteSku);
|
||||
migratedCount++;
|
||||
} else if (!existingSiteSku.productId) {
|
||||
// 如果已存在但未关联产品,则关联
|
||||
existingSiteSku.productId = product.id;
|
||||
existingSiteSku.isOld = true;
|
||||
await this.siteSkuModel.save(existingSiteSku);
|
||||
migratedCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`成功迁移 ${migratedCount} 条 siteSku 数据`);
|
||||
console.log('数据迁移完成!');
|
||||
} catch (error) {
|
||||
console.error('数据迁移失败:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 运行迁移
|
||||
if (require.main === module) {
|
||||
Bootstrap
|
||||
.run()
|
||||
.then(async (app) => {
|
||||
const migrateService = app.get(MigrateSiteSkus);
|
||||
await migrateService.main();
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('启动失败:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
import { ApiProperty } from '@midwayjs/swagger';
|
||||
import { Rule, RuleType } from '@midwayjs/validate';
|
||||
import { UnifiedSearchParamsDTO } from './api.dto';
|
||||
import { SiteSku } from '../entity/site-sku.entity';
|
||||
|
||||
/**
|
||||
* 属性输入DTO
|
||||
|
|
@ -65,7 +66,7 @@ export class CreateProductDTO {
|
|||
|
||||
@ApiProperty({ description: '站点 SKU 列表', type: 'array', required: false })
|
||||
@Rule(RuleType.array().items(RuleType.string()).optional())
|
||||
siteSkus?: string[];
|
||||
siteSkus?: SiteSku['sku'][];
|
||||
|
||||
// 通用属性输入(通过 attributes 统一提交品牌/口味/强度/尺寸/干湿等)
|
||||
// 当 type 为 'single' 时必填,当 type 为 'bundle' 时可选
|
||||
|
|
@ -152,7 +153,7 @@ export class UpdateProductDTO {
|
|||
|
||||
@ApiProperty({ description: '站点 SKU 列表', type: 'array', required: false })
|
||||
@Rule(RuleType.array().items(RuleType.string()).optional())
|
||||
siteSkus?: string[];
|
||||
siteSkus?: SiteSku['sku'][];
|
||||
|
||||
// 商品价格
|
||||
@ApiProperty({ description: '价格', example: 99.99, required: false })
|
||||
|
|
@ -197,7 +198,6 @@ export class UpdateProductDTO {
|
|||
components?: { sku: string; quantity: number }[];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* DTO 用于批量更新产品属性
|
||||
*/
|
||||
|
|
@ -232,7 +232,7 @@ export class BatchUpdateProductDTO {
|
|||
|
||||
@ApiProperty({ description: '站点 SKU 列表', type: 'array', required: false })
|
||||
@Rule(RuleType.array().items(RuleType.string()).optional())
|
||||
siteSkus?: string[];
|
||||
siteSkus?: SiteSku['sku'][];
|
||||
|
||||
@ApiProperty({ description: '价格', example: 99.99, required: false })
|
||||
@Rule(RuleType.number().optional())
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ import { ApiProperty } from '@midwayjs/swagger';
|
|||
import { DictItem } from './dict_item.entity';
|
||||
import { ProductStockComponent } from './product_stock_component.entity';
|
||||
import { Category } from './category.entity';
|
||||
import { SiteSku } from './site-sku.entity';
|
||||
|
||||
@Entity('product')
|
||||
export class Product {
|
||||
|
|
@ -98,9 +99,11 @@ export class Product {
|
|||
@OneToMany(() => ProductStockComponent, (component) => component.product, { cascade: true })
|
||||
components: ProductStockComponent[];
|
||||
|
||||
@ApiProperty({ description: '站点 SKU 列表', type: 'string', isArray: true })
|
||||
@Column({ type: 'simple-array' ,nullable:true})
|
||||
siteSkus: string[];
|
||||
|
||||
// 站点 SKU 关联
|
||||
@ApiProperty({ description: '站点 SKU关联', type: SiteSku, isArray: true })
|
||||
@OneToMany(() => SiteSku, siteSku => siteSku.product, { cascade: true })
|
||||
siteSkus: SiteSku[];
|
||||
|
||||
// 来源
|
||||
@ApiProperty({ description: '来源', example: '1' })
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
import { ApiProperty } from '@midwayjs/swagger';
|
||||
import { Column, CreateDateColumn, Entity, ManyToOne, PrimaryGeneratedColumn, UpdateDateColumn } from 'typeorm';
|
||||
import { Product } from './product.entity';
|
||||
|
||||
// 套餐产品构成 item(sku,0013,xx20,mixed008)n--siteSkus(0013,xx20,xx)旧版数据-->product-component(0020)(套装(1 5 10 20 50 100 (erp 手动创建)mixed))->(product(单品)+ quantity)->sales
|
||||
// CA -> site-product -> components
|
||||
// PRODUCT->compoentn(一次性做完) product+关联sku
|
||||
@Entity('product_stock_component')
|
||||
export class ProductStockComponent {
|
||||
@ApiProperty({ type: Number })
|
||||
|
|
@ -11,15 +13,15 @@ export class ProductStockComponent {
|
|||
@ApiProperty({ type: Number })
|
||||
@Column()
|
||||
productId: number;
|
||||
|
||||
// zi
|
||||
@ApiProperty({ description: '组件所关联的 SKU', type: 'string' })
|
||||
@Column({ type: 'varchar', length: 64 })
|
||||
sku: string;
|
||||
|
||||
// zi
|
||||
@ApiProperty({ type: Number, description: '组成数量' })
|
||||
@Column({ type: 'int', default: 1 })
|
||||
quantity: number;
|
||||
|
||||
// baba
|
||||
// 多对一,组件隶属于一个产品
|
||||
@ManyToOne(() => Product, (product) => product.components, { onDelete: 'CASCADE' })
|
||||
product: Product;
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ export class SiteProduct {
|
|||
sku: string;
|
||||
|
||||
@ApiProperty({ description: '类型' })
|
||||
@Column({ length: 16, default: 'single' })
|
||||
@Column({ length: 16, default: 'simple' })
|
||||
type: string;
|
||||
|
||||
@ApiProperty({
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
import {
|
||||
Column,
|
||||
Entity,
|
||||
ManyToOne,
|
||||
JoinColumn,
|
||||
} from 'typeorm';
|
||||
import { ApiProperty } from '@midwayjs/swagger';
|
||||
import { Product } from './product.entity';
|
||||
@Entity('product_site_sku')
|
||||
export class SiteSku {
|
||||
@ApiProperty({ description: 'sku'})
|
||||
@Column({ primary: true })
|
||||
sku: string;
|
||||
|
||||
@ApiProperty({ description: '商品ID' })
|
||||
@Column({ nullable: true })
|
||||
productId: number;
|
||||
// 商品关联
|
||||
@ManyToOne(() => Product, product => product.siteSkus)
|
||||
@JoinColumn({ name: 'productId' })
|
||||
product: Product;
|
||||
|
||||
@ApiProperty({ description: '是否旧版数据' })
|
||||
@Column({ default: false })
|
||||
isOld: boolean;
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
import { Inject, Logger, Provide } from '@midwayjs/core';
|
||||
import { WPService } from './wp.service';
|
||||
import * as xlsx from 'xlsx';
|
||||
import { Order } from '../entity/order.entity';
|
||||
import { In, Like, Repository } from 'typeorm';
|
||||
import { InjectEntityModel, TypeORMDataSourceManager } from '@midwayjs/typeorm';
|
||||
|
|
@ -32,7 +33,7 @@ import { UpdateStockDTO } from '../dto/stock.dto';
|
|||
import { StockService } from './stock.service';
|
||||
import { OrderItemOriginal } from '../entity/order_item_original.entity';
|
||||
import { SiteApiService } from './site-api.service';
|
||||
import { SyncOperationResult } from '../dto/api.dto';
|
||||
import { BatchErrorItem, SyncOperationResult } from '../dto/api.dto';
|
||||
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
|
@ -2176,6 +2177,7 @@ export class OrderService {
|
|||
* @param data 订单数据
|
||||
* @returns 创建的订单
|
||||
*/
|
||||
// TODO 与 sync 逻辑不一致 如需要请修复。
|
||||
async createOrder(data: Record<string, any>) {
|
||||
// 从数据中解构出需要用的属性
|
||||
const { siteId, sales, total, billing, customer_email, billing_phone } = data;
|
||||
|
|
@ -2653,6 +2655,82 @@ export class OrderService {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出数据为XLSX格式
|
||||
* @param {any[]} data 数据数组
|
||||
* @param {Object} options 配置选项
|
||||
* @param {string} [options.type='buffer'] 输出类型:'buffer' | 'string' (XLSX默认返回buffer)
|
||||
* @param {string} [options.fileName] 文件名(仅当需要写入文件时使用)
|
||||
* @param {boolean} [options.writeFile=false] 是否写入文件
|
||||
* @returns {string|Buffer} 根据type返回字符串或Buffer
|
||||
*/
|
||||
async exportToXlsx(data: any[], options: { type?: 'buffer' | 'string'; fileName?: string; writeFile?: boolean } = {}): Promise<string | Buffer> {
|
||||
try {
|
||||
// 检查数据是否为空
|
||||
if (!data || data.length === 0) {
|
||||
throw new Error('导出数据不能为空');
|
||||
}
|
||||
|
||||
const { type = 'buffer', fileName, writeFile = false } = options;
|
||||
|
||||
// 获取表头
|
||||
const headers = Object.keys(data[0]);
|
||||
|
||||
// 构建二维数组数据(包含表头)
|
||||
const aoaData = [headers];
|
||||
data.forEach(item => {
|
||||
const row = headers.map(key => {
|
||||
const value = item[key as keyof any];
|
||||
// 处理undefined和null
|
||||
if (value === undefined || value === null) {
|
||||
return '';
|
||||
}
|
||||
// 处理日期类型
|
||||
if (value instanceof Date) {
|
||||
return value.toISOString();
|
||||
}
|
||||
return value;
|
||||
});
|
||||
aoaData.push(row);
|
||||
});
|
||||
|
||||
// 创建工作簿和工作表
|
||||
const workbook = xlsx.utils.book_new();
|
||||
const worksheet = xlsx.utils.aoa_to_sheet(aoaData);
|
||||
xlsx.utils.book_append_sheet(workbook, worksheet, 'Sheet1');
|
||||
|
||||
// 生成XLSX buffer
|
||||
const buffer = xlsx.write(workbook, { bookType: 'xlsx', type: 'buffer' });
|
||||
|
||||
// 如果需要写入文件
|
||||
if (writeFile && fileName) {
|
||||
// 获取当前用户目录
|
||||
const userHomeDir = os.homedir();
|
||||
|
||||
// 构建目标路径(下载目录)
|
||||
const downloadsDir = path.join(userHomeDir, 'Downloads');
|
||||
|
||||
// 确保下载目录存在
|
||||
if (!fs.existsSync(downloadsDir)) {
|
||||
fs.mkdirSync(downloadsDir, { recursive: true });
|
||||
}
|
||||
const filePath = path.join(downloadsDir, fileName);
|
||||
// 写入文件
|
||||
fs.writeFileSync(filePath, buffer);
|
||||
return filePath;
|
||||
}
|
||||
|
||||
// 根据类型返回不同结果
|
||||
if (type === 'string') {
|
||||
return buffer.toString('base64');
|
||||
}
|
||||
|
||||
return buffer;
|
||||
} catch (error) {
|
||||
throw new Error(`导出XLSX文件失败: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除每个分号前面一个左右括号和最后一个左右括号包含的内容(包括括号本身)
|
||||
* @param str 输入字符串
|
||||
|
|
@ -2728,4 +2806,108 @@ export class OrderService {
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
// 从 CSV 导入产品;存在则更新,不存在则创建
|
||||
/**
|
||||
* 导入 Wintopay 表格并回填物流信息
|
||||
* @param file 上传的文件
|
||||
* @returns 处理后的数据(包含更新的物流信息)
|
||||
*/
|
||||
async importWintopayTable(file: any): Promise<any> {
|
||||
let updated = 0;
|
||||
const errors: BatchErrorItem[] = [];
|
||||
|
||||
// 解析文件获取工作表
|
||||
let buffer: Buffer;
|
||||
if (Buffer.isBuffer(file)) {
|
||||
buffer = file;
|
||||
} else if (file?.data) {
|
||||
if (typeof file.data === 'string') {
|
||||
buffer = fs.readFileSync(file.data);
|
||||
} else {
|
||||
buffer = file.data;
|
||||
}
|
||||
} else {
|
||||
throw new Error('无效的文件输入');
|
||||
}
|
||||
|
||||
const workbook = xlsx.read(buffer, { type: 'buffer', codepage: 65001 });
|
||||
const worksheet = workbook.Sheets[workbook.SheetNames[0]];
|
||||
|
||||
// 获取表头和数据
|
||||
const jsonData = xlsx.utils.sheet_to_json(worksheet, { header: 1 });
|
||||
const headers = jsonData[0] as string[];
|
||||
const dataRows = jsonData.slice(1) as any[][];
|
||||
|
||||
// 查找各列的索引
|
||||
const columnIndices = {
|
||||
orderNumber: headers.indexOf('订单号'),
|
||||
logisticsCompany: headers.indexOf('物流公司'),
|
||||
trackingNumber: headers.indexOf('单号-单元格文本格式'),
|
||||
orderCreateTime: headers.indexOf('订单创建时间'),
|
||||
orderEmail: headers.indexOf('订单邮箱'),
|
||||
orderSite: headers.indexOf('订单网站'),
|
||||
name: headers.indexOf('姓名'),
|
||||
refund: headers.indexOf('退款'),
|
||||
chargeback: headers.indexOf('拒付')
|
||||
};
|
||||
|
||||
// 遍历数据行
|
||||
for (let i = 0; i < dataRows.length; i++) {
|
||||
const row = dataRows[i];
|
||||
const orderNumber = row[columnIndices.orderNumber];
|
||||
|
||||
if (!orderNumber) {
|
||||
errors.push({ identifier: `行 ${i + 2}`, error: '订单号为空' });
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
let orderNumbers="";
|
||||
if (orderNumber.includes('_')&&orderNumber.includes('-')) {
|
||||
orderNumbers = orderNumber.split('_')[0].toString();
|
||||
orderNumbers = orderNumbers.split('-')[1];
|
||||
}
|
||||
// 通过订单号查询订单
|
||||
const order = await this.orderModel.findOne({ where: { externalOrderId: orderNumbers } });
|
||||
if (order) {
|
||||
// 通过orderId查询fulfillments
|
||||
const fulfillments = await this.orderFulfillmentModel.find({ where: { order_id: order.id } });
|
||||
|
||||
if (fulfillments && fulfillments.length > 0) {
|
||||
const fulfillment = fulfillments[0]; // 假设每个订单只有一个物流信息
|
||||
// 回填物流信息
|
||||
if (columnIndices.logisticsCompany !== -1) {
|
||||
row[columnIndices.logisticsCompany] = fulfillment.shipping_provider || '';
|
||||
}
|
||||
if (columnIndices.trackingNumber !== -1) {
|
||||
row[columnIndices.trackingNumber] = fulfillment.tracking_number || '';
|
||||
}
|
||||
|
||||
updated++;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
errors.push({ identifier: `行 ${i + 2}`, error: `处理失败: ${error.message}` });
|
||||
}
|
||||
}
|
||||
|
||||
// 将数据转换为对象数组,与 exportOrder 方法返回格式一致
|
||||
const resultData = dataRows.map((row, index) => {
|
||||
const rowData: any = {};
|
||||
headers.forEach((header, colIndex) => {
|
||||
rowData[header] = row[colIndex] || '';
|
||||
});
|
||||
// 添加行号信息
|
||||
rowData['行号'] = index + 2;
|
||||
return rowData;
|
||||
});
|
||||
|
||||
|
||||
// 返回XLSX buffer内容给前端
|
||||
// const xlsxBuffer = await this.exportToXlsx(resultData, { type: 'buffer' });
|
||||
return resultData;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,10 @@
|
|||
import { Inject, Provide } from '@midwayjs/core';
|
||||
import * as fs from 'fs';
|
||||
import * as xlsx from 'xlsx';
|
||||
import { In, Like, Not, Repository } from 'typeorm';
|
||||
import { Product } from '../entity/product.entity';
|
||||
import { PaginationParams } from '../interface';
|
||||
import { paginate } from '../utils/paginate.util';
|
||||
import { Context } from '@midwayjs/koa';
|
||||
import { InjectEntityModel } from '@midwayjs/typeorm';
|
||||
import * as fs from 'fs';
|
||||
import { In, Like, Not, Repository } from 'typeorm';
|
||||
import * as xlsx from 'xlsx';
|
||||
import { BatchErrorItem, BatchOperationResult, SyncOperationResultDTO, UnifiedSearchParamsDTO } from '../dto/api.dto';
|
||||
import {
|
||||
BatchUpdateProductDTO,
|
||||
CreateProductDTO,
|
||||
|
|
@ -20,20 +18,22 @@ import {
|
|||
SizePaginatedResponse,
|
||||
StrengthPaginatedResponse,
|
||||
} from '../dto/reponse.dto';
|
||||
import { Dict } from '../entity/dict.entity';
|
||||
import { DictItem } from '../entity/dict_item.entity';
|
||||
import { ProductStockComponent } from '../entity/product_stock_component.entity';
|
||||
import { Stock } from '../entity/stock.entity';
|
||||
import { StockPoint } from '../entity/stock_point.entity';
|
||||
import { StockService } from './stock.service';
|
||||
import { TemplateService } from './template.service';
|
||||
|
||||
import { BatchErrorItem, BatchOperationResult, SyncOperationResultDTO, UnifiedSearchParamsDTO } from '../dto/api.dto';
|
||||
import { UnifiedProductDTO } from '../dto/site-api.dto';
|
||||
import { ProductSiteSkuDTO, SyncProductToSiteDTO } from '../dto/site-sync.dto';
|
||||
import { Category } from '../entity/category.entity';
|
||||
import { CategoryAttribute } from '../entity/category_attribute.entity';
|
||||
import { Dict } from '../entity/dict.entity';
|
||||
import { DictItem } from '../entity/dict_item.entity';
|
||||
import { Product } from '../entity/product.entity';
|
||||
import { ProductStockComponent } from '../entity/product_stock_component.entity';
|
||||
import { SiteSku } from '../entity/site-sku.entity';
|
||||
import { Stock } from '../entity/stock.entity';
|
||||
import { StockPoint } from '../entity/stock_point.entity';
|
||||
import { PaginationParams } from '../interface';
|
||||
import { paginate } from '../utils/paginate.util';
|
||||
import { SiteApiService } from './site-api.service';
|
||||
import { StockService } from './stock.service';
|
||||
import { TemplateService } from './template.service';
|
||||
|
||||
@Provide()
|
||||
export class ProductService {
|
||||
|
|
@ -221,7 +221,7 @@ export class ProductService {
|
|||
}
|
||||
|
||||
async findProductBySku(sku: string): Promise<Product> {
|
||||
return this.productModel.findOne({
|
||||
return await this.productModel.findOne({
|
||||
where: {
|
||||
sku,
|
||||
},
|
||||
|
|
@ -234,7 +234,8 @@ export class ProductService {
|
|||
.createQueryBuilder('product')
|
||||
.leftJoinAndSelect('product.attributes', 'attribute')
|
||||
.leftJoinAndSelect('attribute.dict', 'dict')
|
||||
.leftJoinAndSelect('product.category', 'category');
|
||||
.leftJoinAndSelect('product.category', 'category')
|
||||
.leftJoinAndSelect('product.siteSkus', 'siteSku');
|
||||
// 处理分页参数(支持新旧两种格式)
|
||||
const page = query.page || 1;
|
||||
const pageSize = query.per_page || 10;
|
||||
|
|
@ -474,7 +475,8 @@ export class ProductService {
|
|||
.createQueryBuilder('product')
|
||||
.leftJoinAndSelect('product.attributes', 'attribute')
|
||||
.leftJoinAndSelect('attribute.dict', 'dict')
|
||||
.leftJoinAndSelect('product.category', 'category');
|
||||
.leftJoinAndSelect('product.category', 'category')
|
||||
.leftJoinAndSelect('product.siteSkus', 'siteSkus');
|
||||
|
||||
// 验证分组字段
|
||||
const groupBy = query.groupBy;
|
||||
|
|
@ -870,7 +872,7 @@ export class ProductService {
|
|||
const product = new Product();
|
||||
|
||||
// 使用 merge 填充基础字段,排除特殊处理字段
|
||||
const { attributes: _attrs, categoryId: _cid, sku: _sku, components: _components, ...simpleFields } = createProductDTO;
|
||||
const { attributes: _attrs, categoryId: _cid, sku: _sku, components: _components, siteSkus: _siteSkus, ...simpleFields } = createProductDTO;
|
||||
this.productModel.merge(product, simpleFields);
|
||||
|
||||
product.attributes = resolvedAttributes;
|
||||
|
|
@ -880,6 +882,16 @@ export class ProductService {
|
|||
// 确保默认类型
|
||||
if (!product.type) product.type = 'single';
|
||||
|
||||
// 处理站点 SKU
|
||||
if (Array.isArray(_siteSkus)) {
|
||||
product.siteSkus = _siteSkus.map(sku => {
|
||||
const siteSku = new SiteSku();
|
||||
siteSku.sku = sku;
|
||||
siteSku.isOld = false;
|
||||
return siteSku;
|
||||
});
|
||||
}
|
||||
|
||||
// 生成或设置 SKU(基于属性字典项的 name 生成)
|
||||
if (sku) {
|
||||
product.sku = sku;
|
||||
|
|
@ -893,7 +905,7 @@ export class ProductService {
|
|||
if (createProductDTO.components && createProductDTO.components.length > 0) {
|
||||
await this.setProductComponents(savedProduct.id, createProductDTO.components);
|
||||
// 重新加载带组件的产品
|
||||
return await this.productModel.findOne({ where: { id: savedProduct.id }, relations: ['attributes', 'attributes.dict', 'category', 'components', 'siteSkus'] });
|
||||
return await this.productModel.findOne({ where: { id: savedProduct.id }, relations: ['attributes', 'attributes.dict', 'category', 'components'] });
|
||||
}
|
||||
|
||||
return savedProduct;
|
||||
|
|
@ -910,7 +922,7 @@ export class ProductService {
|
|||
}
|
||||
|
||||
// 使用 merge 更新基础字段,排除特殊处理字段
|
||||
const { attributes, categoryId, categoryName, sku, components, ...simpleFields } = updateProductDTO;
|
||||
const { attributes, categoryId, categoryName, sku, components, siteSkus, ...simpleFields } = updateProductDTO;
|
||||
this.productModel.merge(product, simpleFields);
|
||||
// 解析属性输入(按 id 或 dictName 创建/关联字典项)
|
||||
let categoryItem: Category | null = null;
|
||||
|
|
@ -994,6 +1006,22 @@ export class ProductService {
|
|||
product.type = updateProductDTO.type as any;
|
||||
}
|
||||
|
||||
// 处理站点 SKU 更新
|
||||
if (siteSkus !== undefined) {
|
||||
if (Array.isArray(siteSkus)) {
|
||||
// 转换为 SiteSku 实体数组
|
||||
product.siteSkus = siteSkus.map(sku => {
|
||||
const siteSku = new SiteSku();
|
||||
siteSku.sku = sku;
|
||||
siteSku.isOld = false;
|
||||
return siteSku;
|
||||
});
|
||||
} else {
|
||||
// 如果不是数组,清空siteSkus
|
||||
product.siteSkus = [];
|
||||
}
|
||||
}
|
||||
|
||||
// 保存更新后的产品
|
||||
const saved = await this.productModel.save(product);
|
||||
|
||||
|
|
@ -1001,9 +1029,12 @@ export class ProductService {
|
|||
if (updateProductDTO.components !== undefined) {
|
||||
// 如果 components 为空数组,则删除所有组件? setProductComponents 会处理
|
||||
await this.setProductComponents(saved.id, updateProductDTO.components);
|
||||
// 重新加载带组件的产品
|
||||
return await this.productModel.findOne({ where: { id: saved.id }, relations: ['attributes', 'attributes.dict', 'category', 'components'] });
|
||||
}
|
||||
|
||||
return saved;
|
||||
// 重新加载产品以获取完整的关联关系
|
||||
return await this.productModel.findOne({ where: { id: saved.id }, relations: ['attributes', 'attributes.dict', 'category'] });
|
||||
}
|
||||
|
||||
async batchUpdateProduct(
|
||||
|
|
@ -1014,13 +1045,14 @@ export class ProductService {
|
|||
throw new Error('未选择任何产品');
|
||||
}
|
||||
|
||||
// 检查 updateData 中是否有复杂字段 (attributes, categoryId, type, sku)
|
||||
// 检查 updateData 中是否有复杂字段 (attributes, categoryId, type, sku, siteSkus)
|
||||
// 如果包含复杂字段,需要复用 updateProduct 的逻辑
|
||||
const hasComplexFields =
|
||||
updateData.attributes !== undefined ||
|
||||
updateData.categoryId !== undefined ||
|
||||
updateData.type !== undefined ||
|
||||
updateData.sku !== undefined;
|
||||
updateData.sku !== undefined ||
|
||||
updateData.siteSkus !== undefined;
|
||||
|
||||
if (hasComplexFields) {
|
||||
// 循环调用 updateProduct
|
||||
|
|
@ -1032,7 +1064,7 @@ export class ProductService {
|
|||
}
|
||||
} else {
|
||||
// 简单字段,直接批量更新以提高性能
|
||||
// UpdateProductDTO 里的简单字段: name, nameCn, description, shortDescription, price, promotionPrice, image, siteSkus
|
||||
// UpdateProductDTO 里的简单字段: name, nameCn, description, shortDescription, price, promotionPrice, image
|
||||
|
||||
const simpleUpdate: any = {};
|
||||
if (updateData.name !== undefined) simpleUpdate.name = updateData.name;
|
||||
|
|
@ -1042,7 +1074,6 @@ export class ProductService {
|
|||
if (updateData.price !== undefined) simpleUpdate.price = updateData.price;
|
||||
if (updateData.promotionPrice !== undefined) simpleUpdate.promotionPrice = updateData.promotionPrice;
|
||||
if (updateData.image !== undefined) simpleUpdate.image = updateData.image;
|
||||
if (updateData.siteSkus !== undefined) simpleUpdate.siteSkus = updateData.siteSkus;
|
||||
|
||||
if (Object.keys(simpleUpdate).length > 0) {
|
||||
await this.productModel.update({ id: In(ids) }, simpleUpdate);
|
||||
|
|
@ -1923,9 +1954,10 @@ export class ProductService {
|
|||
}
|
||||
|
||||
// 导出所有产品为 CSV 文本
|
||||
async exportProductsCSV(): Promise<string> {
|
||||
async exportProductsCSV(params?: UnifiedSearchParamsDTO): Promise<string> {
|
||||
// 查询所有产品及其属性(包含字典关系)、组成和分类
|
||||
const products = await this.productModel.find({
|
||||
where: params.where,
|
||||
relations: ['attributes', 'attributes.dict', 'components', 'category'],
|
||||
order: { id: 'ASC' },
|
||||
});
|
||||
|
|
@ -2150,7 +2182,7 @@ export class ProductService {
|
|||
if (!product) {
|
||||
throw new Error(`产品 ID ${productId} 不存在`);
|
||||
}
|
||||
return product.siteSkus || [];
|
||||
return product.siteSkus.map(({sku})=>sku) || [];
|
||||
}
|
||||
|
||||
// 绑定产品的站点SKU列表
|
||||
|
|
@ -2162,9 +2194,17 @@ export class ProductService {
|
|||
const normalizedSiteSkus = (siteSkus || [])
|
||||
.map(c => String(c).trim())
|
||||
.filter(c => c.length > 0);
|
||||
product.siteSkus = normalizedSiteSkus;
|
||||
|
||||
// 更新产品的站点SKU列表
|
||||
product.siteSkus = normalizedSiteSkus.map(sku => {
|
||||
const siteSku = new SiteSku();
|
||||
siteSku.sku = sku;
|
||||
siteSku.isOld = false;
|
||||
return siteSku;
|
||||
});
|
||||
|
||||
await this.productModel.save(product);
|
||||
return product.siteSkus || [];
|
||||
return normalizedSiteSkus;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -547,7 +547,7 @@ export class StockService {
|
|||
qb
|
||||
.select([
|
||||
'ti.transferId AS transferId',
|
||||
"JSON_ARRAYAGG(JSON_OBJECT('id', ti.id, 'productName', ti.productName,'sku', ti.sku, 'quantity', ti.quantity)) AS items",
|
||||
"JSON_ARRAYAGG(JSON_OBJECT('id', ti.id, 'productName', ti.name,'sku', ti.sku, 'quantity', ti.quantity)) AS items",
|
||||
])
|
||||
.from(TransferItem, 'ti')
|
||||
.groupBy('ti.transferId'),
|
||||
|
|
|
|||
Loading…
Reference in New Issue