فهرست منبع

Merge branch 'dev'

lrf 2 سال پیش
والد
کامیت
5c6051c8ee
5فایلهای تغییر یافته به همراه139 افزوده شده و 7 حذف شده
  1. 29 0
      app/model/shop/goodsConfig.js
  2. 17 0
      app/service/trade/order.js
  3. 3 1
      app/service/util/order.js
  4. 36 0
      app/service/view/goods.js
  5. 54 6
      app/service/view/order.js

+ 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;

+ 54 - 6
app/service/view/order.js

@@ -3,12 +3,15 @@ const { CrudService } = require('naf-framework-mongoose-free/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 const _ = require('lodash');
 const assert = require('assert');
-
+const { ObjectId } = require('mongoose').Types;
 //
 class OrderService extends CrudService {
   constructor(ctx) {
     super(ctx, 'order');
     this.orderModel = this.ctx.model.Trade.Order;
+    this.shopModel = this.ctx.model.Shop.Shop;
+    this.goodsRateModel = this.ctx.model.Shop.GoodsRate;
+    this.afterSaleModel = this.ctx.model.Trade.AfterSale;
   }
 
   async allOrder(query = {}) {
@@ -47,8 +50,10 @@ class OrderService extends CrudService {
     if (skip && limit) qp.push({ $skip: parseInt(skip) }, { $limit: parseInt(limit) });
     const data = await this.orderModel.aggregate(qp);
     const tr = await this.orderModel.aggregate([ ...pipeline, { $count: 'total' }]);
+    const newArr = [];
     for (const i of data) {
       const { pay, goods } = i;
+      let obj = {};
       if (pay) {
         // 这个是order表的数据,需要用order的计算方式
         const buy_num_total = goods.reduce(
@@ -59,17 +64,60 @@ class OrderService extends CrudService {
             ),
           0
         );
-        i.buy_num_total = buy_num_total;
-        i.real_pay = _.get(i, 'pay.pay_money');
+        obj.buy_num_total = buy_num_total;
+        obj.real_pay = _.get(i, 'pay.pay_money');
+        const shopData = _.pick(_.head(goods), [ 'shop', 'shop_name' ]);
+        obj.shop = { _id: shopData.shop, name: shopData.shop_name };
+        const specs = [];
+        for (const shop of goods) {
+          const { goods: specGoods } = shop;
+          for (const sg of specGoods) {
+            const spec = _.pick(sg, [ '_id', 'name', 'file', 'act', 'buy_num', 'gift', 'price', 'freight', 'sp_price' ]);
+            if (!spec.price) spec.price = _.get(sg, 'sell_money');
+            const goods = _.pick(sg.goods, [ '_id', 'name', 'file' ]);
+            spec.goods = goods;
+            specs.push(spec);
+          }
+        }
+        obj.spec = specs;
+        const others = _.pick(i, [ '_id', 'status', 'is_afterSale', 'is_rate', 'price', 'total_detail', 'type' ]);
+        obj = { ...obj, ...others, is_order: true };
       } else {
         // 是orderDetail表的数据,需要用orderDetail的计算方式
         const real_pay = this.ctx.service.util.orderDetail.computedRealPay(i);
-        i.real_pay = real_pay;
-        i.buy_num_total = goods.reduce((p, n) => this.ctx.plus(p, n.buy_num), 0);
+        obj.real_pay = real_pay;
+        obj.buy_num_total = goods.reduce((p, n) => this.ctx.plus(p, n.buy_num), 0);
+        const others = _.pick(i, [ '_id', 'status', 'is_afterSale', 'is_rate', 'price', 'total_detail', 'type', 'transport', 'transport_type' ]);
+        const shop = await this.shopModel.findById(i.shop, { name: 1 }).lean();
+        obj.shop = shop;
+        const afterSale = await this.afterSaleModel.find({ order_detail: i._id });
+        const rate = await this.goodsRateModel.find({ orderDetail: i._id });
+        const specs = [];
+        for (const sg of goods) {
+          const spec = _.pick(sg, [ '_id', 'name', 'file', 'act', 'buy_num', 'gift', 'price', 'freight', 'sp_price' ]);
+          if (!spec.price) spec.price = _.get(sg, 'sell_money');
+          const goods = _.pick(sg.goods, [ '_id', 'name', 'file' ]);
+          spec.goods = goods;
+
+          const r = afterSale.find(f => ObjectId(_.get(f, 'goods._id')).equals(sg._id));
+          if (r) spec.is_afterSale = true;
+          else spec.is_afterSale = false;
+
+          const r2 = rate.find(f => ObjectId(_.get(f, 'goodsSpec')).equals(sg._id));
+          if (r2) {
+            spec.is_rate = true;
+            spec.rate = r2._id;
+          } else spec.is_rate = false;
+
+          specs.push(spec);
+        }
+        obj.spec = specs;
+        obj = { ...obj, ...others, is_order: false };
       }
+      newArr.push(obj);
     }
     const total = _.get(_.head(tr), 'total', 0);
-    return { data, total };
+    return { data: newArr, total };
   }
 }