lrf 3 anos atrás
pai
commit
1d5b17fdfe

+ 84 - 81
app/controller/users/.product.js

@@ -1,110 +1,113 @@
 module.exports = {
   create: {
     requestBody: [
-      "name",
-      "contacts",
-      "phone",
-      "qqwx",
-      "email",
-      "type",
-      "status",
-      "user_id",
-      "field",
-      "cooperation",
-      "company",
-      "condition",
-      "image",
-      "expect",
-      "demand",
-      "budget",
-      "requirementdesc",
-      "present",
-      "achievestatus",
-      "achieveown",
-      "achievesource",
-      "intentionprice",
-      "patent",
-      "roadshow",
-      "achievebrief",
-      "features",
-      "team",
-      "messattribute",
-      "informationdesc",
-      "coreelements",
-      "priceinfo",
-      "remark",
+      'name',
+      'contacts',
+      'phone',
+      'qqwx',
+      'email',
+      'type',
+      'status',
+      'user_id',
+      'field',
+      'cooperation',
+      'company',
+      'condition',
+      'image',
+      'expect',
+      'demand',
+      'budget',
+      'requirementdesc',
+      'present',
+      'achievestatus',
+      'achieveown',
+      'achievesource',
+      'intentionprice',
+      'patent',
+      'roadshow',
+      'achievebrief',
+      'features',
+      'team',
+      'messattribute',
+      'informationdesc',
+      'coreelements',
+      'priceinfo',
+      'remark',
     ],
   },
   destroy: {
-    params: ["!id"],
-    service: "delete",
+    params: ['!id'],
+    service: 'delete',
   },
   update: {
-    params: ["!id"],
+    params: ['!id'],
     requestBody: [
-      "name",
-      "contacts",
-      "phone",
-      "qqwx",
-      "email",
-      "type",
-      "status",
-      "user_id",
-      "field",
-      "cooperation",
-      "company",
-      "condition",
-      "image",
-      "expect",
-      "demand",
-      "budget",
-      "requirementdesc",
-      "present",
-      "achievestatus",
-      "achieveown",
-      "achievesource",
-      "intentionprice",
-      "patent",
-      "roadshow",
-      "achievebrief",
-      "features",
-      "team",
-      "messattribute",
-      "informationdesc",
-      "coreelements",
-      "priceinfo",
-      "remark",
+      'name',
+      'contacts',
+      'phone',
+      'qqwx',
+      'email',
+      'type',
+      'status',
+      'user_id',
+      'field',
+      'cooperation',
+      'company',
+      'condition',
+      'image',
+      'expect',
+      'demand',
+      'budget',
+      'requirementdesc',
+      'present',
+      'achievestatus',
+      'achieveown',
+      'achievesource',
+      'intentionprice',
+      'patent',
+      'roadshow',
+      'achievebrief',
+      'features',
+      'team',
+      'messattribute',
+      'informationdesc',
+      'coreelements',
+      'priceinfo',
+      'remark',
     ],
   },
   show: {
     parameters: {
-      params: ["!id"],
+      params: ['!id'],
     },
-    service: "fetch",
+    service: 'fetch',
   },
   index: {
     parameters: {
       query: {
-        name: "%name%",
-        code:"code",
-        qqwx: "qqwx",
-        type: "type",
-        status: "status",
-        user_id: "user_id",
-        company: "company",
-        "create_time@start": "create_time@start",
-        "create_time@end": "create_time@end",
+        name: '%name%',
+        code: 'code',
+        qqwx: 'qqwx',
+        type: 'type',
+        status: 'status',
+        user_id: 'user_id',
+        company: 'company',
+        'create_time@start': 'create_time@start',
+        'create_time@end': 'create_time@end',
       },
       // options: {
       //   "meta.state": 0 // 默认条件
       // },
     },
-    service: "query",
+    service: 'query',
     options: {
-      query: ["skip", "limit"],
-      sort: ["meta.createdAt"],
+      query: ['skip', 'limit'],
+      sort: ['meta.createdAt'],
       desc: true,
       count: true,
     },
   },
+  import: {
+    requestBody: ['uri', 'defObject'],
+  },
 };

+ 3 - 2
app/router/users/product.js

@@ -1,12 +1,13 @@
 'use strict';
 
 
-module.exports = app => {
+module.exports = (app) => {
   const { router, controller } = app;
   const profix = '/api/live/';
   const vision = 'v0';
   const index = 'users';
   const target = 'product';
+  router.post(target, `${profix}${vision}/${index}/${target}/import`, controller[index][target].import);
   router.resources(target, `${profix}${vision}/${index}/${target}`, controller[index][target]); // index、create、show、destroy
   router.post(target, `${profix}${vision}/${index}/${target}/update/:id`, controller[index][target].update);
-};
+};;

+ 81 - 15
app/service/users/product.js

@@ -1,6 +1,9 @@
 '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) {
@@ -26,27 +29,32 @@ class ProductService extends CrudService {
         res = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit));
         total = await this.model.count(query);
       } else {
-      // 处理company特殊的情况
+        // 处理company特殊的情况
         let nquery = {};
-        if (company === '中科系') nquery.company = [ '中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所' ];
-        else if (company === '其他')nquery.company = { $nin: [ '中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所', '吉林大学', '长春工业大学' ] };
+        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));
+      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));
+      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 (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));
+        res = await this.model
+          .find({ ...oq, user_id: ids })
+          .skip(parseInt(skip))
+          .limit(parseInt(limit));
         total = await this.model.count({ ...oq, user_id: ids });
       }
     }
@@ -71,10 +79,10 @@ class ProductService extends CrudService {
     const expert = await this.getSample('expert', 8);
     console.log('in function:6');
     // 专家需要姓名
-    const ids = expert.map(i => i.user_id);
+    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));
+      const r = await personal.find((f) => ObjectId(f._id).equals(exp.user_id));
       if (r) exp.name = r.name;
     }
     // 路演:roadShow:limit=>5
@@ -83,12 +91,70 @@ class ProductService extends CrudService {
   }
 
   async getSample(model, limit = 6, match = {}) {
-    const res = await this[model].aggregate([
-      { $match: match },
-      { $sample: { size: parseInt(limit) } },
-    ]);
+    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;