|
@@ -3,9 +3,97 @@ import { InjectEntityModel } from '@midwayjs/typegoose';
|
|
|
import { ReturnModelType } from '@typegoose/typegoose';
|
|
|
import { BaseService } from 'free-midway-component';
|
|
|
import { Notice } from '../entity/notice.entity';
|
|
|
+import { InjectClient } from '@midwayjs/core';
|
|
|
+const assert = require('assert');
|
|
|
+import { HttpServiceFactory, HttpService } from '@midwayjs/axios';
|
|
|
+import _ = require('lodash');
|
|
|
type modelType = ReturnModelType<typeof Notice>;
|
|
|
@Provide()
|
|
|
export class NoticeService extends BaseService<modelType> {
|
|
|
@InjectEntityModel(Notice)
|
|
|
model: modelType;
|
|
|
+
|
|
|
+ @InjectClient(HttpServiceFactory, 'zkzx_admin')
|
|
|
+ adminAxios: HttpService;
|
|
|
+
|
|
|
+ async create(body) {
|
|
|
+ const code = _.get(this.ctx.user, 'code');
|
|
|
+ assert(code, '缺少用户信息');
|
|
|
+ const send_type = _.get(body, 'send_type');
|
|
|
+ const type = _.get(body, 'type');
|
|
|
+ if (send_type === '1') {
|
|
|
+ if (type === '0') {
|
|
|
+ const p1List = await this.findByAdmin(code);
|
|
|
+ const p2List = await this.findPersonal(code);
|
|
|
+ const p3List = await this.findByOrg(code);
|
|
|
+ body.receive = _.concat(p1List, p2List, p3List);
|
|
|
+ } else if (type === '1') {
|
|
|
+ const p1List = await this.findByAdmin(code);
|
|
|
+ body.receive = p1List;
|
|
|
+ } else if (type === '2') {
|
|
|
+ const p2List = await this.findPersonal(code);
|
|
|
+ body.receive = p2List;
|
|
|
+ }
|
|
|
+ } else if (send_type === '2') {
|
|
|
+ if (type === '2') {
|
|
|
+ const p2List = await this.findPersonal(code);
|
|
|
+ body.receive = p2List;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ const res = await this.model.create(body);
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+ // 查询管理下机构
|
|
|
+ async findByAdmin(code) {
|
|
|
+ const res = await this.adminAxios.get(`/admin?code=${code}`);
|
|
|
+ if (!res) return [];
|
|
|
+ const receive = [];
|
|
|
+ for (const r of res.data) {
|
|
|
+ const pr = await this.adminAxios.get(`/admin?pid=${r._id}&type=${'2'}`);
|
|
|
+ const list = pr.data.map(i => ({
|
|
|
+ user_id: i._id,
|
|
|
+ name: i.name,
|
|
|
+ is_read: '0',
|
|
|
+ }));
|
|
|
+ receive.push(...list);
|
|
|
+ }
|
|
|
+ return receive;
|
|
|
+ }
|
|
|
+ // 查询管理下的个人或机构下的个人
|
|
|
+ async findPersonal(code) {
|
|
|
+ const res = await this.adminAxios.get(`/personal?code=${code}`);
|
|
|
+ if (res.data)
|
|
|
+ return res.data.map(i => ({
|
|
|
+ user_id: i._id,
|
|
|
+ name: i.name,
|
|
|
+ is_read: '0',
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ // 查询管理下机构下的个人
|
|
|
+ async findByOrg(code) {
|
|
|
+ const arr = await this.adminAxios.get(`/admin?code=${code}`);
|
|
|
+ if (!arr) return [];
|
|
|
+ const receive = [];
|
|
|
+ for (const val of arr.data) {
|
|
|
+ const res = await this.adminAxios.get(
|
|
|
+ `/admin?pid=${val._id}&type=${'2'}`
|
|
|
+ );
|
|
|
+ for (const i of res.data) {
|
|
|
+ const orgCode = _.get(i, 'code');
|
|
|
+ const pList = await this.findPersonal(orgCode);
|
|
|
+ receive.push(...pList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return receive;
|
|
|
+ }
|
|
|
+ // 特殊查询处理
|
|
|
+ async beforeQuery(query) {
|
|
|
+ const { skip = 0, limit = 0, user_id, ...info } = query;
|
|
|
+ if (user_id) {
|
|
|
+ info.receive = { $elemMatch: { user_id: user_id } };
|
|
|
+ }
|
|
|
+ const data = await this.model.find(info).skip(skip).limit(limit);
|
|
|
+ const total = await this.model.count(info);
|
|
|
+ return { data, total };
|
|
|
+ }
|
|
|
}
|