lrf 2 лет назад
Родитель
Сommit
e6948aed66
2 измененных файлов с 26 добавлено и 0 удалено
  1. 1 0
      app/model/shop/goods.js
  2. 25 0
      app/service/trade/pay.js

+ 1 - 0
app/model/shop/goods.js

@@ -11,6 +11,7 @@ const goods = {
   brief: { type: String, required: false, zh: '商品介绍' }, //
   file: { type: Array, required: false, zh: '商品图片' }, //
   tags: { type: Array, required: false, zh: '商品标签' }, //
+  sell_num: { type: Number, required: false, default: 0, zh: '销售量' }, //
   view_num: { type: Number, required: false, default: 0, zh: '浏览量' }, //
   status: { type: String, required: false, default: '0', zh: '商品状态' }, // 字典:goods_status
   act_tags: { type: Array, required: false, zh: '活动标签' }, // 字典:act_tags

+ 25 - 0
app/service/trade/pay.js

@@ -11,6 +11,7 @@ class PayService extends CrudService {
     super(ctx, 'pay');
     this.httpUtil = this.ctx.service.util.httpUtil;
     this.appConfig = this.app.config.wxPayConfig;
+    this.goodsModel = this.ctx.model.Shop.Goods;
     this.orderModel = this.ctx.model.Trade.Order;
     this.dictDataModel = this.ctx.model.Dev.DictData;
     this.payOrderReturnUrl = this.app.config.payReturn.order;
@@ -87,10 +88,13 @@ class PayService extends CrudService {
 
   async withoutPay({ order_id }) {
     try {
+      const orderData = await this.orderModel.findById(order_id);
       this.tran.update('Order', order_id, { status: '1' });
       await this.tran.run();
       // 拆订单
       await this.ctx.service.trade.orderDetail.create({ order_id }, this.tran);
+      // 加销量
+      await this.addSell(orderData, this.tran);
       await this.tran.run();
     } catch (error) {
       console.error(error);
@@ -134,6 +138,8 @@ class PayService extends CrudService {
       await this.tran.run();
       // 拆订单
       await this.ctx.service.trade.orderDetail.create({ order_id: orderData._id }, this.tran);
+      // 加销量
+      await this.addSell(orderData, this.tran);
       await this.tran.run();
     } catch (error) {
       console.error(error);
@@ -159,6 +165,25 @@ class PayService extends CrudService {
       this.tran.clean();
     }
   }
+  /**
+   * 加销量
+   * @param {Object} order 支付订单数据
+   * @param {Transaction} tran 数据库事务
+   */
+  async addSell(order, tran) {
+    const goods = _.get(order, 'goods', []);
+    for (const sg of goods) {
+      const sgList = _.get(sg, 'goods', []);
+      for (const g of sgList) {
+        const buy_num = _.get(g, 'buy_num', 0);
+        const goods_id = _.get(goods, '_id');
+        if (!goods_id) return;
+        const goodsInfo = await this.goodsModel.findById(goods_id, { sell_num: 1 });
+        const newSell_num = this.ctx.plus(buy_num, _.get(goodsInfo, 'sell_num'));
+        tran.update('Goods', goods_id, { sell_num: newSell_num });
+      }
+    }
+  }
 
   /**
    * 关闭订单