lrf 2 tahun lalu
induk
melakukan
38f5b1fbab

+ 29 - 0
app/model/shop/goodsConfig.js

@@ -0,0 +1,29 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
+
+const MoneyPlugin = require('naf-framework-mongoose-free/lib/model/type-money-plugin');
+
+// 商品设置
+const goodsConfig = {
+  shop: { type: String, required: false, zh: '店铺id', ref: 'Shop' }, //
+  goods: { type: String, required: false, zh: '商品id', ref: 'Goods' }, //
+  spec: { type: String, required: false, zh: '规格id', ref: 'GoodsSpec' }, //
+};
+const schema = new Schema(goodsConfig, { toJSON: { getters: true, virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ 'meta.createdAt': 1 });
+schema.index({ shop: 1 });
+schema.index({ goods: 1 });
+schema.index({ spec: 1 });
+
+schema.plugin(metaPlugin);
+schema.plugin(MoneyPlugin({ zh: '团长价', required: false, key: 'leader_price' }));
+schema.plugin(MoneyPlugin({ zh: '团购价', required: false, key: 'price' }));
+schema.plugin(MoneyPlugin({ zh: '团长提成金额', required: false, key: 'leader_get' }));
+schema.plugin(MoneyPlugin({ zh: '运费', required: false, key: 'freight' }));
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('GoodsConfig', schema, 'goodsConfig');
+};

+ 17 - 0
app/service/trade/order.js

@@ -21,6 +21,7 @@ class OrderService extends CrudService {
     this.platformActModel = this.ctx.model.System.PlatformAct;
     this.gjaModel = this.ctx.model.Shop.GoodsJoinAct;
     this.orderUtil = this.ctx.service.util.order;
+    this.goodsConfigModel = this.ctx.model.Shop.GoodsConfig;
     this.tran = new Transaction();
   }
 
@@ -206,6 +207,7 @@ class OrderService extends CrudService {
     }
     const { result, msg } = await this.ctx.service.util.trade.checkCanBuy(data, false);
     if (!result) throw new BusinessError(ErrorCode.DATA_INVALID, msg);
+
     // 本次订单 有关活动的数据
     const actList = await this.getActList(data, true);
     // 正常整理商品的内容,与活动结合
@@ -391,6 +393,21 @@ class OrderService extends CrudService {
       if (cart_id) gs.cart_id = cart_id;
       arr.push(gs);
     }
+    // 检测是否是团长,使用团长价格
+    const user = _.get(this.ctx, 'user');
+    if (!user) throw new BusinessError(ErrorCode.NOT_LOGIN);
+    const is_leader = _.get(user, 'is_leader', '1');
+    if (is_leader === '0') {
+      for (const i of arr) {
+        const { goods_id: goods, goodsSpec_id: spec } = i;
+        const r = await this.goodsConfigModel.findOne({ goods, spec });
+        if (r) {
+          i.leader_price = r.leader_price;
+          i.price = r.leader_price;
+        }
+      }
+    }
+
     // 平铺数据后,需要处理活动相关部分
     // 经过处理后的数据,会添加act字段,表明与活动有关的信息
     await this.dealAct(arr, actList);

+ 3 - 1
app/service/util/order.js

@@ -308,7 +308,9 @@ class OrderService extends CrudService {
       if (goods) {
         // 默认特价为商品金额
         goods.sp_price = sp_price;
-        goods.price = sp_price;
+        // 有团长价格,且团长价格比特价低,就用团长价格
+        if (goods.leader_price && this.ctx.minus(goods.leader_price, goods.sp_price) < 0) goods.price = goods.leader_price;
+        else goods.price = sp_price;
         const { act = [] } = goods;
         act.push({ platform_act_type, platform_act, sp_price });
         goods.act = act;

+ 36 - 0
app/service/view/goods.js

@@ -16,6 +16,7 @@ class GoodsService extends CrudService {
     this.platformActModel = this.ctx.model.System.PlatformAct;
     this.gjaModel = this.ctx.model.Shop.GoodsJoinAct;
     this.actTagsModel = this.ctx.model.System.ActTags;
+    this.goodsConfigModel = this.ctx.model.Shop.GoodsConfig;
   }
   /**
    *
@@ -157,6 +158,22 @@ class GoodsService extends CrudService {
       }
     }
     data.act = _.uniqBy(data.act, '_id');
+    const user = _.get(this.ctx, 'user');
+    if (user) {
+      const is_leader = _.get(user, 'is_leader', '1');
+      if (is_leader === '0') {
+        const list = await this.goodsConfigModel.find({ goods: id }).lean();
+        for (const i of list) {
+          const { spec, leader_price } = i;
+          const r = data.specs.find(f => f._id === spec);
+          if (r) {
+            r.o_sell_money = JSON.parse(r.sell_money);
+            r.leader_price = leader_price;
+            r.sell_money = leader_price;
+          }
+        }
+      }
+    }
     return data;
   }
 
@@ -238,9 +255,18 @@ class GoodsService extends CrudService {
     // 只找: 买赠;特价;满减/折; 特价需要修改价格; 剩下的打标签
     const platformActList = await this.platformActModel.find({ is_use: '0', type: [ '2', '3', '5', '6' ] });
     await this.getAboutAct(platformActList, list);
+    // 检查团长价格
+    const user = _.get(this.ctx, 'user');
+    if (user) {
+      const is_leader = _.get(user, 'is_leader', '1');
+      if (is_leader === '0') {
+        list = await this.searchLeaderPrice(list);
+      }
+    }
     const tpipeline = _.cloneDeep(pipeline);
     tpipeline.push({ $count: 'total' });
     const total = await this.goodsModel.aggregate(tpipeline);
+
     return { list, total: _.get(_.head(total), 'total', 0) };
   }
 
@@ -325,6 +351,16 @@ class GoodsService extends CrudService {
     const list = await this.goodsModel.aggregate(pipeline);
     return list;
   }
+
+  async searchLeaderPrice(list) {
+    for (const i of list) {
+      const { _id: goods } = i;
+      let gcl = await this.goodsConfigModel.find({ goods }).lean();
+      gcl = _.orderBy(gcl, [ 'leader_price' ], [ 'asc' ]);
+      i.leader_price = _.get(_.head(gcl), 'leader_price');
+    }
+    return list;
+  }
 }
 
 module.exports = GoodsService;