orderDetail.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. //
  7. class OrderDetailService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'orderdetail');
  10. }
  11. /**
  12. * 计算订单详情实际支付金额
  13. * @param {Object} data 订单详情数据
  14. * @return {Number} 实际支付金额
  15. */
  16. async computedRealPay(data) {
  17. const { goods = [], total_detail = {} } = data;
  18. let total = goods.reduce((p, n) => {
  19. const price = this.ctx.plus(n.sell_money, n.freight);
  20. const goodsTotal = this.ctx.multiply(price, n.buy_num);
  21. return this.ctx.plus(p, goodsTotal);
  22. }, 0);
  23. let dmt = 0;
  24. const discount_detail = _.get(total_detail, 'discount_detail', {});
  25. for (const uc_id in discount_detail) {
  26. const detail = _.get(discount_detail, uc_id, {});
  27. const values = Object.values(detail);
  28. const dm = values.reduce((p, n) => this.ctx.plus(p, n), 0);
  29. dmt = this.ctx.plus(dmt, dm);
  30. }
  31. total = this.ctx.minus(total, dmt);
  32. if (total <= 0) total = 0;
  33. return total;
  34. }
  35. }
  36. module.exports = OrderDetailService;