lrf il y a 2 ans
Parent
commit
eb4441df5b
1 fichiers modifiés avec 35 ajouts et 1 suppressions
  1. 35 1
      app/service/trade/afterSale.js

+ 35 - 1
app/service/trade/afterSale.js

@@ -88,6 +88,7 @@ class AfterSaleService extends CrudService {
         // 3.修改数据, 直接修改成退完款的状态.如果后面退款失败了.直接回滚了
         update.status = '-1';
         update.end_time = moment().format('YYYY-MM-DD HH:mm:ss');
+        await this.refundOrder(entity, this.tran);
       } else if (type === '2') {
         // 退货(退库存),退款,退优惠券
         // 做记录,但是不是结束状态,都不退
@@ -96,7 +97,8 @@ class AfterSaleService extends CrudService {
           refundInfo = await this.toReturnMoney(entity, update, this.tran);
           // 2.检查是否退优惠券
           await this.toReturnCoupons(entity, update, this.tran);
-          // TODO: 3.退库存, 有待商讨
+          // 3.修改订单状态
+          await this.refundOrder(entity, this.tran);
         }
       } else if (type === '3') {
         // 换货,不需要退款
@@ -241,6 +243,38 @@ class AfterSaleService extends CrudService {
    */
   async toReturnStock(data, tran) {}
 
+  /**
+   * 售后:修改拆分订单
+   * @param {Object} data 售后修改前的数据
+   * @param {Transaction} tran 数据库事务
+   */
+  async refundOrder(data, tran) {
+    const { order_detail } = data;
+    const orderDetail = await this.orderDetailModel.findById(order_detail);
+    if (!orderDetail) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单信息');
+    // 获取拆分订单的数据,并找到商品列表
+    const goodsList = _.get(orderDetail, 'goods');
+    // 依次找这些商品是否都售后完成,都售后完成,就改订单状态
+    const asList = await this.model.find({ order_detail, status: { $nin: [ '0', '!1', '!2', '!3', '!4', '!5' ] } });
+    let status;
+    const fl = [];
+    // 将当前数据添加进去
+    fl.push({ goods: _.get(data, 'goods._id'), status: 'finish' });
+    for (const gs of goodsList) {
+      const r = asList.find(f => ObjectId(_.get(f, 'goods._id')).equals(_.get(gs, '_id')));
+      if (r) {
+        const finishList = [ '-1', '-2', '-3', '-4', '-5' ];
+        if (finishList.includes(r.status)) fl.push({ goods: gs._id, status: 'finish' });
+      }
+    }
+    // 说明所有的商品 都有 已处理的售后
+    if (fl.length === goodsList.length) status = '-4';
+    // 有状态码,则修改订单的状态
+    if (status) {
+      tran.update('OrderDetail', order_detail, { status });
+    }
+  }
+
   /**
    * 售后 取消订单 & 拒收处理完成
    * @param {Object} data 售后修改前的数据