order.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 OrderService extends CrudService {
  10. constructor(ctx) {
  11. super(ctx, 'order');
  12. this.redis = this.app.redis;
  13. this.redisKey = this.app.config.redisKey;
  14. this.model = this.ctx.model.Trade.Order;
  15. this.goodsModel = this.ctx.model.Shop.Goods;
  16. this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
  17. this.addressModel = this.ctx.model.User.Address;
  18. this.cartModel = this.ctx.model.Trade.Cart;
  19. this.tran = new Transaction();
  20. }
  21. /**
  22. * 创建订单
  23. * 1.检测商品是否可以购买
  24. * 2.数据做快照处理
  25. * @param {Object} body
  26. */
  27. async create(body) {
  28. // 声明事务
  29. try {
  30. const user = this.ctx.user;
  31. const customer = _.get(user, '_id');
  32. if (!customer) throw new BusinessError(ErrorCode.NOT_LOGIN, '未找到用户信息');
  33. const { address, goods, total_detail } = body;
  34. // 检测商品是否可以下单
  35. for (const i of goods) {
  36. const { shop } = i;
  37. for (const g of i.goods) {
  38. const { goods_id: goods, goodsSpec_id: goodsSpec, num } = g;
  39. const { result, msg } = await this.ctx.service.util.trade.checkCanBuy({ shop, goods, goodsSpec, num }, false);
  40. if (!result) throw new BusinessError(ErrorCode.DATA_INVALID, msg);
  41. }
  42. }
  43. const orderData = {};
  44. // 数据做快照处理
  45. // 1.地址快照
  46. const addressData = await this.addressModel.findById(address._id);
  47. if (!addressData) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到邮寄地址数据');
  48. // 2.商品快照
  49. const goodsData = [];
  50. // 商店不做快照,但是商品和商品对应的规格做快照
  51. const { populate } = this.ctx.service.shop.goodsSpec.getRefMods();
  52. for (const i of goods) {
  53. const { goods: goodsList, ...others } = i;
  54. const qp = [];
  55. for (const g of goodsList) {
  56. const { goodsSpec_id, num, cart_id } = g;
  57. let d = await this.goodsSpecModel.findById(goodsSpec_id).populate(populate);
  58. if (!d) continue;
  59. d = JSON.parse(JSON.stringify(d));
  60. // 将商店内容剔除
  61. const { goods: gd, ...dOthers } = d;
  62. const gdOthers = _.omit(gd, [ 'shop' ]);
  63. const obj = { ...dOthers, goods: gdOthers, buy_num: num };
  64. if (cart_id) obj.cart_id = cart_id;
  65. qp.push(obj);
  66. }
  67. goodsData.push({ ...others, goods: qp });
  68. }
  69. // TODO: 3.商品总计明细.这地方没加优惠券,所以先直接复制即可
  70. const totalDetailData = total_detail;
  71. // 接下来组织订单数据
  72. orderData.address = addressData;
  73. orderData.goods = goodsData;
  74. orderData.total_detail = totalDetailData;
  75. // 1.用户数据
  76. orderData.customer = customer;
  77. // 2.下单时间
  78. orderData.buy_time = moment().format('YYYY-MM-DD HH:mm:ss');
  79. // 3.订单号
  80. const str = this.ctx.service.util.trade.createNonceStr();
  81. orderData.no = `${moment().format('YYYYMMDDHHmmss')}-${str}`;
  82. // 4.状态
  83. orderData.status = '0';
  84. // 生成数据
  85. // const order = await this.model.create(orderData);
  86. const order_id = this.tran.insert('Order', orderData);
  87. // 处理库存问题
  88. // 处理库存,删除购物车
  89. await this.dealGoodsNum(goodsData);
  90. await this.tran.run();
  91. return order_id;
  92. } catch (error) {
  93. await this.tran.rollback();
  94. console.error(error);
  95. throw new BusinessError(ErrorCode.SERVICE_FAULT, '订单发生错误,下单失败');
  96. } finally {
  97. // 清空事务
  98. this.tran.clean();
  99. }
  100. }
  101. /**
  102. * 减库存,删除购物车
  103. * @param {Array} list 商品
  104. */
  105. async dealGoodsNum(list) {
  106. for (const i of list) {
  107. for (const g of i.goods) {
  108. const { _id, buy_num, cart_id } = g;
  109. const goodsSpec = await this.goodsSpecModel.findById(_id);
  110. const newNum = parseInt(goodsSpec.num - buy_num);
  111. this.tran.update('GoodsSpec', _id, { num: newNum });
  112. if (cart_id) this.tran.remove('Cart', cart_id);
  113. }
  114. }
  115. }
  116. /**
  117. * 进入下单页面
  118. * @param {Object} body 请求参数
  119. * @param body.key 缓存key
  120. */
  121. async toMakeOrder({ key }) {
  122. key = `${this.redisKey.orderKeyPrefix}${key}`;
  123. let data = await this.redis.get(key);
  124. if (!data) throw new BusinessError(ErrorCode.SERVICE_FAULT, '请求超时,请重新进入下单页');
  125. data = JSON.parse(data);
  126. let specsData = [];
  127. if (_.isArray(data)) {
  128. // 购物车来的: 1.循环校验 规格商品; 2.按店铺分组
  129. const { populate } = this.ctx.service.trade.cart.getRefMods();
  130. const carts = await this.cartModel.find({ _id: data }).populate(populate);
  131. for (const cart of carts) {
  132. const { result, msg } = await this.ctx.service.util.trade.checkCanBuy(cart, false);
  133. if (!result) throw new BusinessError(ErrorCode.DATA_INVALID, msg);
  134. }
  135. specsData = this.setCartsGoodsToPageData(carts);
  136. } else if (_.isObject(data)) {
  137. // 商品页单独买: 1.校验规格商品; 2:按店铺分组
  138. const { result, msg } = await this.ctx.service.util.trade.checkCanBuy(data, false);
  139. if (!result) throw new BusinessError(ErrorCode.DATA_INVALID, msg);
  140. const { populate } = this.ctx.service.shop.goodsSpec.getRefMods();
  141. const list = await this.goodsSpecModel.find({ _id: data.goodsSpec }).populate(populate);
  142. specsData = this.setGoodsToPageData(list, data.num);
  143. } else throw new BusinessError(ErrorCode.DATA_INVALID, '数据不正确,请重新下单');
  144. // 组装页面的数据
  145. const user = this.ctx.user;
  146. const customer = _.get(user, '_id');
  147. if (!customer) throw new BusinessError(ErrorCode.NOT_LOGIN, '未找到用户信息');
  148. const pageData = {};
  149. // 找到默认地址
  150. const address = await this.addressModel.findOne({ customer, is_default: '1' });
  151. pageData.address = address;
  152. // // 商品总价,各店铺的价格明细
  153. specsData = this.computedShopTotal(specsData);
  154. const shopTotalDetail = this.computedAllTotal(specsData);
  155. pageData.goodsData = specsData;
  156. pageData.orderTotal = shopTotalDetail;
  157. return pageData;
  158. }
  159. /**
  160. * 计算商店的结算
  161. * @param {Array} list 按店铺分组的商品列表
  162. */
  163. computedShopTotal(list) {
  164. for (const i of list) {
  165. i.goods_total = _.floor(
  166. i.goods.reduce((p, n) => p + (n.money || 0) * (n.num || 0), 0),
  167. 2
  168. );
  169. i.freight_total = _.floor(
  170. i.goods.reduce((p, n) => p + (n.freight || 0) * (n.num || 0), 0),
  171. 2
  172. );
  173. }
  174. return list;
  175. }
  176. /**
  177. * 计算订单的结算
  178. * @param {Array} list 按店铺分组的商品列表
  179. */
  180. computedAllTotal(list) {
  181. const obj = {
  182. goods_total: _.floor(
  183. list.reduce((p, n) => p + (n.goods_total || 0), 0),
  184. 2
  185. ),
  186. freight_total: _.floor(
  187. list.reduce((p, n) => p + (n.freight_total || 0), 0),
  188. 2
  189. ),
  190. };
  191. return obj;
  192. }
  193. /**
  194. * 单商品整理数据,剩下的可以简略
  195. * @param {Array} list 规格数组
  196. * @param num 购买数量
  197. */
  198. setGoodsToPageData(list, num) {
  199. // 按店铺分组,精简字段
  200. const arr = [];
  201. for (const i of list) {
  202. const obj = {};
  203. obj.shop_name = _.get(i.goods, 'shop.name');
  204. obj.shop = _.get(i.goods, 'shop._id');
  205. const goods = {};
  206. goods.goods_id = _.get(i.goods, '_id');
  207. goods.goods_name = _.get(i.goods, 'name');
  208. goods.goodsSpec_id = _.get(i, '_id');
  209. goods.goodsSpec_name = _.get(i, 'name');
  210. goods.freight = _.get(i, 'freight');
  211. goods.money = _.get(i, 'sell_money');
  212. goods.num = num;
  213. goods.file = _.get(i.goods, 'file');
  214. obj.goods = [ goods ];
  215. arr.push(obj);
  216. }
  217. return arr;
  218. }
  219. /**
  220. *购物车数据整理
  221. * @param {Array} list 规格数组
  222. */
  223. setCartsGoodsToPageData(list) {
  224. const arr = [];
  225. list = _.groupBy(list, 'shop._id');
  226. for (const key in list) {
  227. const data = list[key];
  228. const shopData = _.get(_.head(data), 'shop');
  229. const obj = {};
  230. obj.shop = _.get(shopData, '_id');
  231. obj.shop_name = _.get(shopData, 'name');
  232. const goodsList = [];
  233. for (const i of data) {
  234. const goods = {};
  235. goods.cart_id = _.get(i, '_id');
  236. goods.goods_id = _.get(i.goods, '_id');
  237. goods.goods_name = _.get(i.goods, 'name');
  238. goods.goodsSpec_id = _.get(i.goodsSpec, '_id');
  239. goods.goodsSpec_name = _.get(i.goodsSpec, 'name');
  240. goods.freight = _.get(i.goodsSpec, 'freight');
  241. goods.money = _.get(i.goodsSpec, 'sell_money');
  242. goods.num = _.get(i, 'num');
  243. goods.file = _.get(i.goods, 'file', []);
  244. goodsList.push(goods);
  245. }
  246. obj.goods = goodsList;
  247. arr.push(obj);
  248. }
  249. return arr;
  250. }
  251. }
  252. module.exports = OrderService;