order.js 6.5 KB

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