|
@@ -3,12 +3,71 @@ const { CrudService } = require('naf-framework-mongoose/lib/service');
|
|
|
const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
const _ = require('lodash');
|
|
|
const assert = require('assert');
|
|
|
+const { trimData } = require('naf-core').Util;
|
|
|
|
|
|
// 通知
|
|
|
class PatentnoticeService extends CrudService {
|
|
|
constructor(ctx) {
|
|
|
super(ctx, 'patentnotice');
|
|
|
this.model = this.ctx.model.Patent.Patentnotice;
|
|
|
+ this.personalModel = this.ctx.model.Personal;
|
|
|
+ this.adminModel = this.ctx.model.Admin;
|
|
|
+ }
|
|
|
+
|
|
|
+ async query(query, { skip = 0, limit = 0 } = 0) {
|
|
|
+ query = await this.resetQuery(query);
|
|
|
+ const data = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit))
|
|
|
+ .sort({ 'meta.createdAt': -1 });
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ async count(query) {
|
|
|
+ query = await this.resetQuery(query);
|
|
|
+ const res = await this.model.countDocuments(trimData(query)).exec();
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ async resetQuery(condition) {
|
|
|
+ const { to_id } = condition;
|
|
|
+ if (to_id) {
|
|
|
+ condition.to_id = { $elemMatch: { $in: [ to_id ] } };
|
|
|
+ }
|
|
|
+ return condition;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据send_id,和to_type,查询范围内的用户数据
|
|
|
+ * to_type=1:只查机构用户
|
|
|
+ * ...=2:只查平台用户
|
|
|
+ * ...=0:都查
|
|
|
+ * @param {Object} body 数据
|
|
|
+ */
|
|
|
+ async create(body) {
|
|
|
+ const arr = []; // 最后insert的数据集
|
|
|
+ const { send_id, to_type, content, send_name } = body;
|
|
|
+ const admin = await this.adminModel.findById(send_id);
|
|
|
+ if (!admin) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到管理员信息');
|
|
|
+ const org = await this.personalModel.find({ pid: admin._id }, { id: 1, code: 1 });
|
|
|
+ if (org.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到相关机构信息');
|
|
|
+ if (to_type === '1') {
|
|
|
+ const ids = org.map(i => i._id);
|
|
|
+ for (const id of ids) {
|
|
|
+ const obj = { send_id, send_name, to_type, to_id: id, content };
|
|
|
+ arr.push(obj);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ const p1 = await this.personalModel.find({ code: admin.code }, { id: 1 });
|
|
|
+ const p2 = await this.personalModel.find({ code: org.map(i => i.code) }, { id: 1 });
|
|
|
+ let ids = [ ...p1.map(i => i._id), ...p2.map(i => i._id) ];
|
|
|
+ if (to_type === 0) {
|
|
|
+ ids = [ ...ids, ...org.map(i => i._id) ];
|
|
|
+ }
|
|
|
+ for (const id of ids) {
|
|
|
+ const obj = { send_id, send_name, to_type, to_id: id, content };
|
|
|
+ arr.push(obj);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return await this.model.insertMany(arr);
|
|
|
}
|
|
|
}
|
|
|
|