123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { ObjectId } = require('mongoose').Types;
- const assert = require('assert');
- const Excel = require('exceljs');
- // 产品
- class ProductService extends CrudService {
- constructor(ctx) {
- super(ctx, 'product');
- this.model = this.ctx.model.Product;
- this.patent = this.ctx.model.Dock.Patent;
- this.roadShow = this.ctx.model.RoadShow;
- this.personal = this.ctx.model.Personal;
- this.expert = this.ctx.model.Expert;
- this.organization = this.ctx.model.Organization;
- }
- 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' });
- console.log('in function:1');
- // 技术成果
- const achieve = await this.getSample('model', 6, { type: '1', status: '2' });
- console.log('in function:2');
- // 商务服务
- const serve = await this.getSample('model', 5, { type: '2', status: '2' });
- console.log('in function:3');
- // 专利:patent limit=>6
- const patent = await this.getSample('patent');
- console.log('in function:4');
- // 专家:expert:limit=>8
- const expert = await this.getSample('expert', 8);
- console.log('in function:6');
- // 专家需要姓名
- 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;
- }
- /**
- * 导入
- * @param {String} uri 文件地址
- * @property {Object} defObject 默认属性
- */
- async import({ uri, defObject }) {
- assert(uri, '未接接收到文件地址,无法导入!');
- const domain = 'http://127.0.0.1';
- const file = await this.ctx.curl(`${domain}${uri}`);
- if (!(file && file.data)) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定文件');
- }
- const workbook = new Excel.Workbook();
- await workbook.xlsx.load(file.data);
- const sheet = workbook.getWorksheet(1);
- const head = sheet.getRow(1).values;
- // 成果名称相同 且 用户id相同 不需要导入
- let cols = this.getMeta();
- for (const i of cols) {
- const { zh } = i;
- const index = head.findIndex((f) => f === zh);
- if (index > -1) i.index = index;
- }
- cols = cols.filter((f) => f.index || f.index === 0);
- const data = [];
- sheet.eachRow((row, index) => {
- if (index === 1) {
- // 啥也不做,第一行是表头
- } else {
- const obj = {};
- for (const i of cols) {
- const { key, index } = i;
- obj[key] = row.values[index];
- }
- // 添加默认值
- data.push({ ...obj, ...defObject });
- }
- });
- for (const d of data) {
- const count = await this.model.count({ name: d.name, user_id: defObject.user_id });
- if (count <= 0) {
- this.model.create(d);
- }
- }
- }
- getMeta() {
- return [
- { zh: '企业名称', key: 'company' },
- { zh: '联系电话', key: 'phone' },
- { zh: '电子邮箱', key: 'email' },
- { zh: '联系人', key: 'contacts' },
- { zh: '成果名称', key: 'name' },
- { zh: '所属领域', key: 'field' },
- { zh: '合作方式', key: 'cooperation' },
- { zh: '成果状态', key: 'achievestatus' },
- { zh: '成果简介', key: 'achievebrief' },
- { zh: '技术特点', key: 'features' },
- ];
- }
- }
- module.exports = ProductService;
|