guhongwei 2 роки тому
батько
коміт
b99e91c033
1 змінених файлів з 40 додано та 43 видалено
  1. 40 43
      app/service/user/cashBack.js

+ 40 - 43
app/service/user/cashBack.js

@@ -59,49 +59,46 @@ class CashBackService extends CrudService {
    * @param {Transaction} tran 数据库事务
    */
   async create(orderDetail_id, tran) {
-    console.log(orderDetail_id);
-    return {name:'1'}
-    // const { populate } = this.ctx.service.trade.orderDetail.getRefMods();
-    // const orderDetail = await this.orderDetailModel.findById(orderDetail_id).populate(populate);
-    // if (!orderDetail) return;
-    // // 找到商品的返现类型
-    // 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');
-    //   // 返现/比例小于等于 0则不需要进行
-    //   if (this.ctx.minus(money, 0) <= 0) continue;
-    //   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, 100));
-    //     rmoney = this.ctx.plus(rmoney, grm);
-    //   }
-    // }
-    // const { inviter, _id: order_detail } = orderDetail;
-    // if (!inviter) return;
-    // // 查询用户是否是会员,若不是会员不返现
-    // const leaderInfo = await this.userModel.findById(inviter, { is_leader });
-    // return leaderInfo;
-    // if(leaderInfo.is_leader !=='0') return;
-    // const obj = { inviter: _.get(inviter, '_id'), source_id: order_detail, source: '0', money: rmoney, time: moment().format('YYYY-MM-DD HH:mm:ss') };
-    // // 检查是否已经返利
-    // const num = await this.model.count({ inviter: _.get(inviter, '_id'), source_id: order_detail, source: '0' });
-    // if (num > 0) return;
-    // tran.insert('CashBack', obj);
+    const { populate } = this.ctx.service.trade.orderDetail.getRefMods();
+    const orderDetail = await this.orderDetailModel.findById(orderDetail_id).populate(populate);
+    if (!orderDetail) return;
+    // 找到商品的返现类型
+    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');
+      // 返现/比例小于等于 0则不需要进行
+      if (this.ctx.minus(money, 0) <= 0) continue;
+      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, 100));
+        rmoney = this.ctx.plus(rmoney, grm);
+      }
+    }
+    const { inviter, _id: order_detail } = orderDetail;
+    if (!inviter) return;
+    // 查询用户是否是会员,若不是会员不返现
+    const leaderInfo = await this.userModel.findById(inviter, { is_leader });
+    if(leaderInfo.is_leader !=='0') return;
+    const obj = { inviter: _.get(inviter, '_id'), source_id: order_detail, source: '0', money: rmoney, time: moment().format('YYYY-MM-DD HH:mm:ss') };
+    // 检查是否已经返利
+    const num = await this.model.count({ inviter: _.get(inviter, '_id'), source_id: order_detail, source: '0' });
+    if (num > 0) return;
+    tran.insert('CashBack', obj);
   }
   /**
    * 退货退流水