lrf 2 years ago
parent
commit
65ed5f5630

+ 3 - 0
app/controller/shop/config/.shopInBill.js

@@ -37,4 +37,7 @@ module.exports = {
       count: true,
     },
   },
+  computedTotal: {
+    params: ['!shop'],
+  },
 };

+ 36 - 0
app/service/shop/shopInBill.js

@@ -12,6 +12,7 @@ class ShopInBillService extends CrudService {
     this.model = this.ctx.model.Shop.ShopInBill;
     this.shopModel = this.ctx.model.Shop.Shop;
     this.orderUtil = this.ctx.service.util.orderDetail;
+    this.configModel = this.ctx.model.System.Config;
   }
   /**
    * 正常购物创建店铺流水
@@ -57,6 +58,41 @@ class ShopInBillService extends CrudService {
     else obj.receipts = limit;
     tran.insert('ShopInBill', obj);
   }
+
+  async computedTotal({ shop }) {
+    const list = await this.model.find({ shop }).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;
+    for (const i of list) {
+      const { source, receipts: money } = i;
+      const sn = parseInt(source);
+      if (sn >= 0) total = this.ctx.plus(total, money);
+      else total = this.ctx.minus(total, money);
+    }
+    let canGet = 0;
+    const inBill = list.filter(f => parseInt(f.source) >= 0);
+    const outBill = list.filter(f => parseInt(f.source) < 0);
+    const getOutBill = list.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 { receipts: 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.receipts);
+        }
+        canGet = this.ctx.plus(canGet, cg);
+      }
+    }
+    canGet = this.ctx.minus(canGet, outBillMoney);
+    return { total, canGet };
+  }
 }
 
 module.exports = ShopInBillService;

+ 1 - 0
app/z_router/shop/shopInBill.js

@@ -7,6 +7,7 @@ const rkey = 'shopInBill';
 const ckey = 'shop.shopInBill';
 const keyZh = '店铺流水信息';
 const routes = [
+  { method: 'post', path: `${rkey}/total/:shop`, controller: `${ckey}.computedTotal`, name: `${ckey}computedTotal`, zh: `${keyZh}-计算金额` },
   { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
   { method: 'get', path: `${rkey}/:id`, controller: `${ckey}.show`, name: `${ckey}Show`, zh: `${keyZh}查询` },
   // { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, name: `${ckey}Create`, zh: `创建${keyZh}` },