orderDetail.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 OrderDetailService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'orderdetail');
  11. this.model = this.ctx.model.Trade.OrderDetail;
  12. this.orderModel = this.ctx.model.Trade.Order;
  13. }
  14. /**
  15. * 创建:用户支付完成后的订单
  16. * @param {Object} body 请求参数体
  17. * @param body.order_id 下单的id
  18. * @param {Transaction} tran 数据库事务实例
  19. */
  20. async create({ order_id }, tran) {
  21. assert(order_id, '缺少支付订单信息');
  22. // if (!tran) throw new BusinessError(ErrorCode.DATA_INVALID, '支付成功添加积分:缺少事务参数');
  23. const order = await this.orderModel.findById(order_id);
  24. if (!order) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到支付订单的数据');
  25. const { customer, address, goods, no, status, buy_time, pay, total_detail: otd } = order;
  26. if (status === '0') throw new BusinessError(ErrorCode.DATA_INVALID, '订单未支付');
  27. const orderDetailData = { customer, address, order: order_id, buy_time, pay_time: _.get(pay, 'pay_time'), status };
  28. // 补全 shop, goods, total_detail;
  29. // const arr = [];
  30. // 分订单计数器
  31. let noTimes = 1;
  32. for (const s of goods) {
  33. const shop = _.get(s, 'shop');
  34. const goodsList = _.get(s, 'goods', []);
  35. const detailNo = `${no}-${noTimes}`;
  36. const total_detail = this.getTotalDetail(goodsList);
  37. // 优惠部分分割
  38. if (_.get(otd, 'discount_detail')) {
  39. // 如果有优惠部分,那就得找,优惠里面有没有对应的商品规格
  40. const discount_detail = this.getGoodsListDiscountDetail(goodsList, _.get(otd, 'discount_detail'));
  41. total_detail.discount_detail = discount_detail;
  42. }
  43. noTimes++;
  44. const obj = { ...orderDetailData, shop, goods: goodsList, no: detailNo, total_detail };
  45. tran.insert('OrderDetail', obj);
  46. // arr.push(obj);
  47. }
  48. // await this.model.insertMany(arr);
  49. }
  50. /**
  51. * 将商品规格列表中,优惠的部分提取出来,分单用
  52. * @param {Array} goodsList 某店的商品列表
  53. * @param {Object} odd discount_detail 支付订单的优惠券设置
  54. */
  55. getGoodsListDiscountDetail(goodsList, odd) {
  56. const result = {};
  57. for (const uc_id in odd) {
  58. const detail = odd[uc_id];
  59. const obj = {};
  60. for (const g of goodsList) {
  61. const { _id } = g;
  62. const gdd = detail[_id];
  63. if (gdd) obj[_id] = gdd;
  64. }
  65. result[uc_id] = obj;
  66. }
  67. return result;
  68. }
  69. /**
  70. * 计算某店铺的金额总计
  71. * @param {Array} goodsList 商品规格列表
  72. */
  73. getTotalDetail(goodsList) {
  74. const goods_total = goodsList.reduce((p, n) => this.ctx.plus(p, this.ctx.multiply(n.sell_money, n.buy_num)), 0);
  75. const freight_total = goodsList.reduce((p, n) => this.ctx.plus(p, this.ctx.multiply(n.freight, n.buy_num)), 0);
  76. return { goods_total, freight_total };
  77. }
  78. async fetch(filter) {
  79. assert(filter);
  80. filter = await this.beforeFetch(filter);
  81. const { _id, id } = filter;
  82. if (_id || id) filter = { _id: ObjectId(_id || id) };
  83. const { populate } = this.getRefMods();
  84. let res = await this.model.findOne(filter).populate(populate).exec();
  85. res = await this.afterFetch(filter, res);
  86. return res;
  87. }
  88. }
  89. module.exports = OrderDetailService;