lrf 2 rokov pred
rodič
commit
af513bbd87

+ 6 - 1
app/controller/user/notice.js

@@ -3,11 +3,16 @@ const meta = require('./config/.notice.js');
 const Controller = require('egg').Controller;
 const { CrudController } = require('naf-framework-mongoose-free/lib/controller');
 
-// 
+//
 class NoticeController extends Controller {
   constructor(ctx) {
     super(ctx);
     this.service = this.ctx.service.user.notice;
   }
+
+  async msgList() {
+    const res = await this.service.msgList(this.ctx.query);
+    this.ctx.ok(res);
+  }
 }
 module.exports = CrudController(NoticeController, meta);

+ 26 - 0
app/service/user/notice.js

@@ -3,6 +3,7 @@ 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 NoticeService extends CrudService {
@@ -15,9 +16,34 @@ class NoticeService extends CrudService {
     const { customer = [], ...others } = body;
     if (!_.isArray(customer)) throw new BusinessError(ErrorCode.DATA_INVALID, '数据格式错误');
     if (customer.length <= 0) throw new BusinessError(ErrorCode.BADPARAM, '缺少发送对象');
+    if (!_.get(others, 'source_id') && _.get(others, 'source') === '0') others.source_id = ObjectId();
     const arr = customer.map(i => ({ customer: i, ...others }));
     await this.model.insertMany(arr);
   }
+
+  async msgList(query) {
+    const { skip, limit, ...others } = query;
+    const pipeline = [];
+    const $match = {};
+    if (_.get(others, 'source')) $match.source = _.get(others, 'source');
+    if (_.get(others, 'source_id')) $match.source_id = _.get(others, 'source_id');
+    if (_.get(others, 'customer')) $match.customer = _.get(others, 'customer');
+    if (Object.keys($match).length > 0) pipeline.push({ $match });
+    pipeline.push({ $group: { _id: '$source_id', source: { $first: '$source' }, time: { $first: '$time' } } });
+    pipeline.push({ $sort: { time: -1 } });
+
+    const qp = _.cloneDeep(pipeline);
+    if (skip && limit) {
+      qp.push({ $skip: parseInt(skip) });
+      qp.push({ $limit: parseInt(limit) });
+    }
+    const data = await this.model.aggregate(qp);
+    const tp = _.cloneDeep(pipeline);
+    tp.push({ $count: 'total' });
+    const tr = await this.model.aggregate(tp);
+    const total = _.get(_.head(tr), 'total', 0);
+    return { data, total };
+  }
 }
 
 module.exports = NoticeService;

+ 1 - 0
app/z_router/user/notice.js

@@ -7,6 +7,7 @@ const rkey = 'notice';
 const ckey = 'user.notice';
 const keyZh = '消息通知';
 const routes = [
+  { method: 'get', path: `${rkey}/msgList`, controller: `${ckey}.msgList`, name: `${ckey}msgList`, zh: `${keyZh}管理员列表查询` },
   { 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}` },