Procházet zdrojové kódy

添加user_id的添加

ruifeng_liu před 3 roky
rodič
revize
c46639a812
1 změnil soubory, kde provedl 225 přidání a 34 odebrání
  1. 225 34
      app/service/patent/patentinfo.js

+ 225 - 34
app/service/patent/patentinfo.js

@@ -19,7 +19,7 @@ class PatentinfoService extends CrudService {
     this.personalModel = this.ctx.model.Personal;
 
     this.root_path = _.get(this.ctx.app.config.export, 'root_path');
-    if (process.env.NODE_ENV === 'development') this.root_path = 'E:\\exportFile\\';
+    if (process.env.NODE_ENV === 'development') { this.root_path = 'E:\\exportFile\\'; }
     this.file_type = '';
     if (!fs.existsSync(`${this.root_path}${this.file_type}`)) {
       // 如果不存在文件夹,就创建
@@ -27,16 +27,18 @@ class PatentinfoService extends CrudService {
     }
     this.excel_path = `${sep}excel${sep}`;
     this.domain = 'http://127.0.0.1';
-    if (process.env.NODE_ENV === 'development') this.domain = 'http://127.0.0.1:8000';
+    if (process.env.NODE_ENV === 'development') { this.domain = 'http://127.0.0.1:8000'; }
     this.export_limit = 50;
   }
 
   async query(query, { skip = 0, limit = 0 }) {
     const newquery = await this.resetCode(query);
-    const res = await this.model.find(newquery).skip(parseInt(skip)).limit(parseInt(limit))
+    const res = await this.model
+      .find(newquery)
+      .skip(parseInt(skip))
+      .limit(parseInt(limit))
       .sort({ 'meta.createdAt': -1 });
     return res;
-
   }
   async count(query) {
     const newquery = await this.resetCode(query);
@@ -49,7 +51,10 @@ class PatentinfoService extends CrudService {
     newquery = this.ctx.service.util.util.dealQuery(newquery);
     const { type } = newquery;
     if (type === 'else') {
-      newquery.$and = [{ type: { $ne: '发明' } }, { type: { $ne: '实用新型' } }];
+      newquery.$and = [
+        { type: { $ne: '发明' } },
+        { type: { $ne: '实用新型' } },
+      ];
       delete newquery.type;
     }
     const { code, user_id } = newquery;
@@ -76,13 +81,16 @@ class PatentinfoService extends CrudService {
     const query = { user_id: { $elemMatch: { $in: pids } } };
     if (status) query.status = status;
     if (term) query.term = term;
-    const data = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit))
+    const data = await this.model
+      .find(query)
+      .skip(parseInt(skip))
+      .limit(parseInt(limit))
       .sort({ 'meta.createdAt': -1 });
     const total = await this.model.count(query);
     return { data, total };
   }
 
-  async toImport({ uri, origin, user_id }) {
+  async toImport({ uri, origin }) {
     assert(uri, '未获取到文件地址');
     const file = await this.ctx.curl(`${this.domain}${uri}`);
     console.log(file);
@@ -95,11 +103,13 @@ class PatentinfoService extends CrudService {
     const arr = [];
     const allNotice = [];
     const sheetImageInfo = sheet.getImages();
-    const imgids = _.compact(sheetImageInfo.map(i => {
-      const { imageId, range } = i;
-      const row = _.get(range, 'tl.nativeRow');
-      if (row) return { row, imageId };
-    }));
+    const imgids = _.compact(
+      sheetImageInfo.map(i => {
+        const { imageId, range } = i;
+        const row = _.get(range, 'tl.nativeRow');
+        if (row) return { row, imageId };
+      })
+    );
     sheet.eachRow((row, rindex) => {
       if (rindex !== 1) {
         // 组织数据,图片的索引和行索引不一致,准确的说是:图片索引比行索引少1
@@ -144,7 +154,7 @@ class PatentinfoService extends CrudService {
           img_url,
           number,
           origin,
-          user_id: [ user_id ],
+          user_id: [],
         };
         // 此处添加判断条件,不限制则不需要加,直接放过即可
         const { result, notice } = this.tocheckData(obj);
@@ -156,8 +166,27 @@ class PatentinfoService extends CrudService {
       }
     });
     if (allNotice.length > 0) return allNotice;
-    await this.model.insertMany(arr);
 
+    let nameList = [];
+    for (const i of arr) {
+      const { inventor } = i;
+      const midList = inventor.split(/[,;/]/);
+      nameList = [ ...nameList, ...midList ];
+    }
+    const l1 = await this.personalModel.find({ name: nameList });
+    const l2 = await this.organizationModel.find({ name: nameList });
+    // 查出来的所有人,添加到user_id中
+    const nList = [ ...l1, ...l2 ];
+    for (const i of arr) {
+      const { inventor } = i;
+      const midNameList = inventor.split(/[,;/]/);
+      if (!_.isArray(i.user_id)) i.user_id = [];
+      for (const name of midNameList) {
+        const rList = nList.filter(f => f.name === name);
+        i.user_id = [ ...i.user_id, ...rList.map(i => i._id) ];
+      }
+    }
+    await this.model.insertMany(arr);
   }
   async toExport({ user }) {
     const data = {
@@ -171,11 +200,14 @@ class PatentinfoService extends CrudService {
       tenant: 'live',
     };
     try {
-      await this.ctx.service.util.httpUtil.cpost('/api/mission', 'mission', data);
+      await this.ctx.service.util.httpUtil.cpost(
+        '/api/mission',
+        'mission',
+        data
+      );
     } catch (error) {
       console.log(error);
       throw new BusinessError(ErrorCode.SERVICE_FAULT, '任务创建失败');
-
     }
   }
 
@@ -204,7 +236,11 @@ class PatentinfoService extends CrudService {
           status: '1',
           id: missionid,
         };
-        this.ctx.service.util.httpUtil.cpost('/api/mission/progress', 'mission', data);
+        this.ctx.service.util.httpUtil.cpost(
+          '/api/mission/progress',
+          'mission',
+          data
+        );
       } catch (error) {
         this.logger.error(`任务id:${missionid},进度更新失败`);
       }
@@ -215,12 +251,15 @@ class PatentinfoService extends CrudService {
         status: '2',
         uri: downloadPath,
       };
-      await this.ctx.service.util.httpUtil.cpost(`/api/mission/update/${missionid}`, 'mission', data);
+      await this.ctx.service.util.httpUtil.cpost(
+        `/api/mission/update/${missionid}`,
+        'mission',
+        data
+      );
     } catch (error) {
       this.logger.error(`任务id:${missionid},已完成更新失败`);
     }
     return downloadPath;
-
   }
 
   async asyncExport(list, filename, path, downloadPath) {
@@ -273,7 +312,6 @@ class PatentinfoService extends CrudService {
       } catch (error) {
         this.ctx.logger.error(`导出,id为:${_id}的图片处理出现错误!`);
       }
-
     }
     const filepath = `${path}${filename}`;
     if (list.length <= 0) return;
@@ -327,20 +365,173 @@ class PatentinfoService extends CrudService {
 
   getHeader() {
     const arr = [
-      { header: '申请号', key: 'create_number', width: 30, style: { alignment: { wrapText: true, vertical: 'middle', horizontal: 'center' } } },
-      { header: '申请日', key: 'create_date', width: 30, style: { alignment: { wrapText: true, vertical: 'middle', horizontal: 'center' } } },
-      { header: '公开(公告)号', key: 'success_number', width: 30, style: { alignment: { wrapText: true, vertical: 'middle', horizontal: 'center' } } },
-      { header: '公开(公告)日', key: 'success_date', width: 30, style: { alignment: { wrapText: true, vertical: 'middle', horizontal: 'center' } } },
-      { header: '发明人', key: 'inventor', width: 30, style: { alignment: { wrapText: true, vertical: 'middle', horizontal: 'center' } } },
-      { header: '代理机构', key: 'agent', width: 30, style: { alignment: { wrapText: true, vertical: 'middle', horizontal: 'center' } } },
-      { header: '代理人', key: 'agent_personal', width: 30, style: { alignment: { wrapText: true, vertical: 'middle', horizontal: 'center' } } },
-      { header: '摘要', key: 'abstract', width: 30, style: { alignment: { wrapText: true, vertical: 'middle', horizontal: 'center' } } },
-      { header: '发明人地址', key: 'address', width: 30, style: { alignment: { wrapText: true, vertical: 'middle', horizontal: 'center' } } },
-      { header: '标题', key: 'name', width: 30, style: { alignment: { wrapText: true, vertical: 'middle', horizontal: 'center' } } },
-      { header: '申请人', key: 'apply_personal', width: 30, style: { alignment: { wrapText: true, vertical: 'middle', horizontal: 'center' } } },
-      { header: '专利有效性', key: 'term', width: 30, style: { alignment: { wrapText: true, vertical: 'middle', horizontal: 'center' } } },
-      { header: '专利类型', key: 'type', width: 30, style: { alignment: { wrapText: true, vertical: 'middle', horizontal: 'center' } } },
-      { header: '首页附图', width: 30, style: { alignment: { wrapText: true, vertical: 'middle', horizontal: 'center' } } },
+      {
+        header: '申请号',
+        key: 'create_number',
+        width: 30,
+        style: {
+          alignment: {
+            wrapText: true,
+            vertical: 'middle',
+            horizontal: 'center',
+          },
+        },
+      },
+      {
+        header: '申请日',
+        key: 'create_date',
+        width: 30,
+        style: {
+          alignment: {
+            wrapText: true,
+            vertical: 'middle',
+            horizontal: 'center',
+          },
+        },
+      },
+      {
+        header: '公开(公告)号',
+        key: 'success_number',
+        width: 30,
+        style: {
+          alignment: {
+            wrapText: true,
+            vertical: 'middle',
+            horizontal: 'center',
+          },
+        },
+      },
+      {
+        header: '公开(公告)日',
+        key: 'success_date',
+        width: 30,
+        style: {
+          alignment: {
+            wrapText: true,
+            vertical: 'middle',
+            horizontal: 'center',
+          },
+        },
+      },
+      {
+        header: '发明人',
+        key: 'inventor',
+        width: 30,
+        style: {
+          alignment: {
+            wrapText: true,
+            vertical: 'middle',
+            horizontal: 'center',
+          },
+        },
+      },
+      {
+        header: '代理机构',
+        key: 'agent',
+        width: 30,
+        style: {
+          alignment: {
+            wrapText: true,
+            vertical: 'middle',
+            horizontal: 'center',
+          },
+        },
+      },
+      {
+        header: '代理人',
+        key: 'agent_personal',
+        width: 30,
+        style: {
+          alignment: {
+            wrapText: true,
+            vertical: 'middle',
+            horizontal: 'center',
+          },
+        },
+      },
+      {
+        header: '摘要',
+        key: 'abstract',
+        width: 30,
+        style: {
+          alignment: {
+            wrapText: true,
+            vertical: 'middle',
+            horizontal: 'center',
+          },
+        },
+      },
+      {
+        header: '发明人地址',
+        key: 'address',
+        width: 30,
+        style: {
+          alignment: {
+            wrapText: true,
+            vertical: 'middle',
+            horizontal: 'center',
+          },
+        },
+      },
+      {
+        header: '标题',
+        key: 'name',
+        width: 30,
+        style: {
+          alignment: {
+            wrapText: true,
+            vertical: 'middle',
+            horizontal: 'center',
+          },
+        },
+      },
+      {
+        header: '申请人',
+        key: 'apply_personal',
+        width: 30,
+        style: {
+          alignment: {
+            wrapText: true,
+            vertical: 'middle',
+            horizontal: 'center',
+          },
+        },
+      },
+      {
+        header: '专利有效性',
+        key: 'term',
+        width: 30,
+        style: {
+          alignment: {
+            wrapText: true,
+            vertical: 'middle',
+            horizontal: 'center',
+          },
+        },
+      },
+      {
+        header: '专利类型',
+        key: 'type',
+        width: 30,
+        style: {
+          alignment: {
+            wrapText: true,
+            vertical: 'middle',
+            horizontal: 'center',
+          },
+        },
+      },
+      {
+        header: '首页附图',
+        width: 30,
+        style: {
+          alignment: {
+            wrapText: true,
+            vertical: 'middle',
+            horizontal: 'center',
+          },
+        },
+      },
     ];
     return arr;
   }