order.js 4.5 KB

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