zrOrder.js 6.7 KB

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