trade.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 makeCache 生成缓存
  50. */
  51. async checkCanBuy({ shop, goods, goodsSpec, num, type = '0', group }, makeCache = true) {
  52. assert(shop, '缺少店铺信息');
  53. assert(goods, '缺少商品信息');
  54. assert(goodsSpec, '缺少商品规格信息');
  55. assert(num, '缺少购买数量');
  56. const result = { result: true };
  57. // 1.检查商店是否正常运行
  58. const shopData = await this.shopModel.findById(shop);
  59. const shopRes = this.checkShop(shopData);
  60. if (shopRes.result !== true) return shopRes;
  61. // 2.检查商品是否可以购买
  62. const goodsData = await this.goodsModel.findById(goods);
  63. const goodsRes = this.checkGoods(goodsData);
  64. if (shopRes.result !== true) return goodsRes;
  65. // 3.检验该规格是否可以购买
  66. const goodsSpecData = await this.goodsSpecModel.findById(goodsSpec);
  67. const gsRes = this.checkGoodsSpec(goodsSpecData, num);
  68. if (gsRes.result !== true) return gsRes;
  69. // #region 团购部分
  70. // 4.检查是否是团购单
  71. if (type === '1' && group) {
  72. // 为团购单,但是没有团id,团长开团.不需要检查
  73. // 为团购单,且有团id: 团员参团
  74. const groupRes = await this.ctx.service.group.group.checkGroupCanJoin({ id: group });
  75. if (groupRes.result !== true) return groupRes;
  76. }
  77. // #endregion
  78. if (makeCache) {
  79. const key = await this.makeOrderKey({ shop, goods, goodsSpec, num, type, group });
  80. result.key = key;
  81. }
  82. return result;
  83. }
  84. /**
  85. * 检查商店是否满足条件
  86. * @param {Object} shopData 店铺数据
  87. */
  88. checkShop(shopData) {
  89. const result = { result: true };
  90. if (!shopData) {
  91. result.msg = '未找到店铺';
  92. result.result = false;
  93. return result;
  94. }
  95. if (shopData.status !== '1') {
  96. result.msg = '店铺不处于营业状态';
  97. result.result = false;
  98. return result;
  99. }
  100. return result;
  101. }
  102. /**
  103. * 检查商品是否满足条件
  104. * @param {Object} goodsData 商品数据
  105. */
  106. checkGoods(goodsData) {
  107. const result = { result: true };
  108. if (!goodsData) {
  109. result.msg = '未找到商品';
  110. result.result = false;
  111. return result;
  112. }
  113. if (goodsData.status === '0') {
  114. result.msg = '该商品已下架';
  115. result.result = false;
  116. return result;
  117. }
  118. return result;
  119. }
  120. /**
  121. * 检查商品的规格是否满足购买条件
  122. * @param {Object} goodsSpecData 商品规格的数据
  123. * @param {Number} num 购买数量
  124. */
  125. checkGoodsSpec(goodsSpecData, num) {
  126. const result = { result: true };
  127. if (!goodsSpecData) {
  128. result.msg = '未找到商品的指定规格';
  129. result.result = false;
  130. return result;
  131. }
  132. if (goodsSpecData.status !== '0') {
  133. result.msg = '该规格的商品已下架';
  134. result.result = false;
  135. return result;
  136. }
  137. if (goodsSpecData.num < num) {
  138. result.msg = '库存量不足';
  139. result.result = false;
  140. return result;
  141. }
  142. return result;
  143. }
  144. // 随机字符串产生函数
  145. createNonceStr() {
  146. return Math.random().toString(36).substr(2, 15);
  147. }
  148. // 生成key
  149. async makeOrderKey(data) {
  150. const str = this.createNonceStr();
  151. const key = `${this.redisKey.orderKeyPrefix}${str}`;
  152. const value = JSON.stringify(data);
  153. await this.redis.set(key, value, 'EX', this.redisTimeout);
  154. return str;
  155. }
  156. }
  157. module.exports = TradeService;