'use strict'; const { CrudService } = require('naf-framework-mongoose-free/lib/service'); const { ObjectId } = require('mongoose').Types; const { BusinessError, ErrorCode } = require('naf-core').Error; const _ = require('lodash'); // 产品 class ProductService extends CrudService { constructor(ctx) { super(ctx, 'product'); this.model = this.ctx.model.News.Product; this.patent = this.ctx.model.Dock.Patent; this.roadShow = this.ctx.model.News.RoadShow; this.personal = this.ctx.model.User.Personal; this.expert = this.ctx.model.User.Expert; 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 } = {}) { if (query.name) { query['%name%'] = query.name; delete query.name; } query = this.ctx.service.util.util.dealQuery(query); const { code, company, ...oq } = query; // 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); // 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 }; } 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() { // product,limit=>6;status=>2;type=>0/1/2 // 科技需求 const require = await this.getSample('model', 6, { type: '0', status: '2' }); console.log('in function:1'); // 技术成果 const achieve = await this.getSample('model', 6, { type: '1', status: '2' }); console.log('in function:2'); // 商务服务 const serve = await this.getSample('model', 5, { type: '2', status: '2' }); console.log('in function:3'); // 专利:patent limit=>6 const patent = await this.getSample('patent'); console.log('in function:4'); // 专家:expert:limit=>8 const expert = await this.getSample('expert', 8); console.log('in function:6'); // 专家需要姓名 const ids = expert.map(i => i.user_id); const personal = await this.personal.find({ _id: ids }, 'name'); for (const exp of expert) { const r = await personal.find(f => ObjectId(f._id).equals(exp.user_id)); if (r) exp.name = r.name; } // 路演:roadShow:limit=>5 const roadShow = await this.getSample('roadShow', 5); return { require, achieve, serve, patent, expert, roadShow }; } async getSample(model, limit = 6, match = {}) { const res = await this[model].aggregate([{ $match: match }, { $sample: { size: parseInt(limit) } }]); return res; } } module.exports = ProductService;