zrOrder.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. const moment = require('moment');
  7. const Transaction = require('mongoose-transactions');
  8. //
  9. class ZrOrderService extends CrudService {
  10. constructor(ctx) {
  11. super(ctx, 'zrorder');
  12. this.redis = this.app.redis;
  13. this.redisKey = this.app.config.redisKey;
  14. this.redisTimeout = this.app.config.redisTimeout;
  15. this.model = this.ctx.model.ZrOrder;
  16. this.goodsModel = this.ctx.model.ZrGoods;
  17. this.pointModel = this.ctx.model.Base.Point;
  18. this.shopModel = this.ctx.model.Base.Shop;
  19. this.addressModel = this.ctx.model.Base.Address;
  20. this.tran = new Transaction();
  21. }
  22. // 进入订单页前,通过函数判断,存储是否可以购买 指定商品 的 指定数量
  23. // 下单,走同样的函数判断.
  24. // 直接生成订单,扣除积分
  25. async create(data) {
  26. const { shop, goods, buy_num, address, remarks } = data;
  27. const res = await this.checkCanBuy({ shop, goods, num: buy_num }, false);
  28. if (!res.result) throw new BusinessError(ErrorCode.DATA_INVALID, res.msg);
  29. // 可以购买
  30. const customer = _.get(this.ctx.user, '_id');
  31. const goodsInfo = await this.goodsModel.findById(goods);
  32. const buy_time = moment().format('YYYY-MM-DD HH:mm:ss');
  33. const no = `${moment().format('YYYYMMDDHHmmss')}-${this.createNonceStr()}`;
  34. try {
  35. const orderData = {
  36. customer,
  37. shop,
  38. goods: goodsInfo,
  39. buy_num,
  40. buy_time,
  41. no,
  42. address,
  43. status: '1',
  44. remarks,
  45. };
  46. const order_id = this.tran.insert('ZrOrder', orderData);
  47. // #region 积分处理
  48. const costTotal = this.ctx.multiply(_.get(goodsInfo, 'cost'), buy_num);
  49. const pointData = { customer, point: costTotal, time: buy_time, source: '-2', source_id: order_id };
  50. this.tran.insert('Point', pointData);
  51. // #endregion
  52. // #region 库存处理
  53. const newNum = this.ctx.minus(_.get(goodsInfo, 'num'), buy_num);
  54. this.tran.update('ZrGoods', goods, { num: newNum });
  55. // #endregion
  56. await this.tran.run();
  57. return 'ok';
  58. } catch (error) {
  59. await this.tran.rollback();
  60. console.error(error);
  61. } finally {
  62. this.tran.clean();
  63. }
  64. }
  65. async toMakeOrder({ key }) {
  66. key = `${this.redisKey.orderKeyPrefix}${key}`;
  67. let data = await this.redis.get(key);
  68. if (!data) throw new BusinessError(ErrorCode.SERVICE_FAULT, '请求超时,请重新进入下单页');
  69. data = JSON.parse(data);
  70. const { shop, goods, num: buy_num } = data;
  71. // 店铺信息
  72. const shopInfo = await this.shopModel.findById(shop);
  73. // 商品信息
  74. const goodsInfo = await this.goodsModel.findById(goods);
  75. // 消耗积分
  76. const costTotal = this.ctx.multiply(_.get(goodsInfo, 'cost'), buy_num);
  77. // 积分信息
  78. const customer = _.get(this.ctx.user, '_id');
  79. const points = await this.pointComputedTotal({ customer });
  80. // 地址信息
  81. const address = await this.addressModel.findOne({ customer, is_default: '1' });
  82. return { buy_num, shop: shopInfo, goods: goodsInfo, points, address, costTotal };
  83. }
  84. /**
  85. * 检查是否可以购买商品,并生成缓存
  86. * @param {Object} body 参数体
  87. * @param body.shop 店铺id
  88. * @param body.goods 尊荣商品id
  89. * @param body.num 购买数量
  90. * @param {Boolean} makeCache 是否生成缓存
  91. */
  92. async checkCanBuy({ shop, goods, num }, makeCache = true) {
  93. const result = { result: true };
  94. const shopInfo = await this.shopModel.findById(shop);
  95. if (!shopInfo || _.get(shopInfo, '1')) {
  96. result.result = false;
  97. result.msg = '店铺当前不处于营业状态';
  98. return result;
  99. }
  100. const goodsInfo = await this.goodsModel.findById(goods);
  101. if (!goodsInfo) {
  102. result.result = false;
  103. result.msg = '未找到尊荣商品';
  104. return result;
  105. }
  106. const user = this.ctx.user;
  107. const customer = _.get(user, '_id');
  108. if (!customer) {
  109. result.result = false;
  110. result.msg = '未找到用户信息';
  111. }
  112. // #region 库存判断
  113. const knum = _.get(goodsInfo, 'num');
  114. const res = this.ctx.minus(knum, num);
  115. if (res <= 0) {
  116. result.result = false;
  117. result.msg = '库存不足';
  118. return result;
  119. }
  120. // #endregion
  121. // #region 积分判断
  122. const pointTotal = await this.pointComputedTotal({ customer });
  123. // 计算购买所需积分
  124. const cost = _.get(goodsInfo, 'cost', 0);
  125. if (cost === 0) {
  126. result.result = false;
  127. result.msg = '商品设置的尊荣有误,无法进行购买';
  128. return result;
  129. }
  130. const costTotal = this.ctx.multiply(cost, num);
  131. const afterCost = this.ctx.minus(pointTotal, costTotal);
  132. if (afterCost < 0) {
  133. result.result = false;
  134. result.msg = '您的积分不足';
  135. return result;
  136. }
  137. // #endregion
  138. if (makeCache) {
  139. const key = await this.makeOrderKey({ shop, goods, num });
  140. result.key = key;
  141. }
  142. return result;
  143. }
  144. // 生成key
  145. async makeOrderKey(data) {
  146. const str = this.createNonceStr();
  147. const key = `${this.redisKey.orderKeyPrefix}${str}`;
  148. const value = JSON.stringify(data);
  149. await this.redis.set(key, value, 'EX', this.redisTimeout);
  150. return str;
  151. }
  152. /**
  153. * 计算用户的积分
  154. * @param {Object} data 数据对象
  155. * @param data.customer 用户数据id
  156. */
  157. async pointComputedTotal({ customer }) {
  158. assert(customer, '缺少用户信息');
  159. const res = await this.pointModel.find({ customer });
  160. const total = res.reduce((p, n) => {
  161. let point = n.point;
  162. if (!(n.source === '0' || n.source === '1')) point = -point;
  163. return this.ctx.plus(p, point);
  164. }, 0);
  165. return total;
  166. }
  167. // 随机字符串产生函数
  168. createNonceStr() {
  169. return Math.random().toString(36).substr(2, 15);
  170. }
  171. }
  172. module.exports = ZrOrderService;