cashBack.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. const { populate } = this.ctx.service.trade.orderDetail.getRefMods();
  62. const orderDetail = await this.orderDetailModel.findById(orderDetail_id).populate(populate);
  63. if (!orderDetail) return;
  64. // 找到商品的返现类型
  65. let rmoney = 0;
  66. const { goods: goodsSpec, type } = orderDetail;
  67. // 根据订单类型判断应该取哪个价钱,为下面的百分比计算取值用
  68. let priceKey;
  69. if (type === '1') priceKey = 'ggrp';
  70. else priceKey = 'grp';
  71. const moneyDetail = this.ctx.service.util.orderDetail.moneyDetail(orderDetail);
  72. for (const gs of goodsSpec) {
  73. const goods = _.get(gs, 'goods');
  74. if (!goods) continue;
  75. const { is_cashBack, cb_config } = goods;
  76. // 0:返现(使用)
  77. if (is_cashBack !== '0') continue;
  78. const type = _.get(cb_config, 'back_type');
  79. if (!type) continue;
  80. const money = _.get(cb_config, 'money');
  81. // 返现/比例小于等于 0则不需要进行
  82. if (this.ctx.minus(money, 0) <= 0) continue;
  83. if (type === 'fixed') rmoney = this.ctx.plus(rmoney, money);
  84. else if (type === 'percent') {
  85. const specId = _.get(gs, '_id');
  86. const realPay = _.get(moneyDetail, `${specId}.${priceKey}`);
  87. const grm = this.ctx.multiply(realPay, this.ctx.divide(money, 100));
  88. rmoney = this.ctx.plus(rmoney, grm);
  89. }
  90. }
  91. const { inviter, _id: order_detail } = orderDetail;
  92. if (!inviter) return;
  93. // 查询用户是否是会员,若不是会员不返现
  94. const inviter_id = _.get(inviter, "_id");
  95. const leaderInfo = await this.userModel.findById(inviter_id, { is_leader });
  96. if(leaderInfo.is_leader !=='0') return;
  97. const obj = { inviter: _.get(inviter, '_id'), source_id: order_detail, source: '0', money: rmoney, time: moment().format('YYYY-MM-DD HH:mm:ss') };
  98. // 检查是否已经返利
  99. const num = await this.model.count({ inviter: _.get(inviter, '_id'), source_id: order_detail, source: '0' });
  100. if (num > 0) return;
  101. tran.insert('CashBack', obj);
  102. }
  103. /**
  104. * 退货退流水
  105. * @param {Object} afterSale 修改前的售后数据
  106. * @param {Transaction} tran 数据库事务
  107. */
  108. async refund(afterSale, tran) {
  109. const source_id = _.get(afterSale, 'order_detail');
  110. const inBill = await this.model.findOne({ source_id }).lean();
  111. if (!inBill) return;
  112. const outBill = _.pick(inBill, [ 'inviter', 'money', 'source', 'source_id' ]);
  113. outBill.time = moment().format('YYYY-MM-DD HH:mm:ss');
  114. outBill.status = '-1';
  115. tran.insert('CashBack', outBill);
  116. }
  117. }
  118. module.exports = CashBackService;