|
@@ -1,5 +1,5 @@
|
|
|
import { GenericCrudService } from '@d8d/shared-crud';
|
|
import { GenericCrudService } from '@d8d/shared-crud';
|
|
|
-import { DataSource, DeepPartial } from 'typeorm';
|
|
|
|
|
|
|
+import { DataSource, DeepPartial, In } from 'typeorm';
|
|
|
import { GoodsMt } from '../entities/goods.entity.mt';
|
|
import { GoodsMt } from '../entities/goods.entity.mt';
|
|
|
|
|
|
|
|
export class GoodsServiceMt extends GenericCrudService<GoodsMt> {
|
|
export class GoodsServiceMt extends GenericCrudService<GoodsMt> {
|
|
@@ -114,6 +114,7 @@ export class GoodsServiceMt extends GenericCrudService<GoodsMt> {
|
|
|
|
|
|
|
|
// 将子商品列表添加到返回结果中
|
|
// 将子商品列表添加到返回结果中
|
|
|
(goods as any).children = children;
|
|
(goods as any).children = children;
|
|
|
|
|
+ (goods as any).childGoodsIds = children.map(child => child.id);
|
|
|
} else if (goods.spuId > 0) {
|
|
} else if (goods.spuId > 0) {
|
|
|
// 子商品:获取父商品基本信息
|
|
// 子商品:获取父商品基本信息
|
|
|
// 添加租户ID过滤,确保父商品与子商品在同一租户下
|
|
// 添加租户ID过滤,确保父商品与子商品在同一租户下
|
|
@@ -128,4 +129,115 @@ export class GoodsServiceMt extends GenericCrudService<GoodsMt> {
|
|
|
|
|
|
|
|
return goods;
|
|
return goods;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 重写getList方法,批量填充父商品的childGoodsIds字段和子商品的parent对象
|
|
|
|
|
+ */
|
|
|
|
|
+ async getList(
|
|
|
|
|
+ page: number = 1,
|
|
|
|
|
+ pageSize: number = 10,
|
|
|
|
|
+ keyword?: string,
|
|
|
|
|
+ searchFields?: string[],
|
|
|
|
|
+ where?: Partial<GoodsMt>,
|
|
|
|
|
+ relations: string[] = [],
|
|
|
|
|
+ order: { [P in keyof GoodsMt]?: 'ASC' | 'DESC' } = {},
|
|
|
|
|
+ filters?: { [key: string]: any },
|
|
|
|
|
+ userId?: string | number
|
|
|
|
|
+ ): Promise<[GoodsMt[], number]> {
|
|
|
|
|
+ // 1. 先调用父类的getList获取数据
|
|
|
|
|
+ const [data, total] = await super.getList(
|
|
|
|
|
+ page, pageSize, keyword, searchFields, where, relations, order, filters, userId
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 批量查询所有父商品的子商品ID
|
|
|
|
|
+ const parentGoodsIds = data.filter(goods => goods.spuId === 0).map(goods => goods.id);
|
|
|
|
|
+ if (parentGoodsIds.length > 0) {
|
|
|
|
|
+ const childGoodsMap = await this.getChildGoodsIdsByParentIds(parentGoodsIds);
|
|
|
|
|
+
|
|
|
|
|
+ // 3. 为每个父商品设置childGoodsIds
|
|
|
|
|
+ data.forEach(goods => {
|
|
|
|
|
+ if (goods.spuId === 0) {
|
|
|
|
|
+ (goods as any).childGoodsIds = childGoodsMap.get(goods.id) || [];
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 4. 批量查询所有子商品的父商品信息
|
|
|
|
|
+ const childGoodsList = data.filter(goods => goods.spuId > 0);
|
|
|
|
|
+ if (childGoodsList.length > 0) {
|
|
|
|
|
+ const parentGoodsMap = await this.getParentGoodsByChildIds(childGoodsList);
|
|
|
|
|
+
|
|
|
|
|
+ // 5. 为每个子商品设置parent对象
|
|
|
|
|
+ data.forEach(goods => {
|
|
|
|
|
+ if (goods.spuId > 0) {
|
|
|
|
|
+ const parent = parentGoodsMap.get(goods.spuId);
|
|
|
|
|
+ if (parent) {
|
|
|
|
|
+ (goods as any).parent = parent;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return [data, total];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 辅助方法:批量查询父商品的子商品ID
|
|
|
|
|
+ */
|
|
|
|
|
+ private async getChildGoodsIdsByParentIds(parentIds: number[]): Promise<Map<number, number[]>> {
|
|
|
|
|
+ const childGoods = await this.repository.find({
|
|
|
|
|
+ where: { spuId: In(parentIds), state: 1 } as any,
|
|
|
|
|
+ select: ['id', 'spuId']
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ const map = new Map<number, number[]>();
|
|
|
|
|
+ childGoods.forEach(child => {
|
|
|
|
|
+ const parentId = child.spuId;
|
|
|
|
|
+ if (!map.has(parentId)) {
|
|
|
|
|
+ map.set(parentId, []);
|
|
|
|
|
+ }
|
|
|
|
|
+ map.get(parentId)!.push(child.id);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ return map;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 辅助方法:批量查询子商品的父商品信息
|
|
|
|
|
+ */
|
|
|
|
|
+ private async getParentGoodsByChildIds(childGoodsList: GoodsMt[]): Promise<Map<number, any>> {
|
|
|
|
|
+ // 收集所有父商品ID
|
|
|
|
|
+ const parentIds = [...new Set(childGoodsList.map(child => child.spuId))];
|
|
|
|
|
+ if (parentIds.length === 0) {
|
|
|
|
|
+ return new Map();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 假设所有商品在同一租户下(通过查询过滤)
|
|
|
|
|
+ const tenantId = childGoodsList[0]?.tenantId;
|
|
|
|
|
+ if (!tenantId) {
|
|
|
|
|
+ return new Map();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 查询父商品信息,选择与ParentGoodsSchema匹配的字段
|
|
|
|
|
+ const parentGoods = await this.repository.find({
|
|
|
|
|
+ where: { id: In(parentIds), tenantId } as any,
|
|
|
|
|
+ select: ['id', 'name', 'price', 'costPrice', 'stock', 'imageFileId', 'goodsType', 'spuId']
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ const map = new Map<number, any>();
|
|
|
|
|
+ parentGoods.forEach(parent => {
|
|
|
|
|
+ map.set(parent.id, {
|
|
|
|
|
+ id: parent.id,
|
|
|
|
|
+ name: parent.name,
|
|
|
|
|
+ price: parent.price,
|
|
|
|
|
+ costPrice: parent.costPrice,
|
|
|
|
|
+ stock: parent.stock,
|
|
|
|
|
+ imageFileId: parent.imageFileId,
|
|
|
|
|
+ goodsType: parent.goodsType,
|
|
|
|
|
+ spuId: parent.spuId // 父商品的spuId总是0
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ return map;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|