goods.service.mt.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { GenericCrudService } from '@d8d/shared-crud';
  2. import { DataSource, DeepPartial } from 'typeorm';
  3. import { GoodsMt } from '../entities/goods.entity.mt';
  4. export class GoodsServiceMt extends GenericCrudService<GoodsMt> {
  5. constructor(dataSource: DataSource) {
  6. super(dataSource, GoodsMt, {
  7. userTracking: {
  8. createdByField: 'createdBy',
  9. updatedByField: 'updatedBy'
  10. },
  11. relationFields: {
  12. slideImageIds: {
  13. relationName: 'slideImages',
  14. targetEntity: Object // 这里需要替换为实际的File实体
  15. }
  16. },
  17. tenantOptions: {
  18. enabled: true,
  19. tenantIdField: 'tenantId'
  20. }
  21. });
  22. }
  23. /**
  24. * 重写create方法,处理子商品分类继承
  25. */
  26. async create(data: DeepPartial<GoodsMt>, userId?: string | number): Promise<GoodsMt> {
  27. // 如果是子商品(spuId > 0)且没有指定分类ID,从父商品继承分类信息
  28. if (data.spuId && data.spuId > 0) {
  29. // 检查是否缺少分类ID(值为0或未定义)
  30. const needsCategoryInheritance =
  31. (!data.categoryId1 || data.categoryId1 === 0) ||
  32. (!data.categoryId2 || data.categoryId2 === 0) ||
  33. (!data.categoryId3 || data.categoryId3 === 0);
  34. if (needsCategoryInheritance) {
  35. // 获取父商品信息
  36. const parentGoods = await this.repository.findOne({
  37. where: { id: data.spuId } as any,
  38. select: ['categoryId1', 'categoryId2', 'categoryId3', 'goodsType', 'supplierId', 'merchantId']
  39. });
  40. if (parentGoods) {
  41. console.debug('从父商品继承分类信息:', {
  42. parentId: data.spuId,
  43. parentCategories: {
  44. categoryId1: parentGoods.categoryId1,
  45. categoryId2: parentGoods.categoryId2,
  46. categoryId3: parentGoods.categoryId3
  47. },
  48. childCategories: {
  49. categoryId1: data.categoryId1,
  50. categoryId2: data.categoryId2,
  51. categoryId3: data.categoryId3
  52. }
  53. });
  54. // 继承父商品的分类信息(只有当子商品没有指定或为0时才继承)
  55. if (!data.categoryId1 || data.categoryId1 === 0) {
  56. data.categoryId1 = parentGoods.categoryId1;
  57. }
  58. if (!data.categoryId2 || data.categoryId2 === 0) {
  59. data.categoryId2 = parentGoods.categoryId2;
  60. }
  61. if (!data.categoryId3 || data.categoryId3 === 0) {
  62. data.categoryId3 = parentGoods.categoryId3;
  63. }
  64. // 可选:继承其他字段(如果子商品没有指定)
  65. if (!data.goodsType) {
  66. data.goodsType = parentGoods.goodsType;
  67. }
  68. if (!data.supplierId && parentGoods.supplierId) {
  69. data.supplierId = parentGoods.supplierId;
  70. }
  71. if (!data.merchantId && parentGoods.merchantId) {
  72. data.merchantId = parentGoods.merchantId;
  73. }
  74. console.debug('继承后的分类信息:', {
  75. categoryId1: data.categoryId1,
  76. categoryId2: data.categoryId2,
  77. categoryId3: data.categoryId3
  78. });
  79. } else {
  80. console.debug('父商品不存在,无法继承分类信息:', data.spuId);
  81. }
  82. }
  83. }
  84. // 调用父类的create方法
  85. return super.create(data, userId);
  86. }
  87. /**
  88. * 重写getById方法,增强父子商品详情支持
  89. */
  90. async getById(id: number, relations: string[] = [], userId?: string | number): Promise<GoodsMt | null> {
  91. // 先调用父类的getById方法获取商品详情
  92. const goods = await super.getById(id, relations, userId);
  93. if (!goods) {
  94. return null;
  95. }
  96. // 根据商品类型添加额外的父子商品信息
  97. if (goods.spuId === 0) {
  98. // 父商品:获取子商品列表
  99. const children = await this.repository.find({
  100. where: { spuId: id, state: 1 } as any,
  101. relations: ['category1', 'category2', 'category3', 'supplier', 'merchant', 'imageFile'],
  102. order: { sort: 'ASC', createdAt: 'ASC' }
  103. });
  104. // 将子商品列表添加到返回结果中
  105. (goods as any).children = children;
  106. } else if (goods.spuId > 0) {
  107. // 子商品:获取父商品基本信息
  108. // 添加租户ID过滤,确保父商品与子商品在同一租户下
  109. const parent = await this.repository.findOne({
  110. where: { id: goods.spuId, tenantId: goods.tenantId } as any,
  111. select: ['id', 'name', 'price', 'costPrice', 'stock', 'imageFileId', 'goodsType']
  112. });
  113. // 将父商品信息添加到返回结果中
  114. (goods as any).parent = parent;
  115. }
  116. return goods;
  117. }
  118. }