zrOrder.js 3.8 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. //
  7. class ZrOrderService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'zrorder');
  10. this.redis = this.app.redis;
  11. this.redisKey = this.app.config.redisKey;
  12. this.redisTimeout = this.app.config.redisTimeout;
  13. this.model = this.ctx.model.ZrOrder;
  14. this.goodsModel = this.ctx.model.ZrGoods;
  15. this.pointModel = this.ctx.model.Base.Point;
  16. this.shopModel = this.ctx.model.Base.Shop;
  17. }
  18. // 进入订单页前,通过函数判断,存储是否可以购买 指定商品 的 指定数量
  19. // 下单,走同样的函数判断.
  20. // 直接生成订单,扣除积分
  21. async toMakeOrder({ key }) {
  22. key = `${this.redisKey.orderKeyPrefix}${key}`;
  23. let data = await this.redis.get(key);
  24. if (!data) throw new BusinessError(ErrorCode.SERVICE_FAULT, '请求超时,请重新进入下单页');
  25. data = JSON.parse(data);
  26. const { shop, goods, num: buy_num } = data;
  27. const shopInfo = await this.shopModel.findById(shop);
  28. // const goodsInfo = await this.
  29. }
  30. /**
  31. * 检查是否可以购买商品,并生成缓存
  32. * @param {Object} body 参数体
  33. * @param body.shop 店铺id
  34. * @param body.goods 尊荣商品id
  35. * @param body.num 购买数量
  36. * @param {Boolean} makeCache 是否生成缓存
  37. */
  38. async checkCanBuy({ shop, goods, num }, makeCache = true) {
  39. const result = { result: true };
  40. const shopInfo = await this.shopModel.findById(shop);
  41. if (!shopInfo || _.get(shopInfo, '1')) {
  42. result.result = false;
  43. result.msg = '店铺当前不处于营业状态';
  44. return result;
  45. }
  46. const goodsInfo = await this.goodsModel.findById(goods);
  47. if (!goodsInfo) {
  48. result.result = false;
  49. result.msg = '未找到尊荣商品';
  50. return result;
  51. }
  52. const user = this.ctx.user;
  53. const customer = _.get(user, '_id');
  54. if (!customer) {
  55. result.result = false;
  56. result.msg = '未找到用户信息';
  57. }
  58. // #region 库存判断
  59. const knum = _.get(goodsInfo, 'num');
  60. const res = this.ctx.minus(knum, num);
  61. if (res <= 0) {
  62. result.result = false;
  63. result.msg = '库存不足';
  64. return result;
  65. }
  66. // #endregion
  67. // #region 积分判断
  68. const pointTotal = await this.pointComputedTotal({ customer });
  69. // 计算购买所需积分
  70. const cost = _.get(goodsInfo, 'cost', 0);
  71. if (cost === 0) {
  72. result.result = false;
  73. result.msg = '商品设置的尊荣有误,无法进行购买';
  74. return result;
  75. }
  76. const costTotal = this.ctx.multiply(cost, num);
  77. const afterCost = this.ctx.minus(pointTotal, costTotal);
  78. if (afterCost < 0) {
  79. result.result = false;
  80. result.msg = '您的积分不足';
  81. return result;
  82. }
  83. // #endregion
  84. if (makeCache) {
  85. const key = await this.makeOrderKey({ shop, goods, num });
  86. result.key = key;
  87. }
  88. return result;
  89. }
  90. // 生成key
  91. async makeOrderKey(data) {
  92. const str = this.createNonceStr();
  93. const key = `${this.redisKey.orderKeyPrefix}${str}`;
  94. const value = JSON.stringify(data);
  95. await this.redis.set(key, value, 'EX', this.redisTimeout);
  96. return str;
  97. }
  98. /**
  99. * 计算用户的积分
  100. * @param {Object} data 数据对象
  101. * @param data.customer 用户数据id
  102. */
  103. async pointComputedTotal({ customer }) {
  104. assert(customer, '缺少用户信息');
  105. const res = await this.pointModel.find({ customer });
  106. const total = res.reduce((p, n) => {
  107. let point = n.point;
  108. if (!(n.source === '0' || n.source === '1')) point = -point;
  109. return this.ctx.plus(p, point);
  110. }, 0);
  111. return total;
  112. }
  113. }
  114. module.exports = ZrOrderService;