orderDetail.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. async searchOrderTransport({ id }) {
  19. const orderDetail = await this.model.findById(id);
  20. if (!id) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单信息');
  21. const { transport = {} } = orderDetail;
  22. const { shop_transport_no: no, shop_transport_type: type } = transport;
  23. if (!no || !type) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '缺少快递信息');
  24. const res = await this.ctx.service.util.kd100.search({ no, type });
  25. return res;
  26. }
  27. /**
  28. * 创建:用户支付完成后的订单
  29. * @param {Object} body 请求参数体
  30. * @param body.order_id 下单的id
  31. * @param {Transaction} tran 数据库事务实例
  32. */
  33. async create({ order_id }, tran) {
  34. assert(order_id, '缺少支付订单信息');
  35. const order = await this.orderModel.findById(order_id);
  36. if (!order) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到支付订单的数据');
  37. const { customer, address, goods: shopGoods, no, status, buy_time, pay, total_detail: otd } = order;
  38. if (status === '0') throw new BusinessError(ErrorCode.DATA_INVALID, '订单未支付');
  39. const orderDetailData = { customer, address, order: order_id, buy_time, pay_time: _.get(pay, 'pay_time'), status };
  40. const shopMoneyDetail = this.ctx.service.util.order.shopMoneyDetail(order);
  41. // 分订单计数器
  42. let noTimes = 1;
  43. for (const s of shopGoods) {
  44. const shop = _.get(s, 'shop');
  45. const remarks = _.get(s, 'remarks');
  46. const goodsList = _.get(s, 'goods', []);
  47. const detailNo = `${no}-${noTimes}`;
  48. const total_detail = shopMoneyDetail[shop];
  49. // 优惠部分分割
  50. if (_.get(otd, 'discount_detail')) {
  51. // 如果有优惠部分,那就得找,优惠里面有没有对应的商品规格
  52. const discount_detail = this.getGoodsListDiscountDetail(goodsList, _.get(otd, 'discount_detail'));
  53. total_detail.discount_detail = discount_detail;
  54. }
  55. noTimes++;
  56. const obj = { ...orderDetailData, shop, goods: goodsList, no: detailNo, total_detail, remarks };
  57. // #region 团购
  58. const type = _.get(order, 'type');
  59. let group = _.get(order, 'group');
  60. if (type === '1') {
  61. // 说明是团购,需要找订单中有没有团的id
  62. obj.type = '1';
  63. obj.group = group;
  64. if (!group) {
  65. // 需要创建团,这是团长支付的单子,开团;然后把id回补
  66. group = await this.ctx.service.group.group.create(obj, tran);
  67. obj.group = group;
  68. tran.update('Order', order_id, { group, type: '1' });
  69. } else {
  70. // 需要参团操作,这是团员的单子,将人加入团中
  71. await this.ctx.service.group.group.join(customer, group, tran);
  72. }
  73. }
  74. // #endregion
  75. tran.insert('OrderDetail', obj);
  76. // arr.push(obj);
  77. }
  78. // await this.model.insertMany(arr);
  79. }
  80. async afterUpdate(filter, body, data) {
  81. const status = _.get(data, 'status');
  82. if (status === '3') {
  83. // 处理积分,签收再加
  84. await this.ctx.service.user.point.addPoints(data._id);
  85. }
  86. return data;
  87. }
  88. /**
  89. * 将商品规格列表中,优惠的部分提取出来,分单用
  90. * @param {Array} goodsList 某店的商品列表
  91. * @param {Object} odd discount_detail 支付订单的优惠券设置
  92. */
  93. getGoodsListDiscountDetail(goodsList, odd) {
  94. const result = {};
  95. for (const uc_id in odd) {
  96. const detail = odd[uc_id];
  97. const obj = {};
  98. for (const g of goodsList) {
  99. const { _id } = g;
  100. const gdd = detail[_id];
  101. if (gdd) obj[_id] = gdd;
  102. }
  103. result[uc_id] = obj;
  104. }
  105. return result;
  106. }
  107. /**
  108. * 计算某店铺的金额总计
  109. * @param {Array} goodsList 商品规格列表
  110. */
  111. getTotalDetail(goodsList) {
  112. const goods_total = goodsList.reduce((p, n) => this.ctx.plus(p, this.ctx.multiply(n.sell_money, n.buy_num)), 0);
  113. const freight_total = goodsList.reduce((p, n) => this.ctx.plus(p, this.ctx.multiply(n.freight, n.buy_num)), 0);
  114. return { goods_total, freight_total };
  115. }
  116. async fetch(filter) {
  117. assert(filter);
  118. filter = await this.beforeFetch(filter);
  119. const { _id, id } = filter;
  120. if (_id || id) filter = { _id: ObjectId(_id || id) };
  121. const { populate } = this.getRefMods();
  122. let res = await this.model.findOne(filter).populate(populate).exec();
  123. res = await this.afterFetch(filter, res);
  124. return res;
  125. }
  126. async afterQuery(filter, data) {
  127. data = JSON.parse(JSON.stringify(data));
  128. for (const i of data) {
  129. const { goods } = i;
  130. const buy_num_total = goods.reduce((p, n) => this.ctx.plus(p, n.buy_num), 0);
  131. i.buy_num_total = buy_num_total;
  132. let discount = 0;
  133. const dd = _.get(i, 'total_detail.discount_detail', {});
  134. for (const uc_id in dd) {
  135. const obj = _.get(dd, uc_id, {});
  136. const values = Object.values(obj);
  137. if (!values.every(e => _.isNumber(e))) continue;
  138. const dm = values.reduce((p, n) => this.ctx.plus(p, n), 0);
  139. discount = this.ctx.plus(discount, dm);
  140. }
  141. i.real_pay = this.ctx.service.util.orderDetail.computedRealPay(i);
  142. }
  143. return data;
  144. }
  145. }
  146. module.exports = OrderDetailService;