lrf 2 年之前
父節點
當前提交
3b72998c8f
共有 2 個文件被更改,包括 28 次插入4 次删除
  1. 2 2
      app/service/trade/order.js
  2. 26 2
      app/service/user/cashBack.js

+ 2 - 2
app/service/trade/order.js

@@ -95,8 +95,8 @@ class OrderService extends CrudService {
         orderData.group = group;
       }
       // #endregion
-      // 5.返现部分:邀请人
-      orderData.inviter = inviter;
+      // 5.返现部分:邀请人: 自己发链接自己买不行
+      if (customer !== inviter) orderData.inviter = inviter;
 
       // 生成数据
       const order_id = this.tran.insert('Order', orderData);

+ 26 - 2
app/service/user/cashBack.js

@@ -20,10 +20,34 @@ class CashBackService extends CrudService {
     const { populate } = this.ctx.service.trade.orderDetail.getRefMods();
     const orderDetail = await this.orderDetailModel.findById(orderDetail_id).populate(populate);
     if (!orderDetail) return;
-    const money = this.ctx.service.util.orderDetail.computedRealPay(orderDetail);
+    // 找到商品的返现类型
+    let rmoney = 0;
+    const { goods: goodsSpec, type } = orderDetail;
+    // 根据订单类型判断应该取哪个价钱,为下面的百分比计算取值用
+    let priceKey;
+    if (type === '1') priceKey = 'ggrp';
+    else priceKey = 'grp';
+    const moneyDetail = this.ctx.service.util.orderDetail.moneyDetail(orderDetail);
+    for (const gs of goodsSpec) {
+      const goods = _.get(gs, 'goods');
+      if (!goods) continue;
+      const { is_cashBack, cb_config } = goods;
+      // 0:返现(使用)
+      if (is_cashBack !== '0') continue;
+      const type = _.get(cb_config, 'back_type');
+      if (!type) continue;
+      const money = _.get(cb_config, 'money');
+      if (type === 'fixed') rmoney = this.ctx.plus(rmoney, money);
+      else if (type === 'percent') {
+        const specId = _.get(gs, '_id');
+        const realPay = _.get(moneyDetail, `${specId}.${priceKey}`);
+        const grm = this.ctx.multiply(realPay, this.ctx.divide(money, 10));
+        rmoney = this.ctx.plus(rmoney, grm);
+      }
+    }
     const { inviter, _id: order_detail } = orderDetail;
     if (!inviter) return;
-    const obj = { inviter, order_detail, money, time: moment().format('YYYY-MM-DD HH:mm:ss') };
+    const obj = { inviter, order_detail, money: rmoney, time: moment().format('YYYY-MM-DD HH:mm:ss') };
     tran.insert('CashBack', obj);
   }
 }