order.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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, is_set = '1' } = shop;
  76. if (is_set === '1') {
  77. for (const sg of specGoods) {
  78. const spec = _.pick(sg, [ '_id', 'name', 'file', 'act', 'buy_num', 'gift', 'price', 'freight', 'sp_price' ]);
  79. if (!spec.price) spec.price = _.get(sg, 'sell_money');
  80. const goods = _.pick(sg.goods, [ '_id', 'name', 'file' ]);
  81. spec.goods = goods;
  82. specs.push(spec);
  83. }
  84. } else {
  85. const goods = _.get(shop, 'goods', []);
  86. const obj = _.omit(shop, [ 'goods', 'meta' ]);
  87. const newGoods = goods.map(i => {
  88. const ngo = {
  89. goods: { name: _.get(i, 'goods.name'), file: _.get(i, 'goods.file') },
  90. spec: { name: _.get(i, 'spec.name'), file: _.get(i, 'spec.file') },
  91. set_num: _.get(i, 'set_num'),
  92. };
  93. return ngo;
  94. });
  95. obj.goods = newGoods;
  96. specs.push(obj);
  97. }
  98. }
  99. obj.spec = specs;
  100. const others = _.pick(i, [ '_id', 'status', 'is_afterSale', 'is_rate', 'price', 'total_detail', 'type' ]);
  101. obj = { ...obj, ...others, is_order: true };
  102. } else {
  103. // 是orderDetail表的数据,需要用orderDetail的计算方式
  104. const real_pay = this.ctx.service.util.orderDetail.computedRealPay(i);
  105. obj.real_pay = real_pay;
  106. obj.buy_num_total = goods.reduce((p, n) => this.ctx.plus(p, n.buy_num), 0);
  107. const others = _.pick(i, [ '_id', 'status', 'is_afterSale', 'is_rate', 'price', 'total_detail', 'type', 'transport', 'transport_type' ]);
  108. const shop = await this.shopModel.findById(i.shop, { name: 1 }).lean();
  109. obj.shop = shop;
  110. const afterSale = await this.afterSaleModel.find({ order_detail: i._id });
  111. const rate = await this.goodsRateModel.find({ orderDetail: i._id });
  112. const specs = [];
  113. for (const sg of goods) {
  114. const { is_set = '1' } = sg;
  115. if (is_set === '1') {
  116. const spec = _.pick(sg, [ '_id', 'name', 'file', 'act', 'buy_num', 'gift', 'price', 'freight', 'sp_price' ]);
  117. if (!spec.price) spec.price = _.get(sg, 'sell_money');
  118. const goods = _.pick(sg.goods, [ '_id', 'name', 'file' ]);
  119. spec.goods = goods;
  120. const r = afterSale.find(f => ObjectId(_.get(f, 'goods._id')).equals(sg._id));
  121. if (r) spec.is_afterSale = true;
  122. else spec.is_afterSale = false;
  123. const r2 = rate.find(f => ObjectId(_.get(f, 'goodsSpec')).equals(sg._id));
  124. if (r2) {
  125. spec.is_rate = true;
  126. spec.rate = r2._id;
  127. } else spec.is_rate = false;
  128. specs.push(spec);
  129. } else {
  130. const goods = _.get(sg, 'goods', []);
  131. const obj = _.omit(sg, [ 'goods', 'meta' ]);
  132. const newGoods = goods.map(i => {
  133. const ngo = {
  134. goods: { name: _.get(i, 'goods.name'), file: _.get(i, 'goods.file') },
  135. spec: { name: _.get(i, 'spec.name'), file: _.get(i, 'spec.file') },
  136. set_num: _.get(i, 'set_num'),
  137. };
  138. const r = afterSale.find(f => _.get(f, 'goods._id') === _.get(i, 'spec._id'));
  139. if (r) ngo.is_afterSale = true;
  140. else ngo.is_afterSale = false;
  141. const r2 = rate.find(f => _.get(f, 'goodsSpec') === _.get(i, 'spec._id'));
  142. if (r2) {
  143. ngo.is_rate = true;
  144. ngo.rate = r2._id;
  145. } else ngo.is_rate = false;
  146. return ngo;
  147. });
  148. obj.goods = newGoods;
  149. specs.push(obj);
  150. }
  151. }
  152. obj.spec = specs;
  153. obj = { ...obj, ...others, is_order: false };
  154. }
  155. newArr.push(obj);
  156. }
  157. const total = _.get(_.head(tr), 'total', 0);
  158. return { data: newArr, total };
  159. }
  160. }
  161. module.exports = OrderService;