lrf 2 years ago
parent
commit
a31c0b5b58
1 changed files with 28 additions and 1 deletions
  1. 28 1
      app/service/user/cashBack.js

+ 28 - 1
app/service/user/cashBack.js

@@ -9,6 +9,7 @@ class CashBackService extends CrudService {
   constructor(ctx) {
   constructor(ctx) {
     super(ctx, 'cashback');
     super(ctx, 'cashback');
     this.model = this.ctx.model.User.CashBack;
     this.model = this.ctx.model.User.CashBack;
+    this.configModel = this.ctx.model.System.Config;
     this.orderDetailModel = this.ctx.model.Trade.OrderDetail;
     this.orderDetailModel = this.ctx.model.Trade.OrderDetail;
   }
   }
 
 
@@ -16,6 +17,9 @@ class CashBackService extends CrudService {
     assert(customer, '缺少用户信息');
     assert(customer, '缺少用户信息');
     // status(状态) 和 source(来源) 均表示钱是入账还是出账,判断一个就行,主要还是依靠来源吧
     // status(状态) 和 source(来源) 均表示钱是入账还是出账,判断一个就行,主要还是依靠来源吧
     const res = await this.model.find({ inviter: customer }).lean();
     const res = await this.model.find({ inviter: customer }).lean();
+    const config = await this.configModel.findOne({}, { reward_day: 1 }).lean();
+    const rd = _.get(config, 'reward_day', 14);
+    const min = this.ctx.multiply(rd, this.ctx.multiply(24, 60));
     let total = 0;
     let total = 0;
     for (const i of res) {
     for (const i of res) {
       const { source, money } = i;
       const { source, money } = i;
@@ -23,7 +27,30 @@ class CashBackService extends CrudService {
       if (sn >= 0) total = this.ctx.plus(total, money);
       if (sn >= 0) total = this.ctx.plus(total, money);
       else total = this.ctx.minus(total, money);
       else total = this.ctx.minus(total, money);
     }
     }
-    return total;
+    // 计算提现金额
+    // 上面总价包含了提现金额
+    // 下面计算的金额是可以提现的金额 提现总金额 - 提现金额
+    let canGet = 0;
+    const inBill = res.filter(f => parseInt(f.source) >= 0);
+    const outBill = res.filter(f => parseInt(f.source) < 0);
+    const getOutBill = res.filter(f => f.source === '-1');
+    const outBillMoney = getOutBill.reduce((p, n) => this.ctx.plus(p, n.money), 0);
+    for (const i of inBill) {
+      const cAt = _.get(i, 'time');
+      const m = moment().diff(cAt, 'm');
+      if (m >= min) {
+        // 已经过了提款时间,那就再找下,是否有该记录相应的退款
+        const { money, source_id } = i;
+        let cg = money;
+        const os = outBill.filter(f => f.source_id === source_id);
+        for (const o of os) {
+          cg = this.ctx.minus(cg, o.money);
+        }
+        canGet = this.ctx.plus(canGet, cg);
+      }
+    }
+    canGet = this.ctx.minus(canGet, outBillMoney);
+    return { total, canGet };
   }
   }
   /**
   /**
    * 添加检查返现
    * 添加检查返现