cashBack.js 4.7 KB

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