lrf 2 năm trước cách đây
mục cha
commit
a0a474f969
1 tập tin đã thay đổi với 36 bổ sung14 xóa
  1. 36 14
      app/service/trade/afterSale.js

+ 36 - 14
app/service/trade/afterSale.js

@@ -36,8 +36,8 @@ class AfterSaleService extends CrudService {
     if (!orderDetail) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单信息');
     // 查看该商品是否已经申请售后
     let goods;
-    const aData = await this.model.findOne({ order_detail, set_id, 'goods._id': goods_id, status: { $nin: [ '!1', '!2', '!3', '!4', '!5' ] } });
-    if (aData) throw new BusinessError(ErrorCode.DATA_EXISTED, '该商品已有正在处理中的售后申请.请勿重复申请');
+    const hasData = await this.model.count({ order_detail, set_id, 'goods._id': goods_id, status: { $nin: [ '!1', '!2', '!3', '!4', '!5' ] } });
+    if (hasData > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该商品已有正在处理中的售后申请.请勿重复申请');
     if (type !== '4' && type !== '5') {
       const { goods: goodsList } = orderDetail;
       goods = goodsList.find(f => {
@@ -74,7 +74,7 @@ class AfterSaleService extends CrudService {
     }
   }
 
-  async update(filter, update, { projection } = {}) {
+  async update(filter, update) {
     assert(filter);
     assert(update);
     const { _id, id } = filter;
@@ -168,12 +168,12 @@ class AfterSaleService extends CrudService {
       }
       // 修改数据
       this.tran.update('AfterSale', entity._id, update);
-      // await this.tran.run();
+      await this.tran.run();
       // 退钱
       if (!refundInfo) return;
       // console.log(refundInfo);
-      // const res = await this.ctx.service.trade.pay.refund(refundInfo);
-      // if (res.errcode && res.errcode !== 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, res.errmsg);
+      const res = await this.ctx.service.trade.pay.refund(refundInfo);
+      if (res.errcode && res.errcode !== 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, res.errmsg);
     } catch (error) {
       console.error(error);
       await this.tran.rollback();
@@ -191,18 +191,26 @@ class AfterSaleService extends CrudService {
    * @param {Transaction} tran 数据库事务
    */
   async toReturnMoney(data, update, tran) {
-    const { order_detail, goods } = data;
+    const { order_detail, goods, set_id } = data;
     const orderDetail = await this.orderDetailModel.findById(order_detail);
     if (!orderDetail) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单信息');
     // 取出订单详情的每种商品规格的价格明细
     const moneyDetail = this.ctx.service.util.orderDetail.moneyDetail(orderDetail);
     // 根据订单类型使用价格的key
     const priceKey = 'grp';
-    // 商品实际支付的金额
-    const goodsRealPay = _.get(moneyDetail, `${goods._id}.${priceKey}`);
+    let returnMoney = 0;
+    let goodsRealPay = 0;
+    if (!set_id) {
+      // 商品实际支付的金额
+      goodsRealPay = _.get(moneyDetail, `${goods._id}.${priceKey}`);
+    } else {
+      // 商品实际支付的金额
+      goodsRealPay = _.get(moneyDetail, `${set_id}.${priceKey}`);
+    }
     // 需要退还的金额,如果传来的数据有金额,就使用传来的,没有的话就用原来的
-    const returnMoney = _.get(update, 'money', _.get(data, 'money'));
+    returnMoney = _.get(update, 'money', _.get(data, 'money'));
     if (goodsRealPay < returnMoney) throw new BusinessError(ErrorCode.DATA_INVALID, '退款金额超出该商品支付的金额');
+
     // 组成退款单号
     const { order: order_id } = orderDetail;
     const order = await this.orderModel.findById(order_id);
@@ -289,10 +297,24 @@ class AfterSaleService extends CrudService {
     // 将当前数据添加进去
     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' });
+      const { set_id } = gs;
+      if (!set_id) {
+        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' });
+        }
+      } else {
+        const { goods } = gs;
+        for (const i of goods) {
+          const { spec } = i;
+          const goods_id = _.get(spec, '_id');
+          const r = asList.find(f => f.set_id === set_id && _.get(f, 'goods._id') === goods_id);
+          if (r) {
+            const finishList = [ '-1', '-2', '-3', '-4', '-5' ];
+            if (finishList.includes(r.status)) fl.push({ set_id, goods: goods_id, status: 'finish' });
+          }
+        }
       }
     }
     fl = _.uniqBy(fl, 'goods');