trade.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 TradeService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'trade');
  10. this.redis = this.app.redis;
  11. this.redisKey = this.app.config.redisKey;
  12. this.redisTimeout = this.app.config.redisTimeout;
  13. this.shopModel = this.ctx.model.Shop.Shop;
  14. this.goodsModel = this.ctx.model.Shop.Goods;
  15. this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
  16. this.cartModel = this.ctx.model.Trade.Cart;
  17. }
  18. /**
  19. * 检查选中的购物车是否符合购买的条件
  20. * @param {Object} body 请求体
  21. * @param body.cartIds 选中的购物车id列表
  22. * @param {Boolean} makeCache 生成缓存
  23. */
  24. async checkCartBuy({ cartIds }, makeCache = true) {
  25. const list = await this.cartModel.find({ _id: cartIds });
  26. let result = { result: true };
  27. for (const i of list) {
  28. const res = await this.checkCanBuy(i, false);
  29. if (res.result !== true) {
  30. result = res;
  31. break;
  32. }
  33. }
  34. if (makeCache) {
  35. const key = await this.makeOrderKey(cartIds);
  36. result.key = key;
  37. }
  38. return result;
  39. }
  40. /**
  41. * 检测是否可以购买该物品
  42. * @param {Object} param 查询条件
  43. * @param param.shop 商店id
  44. * @param param.goods 商品id
  45. * @param param.goodsSpec 商品规格id
  46. * @param param.num 购买数量
  47. * @param param.type 订单类型: 0:常规单 1:团购单,需要按团购价计算
  48. * @param param.group 团id
  49. * @param param.inviter 邀请用户id 返现部分
  50. * @param makeCache 生成缓存
  51. */
  52. async checkCanBuy({ shop, goods, goodsSpec, num, type = '0', group, inviter }, makeCache = true) {
  53. console.log(inviter);
  54. assert(shop, '缺少店铺信息');
  55. assert(goods, '缺少商品信息');
  56. assert(goodsSpec, '缺少商品规格信息');
  57. assert(num, '缺少购买数量');
  58. const result = { result: true };
  59. // 1.检查商店是否正常运行
  60. const shopData = await this.shopModel.findById(shop);
  61. const shopRes = this.checkShop(shopData);
  62. if (shopRes.result !== true) return shopRes;
  63. // 2.检查商品是否可以购买
  64. const goodsData = await this.goodsModel.findById(goods);
  65. const goodsRes = this.checkGoods(goodsData);
  66. if (shopRes.result !== true) return goodsRes;
  67. // 3.检验该规格是否可以购买
  68. const goodsSpecData = await this.goodsSpecModel.findById(goodsSpec);
  69. const gsRes = this.checkGoodsSpec(goodsSpecData, num);
  70. if (gsRes.result !== true) return gsRes;
  71. // #region 团购部分
  72. // 4.检查是否是团购单
  73. if (type === '1' && group) {
  74. // 为团购单,但是没有团id,团长开团.不需要检查
  75. // 为团购单,且有团id: 团员参团
  76. const groupRes = await this.ctx.service.group.group.checkGroupCanJoin({ id: group });
  77. if (groupRes.result !== true) return groupRes;
  78. }
  79. // #endregion
  80. if (makeCache) {
  81. const key = await this.makeOrderKey({ shop, goods, goodsSpec, num, type, group, inviter });
  82. result.key = key;
  83. }
  84. return result;
  85. }
  86. /**
  87. * 检查商店是否满足条件
  88. * @param {Object} shopData 店铺数据
  89. */
  90. checkShop(shopData) {
  91. const result = { result: true };
  92. if (!shopData) {
  93. result.msg = '未找到店铺';
  94. result.result = false;
  95. return result;
  96. }
  97. if (shopData.status !== '1') {
  98. result.msg = '店铺不处于营业状态';
  99. result.result = false;
  100. return result;
  101. }
  102. return result;
  103. }
  104. /**
  105. * 检查商品是否满足条件
  106. * @param {Object} goodsData 商品数据
  107. */
  108. checkGoods(goodsData) {
  109. const result = { result: true };
  110. if (!goodsData) {
  111. result.msg = '未找到商品';
  112. result.result = false;
  113. return result;
  114. }
  115. if (goodsData.status === '0') {
  116. result.msg = '该商品已下架';
  117. result.result = false;
  118. return result;
  119. }
  120. return result;
  121. }
  122. /**
  123. * 检查商品的规格是否满足购买条件
  124. * @param {Object} goodsSpecData 商品规格的数据
  125. * @param {Number} num 购买数量
  126. */
  127. checkGoodsSpec(goodsSpecData, num) {
  128. const result = { result: true };
  129. if (!goodsSpecData) {
  130. result.msg = '未找到商品的指定规格';
  131. result.result = false;
  132. return result;
  133. }
  134. if (goodsSpecData.status !== '0') {
  135. result.msg = '该规格的商品已下架';
  136. result.result = false;
  137. return result;
  138. }
  139. if (goodsSpecData.num < num) {
  140. result.msg = '库存量不足';
  141. result.result = false;
  142. return result;
  143. }
  144. return result;
  145. }
  146. // 随机字符串产生函数
  147. createNonceStr() {
  148. return Math.random().toString(36).substr(2, 15);
  149. }
  150. // 生成key
  151. async makeOrderKey(data) {
  152. const str = this.createNonceStr();
  153. const key = `${this.redisKey.orderKeyPrefix}${str}`;
  154. const value = JSON.stringify(data);
  155. await this.redis.set(key, value, 'EX', this.redisTimeout);
  156. return str;
  157. }
  158. }
  159. module.exports = TradeService;