lrf 2 years ago
parent
commit
7a9a9ca329
1 changed files with 28 additions and 1 deletions
  1. 28 1
      app/service/shop/goodsJoinAct.js

+ 28 - 1
app/service/shop/goodsJoinAct.js

@@ -12,10 +12,36 @@ class GoodsJoinActService extends CrudService {
     this.model = this.ctx.model.Shop.GoodsJoinAct;
     this.goodsModel = this.ctx.model.Shop.Goods;
     this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
+    this.platformActModel = this.ctx.model.System.PlatformAct;
   }
-  // TODO 需要接口: 检测商品/规格 是否已经处于同种优惠方式的活动中,有就不能添加;
+  // 需要接口: 检测商品/规格 是否已经处于同种优惠方式的活动中,有就不能添加;
   // 可以存在多个不同种优惠方式的活动中: 满减,满折,赠品=> √; 满折,满折 => ×
   // 需要检查活动是否处于活动中,如果在活动中的,则不能添加
+  async toCheckCanCreate(data) {
+    const { spec, platform_act } = data;
+    // 获取当前活动类型
+    const platformAct = await this.platformActModel.findById(platform_act);
+    if (!platformAct) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到活动信息');
+    const { type } = platformAct;
+    // 找到该商品出现过的活动类型是否冲突
+    const pipeline = [];
+    // 找到该 规格 参与同活动类型的商品,再确定这些商品的活动是否正在进行,最后做个数量查询,如果有,就说明有冲突
+    pipeline.push({ $match: { 'spec._id': spec, platform_act_type: type } });
+    pipeline.push({ $addFields: { act_id: { $toObjectId: '$platform_act' } } });
+    pipeline.push({
+      $lookup: {
+        from: 'platformAct',
+        localField: 'act_id',
+        foreignField: '_id',
+        pipeline: [{ $match: { is_use: '0' } }],
+        as: 'act',
+      },
+    });
+    pipeline.push({ $unwind: '$act' });
+    pipeline.push({ $count: 'total' });
+    const list = await this.model.aggregate(pipeline);
+    if (list.length > 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, '该规格,已参加 正在进行中 的同类活动,不能添加');
+  }
 
   async create(data) {
     assert(data);
@@ -23,6 +49,7 @@ class GoodsJoinActService extends CrudService {
     const query = { 'goods._id': goods, 'spec._id': spec, shop, platform_act };
     const num = await this.model.count(query);
     if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该商品已添加进当前活动中');
+    await this.toCheckCanCreate(data);
     // 将商品和规格 变为 快照数据
     const goodsInfo = await this.goodsModel.findById(goods);
     if (!goodsInfo) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到商品信息');