order.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. //
  8. class OrderService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'order');
  11. this.orderModel = this.ctx.model.Trade.Order;
  12. this.shopModel = this.ctx.model.Shop.Shop;
  13. this.goodsRateModel = this.ctx.model.Shop.GoodsRate;
  14. this.afterSaleModel = this.ctx.model.Trade.AfterSale;
  15. }
  16. async allOrder(query = {}) {
  17. const { skip, limit } = query;
  18. const customer = _.get(this.ctx, 'user._id');
  19. if (!customer) throw new BusinessError(ErrorCode.NOT_LOGIN);
  20. const pipeline = [];
  21. const step1 = [
  22. { $addFields: { order_id: { $toString: '$_id' } } },
  23. {
  24. $lookup: {
  25. from: 'orderDetail',
  26. localField: 'order_id',
  27. foreignField: 'order',
  28. as: 'orderDetails',
  29. },
  30. },
  31. ];
  32. pipeline.push(...step1);
  33. const step2 = [
  34. {
  35. $project: {
  36. data: {
  37. $cond: {
  38. if: { $gt: [{ $size: '$orderDetails' }, 0] },
  39. then: '$orderDetails',
  40. else: '$$CURRENT',
  41. },
  42. },
  43. },
  44. },
  45. ];
  46. pipeline.push(...step2);
  47. pipeline.push({ $unwind: '$data' });
  48. pipeline.push({ $replaceRoot: { newRoot: '$data' } });
  49. pipeline.push({ $match: { customer } });
  50. const qp = _.cloneDeep(pipeline);
  51. qp.push({ $sort: { 'meta.createdAt': -1 } });
  52. if (skip && limit) qp.push({ $skip: parseInt(skip) }, { $limit: parseInt(limit) });
  53. const data = await this.orderModel.aggregate(qp);
  54. const tr = await this.orderModel.aggregate([...pipeline, { $count: 'total' }]);
  55. const newArr = [];
  56. for (const i of data) {
  57. const { pay, goods } = i;
  58. let obj = {};
  59. if (pay) {
  60. // 这个是order表的数据,需要用order的计算方式
  61. const buy_num_total = goods.reduce(
  62. (p, n) =>
  63. this.ctx.plus(
  64. p,
  65. n.goods.reduce((np, ng) => this.ctx.plus(np, ng.buy_num), 0)
  66. ),
  67. 0
  68. );
  69. obj.buy_num_total = buy_num_total;
  70. obj.real_pay = _.get(i, 'pay.pay_money');
  71. const shopData = _.pick(_.head(goods), ['shop', 'shop_name']);
  72. obj.shop = { _id: shopData.shop, name: shopData.shop_name };
  73. const specs = [];
  74. for (const shop of goods) {
  75. const { goods: specGoods } = shop;
  76. for (const sg of specGoods) {
  77. const spec = _.pick(sg, ['_id', 'name', 'file', 'act', 'buy_num', 'gift', 'price', 'freight', 'sp_price']);
  78. if (!spec.price) spec.price = _.get(sg, 'sell_money');
  79. const goods = _.pick(sg.goods, ['_id', 'name', 'file']);
  80. spec.goods = goods;
  81. specs.push(spec);
  82. }
  83. }
  84. obj.spec = specs;
  85. const others = _.pick(i, ['_id', 'status', 'is_afterSale', 'is_rate', 'price', 'total_detail', 'type']);
  86. obj = { ...obj, ...others, is_order: true };
  87. } else {
  88. // 是orderDetail表的数据,需要用orderDetail的计算方式
  89. const real_pay = this.ctx.service.util.orderDetail.computedRealPay(i);
  90. obj.real_pay = real_pay;
  91. obj.buy_num_total = goods.reduce((p, n) => this.ctx.plus(p, n.buy_num), 0);
  92. const others = _.pick(i, ['_id', 'status', 'is_afterSale', 'is_rate', 'price', 'total_detail', 'type', 'transport', 'transport_type']);
  93. const shop = await this.shopModel.findById(i.shop, { name: 1 }).lean();
  94. obj.shop = shop;
  95. const afterSale = await this.afterSaleModel.find({ order_detail: i._id });
  96. const rate = await this.goodsRateModel.find({ orderDetail: i._id });
  97. const specs = [];
  98. for (const sg of goods) {
  99. const spec = _.pick(sg, ['_id', 'name', 'file', 'act', 'buy_num', 'gift', 'price', 'freight', 'sp_price']);
  100. if (!spec.price) spec.price = _.get(sg, 'sell_money');
  101. const goods = _.pick(sg.goods, ['_id', 'name', 'file']);
  102. spec.goods = goods;
  103. const r = afterSale.find((f) => ObjectId(_.get(f, 'goods._id')).equals(sg._id));
  104. if (r) spec.is_afterSale = true;
  105. else spec.is_afterSale = false;
  106. const r2 = rate.find((f) => ObjectId(_.get(f, 'goodsSpec')).equals(sg._id));
  107. if (r2) {
  108. spec.is_rate = true;
  109. spec.rate = r2._id;
  110. } else spec.is_rate = false;
  111. specs.push(spec);
  112. }
  113. obj.spec = specs;
  114. obj = { ...obj, ...others, is_order: false };
  115. }
  116. newArr.push(obj);
  117. }
  118. const total = _.get(_.head(tr), 'total', 0);
  119. return { data: newArr, total };
  120. }
  121. }
  122. module.exports = OrderService;