product.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/lib/service');
  3. const { ObjectId } = require('mongoose').Types;
  4. const assert = require('assert');
  5. const Excel = require('exceljs');
  6. // 产品
  7. class ProductService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'product');
  10. this.model = this.ctx.model.Product;
  11. this.patent = this.ctx.model.Dock.Patent;
  12. this.roadShow = this.ctx.model.RoadShow;
  13. this.personal = this.ctx.model.Personal;
  14. this.expert = this.ctx.model.Expert;
  15. this.organization = this.ctx.model.Organization;
  16. }
  17. async query({ skip = 0, limit = 0, ...query } = {}) {
  18. if (query.name) {
  19. query['%name%'] = query.name;
  20. delete query.name;
  21. }
  22. query = this.ctx.service.util.util.dealQuery(query);
  23. const { code, company, ...oq } = query;
  24. let res = [];
  25. let total = 0;
  26. if (!code) {
  27. if (!company) {
  28. res = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit));
  29. total = await this.model.count(query);
  30. } else {
  31. // 处理company特殊的情况
  32. let nquery = {};
  33. if (company === '中科系') nquery.company = ['中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所'];
  34. else if (company === '其他')
  35. nquery.company = {
  36. $nin: ['中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所', '吉林大学', '长春工业大学'],
  37. };
  38. else nquery.company = company;
  39. nquery = { ...oq, ...nquery };
  40. res = await this.model.find(nquery).skip(parseInt(skip)).limit(parseInt(limit));
  41. total = await this.model.count(nquery);
  42. }
  43. } else {
  44. // 使用code查出个人,专家,机构下的产品,skip和limit限制的是最后产品,而不是角色那部分
  45. let pl = await this.personal.find({ code }, '_id');
  46. if (pl.length > 0) pl = JSON.parse(JSON.stringify(pl));
  47. let el = await this.expert.find({ code }, '_id');
  48. if (el.length > 0) el = JSON.parse(JSON.stringify(el));
  49. let ol = await this.organization.find({ code }, '_id');
  50. if (ol.length > 0) ol = JSON.parse(JSON.stringify(ol));
  51. const ids = pl.map((i) => i._id).concat(el.map((i) => i._id).concat(ol.map((i) => i._id)));
  52. if (ids.length > 0) {
  53. res = await this.model
  54. .find({ ...oq, user_id: ids })
  55. .skip(parseInt(skip))
  56. .limit(parseInt(limit));
  57. total = await this.model.count({ ...oq, user_id: ids });
  58. }
  59. }
  60. return { data: res, total };
  61. }
  62. async indexQuery() {
  63. // product,limit=>6;status=>2;type=>0/1/2
  64. // 科技需求
  65. const require = await this.getSample('model', 6, { type: '0', status: '2' });
  66. console.log('in function:1');
  67. // 技术成果
  68. const achieve = await this.getSample('model', 6, { type: '1', status: '2' });
  69. console.log('in function:2');
  70. // 商务服务
  71. const serve = await this.getSample('model', 5, { type: '2', status: '2' });
  72. console.log('in function:3');
  73. // 专利:patent limit=>6
  74. const patent = await this.getSample('patent');
  75. console.log('in function:4');
  76. // 专家:expert:limit=>8
  77. const expert = await this.getSample('expert', 8);
  78. console.log('in function:6');
  79. // 专家需要姓名
  80. const ids = expert.map((i) => i.user_id);
  81. const personal = await this.personal.find({ _id: ids }, 'name');
  82. for (const exp of expert) {
  83. const r = await personal.find((f) => ObjectId(f._id).equals(exp.user_id));
  84. if (r) exp.name = r.name;
  85. }
  86. // 路演:roadShow:limit=>5
  87. const roadShow = await this.getSample('roadShow', 5);
  88. return { require, achieve, serve, patent, expert, roadShow };
  89. }
  90. async getSample(model, limit = 6, match = {}) {
  91. const res = await this[model].aggregate([{ $match: match }, { $sample: { size: parseInt(limit) } }]);
  92. return res;
  93. }
  94. /**
  95. * 导入
  96. * @param {String} uri 文件地址
  97. * @property {Object} defObject 默认属性
  98. */
  99. async import({ uri, defObject }) {
  100. assert(uri, '未接接收到文件地址,无法导入!');
  101. const domain = 'http://127.0.0.1';
  102. const file = await this.ctx.curl(`${domain}${uri}`);
  103. if (!(file && file.data)) {
  104. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定文件');
  105. }
  106. const workbook = new Excel.Workbook();
  107. await workbook.xlsx.load(file.data);
  108. const sheet = workbook.getWorksheet(1);
  109. const head = sheet.getRow(1).values;
  110. // 成果名称相同 且 用户id相同 不需要导入
  111. let cols = this.getMeta();
  112. for (const i of cols) {
  113. const { zh } = i;
  114. const index = head.findIndex((f) => f === zh);
  115. if (index > -1) i.index = index;
  116. }
  117. cols = cols.filter((f) => f.index || f.index === 0);
  118. const data = [];
  119. sheet.eachRow((row, index) => {
  120. if (index === 1) {
  121. // 啥也不做,第一行是表头
  122. } else {
  123. const obj = {};
  124. for (const i of cols) {
  125. const { key, index } = i;
  126. obj[key] = row.values[index];
  127. }
  128. // 添加默认值
  129. data.push({ ...obj, ...defObject });
  130. }
  131. });
  132. for (const d of data) {
  133. const count = await this.model.count({ name: d.name, user_id: defObject.user_id });
  134. if (count <= 0) {
  135. this.model.create(d);
  136. }
  137. }
  138. }
  139. getMeta() {
  140. return [
  141. { zh: '企业名称', key: 'company' },
  142. { zh: '联系电话', key: 'phone' },
  143. { zh: '电子邮箱', key: 'email' },
  144. { zh: '联系人', key: 'contacts' },
  145. { zh: '成果名称', key: 'name' },
  146. { zh: '所属领域', key: 'field' },
  147. { zh: '合作方式', key: 'cooperation' },
  148. { zh: '成果状态', key: 'achievestatus' },
  149. { zh: '成果简介', key: 'achievebrief' },
  150. { zh: '技术特点', key: 'features' },
  151. ];
  152. }
  153. }
  154. module.exports = ProductService;