product.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/lib/service');
  3. const { ObjectId } = require('mongoose').Types;
  4. // 产品
  5. class ProductService extends CrudService {
  6. constructor(ctx) {
  7. super(ctx, 'product');
  8. this.model = this.ctx.model.Product;
  9. }
  10. // async query({ skip = 0, limit = 0, ...query } = {}) {
  11. // if (query.name) {
  12. // query['%name%'] = query.name;
  13. // delete query.name;
  14. // }
  15. // query = this.ctx.service.util.util.dealQuery(query);
  16. // const { code, company, ...oq } = query;
  17. // let res = [];
  18. // let total = 0;
  19. // if (!code) {
  20. // if (!company) {
  21. // res = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit));
  22. // total = await this.model.count(query);
  23. // } else {
  24. // // 处理company特殊的情况
  25. // let nquery = {};
  26. // if (company === '中科系') nquery.company = [ '中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所' ];
  27. // else if (company === '其他')nquery.company = { $nin: [ '中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所', '吉林大学', '长春工业大学' ] };
  28. // else nquery.company = company;
  29. // nquery = { ...oq, ...nquery };
  30. // res = await this.model.find(nquery).skip(parseInt(skip)).limit(parseInt(limit));
  31. // total = await this.model.count(nquery);
  32. // }
  33. // } else {
  34. // // 使用code查出个人,专家,机构下的产品,skip和limit限制的是最后产品,而不是角色那部分
  35. // let pl = await this.personal.find({ code }, '_id');
  36. // if (pl.length > 0)pl = JSON.parse(JSON.stringify(pl));
  37. // let el = await this.expert.find({ code }, '_id');
  38. // if (el.length > 0)el = JSON.parse(JSON.stringify(el));
  39. // let ol = await this.organization.find({ code }, '_id');
  40. // if (ol.length > 0)ol = JSON.parse(JSON.stringify(ol));
  41. // const ids = pl.map(i => i._id).concat(el.map(i => i._id).concat(ol.map(i => i._id)));
  42. // if (ids.length > 0) {
  43. // res = await this.model.find({ ...oq, user_id: ids }).skip(parseInt(skip)).limit(parseInt(limit));
  44. // total = await this.model.count({ ...oq, user_id: ids });
  45. // }
  46. // }
  47. // return { data: res, total };
  48. // }
  49. async indexQuery() {
  50. // product,limit=>6;status=>2;type=>0/1/2
  51. // 科技需求
  52. const require = await this.getSample('model', 6, { type: '0', status: '2' });
  53. // 技术成果
  54. const achieve = await this.getSample('model', 6, { type: '1', status: '2' });
  55. // 商务服务
  56. const serve = await this.getSample('model', 5, { type: '2', status: '2' });
  57. // 专利:patent limit=>6
  58. const patent = await this.getSample('patent');
  59. // 专家:expert:limit=>8
  60. const expert = await this.getSample('expert', 8);
  61. // 专家需要姓名
  62. const ids = expert.map(i => i.user_id);
  63. const personal = await this.personal.find({ _id: ids }, 'name');
  64. for (const exp of expert) {
  65. const r = await personal.find(f => ObjectId(f._id).equals(exp.user_id));
  66. if (r) exp.name = r.name;
  67. }
  68. // 路演:roadShow:limit=>5
  69. const roadShow = await this.getSample('roadShow', 5);
  70. return { require, achieve, serve, patent, expert, roadShow };
  71. }
  72. async getSample(model, limit = 6, match = {}) {
  73. const res = await this[model].aggregate([
  74. { $match: match },
  75. { $sample: { size: parseInt(limit) } },
  76. ]);
  77. return res;
  78. }
  79. }
  80. module.exports = ProductService;