trade.js 4.4 KB

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