'use strict'; const { CrudService } = require('naf-framework-mongoose/lib/service'); const { ObjectId } = require('mongoose').Types; // 产品 class ProductService extends CrudService { constructor(ctx) { super(ctx, 'product'); this.model = this.ctx.model.Product; } // 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; // 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); // } // } 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 indexQuery() { // product,limit=>6;status=>2;type=>0/1/2 // 科技需求 const require = await this.getSample('model', 6, { type: '0', status: '2' }); // 技术成果 const achieve = await this.getSample('model', 6, { type: '1', status: '2' }); // 商务服务 const serve = await this.getSample('model', 5, { type: '2', status: '2' }); // 专利:patent limit=>6 const patent = await this.getSample('patent'); // 专家:expert:limit=>8 const expert = await this.getSample('expert', 8); // 专家需要姓名 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;