lrf 2 gadi atpakaļ
vecāks
revīzija
0fb148e54c
2 mainītis faili ar 43 papildinājumiem un 2 dzēšanām
  1. 2 2
      app/model/zrOrder.js
  2. 41 0
      app/service/zrOrder.js

+ 2 - 2
app/model/zrOrder.js

@@ -6,11 +6,11 @@ const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
 const zrOrder = {
   customer: { type: String, required: false, zh: '顾客', ref: 'Base.User' }, //
   shop: { type: String, required: false, zh: '店铺', ref: 'Base.Shop' }, //
-  goods: { type: String, required: false, zh: '商品', ref: 'ZrGoods' }, // 快照
+  goods: { type: Object, required: false, zh: '商品' }, // 快照
   buy_num: { type: Number, required: false, zh: '购买数量' }, //
   buy_time: { type: String, required: false, zh: '购买时间' }, //
   no: { type: String, required: false, zh: '订单号' }, //
-  address: { type: Object, required: false, zh: '地址' }, // 快照
+  address: { type: Object, required: false, zh: '地址', ref: 'Base.Address' }, // 快照
   transport: { type: Object, required: false, zh: '快递信息' }, //
   remarks: { type: String, required: false, zh: '备注' }, //
   status: { type: String, required: false, default: '0', zh: '订单状态' }, //

+ 41 - 0
app/service/zrOrder.js

@@ -3,6 +3,8 @@ const { CrudService } = require('naf-framework-mongoose-free/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 const _ = require('lodash');
 const assert = require('assert');
+const moment = require('moment');
+const Transaction = require('mongoose-transactions');
 //
 class ZrOrderService extends CrudService {
   constructor(ctx) {
@@ -15,11 +17,50 @@ class ZrOrderService extends CrudService {
     this.pointModel = this.ctx.model.Base.Point;
     this.shopModel = this.ctx.model.Base.Shop;
     this.addressModel = this.ctx.model.Base.Address;
+    this.tran = new Transaction();
   }
   // 进入订单页前,通过函数判断,存储是否可以购买 指定商品 的 指定数量
   // 下单,走同样的函数判断.
   // 直接生成订单,扣除积分
 
+  async create(data) {
+    const { shop, goods, buy_num, address, remarks } = data;
+    const res = await this.checkCanBuy({ shop, goods, num: buy_num }, false);
+    if (!res.result) throw new BusinessError(ErrorCode.DATA_INVALID, res.msg);
+    // 可以购买
+    const customer = _.get(this.ctx.user, '_id');
+    const goodsInfo = await this.goodsModel.findById(goods);
+    const buy_time = moment().format('YYYY-MM-DD HH:mm:ss');
+    const no = `${buy_time}-${this.createNonceStr()}`;
+    try {
+      const orderData = {
+        customer,
+        shop,
+        goods: goodsInfo,
+        buy_num,
+        buy_time,
+        no,
+        address,
+        status: '1',
+        remarks,
+      };
+      const order_id = this.tran.insert('ZrOrder', orderData);
+      // #region 积分处理
+      const costTotal = this.ctx.multiply(_.get(goodsInfo, 'cost'), buy_num);
+      const pointData = { customer, point: costTotal, time: buy_time, source: '-2', source_id: order_id };
+      this.tran.insert('Point', pointData);
+      // #endregion
+      await this.tran.run();
+      return 'ok';
+    } catch (error) {
+      await this.tran.rollback();
+      console.error(error);
+    } finally {
+      this.tran.clean();
+    }
+
+  }
+
   async toMakeOrder({ key }) {
     key = `${this.redisKey.orderKeyPrefix}${key}`;
     let data = await this.redis.get(key);