lrf 2 vuotta sitten
vanhempi
commit
092fbe10c0

+ 13 - 0
app/controller/trade/actOrder.js

@@ -0,0 +1,13 @@
+'use strict';
+const meta = require('./config/.actOrder.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose-free/lib/controller');
+
+// 
+class ActOrderController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.trade.actOrder;
+  }
+}
+module.exports = CrudController(ActOrderController, meta);

+ 40 - 0
app/controller/trade/config/.actOrder.js

@@ -0,0 +1,40 @@
+module.exports = {
+  create: {
+    requestBody: ['platform_act', 'order_detail', 'is_deal', 'goods'],
+  },
+  destroy: {
+    params: ['!id'],
+    service: 'delete',
+  },
+  update: {
+    params: ['!id'],
+    requestBody: ['platform_act', 'order_detail', 'is_deal', 'goods'],
+  },
+  show: {
+    parameters: {
+      params: ['!id'],
+    },
+    service: 'fetch',
+  },
+  index: {
+    parameters: {
+      query: {
+        'meta.createdAt@start': 'meta.createdAt@start',
+        'meta.createdAt@end': 'meta.createdAt@end',
+        platform_act: 'platform_act',
+        order_detail: 'order_detail',
+        is_deal: 'is_deal',
+      },
+      // options: {
+      //   "meta.state": 0 // 默认条件
+      // },
+    },
+    service: 'query',
+    options: {
+      query: ['skip', 'limit'],
+      sort: ['meta.createdAt'],
+      desc: true,
+      count: true,
+    },
+  },
+};

+ 24 - 0
app/model/trade/actOrder.js

@@ -0,0 +1,24 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
+
+// 平台活动-订单
+const actOrder = {
+  platform_act: { type: String, required: false, zh: '平台活动', ref: 'System.PlatformAct' }, //
+  order_detail: { type: String, required: false, zh: '拆分后的订单', ref: 'Trade.Order' }, //
+  is_deal: { type: String, required: false, default: '0', zh: '是否处理' }, // 字典:is_deal
+  goods: { type: Array, required: false, zh: '影响的商品' }, //
+};
+const schema = new Schema(actOrder, { toJSON: { getters: true, virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ 'meta.createdAt': 1 });
+schema.index({ platform_act: 1 });
+schema.index({ order_detail: 1 });
+schema.index({ is_deal: 1 });
+
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('ActOrder', schema, 'actOrder');
+};

+ 52 - 0
app/service/trade/actOrder.js

@@ -0,0 +1,52 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose-free/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const _ = require('lodash');
+const assert = require('assert');
+
+//
+class ActOrderService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'actorder');
+    this.model = this.ctx.model.Trade.ActOrder;
+    this.platformActModel = this.ctx.model.System.PlatformAct;
+    this.gjaModel = this.ctx.model.Shop.GoodsJoinAct;
+  }
+
+  /**
+   * 创建订单与活动的关系
+   * @param {String} id 订单id
+   * @param {Object} data 订单数据
+   * @param {Transaction} tran 数据库事务实例
+   */
+  async create(id, data, tran) {
+    // 检查活动类型为1的 活动,再查下面的商品
+    const actList = await this.platformActModel.find({ is_use: '0', type: { $ne: '0' } }, { _id: 1 });
+    if (actList.length <= 0) return;
+    // 整理出订单内的商品id
+    const odGoods = data.goods.map(i => {
+      const { _id: spec_id, goods } = i;
+      const goods_id = _.get(goods, '_id');
+      return { spec_id, goods_id };
+    });
+    const actOrderList = [];
+    // 循环所有活动,找当前购买的商品,是否出现在这些活动之中,如果出现,就需要添加关系
+    for (const act of actList) {
+      const { _id: platformAct } = act;
+      const query = { goods: odGoods.map(i => i.goods_id), platformAct };
+      const goodsList = await this.gjaModel.find(query);
+      // 获取受影响的商品
+      const influenceGoods = goodsList.map(i => i.goods);
+      if (influenceGoods.length <= 0) continue;
+      const actOrder = { platform_act: platformAct, order_detail: id };
+      // 用受影响的 商品id, 在 odGoods 中 找到对应的数据作为 actOrder的goods
+      actOrder.goods = odGoods.filter(f => influenceGoods.includes(f.goods_id));
+      actOrderList.push(actOrder);
+    }
+    for (const data of actOrderList) {
+      tran.insert('ActOrder', data);
+    }
+  }
+}
+
+module.exports = ActOrderService;

