| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- import { GenericCrudService } from '@d8d/shared-crud';
- import { DataSource, DeepPartial } from 'typeorm';
- import { GoodsMt } from '../entities/goods.entity.mt';
- export class GoodsServiceMt extends GenericCrudService<GoodsMt> {
- constructor(dataSource: DataSource) {
- super(dataSource, GoodsMt, {
- userTracking: {
- createdByField: 'createdBy',
- updatedByField: 'updatedBy'
- },
- relationFields: {
- slideImageIds: {
- relationName: 'slideImages',
- targetEntity: Object // 这里需要替换为实际的File实体
- }
- },
- tenantOptions: {
- enabled: true,
- tenantIdField: 'tenantId'
- }
- });
- }
- /**
- * 重写create方法,处理子商品分类继承
- */
- async create(data: DeepPartial<GoodsMt>, userId?: string | number): Promise<GoodsMt> {
- // 如果是子商品(spuId > 0)且没有指定分类ID,从父商品继承分类信息
- if (data.spuId && data.spuId > 0) {
- // 检查是否缺少分类ID(值为0或未定义)
- const needsCategoryInheritance =
- (!data.categoryId1 || data.categoryId1 === 0) ||
- (!data.categoryId2 || data.categoryId2 === 0) ||
- (!data.categoryId3 || data.categoryId3 === 0);
- if (needsCategoryInheritance) {
- // 获取父商品信息
- const parentGoods = await this.repository.findOne({
- where: { id: data.spuId } as any,
- select: ['categoryId1', 'categoryId2', 'categoryId3', 'goodsType', 'supplierId', 'merchantId']
- });
- if (parentGoods) {
- console.debug('从父商品继承分类信息:', {
- parentId: data.spuId,
- parentCategories: {
- categoryId1: parentGoods.categoryId1,
- categoryId2: parentGoods.categoryId2,
- categoryId3: parentGoods.categoryId3
- },
- childCategories: {
- categoryId1: data.categoryId1,
- categoryId2: data.categoryId2,
- categoryId3: data.categoryId3
- }
- });
- // 继承父商品的分类信息(只有当子商品没有指定或为0时才继承)
- if (!data.categoryId1 || data.categoryId1 === 0) {
- data.categoryId1 = parentGoods.categoryId1;
- }
- if (!data.categoryId2 || data.categoryId2 === 0) {
- data.categoryId2 = parentGoods.categoryId2;
- }
- if (!data.categoryId3 || data.categoryId3 === 0) {
- data.categoryId3 = parentGoods.categoryId3;
- }
- // 可选:继承其他字段(如果子商品没有指定)
- if (!data.goodsType) {
- data.goodsType = parentGoods.goodsType;
- }
- if (!data.supplierId && parentGoods.supplierId) {
- data.supplierId = parentGoods.supplierId;
- }
- if (!data.merchantId && parentGoods.merchantId) {
- data.merchantId = parentGoods.merchantId;
- }
- console.debug('继承后的分类信息:', {
- categoryId1: data.categoryId1,
- categoryId2: data.categoryId2,
- categoryId3: data.categoryId3
- });
- } else {
- console.debug('父商品不存在,无法继承分类信息:', data.spuId);
- }
- }
- }
- // 调用父类的create方法
- return super.create(data, userId);
- }
- /**
- * 重写getById方法,增强父子商品详情支持
- */
- async getById(id: number, relations: string[] = [], userId?: string | number): Promise<GoodsMt | null> {
- // 先调用父类的getById方法获取商品详情
- const goods = await super.getById(id, relations, userId);
- if (!goods) {
- return null;
- }
- // 根据商品类型添加额外的父子商品信息
- if (goods.spuId === 0) {
- // 父商品:获取子商品列表
- const children = await this.repository.find({
- where: { spuId: id, state: 1 } as any,
- relations: ['category1', 'category2', 'category3', 'supplier', 'merchant', 'imageFile'],
- order: { sort: 'ASC', createdAt: 'ASC' }
- });
- // 将子商品列表添加到返回结果中
- (goods as any).children = children;
- } else if (goods.spuId > 0) {
- // 子商品:获取父商品基本信息
- // 添加租户ID过滤,确保父商品与子商品在同一租户下
- const parent = await this.repository.findOne({
- where: { id: goods.spuId, tenantId: goods.tenantId } as any,
- select: ['id', 'name', 'price', 'costPrice', 'stock', 'imageFileId', 'goodsType']
- });
- // 将父商品信息添加到返回结果中
- (goods as any).parent = parent;
- }
- return goods;
- }
- }
|