orderDetail.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 { ObjectId } = require('mongoose').Types;
  7. const Transaction = require('mongoose-transactions');
  8. //
  9. class OrderDetailService extends CrudService {
  10. constructor(ctx) {
  11. super(ctx, 'orderdetail');
  12. this.model = this.ctx.model.Trade.OrderDetail;
  13. this.orderModel = this.ctx.model.Trade.Order;
  14. this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
  15. this.userCouponModel = this.ctx.model.User.UserCoupon;
  16. this.tran = new Transaction();
  17. }
  18. /**
  19. * 创建:用户支付完成后的订单
  20. * @param {Object} body 请求参数体
  21. * @param body.order_id 下单的id
  22. * @param {Transaction} tran 数据库事务实例
  23. */
  24. async create({ order_id }, tran) {
  25. assert(order_id, '缺少支付订单信息');
  26. // if (!tran) throw new BusinessError(ErrorCode.DATA_INVALID, '支付成功添加积分:缺少事务参数');
  27. const order = await this.orderModel.findById(order_id);
  28. if (!order) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到支付订单的数据');
  29. const { customer, address, goods, no, status, buy_time, pay, total_detail: otd } = order;
  30. if (status === '0') throw new BusinessError(ErrorCode.DATA_INVALID, '订单未支付');
  31. const orderDetailData = { customer, address, order: order_id, buy_time, pay_time: _.get(pay, 'pay_time'), status };
  32. // 补全 shop, goods, total_detail;
  33. // const arr = [];
  34. // 分订单计数器
  35. let noTimes = 1;
  36. for (const s of goods) {
  37. const shop = _.get(s, 'shop');
  38. const remarks = _.get(s, 'remarks');
  39. const goodsList = _.get(s, 'goods', []);
  40. const detailNo = `${no}-${noTimes}`;
  41. const total_detail = this.getTotalDetail(goodsList);
  42. // 优惠部分分割
  43. if (_.get(otd, 'discount_detail')) {
  44. // 如果有优惠部分,那就得找,优惠里面有没有对应的商品规格
  45. const discount_detail = this.getGoodsListDiscountDetail(goodsList, _.get(otd, 'discount_detail'));
  46. total_detail.discount_detail = discount_detail;
  47. }
  48. noTimes++;
  49. const obj = { ...orderDetailData, shop, goods: goodsList, no: detailNo, total_detail, remarks };
  50. tran.insert('OrderDetail', obj);
  51. // arr.push(obj);
  52. }
  53. // await this.model.insertMany(arr);
  54. }
  55. /**
  56. * 将商品规格列表中,优惠的部分提取出来,分单用
  57. * @param {Array} goodsList 某店的商品列表
  58. * @param {Object} odd discount_detail 支付订单的优惠券设置
  59. */
  60. getGoodsListDiscountDetail(goodsList, odd) {
  61. const result = {};
  62. for (const uc_id in odd) {
  63. const detail = odd[uc_id];
  64. const obj = {};
  65. for (const g of goodsList) {
  66. const { _id } = g;
  67. const gdd = detail[_id];
  68. if (gdd) obj[_id] = gdd;
  69. }
  70. result[uc_id] = obj;
  71. }
  72. return result;
  73. }
  74. /**
  75. * 计算某店铺的金额总计
  76. * @param {Array} goodsList 商品规格列表
  77. */
  78. getTotalDetail(goodsList) {
  79. const goods_total = goodsList.reduce((p, n) => this.ctx.plus(p, this.ctx.multiply(n.sell_money, n.buy_num)), 0);
  80. const freight_total = goodsList.reduce((p, n) => this.ctx.plus(p, this.ctx.multiply(n.freight, n.buy_num)), 0);
  81. return { goods_total, freight_total };
  82. }
  83. async fetch(filter) {
  84. assert(filter);
  85. filter = await this.beforeFetch(filter);
  86. const { _id, id } = filter;
  87. if (_id || id) filter = { _id: ObjectId(_id || id) };
  88. const { populate } = this.getRefMods();
  89. let res = await this.model.findOne(filter).populate(populate).exec();
  90. res = await this.afterFetch(filter, res);
  91. return res;
  92. }
  93. async afterQuery(filter, data) {
  94. data = JSON.parse(JSON.stringify(data));
  95. for (const i of data) {
  96. const { goods } = i;
  97. const buy_num_total = goods.reduce((p, n) => this.ctx.plus(p, n.buy_num), 0);
  98. i.buy_num_total = buy_num_total;
  99. let discount = 0;
  100. const dd = _.get(i, 'total_detail.discount_detail', {});
  101. for (const uc_id in dd) {
  102. const obj = _.get(dd, uc_id, {});
  103. const values = Object.values(obj);
  104. if (!values.every(e => _.isNumber(e))) continue;
  105. const dm = values.reduce((p, n) => this.ctx.plus(p, n), 0);
  106. discount = this.ctx.plus(discount, dm);
  107. }
  108. i.real_pay = this.ctx.minus(_.get(i, 'total_detail.goods_total'), discount);
  109. }
  110. return data;
  111. }
  112. }
  113. module.exports = OrderDetailService;