+ 3 - 1
app/service/trade/orderDetail.js

@@ -76,8 +76,10 @@ class OrderDetailService extends CrudService {
         }
       }
       // #endregion
-      tran.insert('OrderDetail', obj);
+      const orderDetail_id = tran.insert('OrderDetail', obj);
       // arr.push(obj);
+      // 添加该商品是否和平台活动有关,做记录
+      await this.ctx.service.trade.actOrder.create(orderDetail_id, obj, tran);
     }
     // await this.model.insertMany(arr);
   }

+ 2 - 1
app/service/util/rabbitMq.js

@@ -35,7 +35,7 @@ class RabbitmqService extends Service {
       // 声明正常交换器
       await ch.assertExchange(this.task.ex, 'direct', { durable: true });
       // 声明正常队列(配置死信队列设置)
-      const q = await ch.assertQueue(this.task.queue, { exclusive: false, deadLetterExchange: this.task.deadEx, deadLetterRoutingKey: this.task.deadLetterRoutingKey });
+      const q = await ch.assertQueue(this.task.queue, { durable: true, exclusive: false, deadLetterExchange: this.task.deadEx, deadLetterRoutingKey: this.task.deadLetterRoutingKey });
       // 正常队里绑定至正常交换器
       await ch.bindQueue(q.queue, this.task.ex);
     } catch (error) {
@@ -52,6 +52,7 @@ class RabbitmqService extends Service {
       const ch = await mq.conn.createChannel();
       await ch.assertExchange(this.task.deadEx, 'direct', { durable: true });
       const qr = await ch.assertQueue(this.task.deadQueue, {
+        durable: true,
         exclusive: false,
       });
       await ch.bindQueue(qr.queue, this.task.deadEx, this.task.deadLetterRoutingKey);

+ 19 - 0
app/z_router/trade/actOrder.js

@@ -0,0 +1,19 @@
+'use strict';
+// 路由配置
+const path = require('path');
+const regPath = path.resolve('app', 'public', 'routerRegister');
+const routerRegister = require(regPath);
+const rkey = 'actOrder';
+const ckey = 'trade.actOrder';
+const keyZh = '平台活动-订单';
+const routes = [
+  { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, middleware: [ 'dealQuery' ], name: `${ckey}Query`, zh: `${keyZh}列表查询` },
+  { method: 'get', path: `${rkey}/:id`, controller: `${ckey}.show`, name: `${ckey}Show`, zh: `${keyZh}查询` },
+  // { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, name: `${ckey}Create`, zh: `创建${keyZh}` },
+  { method: 'post', path: `${rkey}/:id`, controller: `${ckey}.update`, name: `${ckey}Update`, zh: `修改${keyZh}` },
+  // { method: 'delete', path: `${rkey}/:id`, controller: `${ckey}.destroy`, name: `${ckey}Delete`, zh: `删除${keyZh}` },
+];
+
+module.exports = app => {
+  routerRegister(app, routes, keyZh, rkey, ckey);
+};

+ 1 - 0
app/z_router/trade/index.js

@@ -7,4 +7,5 @@ module.exports = app => {
   require('./afterSale')(app); // 售后
   require('./cart')(app); // 购物车
   require('./coupon')(app); // 优惠券
+  require('./actOrder')(app); // 活动-订单
 };

+ 1 - 1
app/z_router/trade/orderDetail.js

@@ -10,7 +10,7 @@ const routes = [
   { method: 'get', path: `${rkey}/sot/:id`, controller: `${ckey}.sot`, name: `${ckey}sot`, zh: `${keyZh}查询物流` },
   { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, middleware: [ 'dealQuery' ], name: `${ckey}Query`, zh: `${keyZh}列表查询` },
   { method: 'get', path: `${rkey}/:id`, controller: `${ckey}.show`, name: `${ckey}Show`, zh: `${keyZh}查询` },
-  { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, name: `${ckey}Create`, zh: `创建${keyZh}` },
+  // { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, name: `${ckey}Create`, zh: `创建${keyZh}` },
   { method: 'post', path: `${rkey}/:id`, controller: `${ckey}.update`, name: `${ckey}Update`, zh: `修改${keyZh}` },
   // { method: 'delete', path: `${rkey}/:id`, controller: `${ckey}.destroy`, name: `${ckey}Delete`, zh: `删除${keyZh}` },
 ];