lrf 2 years ago
parent
commit
21ffb34b4c

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

@@ -0,0 +1,40 @@
+module.exports = {
+  create: {
+    requestBody: ['money', 'issue', 'shop', 'can_plus', 'use_time', 'time_start', 'time_end', 'num', 'status', 'is_use', 'tags'],
+  },
+  destroy: {
+    params: ['!id'],
+    service: 'delete',
+  },
+  update: {
+    params: ['!id'],
+    requestBody: ['money', 'issue', 'shop', 'can_plus', 'use_time', 'time_start', 'time_end', 'num', 'status', 'is_use', 'tags'],
+  },
+  show: {
+    parameters: {
+      params: ['!id'],
+    },
+    service: 'fetch',
+  },
+  index: {
+    parameters: {
+      query: {
+        'meta.createdAt@start': 'meta.createdAt@start',
+        'meta.createdAt@end': 'meta.createdAt@end',
+        shop: 'shop',
+        'time_start@start': 'time_start@start',
+        'time_end@end': 'time_end@end',
+      },
+      // options: {
+      //   "meta.state": 0 // 默认条件
+      // },
+    },
+    service: 'query',
+    options: {
+      query: ['skip', 'limit'],
+      sort: ['meta.createdAt'],
+      desc: true,
+      count: true,
+    },
+  },
+};

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

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

+ 33 - 0
app/model/trade/coupon.js

@@ -0,0 +1,33 @@
+'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 coupon = {
+  issue: { type: String, required: false, zh: '发行方' }, // 字典:coupon_issue 平台/店铺
+  shop: { type: String, required: false, zh: '店铺', ref: 'Shop.Shop' }, // 店铺发行的需要有店铺关联
+  can_plus: { type: String, required: false, zh: '可叠加' }, // 字典:use
+  use_time: { type: String, required: false, zh: '时间限制' }, // 字典:use
+  time_start: { type: String, required: false, zh: '开始时间' }, //
+  time_end: { type: String, required: false, zh: '结束时间' }, //
+  num: { type: String, required: false, zh: '数量' }, // 没有就是不限制
+  status: { type: String, required: false, default: '0', zh: '状态' }, // 字典:coupon_status
+  is_use: { type: String, required: false, zh: '是否使用' }, // 字典:use
+  tags: { type: Array, required: false, zh: '商品类型' }, // 优惠券适用于的商品标签.没有就是全都适用
+};
+const schema = new Schema(coupon, { toJSON: { getters: true, virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ 'meta.createdAt': 1 });
+schema.index({ shop: 1 });
+schema.index({ time_start: 1 });
+schema.index({ time_end: 1 });
+
+schema.plugin(metaPlugin);
+schema.plugin(MoneyPlugin({ zh: '适用价格', required: false, key: 'money' }));
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Coupon', schema, 'coupon');
+};

+ 15 - 0
app/service/trade/coupon.js

@@ -0,0 +1,15 @@
+'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 CouponService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'coupon');
+    this.model = this.ctx.model.Trade.Coupon;
+  }
+}
+
+module.exports = CouponService;

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

@@ -0,0 +1,19 @@
+'use strict';
+// 路由配置
+const path = require('path');
+const regPath = path.resolve('app', 'public', 'routerRegister');
+const routerRegister = require(regPath);
+const rkey = 'coupon';
+const ckey = 'trade.coupon';
+const keyZh = '优惠券';
+const routes = [
+  { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, 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

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