zrOrder.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // 店铺信息
  28. const shopInfo = await this.shopModel.findById(shop);
  29. // 商品信息
  30. const goodsInfo = await this.goodsModel.findById(goods);
  31. //
  32. }
  33. /**
  34. * 检查是否可以购买商品,并生成缓存
  35. * @param {Object} body 参数体
  36. * @param body.shop 店铺id
  37. * @param body.goods 尊荣商品id
  38. * @param body.num 购买数量
  39. * @param {Boolean} makeCache 是否生成缓存
  40. */
  41. async checkCanBuy({ shop, goods, num }, makeCache = true) {
  42. const result = { result: true };
  43. const shopInfo = await this.shopModel.findById(shop);
  44. if (!shopInfo || _.get(shopInfo, '1')) {
  45. result.result = false;
  46. result.msg = '店铺当前不处于营业状态';
  47. return result;
  48. }
  49. const goodsInfo = await this.goodsModel.findById(goods);
  50. if (!goodsInfo) {
  51. result.result = false;
  52. result.msg = '未找到尊荣商品';
  53. return result;
  54. }
  55. const user = this.ctx.user;
  56. const customer = _.get(user, '_id');
  57. if (!customer) {
  58. result.result = false;
  59. result.msg = '未找到用户信息';
  60. }
  61. // #region 库存判断
  62. const knum = _.get(goodsInfo, 'num');
  63. const res = this.ctx.minus(knum, num);
  64. if (res <= 0) {
  65. result.result = false;
  66. result.msg = '库存不足';
  67. return result;
  68. }
  69. // #endregion
  70. // #region 积分判断
  71. const pointTotal = await this.pointComputedTotal({ customer });
  72. // 计算购买所需积分
  73. const cost = _.get(goodsInfo, 'cost', 0);
  74. if (cost === 0) {
  75. result.result = false;
  76. result.msg = '商品设置的尊荣有误,无法进行购买';
  77. return result;
  78. }
  79. const costTotal = this.ctx.multiply(cost, num);
  80. const afterCost = this.ctx.minus(pointTotal, costTotal);
  81. if (afterCost < 0) {
  82. result.result = false;
  83. result.msg = '您的积分不足';
  84. return result;
  85. }
  86. // #endregion
  87. if (makeCache) {
  88. const key = await this.makeOrderKey({ shop, goods, num });
  89. result.key = key;
  90. }
  91. return result;
  92. }
  93. // 生成key
  94. async makeOrderKey(data) {
  95. const str = this.createNonceStr();
  96. const key = `${this.redisKey.orderKeyPrefix}${str}`;
  97. const value = JSON.stringify(data);
  98. await this.redis.set(key, value, 'EX', this.redisTimeout);
  99. return str;
  100. }
  101. /**
  102. * 计算用户的积分
  103. * @param {Object} data 数据对象
  104. * @param data.customer 用户数据id
  105. */
  106. async pointComputedTotal({ customer }) {
  107. assert(customer, '缺少用户信息');
  108. const res = await this.pointModel.find({ customer });
  109. const total = res.reduce((p, n) => {
  110. let point = n.point;
  111. if (!(n.source === '0' || n.source === '1')) point = -point;
  112. return this.ctx.plus(p, point);
  113. }, 0);
  114. return total;
  115. }
  116. // 随机字符串产生函数
  117. createNonceStr() {
  118. return Math.random().toString(36).substr(2, 15);
  119. }
  120. }
  121. module.exports = ZrOrderService;