trade.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. this.setModel = this.ctx.model.Shop.GoodsSet;
  18. }
  19. /**
  20. * 检查选中的购物车是否符合购买的条件
  21. * @param {Object} body 请求体
  22. * @param body.cartIds 选中的购物车id列表
  23. * @param {Boolean} makeCache 生成缓存
  24. */
  25. async checkCartBuy({ cartIds }, makeCache = true) {
  26. const list = await this.cartModel.find({ _id: cartIds });
  27. let result = { result: true };
  28. for (const i of list) {
  29. const res = await this.checkCanBuy(i, false);
  30. if (res.result !== true) {
  31. result = res;
  32. break;
  33. }
  34. }
  35. if (makeCache) {
  36. const key = await this.makeOrderKey(cartIds);
  37. result.key = key;
  38. }
  39. return result;
  40. }
  41. /**
  42. * 检测是否可以购买该物品
  43. * @param {Object} data 查询条件
  44. * @param param.shop 商店id
  45. * @param param.goods 商品id
  46. * @param param.goodsSpec 商品规格id
  47. * @param param.num 购买数量
  48. * @param param.type 订单类型: 0:常规单 1:团购单,需要按团购价计算
  49. * @param param.group 团id
  50. * @param param.inviter 邀请用户id 返现部分
  51. * @param param.is_set 是否是套装,默认 1 不是套装
  52. * @param params.set_id 套装id
  53. * @param makeCache 生成缓存
  54. */
  55. async checkCanBuy(data, makeCache = true) {
  56. if (!_.isArray(data)) data = [ data ];
  57. let result = { result: true };
  58. for (const i of data) {
  59. const { is_set = '1' } = i;
  60. if (is_set === '1') {
  61. // 非套装,使用正常的判断
  62. result = await this.checkGoodsCanBuy(i);
  63. } else {
  64. // 套装,使用套装判断
  65. result = await this.checkSetCanBuy(i);
  66. }
  67. if (result.result !== true) break;
  68. }
  69. // #endregion
  70. if (result.result && makeCache) {
  71. const key = await this.makeOrderKey(data);
  72. result.key = key;
  73. }
  74. return result;
  75. }
  76. /**
  77. * 套装判断是否可以购买
  78. * @param {Object} data 购物车/直接购买 数据对象
  79. */
  80. async checkSetCanBuy(data) {
  81. const { num, shop } = data;
  82. // 取set_id是在购物车检查是否可以购买; data._id是下单时使用
  83. // 已经将购物车那边的数据加上了set_id,不过先这么写,兼容
  84. const set_id = _.get(data, 'set_id', data._id);
  85. let result = { result: true };
  86. if (!shop) {
  87. result.result = false;
  88. result.msg = '缺少店铺信息';
  89. return result;
  90. }
  91. // 1.检查商店是否正常运行
  92. const shopData = await this.shopModel.findById(shop);
  93. const shopRes = this.checkShop(shopData);
  94. if (shopRes.result !== true) {
  95. result = shopRes;
  96. return result;
  97. }
  98. const setData = await this.setModel.findById(set_id).lean();
  99. const { set, is_use } = setData;
  100. if (is_use === '1') return { result: false, msg: '套装已下架' };
  101. for (const sd of set) {
  102. const { goods, spec, set_num } = sd;
  103. if (!goods) {
  104. result.result = false;
  105. result.msg = '缺少商品信息';
  106. break;
  107. }
  108. if (!spec) {
  109. result.result = false;
  110. result.msg = '缺少商品规格信息';
  111. break;
  112. }
  113. if (!num) {
  114. result.result = false;
  115. result.msg = '缺少购买数量';
  116. break;
  117. }
  118. // 2.检查商品是否可以购买
  119. const goodsData = await this.goodsModel.findById(goods);
  120. const goodsRes = this.checkGoods(goodsData);
  121. if (goodsRes.result !== true) {
  122. result = goodsRes;
  123. break;
  124. }
  125. // 3.根据套装库存设置判断是走各个商品库存还是走套装库存
  126. // 走各个商品库存
  127. const goodsSpecData = await this.goodsSpecModel.findById(spec);
  128. // 单独计算下商品数量 = 购买数量 * 组成套装的数量
  129. const setGoodsNum = this.ctx.multiply(num, set_num);
  130. const gsRes = this.checkGoodsSpec(goodsSpecData, setGoodsNum, '0');
  131. if (gsRes.result !== true) {
  132. result = gsRes;
  133. break;
  134. }
  135. }
  136. return result;
  137. }
  138. /**
  139. * 常规商品判断是否可以购买
  140. * @param {Object} data 购物车/直接购买 数据对象
  141. */
  142. async checkGoodsCanBuy(data) {
  143. let result = { result: true };
  144. const { shop, goods, goodsSpec, num } = data;
  145. if (!shop) {
  146. result.result = false;
  147. result.msg = '缺少店铺信息';
  148. return result;
  149. }
  150. if (!goods) {
  151. result.result = false;
  152. result.msg = '缺少商品信息';
  153. return result;
  154. }
  155. if (!goodsSpec) {
  156. result.result = false;
  157. result.msg = '缺少商品规格信息';
  158. return result;
  159. }
  160. if (!num) {
  161. result.result = false;
  162. result.msg = '缺少购买数量';
  163. return result;
  164. }
  165. // 1.检查商店是否正常运行
  166. const shopData = await this.shopModel.findById(shop);
  167. const shopRes = this.checkShop(shopData);
  168. if (shopRes.result !== true) {
  169. result = shopRes;
  170. return result;
  171. }
  172. // 2.检查商品是否可以购买
  173. const goodsData = await this.goodsModel.findById(goods);
  174. const goodsRes = this.checkGoods(goodsData);
  175. if (goodsRes.result !== true) {
  176. result = goodsRes;
  177. return result;
  178. }
  179. // 3.检验该规格是否可以购买
  180. const goodsSpecData = await this.goodsSpecModel.findById(goodsSpec);
  181. const gsRes = this.checkGoodsSpec(goodsSpecData, num);
  182. if (gsRes.result !== true) {
  183. result = gsRes;
  184. return result;
  185. }
  186. return result;
  187. }
  188. /**
  189. * 检查商店是否满足条件
  190. * @param {Object} shopData 店铺数据
  191. */
  192. checkShop(shopData) {
  193. const result = { result: true };
  194. if (!shopData) {
  195. result.msg = '未找到店铺';
  196. result.result = false;
  197. return result;
  198. }
  199. if (shopData.status !== '1') {
  200. result.msg = '店铺不处于营业状态';
  201. result.result = false;
  202. return result;
  203. }
  204. return result;
  205. }
  206. /**
  207. * 检查商品是否满足条件
  208. * @param {Object} goodsData 商品数据
  209. */
  210. checkGoods(goodsData) {
  211. const result = { result: true };
  212. if (!goodsData) {
  213. result.msg = '未找到商品';
  214. result.result = false;
  215. return result;
  216. }
  217. if (goodsData.status === '0') {
  218. result.msg = '该商品已下架';
  219. result.result = false;
  220. return result;
  221. }
  222. return result;
  223. }
  224. /**
  225. * 检查商品的规格是否满足购买条件
  226. * @param {Object} goodsSpecData 商品规格的数据
  227. * @param {Number} num 购买数量
  228. * @param {String} is_set 是否是套装;套装不需要检查购买限制
  229. */
  230. checkGoodsSpec(goodsSpecData, num, is_set = '1') {
  231. const result = { result: true };
  232. if (!goodsSpecData) {
  233. result.msg = '未找到商品的指定规格';
  234. result.result = false;
  235. return result;
  236. }
  237. if (goodsSpecData.status !== '0') {
  238. result.msg = '该规格的商品已下架';
  239. result.result = false;
  240. return result;
  241. }
  242. if (goodsSpecData.num < num) {
  243. result.msg = '库存量不足';
  244. result.result = false;
  245. return result;
  246. }
  247. if (is_set !== '1') return result;
  248. // 判断是否有购买限制
  249. if (_.get(goodsSpecData, 'buy_limit', '0') !== '0') {
  250. // 有购买限制,需要检测购买限制
  251. const buy_limit = _.get(goodsSpecData, 'buy_limit');
  252. const limit_num = _.get(goodsSpecData, 'limit_num', 0);
  253. if (buy_limit === '1') {
  254. if (num < limit_num) {
  255. result.msg = `该规格至少购买 ${limit_num} 份, 购买数量不足`;
  256. result.result = false;
  257. return result;
  258. }
  259. } else if (buy_limit === '2') {
  260. if (num > limit_num) {
  261. result.msg = `该规格至多购买 ${limit_num} 份, 购买数量超过上限`;
  262. result.result = false;
  263. return result;
  264. }
  265. } else if (buy_limit === '3') {
  266. if (num !== limit_num) {
  267. result.msg = `该规格只能购买 ${limit_num} 份`;
  268. result.result = false;
  269. return result;
  270. }
  271. }
  272. }
  273. return result;
  274. }
  275. // 随机字符串产生函数
  276. createNonceStr() {
  277. return Math.random().toString(36).substr(2, 15);
  278. }
  279. // 生成key
  280. async makeOrderKey(data) {
  281. const str = this.createNonceStr();
  282. const key = `${this.redisKey.orderKeyPrefix}${str}`;
  283. const value = JSON.stringify(data);
  284. await this.redis.set(key, value, 'EX', this.redisTimeout);
  285. return str;
  286. }
  287. }
  288. module.exports = TradeService;