orderDetail.js 3.2 KB

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