product.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { ObjectId } = require('mongoose').Types;
  4. const { BusinessError, ErrorCode } = require('naf-core').Error;
  5. const _ = require('lodash');
  6. // 产品
  7. class ProductService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'product');
  10. this.model = this.ctx.model.News.Product;
  11. this.patent = this.ctx.model.Dock.Patent;
  12. this.roadShow = this.ctx.model.News.RoadShow;
  13. this.personal = this.ctx.model.User.Personal;
  14. this.expert = this.ctx.model.User.Expert;
  15. this.organization = this.ctx.model.User.Organization;
  16. this.adminModel = this.ctx.model.User.Admin;
  17. this.util = this.ctx.service.util.util;
  18. }
  19. async query({ skip = 0, limit = 0, ...query } = {}) {
  20. if (query.name) {
  21. query['%name%'] = query.name;
  22. delete query.name;
  23. }
  24. query = this.ctx.service.util.util.dealQuery(query);
  25. const { code, company, ...oq } = query;
  26. // const res = [];
  27. // const total = 0;
  28. query = await this.dealQueryCondition(query);
  29. const res = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit));
  30. const total = await this.model.count(query);
  31. // if (!code) {
  32. // if (!company) {
  33. // res = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit));
  34. // total = await this.model.count(query);
  35. // } else {
  36. // // 处理company特殊的情况
  37. // let nquery = {};
  38. // if (company === '中科系') nquery.company = [ '中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所' ];
  39. // else if (company === '其他') {
  40. // nquery.company = {
  41. // $nin: [ '中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所', '吉林大学', '长春工业大学' ],
  42. // };
  43. // } else nquery.company = company;
  44. // nquery = { ...oq, ...nquery };
  45. // res = await this.model.find(nquery).skip(parseInt(skip)).limit(parseInt(limit));
  46. // total = await this.model.count(nquery);
  47. // }
  48. // } else {
  49. // // 使用code查出个人,专家,机构下的产品,skip和limit限制的是最后产品,而不是角色那部分
  50. // let pl = await this.personal.find({ code }, '_id');
  51. // if (pl.length > 0) pl = JSON.parse(JSON.stringify(pl));
  52. // let el = await this.expert.find({ code }, '_id');
  53. // if (el.length > 0) el = JSON.parse(JSON.stringify(el));
  54. // let ol = await this.organization.find({ code }, '_id');
  55. // if (ol.length > 0) ol = JSON.parse(JSON.stringify(ol));
  56. // const ids = pl.map(i => i._id).concat(el.map(i => i._id).concat(ol.map(i => i._id)));
  57. // if (ids.length > 0) {
  58. // res = await this.model
  59. // .find({ ...oq, user_id: ids })
  60. // .skip(parseInt(skip))
  61. // .limit(parseInt(limit));
  62. // total = await this.model.count({ ...oq, user_id: ids });
  63. // }
  64. // }
  65. return { data: res, total };
  66. }
  67. async dealQueryCondition({ role, code, ...condition } = {}) {
  68. condition = this.util.dealQuery(condition);
  69. // 查询业务管理
  70. const busFind = async query => await this.adminModel.find({ ...query, role: '3' }, { code: 1 });
  71. // 查询机构管理
  72. const orgFind = async query => await this.adminModel.find({ ...query, role: '2' }, { code: 1 });
  73. // 查询管理员
  74. const aFind = async query => await this.adminModel.find({ ...query, role: '1' }, { code: 1 });
  75. if (role === '1' && code) {
  76. // 管理员查询
  77. // =>获取该code下的机构管理员列表 => 用机构管理员id 获取业务管理员列表 => 将code都整理出来作为查询条件
  78. const a = await aFind({ code });
  79. if (a.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该管理员');
  80. const aid = _.get(_.head(a), '_id');
  81. const orgList = await orgFind({ pid: aid });
  82. const busList = await busFind({ pid: orgList.map(i => i._id) });
  83. const codes = [ ...orgList.map(i => i.code), ...busList.map(i => i.code), code ];
  84. condition.code = codes;
  85. } else if (role === '2' && code) {
  86. // 机构查询
  87. // =>获取该code下的业务管理员列表 => 将code都整理出来作为查询条件
  88. const o = await orgFind({ code });
  89. if (o.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该机构');
  90. const oid = _.get(_.head(o), '_id');
  91. const busList = await busFind({ pid: oid });
  92. const codes = [ ...busList.map(i => i.code), code ];
  93. condition.code = codes;
  94. } else if (code) {
  95. // 业务查询
  96. // code直接查询用户返回即可
  97. condition.code = code;
  98. }
  99. // 没有code,超级管理员,说明不限制
  100. // 需要用code查出这些code下管理的用户(机构,个人,专家)
  101. if (condition.code) {
  102. const pList = await this.personal.find({ code: condition.code }, { _id: 1 });
  103. const oList = await this.organization.find({ code: condition.code }, { _id: 1 });
  104. const eList = await this.expert.find({ code: condition.code }, { _id: 1 });
  105. const user_id = [ ...pList.map(i => i._id), ...oList.map(i => i._id), ...eList.map(i => i._id) ];
  106. condition.user_id = user_id;
  107. }
  108. // 删除这俩条件,表里没有
  109. delete condition.code;
  110. delete condition.role;
  111. return condition;
  112. }
  113. async indexQuery() {
  114. // product,limit=>6;status=>2;type=>0/1/2
  115. // 科技需求
  116. const require = await this.getSample('model', 6, { type: '0', status: '2' });
  117. console.log('in function:1');
  118. // 技术成果
  119. const achieve = await this.getSample('model', 6, { type: '1', status: '2' });
  120. console.log('in function:2');
  121. // 商务服务
  122. const serve = await this.getSample('model', 5, { type: '2', status: '2' });
  123. console.log('in function:3');
  124. // 专利:patent limit=>6
  125. const patent = await this.getSample('patent');
  126. console.log('in function:4');
  127. // 专家:expert:limit=>8
  128. const expert = await this.getSample('expert', 8);
  129. console.log('in function:6');
  130. // 专家需要姓名
  131. const ids = expert.map(i => i.user_id);
  132. const personal = await this.personal.find({ _id: ids }, 'name');
  133. for (const exp of expert) {
  134. const r = await personal.find(f => ObjectId(f._id).equals(exp.user_id));
  135. if (r) exp.name = r.name;
  136. }
  137. // 路演:roadShow:limit=>5
  138. const roadShow = await this.getSample('roadShow', 5);
  139. return { require, achieve, serve, patent, expert, roadShow };
  140. }
  141. async getSample(model, limit = 6, match = {}) {
  142. const res = await this[model].aggregate([{ $match: match }, { $sample: { size: parseInt(limit) } }]);
  143. return res;
  144. }
  145. }
  146. module.exports = ProductService;