|
@@ -1,6 +1,8 @@
|
|
'use strict';
|
|
'use strict';
|
|
const { CrudService } = require('naf-framework-mongoose-free/lib/service');
|
|
const { CrudService } = require('naf-framework-mongoose-free/lib/service');
|
|
const { ObjectId } = require('mongoose').Types;
|
|
const { ObjectId } = require('mongoose').Types;
|
|
|
|
+const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
|
+const _ = require('lodash');
|
|
// 产品
|
|
// 产品
|
|
class ProductService extends CrudService {
|
|
class ProductService extends CrudService {
|
|
constructor(ctx) {
|
|
constructor(ctx) {
|
|
@@ -11,6 +13,8 @@ class ProductService extends CrudService {
|
|
this.personal = this.ctx.model.User.Personal;
|
|
this.personal = this.ctx.model.User.Personal;
|
|
this.expert = this.ctx.model.User.Expert;
|
|
this.expert = this.ctx.model.User.Expert;
|
|
this.organization = this.ctx.model.User.Organization;
|
|
this.organization = this.ctx.model.User.Organization;
|
|
|
|
+ this.adminModel = this.ctx.model.User.Admin;
|
|
|
|
+ this.util = this.ctx.service.util.util;
|
|
}
|
|
}
|
|
async query({ skip = 0, limit = 0, ...query } = {}) {
|
|
async query({ skip = 0, limit = 0, ...query } = {}) {
|
|
if (query.name) {
|
|
if (query.name) {
|
|
@@ -19,40 +23,97 @@ class ProductService extends CrudService {
|
|
}
|
|
}
|
|
query = this.ctx.service.util.util.dealQuery(query);
|
|
query = this.ctx.service.util.util.dealQuery(query);
|
|
const { code, company, ...oq } = query;
|
|
const { code, company, ...oq } = query;
|
|
- let res = [];
|
|
|
|
- let total = 0;
|
|
|
|
- if (!code) {
|
|
|
|
- if (!company) {
|
|
|
|
- res = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit));
|
|
|
|
- total = await this.model.count(query);
|
|
|
|
- } else {
|
|
|
|
- // 处理company特殊的情况
|
|
|
|
- let nquery = {};
|
|
|
|
- if (company === '中科系') nquery.company = [ '中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所' ];
|
|
|
|
- else if (company === '其他')nquery.company = { $nin: [ '中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所', '吉林大学', '长春工业大学' ] };
|
|
|
|
- else nquery.company = company;
|
|
|
|
- nquery = { ...oq, ...nquery };
|
|
|
|
- res = await this.model.find(nquery).skip(parseInt(skip)).limit(parseInt(limit));
|
|
|
|
- total = await this.model.count(nquery);
|
|
|
|
- }
|
|
|
|
|
|
+ // const res = [];
|
|
|
|
+ // const total = 0;
|
|
|
|
+ query = await this.dealQueryCondition(query);
|
|
|
|
+ const res = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit));
|
|
|
|
+ const total = await this.model.count(query);
|
|
|
|
|
|
- } else {
|
|
|
|
- // 使用code查出个人,专家,机构下的产品,skip和limit限制的是最后产品,而不是角色那部分
|
|
|
|
- let pl = await this.personal.find({ code }, '_id');
|
|
|
|
- if (pl.length > 0)pl = JSON.parse(JSON.stringify(pl));
|
|
|
|
- let el = await this.expert.find({ code }, '_id');
|
|
|
|
- if (el.length > 0)el = JSON.parse(JSON.stringify(el));
|
|
|
|
- let ol = await this.organization.find({ code }, '_id');
|
|
|
|
- if (ol.length > 0)ol = JSON.parse(JSON.stringify(ol));
|
|
|
|
- const ids = pl.map(i => i._id).concat(el.map(i => i._id).concat(ol.map(i => i._id)));
|
|
|
|
- if (ids.length > 0) {
|
|
|
|
- res = await this.model.find({ ...oq, user_id: ids }).skip(parseInt(skip)).limit(parseInt(limit));
|
|
|
|
- total = await this.model.count({ ...oq, user_id: ids });
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ // if (!code) {
|
|
|
|
+ // if (!company) {
|
|
|
|
+ // res = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit));
|
|
|
|
+ // total = await this.model.count(query);
|
|
|
|
+ // } else {
|
|
|
|
+ // // 处理company特殊的情况
|
|
|
|
+ // let nquery = {};
|
|
|
|
+ // if (company === '中科系') nquery.company = [ '中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所' ];
|
|
|
|
+ // else if (company === '其他') {
|
|
|
|
+ // nquery.company = {
|
|
|
|
+ // $nin: [ '中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所', '吉林大学', '长春工业大学' ],
|
|
|
|
+ // };
|
|
|
|
+ // } else nquery.company = company;
|
|
|
|
+ // nquery = { ...oq, ...nquery };
|
|
|
|
+ // res = await this.model.find(nquery).skip(parseInt(skip)).limit(parseInt(limit));
|
|
|
|
+ // total = await this.model.count(nquery);
|
|
|
|
+ // }
|
|
|
|
+ // } else {
|
|
|
|
+ // // 使用code查出个人,专家,机构下的产品,skip和limit限制的是最后产品,而不是角色那部分
|
|
|
|
+ // let pl = await this.personal.find({ code }, '_id');
|
|
|
|
+ // if (pl.length > 0) pl = JSON.parse(JSON.stringify(pl));
|
|
|
|
+ // let el = await this.expert.find({ code }, '_id');
|
|
|
|
+ // if (el.length > 0) el = JSON.parse(JSON.stringify(el));
|
|
|
|
+ // let ol = await this.organization.find({ code }, '_id');
|
|
|
|
+ // if (ol.length > 0) ol = JSON.parse(JSON.stringify(ol));
|
|
|
|
+ // const ids = pl.map(i => i._id).concat(el.map(i => i._id).concat(ol.map(i => i._id)));
|
|
|
|
+ // if (ids.length > 0) {
|
|
|
|
+ // res = await this.model
|
|
|
|
+ // .find({ ...oq, user_id: ids })
|
|
|
|
+ // .skip(parseInt(skip))
|
|
|
|
+ // .limit(parseInt(limit));
|
|
|
|
+ // total = await this.model.count({ ...oq, user_id: ids });
|
|
|
|
+ // }
|
|
|
|
+ // }
|
|
return { data: res, total };
|
|
return { data: res, total };
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ async dealQueryCondition({ role, code, ...condition } = {}) {
|
|
|
|
+ condition = this.util.dealQuery(condition);
|
|
|
|
+ // 查询业务管理
|
|
|
|
+ const busFind = async query => await this.adminModel.find({ ...query, role: '3' }, { code: 1 });
|
|
|
|
+ // 查询机构管理
|
|
|
|
+ const orgFind = async query => await this.adminModel.find({ ...query, role: '2' }, { code: 1 });
|
|
|
|
+ // 查询管理员
|
|
|
|
+ const aFind = async query => await this.adminModel.find({ ...query, role: '1' }, { code: 1 });
|
|
|
|
+ if (role === '1' && code) {
|
|
|
|
+ // 管理员查询
|
|
|
|
+ // =>获取该code下的机构管理员列表 => 用机构管理员id 获取业务管理员列表 => 将code都整理出来作为查询条件
|
|
|
|
+ const a = await aFind({ code });
|
|
|
|
+ if (a.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该管理员');
|
|
|
|
+ const aid = _.get(_.head(a), '_id');
|
|
|
|
+ const orgList = await orgFind({ pid: aid });
|
|
|
|
+ const busList = await busFind({ pid: orgList.map(i => i._id) });
|
|
|
|
+ const codes = [ ...orgList.map(i => i.code), ...busList.map(i => i.code), code ];
|
|
|
|
+ condition.code = codes;
|
|
|
|
+ } else if (role === '2' && code) {
|
|
|
|
+ // 机构查询
|
|
|
|
+ // =>获取该code下的业务管理员列表 => 将code都整理出来作为查询条件
|
|
|
|
+ const o = await orgFind({ code });
|
|
|
|
+ if (o.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该机构');
|
|
|
|
+ const oid = _.get(_.head(o), '_id');
|
|
|
|
+ const busList = await busFind({ pid: oid });
|
|
|
|
+ const codes = [ ...busList.map(i => i.code), code ];
|
|
|
|
+ condition.code = codes;
|
|
|
|
+ } else if (code) {
|
|
|
|
+ // 业务查询
|
|
|
|
+ // code直接查询用户返回即可
|
|
|
|
+ condition.code = code;
|
|
|
|
+ }
|
|
|
|
+ // 没有code,超级管理员,说明不限制
|
|
|
|
+ // 需要用code查出这些code下管理的用户(机构,个人,专家)
|
|
|
|
+ if (condition.code) {
|
|
|
|
+ const pList = await this.personal.find({ code: condition.code }, { _id: 1 });
|
|
|
|
+ const oList = await this.organization.find({ code: condition.code }, { _id: 1 });
|
|
|
|
+ const eList = await this.expert.find({ code: condition.code }, { _id: 1 });
|
|
|
|
+ const user_id = [ ...pList.map(i => i._id), ...oList.map(i => i._id), ...eList.map(i => i._id) ];
|
|
|
|
+ condition.user_id = user_id;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ // 删除这俩条件,表里没有
|
|
|
|
+ delete condition.code;
|
|
|
|
+ delete condition.role;
|
|
|
|
+ return condition;
|
|
|
|
+ }
|
|
|
|
+
|
|
async indexQuery() {
|
|
async indexQuery() {
|
|
// product,limit=>6;status=>2;type=>0/1/2
|
|
// product,limit=>6;status=>2;type=>0/1/2
|
|
// 科技需求
|
|
// 科技需求
|
|
@@ -83,10 +144,7 @@ class ProductService extends CrudService {
|
|
}
|
|
}
|
|
|
|
|
|
async getSample(model, limit = 6, match = {}) {
|
|
async getSample(model, limit = 6, match = {}) {
|
|
- const res = await this[model].aggregate([
|
|
|
|
- { $match: match },
|
|
|
|
- { $sample: { size: parseInt(limit) } },
|
|
|
|
- ]);
|
|
|
|
|
|
+ const res = await this[model].aggregate([{ $match: match }, { $sample: { size: parseInt(limit) } }]);
|
|
return res;
|
|
return res;
|
|
}
|
|
}
|
|
}
|
|
}
|