cashBack.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. const moment = require('moment');
  7. //
  8. class CashBackService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'cashback');
  11. this.model = this.ctx.model.User.CashBack;
  12. this.configModel = this.ctx.model.System.Config;
  13. this.orderDetailModel = this.ctx.model.Trade.OrderDetail;
  14. this.userModel = this.ctx.model.User.User;
  15. }
  16. async computedTotal({ customer }) {
  17. assert(customer, '缺少用户信息');
  18. // status(状态) 和 source(来源) 均表示钱是入账还是出账,判断一个就行,主要还是依靠来源吧
  19. const res = await this.model.find({ inviter: customer }).lean();
  20. const config = await this.configModel.findOne({}, { reward_day: 1 }).lean();
  21. const rd = _.get(config, 'reward_day', 14);
  22. const min = this.ctx.multiply(rd, this.ctx.multiply(24, 60));
  23. let total = 0;
  24. for (const i of res) {
  25. const { status, money } = i;
  26. const sn = parseInt(status);
  27. if (sn >= 0) total = this.ctx.plus(total, money);
  28. else total = this.ctx.minus(total, money);
  29. }
  30. // 计算提现金额
  31. // 上面总价包含了提现金额
  32. // 下面计算的金额是可以提现的金额 提现总金额 - 提现金额
  33. let canGet = 0;
  34. const inBill = res.filter(f => parseInt(f.status) >= 0);
  35. const outBill = res.filter(f => parseInt(f.status) < 0);
  36. // const getOutBill = res.filter(f => f.status === '-1');
  37. // const outBillMoney = getOutBill.reduce((p, n) => this.ctx.plus(p, n.money), 0);
  38. for (const i of inBill) {
  39. const cAt = _.get(i, 'time');
  40. const m = moment().diff(cAt, 'm');
  41. if (m >= min) {
  42. // 已经过了提款时间,那就再找下,是否有该记录相应的退款
  43. const { money, source_id } = i;
  44. let cg = money;
  45. const os = outBill.filter(f => f.source_id === source_id);
  46. for (const o of os) {
  47. cg = this.ctx.minus(cg, o.money);
  48. }
  49. canGet = this.ctx.plus(canGet, cg);
  50. }
  51. }
  52. // canGet = this.ctx.minus(canGet, outBillMoney);
  53. return { total, canGet };
  54. }
  55. /**
  56. * 添加检查返现
  57. * @param {String} orderDetail_id 订单详情id
  58. * @param {Transaction} tran 数据库事务
  59. */
  60. async create(orderDetail_id, tran) {
  61. console.log(orderDetail_id);
  62. return {name:'1'}
  63. // const { populate } = this.ctx.service.trade.orderDetail.getRefMods();
  64. // const orderDetail = await this.orderDetailModel.findById(orderDetail_id).populate(populate);
  65. // if (!orderDetail) return;
  66. // // 找到商品的返现类型
  67. // let rmoney = 0;
  68. // const { goods: goodsSpec, type } = orderDetail;
  69. // // 根据订单类型判断应该取哪个价钱,为下面的百分比计算取值用
  70. // let priceKey;
  71. // if (type === '1') priceKey = 'ggrp';
  72. // else priceKey = 'grp';
  73. // const moneyDetail = this.ctx.service.util.orderDetail.moneyDetail(orderDetail);
  74. // for (const gs of goodsSpec) {
  75. // const goods = _.get(gs, 'goods');
  76. // if (!goods) continue;
  77. // const { is_cashBack, cb_config } = goods;
  78. // // 0:返现(使用)
  79. // if (is_cashBack !== '0') continue;
  80. // const type = _.get(cb_config, 'back_type');
  81. // if (!type) continue;
  82. // const money = _.get(cb_config, 'money');
  83. // // 返现/比例小于等于 0则不需要进行
  84. // if (this.ctx.minus(money, 0) <= 0) continue;
  85. // if (type === 'fixed') rmoney = this.ctx.plus(rmoney, money);
  86. // else if (type === 'percent') {
  87. // const specId = _.get(gs, '_id');
  88. // const realPay = _.get(moneyDetail, `${specId}.${priceKey}`);
  89. // const grm = this.ctx.multiply(realPay, this.ctx.divide(money, 100));
  90. // rmoney = this.ctx.plus(rmoney, grm);
  91. // }
  92. // }
  93. // const { inviter, _id: order_detail } = orderDetail;
  94. // if (!inviter) return;
  95. // // 查询用户是否是会员,若不是会员不返现
  96. // const leaderInfo = await this.userModel.findById(inviter, { is_leader });
  97. // return leaderInfo;
  98. // if(leaderInfo.is_leader !=='0') return;
  99. // const obj = { inviter: _.get(inviter, '_id'), source_id: order_detail, source: '0', money: rmoney, time: moment().format('YYYY-MM-DD HH:mm:ss') };
  100. // // 检查是否已经返利
  101. // const num = await this.model.count({ inviter: _.get(inviter, '_id'), source_id: order_detail, source: '0' });
  102. // if (num > 0) return;
  103. // tran.insert('CashBack', obj);
  104. }
  105. /**
  106. * 退货退流水
  107. * @param {Object} afterSale 修改前的售后数据
  108. * @param {Transaction} tran 数据库事务
  109. */
  110. async refund(afterSale, tran) {
  111. const source_id = _.get(afterSale, 'order_detail');
  112. const inBill = await this.model.findOne({ source_id }).lean();
  113. if (!inBill) return;
  114. const outBill = _.pick(inBill, [ 'inviter', 'money', 'source', 'source_id' ]);
  115. outBill.time = moment().format('YYYY-MM-DD HH:mm:ss');
  116. outBill.status = '-1';
  117. tran.insert('CashBack', outBill);
  118. }
  119. }
  120. module.exports = CashBackService;