product.js 4.0 KB

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