Ver Fonte

Merge branch 'master' of http://git.cc-lotus.info/live/service-live

guhongwei há 3 anos atrás
pai
commit
8922a0c9fc

+ 1 - 1
app.js

@@ -9,7 +9,7 @@ class AppBootHook {
     const ctx = await this.app.createAnonymousContext();
     // 检查种子
     // await ctx.service.install.index();
-    // await ctx.service.util.rabbitMq.mission();
+    await ctx.service.util.rabbitMq.mission();
   }
 
 }

+ 9 - 1
app/model/patent/patentinfo.js

@@ -3,6 +3,14 @@ const Schema = require('mongoose').Schema;
 const moment = require('moment');
 const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
 const { ObjectId } = require('mongoose').Types;
+
+const user = new Schema(
+  {
+    user_id: { type: ObjectId, required: false, maxLength: 500 }, // 名称,
+    name: { type: String, required: false, maxLength: 500 }, // 链接地址,
+  }
+);
+
 // 专利信息表
 const patentinfo = {
   create_number: { type: String, required: false }, // 申请号
@@ -10,7 +18,7 @@ const patentinfo = {
   success_number: { type: String, required: false }, // 公开(公告)号
   success_date: { type: String, required: false }, //  公开(公告)日
   name: { type: String, required: false }, // 标题
-  inventor: { type: Array }, // 发明人
+  inventor: { type: [ user ] }, // 发明人
   address: { type: String, required: false }, // 发明人地址
   apply_personal: { type: String, required: false }, // 申请人
   term: { type: String, required: false }, // 专利有效性

+ 38 - 13
app/service/patent/patentinfo.js

@@ -28,12 +28,13 @@ 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:9999'; }
     this.export_limit = 50;
   }
 
   async query(query, { skip = 0, limit = 0 }) {
     const newquery = await this.resetCode(query);
+    console.log(newquery);
     const res = await this.model
       .find(newquery)
       .skip(parseInt(skip))
@@ -58,19 +59,19 @@ class PatentinfoService extends CrudService {
       ];
       delete newquery.type;
     }
-    const { code, inventor } = newquery;
+    const { code, user_id } = newquery;
     let ids = [];
     if (code) {
       const plist = await this.personalModel.find({ code });
       ids = plist.map(i => i._id);
       if (ids.length > 0) {
-        newquery.inventor = { $elemMatch: { $in: ids } };
+        newquery['inventor.user_id'] = { $in: ids };
         delete newquery.code;
       }
-    } else if (inventor) {
-      newquery.inventor = { $elemMatch: { $in: [ ObjectId(inventor) ] } };
+    } else if (user_id) {
+      newquery['inventor.user_id'] = ObjectId(user_id);
+      delete newquery.user_id;
     }
-
     return newquery;
   }
 
@@ -79,9 +80,10 @@ class PatentinfoService extends CrudService {
     let pids = await this.personalModel.find({ code }, { _id: 1 });
     if (pids.length <= 0) return { data: [], total: 0 };
     pids = pids.map(i => i._id);
-    const query = { inventor: { $elemMatch: { $in: pids } } };
+    const query = { 'inventor.user_id': { $in: pids } };
     if (status) query.status = status;
     if (term) query.term = term;
+    console.log(query);
     const data = await this.model
       .find(query)
       .skip(parseInt(skip))
@@ -94,7 +96,6 @@ class PatentinfoService extends CrudService {
   async toImport({ uri, origin }) {
     assert(uri, '未获取到文件地址');
     const file = await this.ctx.curl(`${this.domain}${uri}`);
-    console.log(file);
     if (!(file && file.data)) {
       throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定文件');
     }
@@ -111,7 +112,7 @@ class PatentinfoService extends CrudService {
         if (row) return { row, imageId };
       })
     );
-    sheet.eachRow((row, rindex) => {
+    sheet.eachRow(async (row, rindex) => {
       if (rindex !== 1) {
         // 组织数据,图片的索引和行索引不一致,准确的说是:图片索引比行索引少1
         // 原因:图片在工作簿中获取,按照1,2,3...顺序排序,但是行的第一行是表头(当前文件),所以当前行数需要减掉表头那一行
@@ -122,6 +123,7 @@ class PatentinfoService extends CrudService {
             url: this.turnImageToBase64(workbook.getImage(imgid.imageId)),
           });
         }
+
         const create_number = row.getCell(2).value || undefined,
           create_date =
             moment(row.getCell(3).value).format('YYYY-MM-DD') || undefined,
@@ -213,19 +215,26 @@ class PatentinfoService extends CrudService {
     nameList = nameList.map(i => _.trim(i));
     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;
       let midNameList = inventor.split(/[,;/]/);
       midNameList = midNameList.map(i => _.trim(i));
-      if (!_.isArray(i.user_id)) i.user_id = [];
+      const iList = [];
+      if (!_.isArray(i.user_id)) i.inventor = iList;
       for (const name of midNameList) {
         const rList = nList.filter(f => f.name === name);
-        i.user_id = [ ...i.user_id, ...rList.map(i => i._id) ];
+        if (rList && rList.length > 0) {
+          for (const r of rList) {
+            iList.push({ user_id: r._id, name: r.name });
+          }
+        }
       }
+      i.inventor = iList;
     }
-    await this.model.insertMany(arr);
+    const res = await this.model.insertMany(arr);
+    return res;
   }
   async toExport({ user }) {
     const data = {
@@ -510,6 +519,22 @@ class PatentinfoService extends CrudService {
     const obj = { $gte: start, $lte: end };
     return obj;
   }
+
+  /**
+   * 根据人名数组查询每个人的数据.组织数据返回
+   * @param {Array[String]} nameList 名单
+   */
+  async toGetUser(nameList) {
+    const res = await this.personalModel.find({ name: { $in: nameList } });
+    const result = [];
+    if (res && res.length > 0) {
+      for (const i of res) {
+        const { _id: id, name } = i;
+        result.push({ id, name });
+      }
+    }
+    return result;
+  }
 }
 
 module.exports = PatentinfoService;

+ 88 - 42
app/service/patent/patentwarning.js

@@ -3,14 +3,12 @@ const { CrudService } = require('naf-framework-mongoose/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 const { ObjectId } = require('mongoose').Types;
 const _ = require('lodash');
-const assert = require('assert');
 const compressing = require('compressing');
 const { sep } = require('path');
 const path = require('path');
 const fs = require('fs');
 const encoding = require('encoding'); // 编码转换
 const fxp = require('fast-xml-parser'); // xml转换
-const mime = require('mime-types'); // 任意类型转换
 // 专利运营专利申请预警表
 class PatentwarningService extends CrudService {
   constructor(ctx) {
@@ -37,47 +35,95 @@ class PatentwarningService extends CrudService {
     } catch (error) {
       throw new BusinessError(ErrorCode.FILE_FAULT, '解压失败');
     }
-    // 找到
-    let filePaths = [];
-    try {
-      filePaths = await this.findFileNameLoop(uncompressFilePath, fileName);
-    } catch (error) {
-      throw new BusinessError(ErrorCode.SERVICE_FAULT, '解析文件具体位置失败');
-    }
-    if (filePaths.length <= 0) {
-      throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到文件');
-    }
-    const xmlPath = filePaths.find(f => f.includes('.xml'));
-    if (!xmlPath) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到xml文件');
-    let create_number;
-    try {
-      create_number = await this.dealXml(xmlPath);
-    } catch (error) {
-      throw new BusinessError(ErrorCode.SERVICE_FAULT, '解析申请号失败');
-    }
-    if (!create_number) throw new BusinessError(ErrorCode.FILE_FAULT, '未找到申请号');
-    let result;
-    try {
-      result = await this.getPatentFromInfo(create_number);
-    } catch (error) {
-      throw new BusinessError(ErrorCode.SERVICE_FAULT, '查询专利信息失败');
-    }
-    // 再找
-    if (!result) result = await this.getPatentFromApply(create_number);
-    // 找不到就算了,弹回去了
-    if (!result) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未通过申请号找到指定专利');
-    let warningData;
-    try {
-      warningData = await this.getWarningData(result);
-    } catch (error) {
-      throw new BusinessError(ErrorCode.SERVICE_FAULT, '组织专利信息警告数据失败');
-    }
-    const tifPaths = filePaths.filter(f => f.includes('.tif'));
-    let lastResult;
-    if (tifPaths && tifPaths.length > 0) lastResult = await this.toUploadTif(warningData, tifPaths);
-    if (lastResult) {
-      await this.dirDelete(`${uncompressFilePath}`);
+    // 修改,多个专利:需要扫描下解压后的文件夹,然后循环处理下面所有的内容
+    const tempDirRootPath = `${uncompressFilePath}${sep}${fileName}`;
+    const tempDir = await fs.readdirSync(tempDirRootPath);
+    const errorList = [];
+    for (const tempDirPath of tempDir) {
+      const thisDirPath = `${tempDirRootPath}${sep}${tempDirPath}`;
+      const patentNumber = tempDirPath;
+      // 找到
+      let filePaths = [];
+      try {
+        filePaths = await this.findFileNameLoop(thisDirPath);
+      } catch (error) {
+        errorList.push({ key: patentNumber, word: '解析文件具体位置失败' });
+        continue;
+      }
+      if (filePaths.length <= 0) {
+        errorList.push({ key: patentNumber, word: '未找到文件' });
+        continue;
+      }
+      const xmlPath = filePaths.find(f => f.includes('.xml'));
+      if (!xmlPath) {
+        errorList.push({ key: patentNumber, word: '未找到xml文件' });
+        continue;
+        // throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到xml文件');
+      }
+      let create_number;
+      try {
+        create_number = await this.dealXml(xmlPath);
+      } catch (error) {
+        errorList.push({ key: patentNumber, word: '解析申请号失败' });
+        continue;
+        // throw new BusinessError(ErrorCode.SERVICE_FAULT, '解析申请号失败');
+      }
+      if (!create_number) {
+        errorList.push({ key: patentNumber, word: '未找到申请号' });
+        continue;
+        // throw new BusinessError(ErrorCode.FILE_FAULT, '未找到申请号');
+      }
+      let result;
+      try {
+        result = await this.getPatentFromInfo(create_number);
+      } catch (error) {
+        errorList.push({ key: patentNumber, word: '查询专利信息失败' });
+        continue;
+        // throw new BusinessError(ErrorCode.SERVICE_FAULT, '查询专利信息失败');
+      }
+      // 再找
+      if (!result) {
+        try {
+          result = await this.getPatentFromApply(create_number);
+        } catch (error) {
+          errorList.push({ key: patentNumber, word: '查询申请信息失败' });
+          continue;
+        }
+      }
+      // 找不到就算了,弹回去了
+      if (!result) {
+        errorList.push({ key: patentNumber, word: '未通过申请号找到指定专利' });
+        continue;
+        // throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未通过申请号找到指定专利');
+      }
+      let warningData;
+      try {
+        warningData = await this.getWarningData(result);
+      } catch (error) {
+        errorList.push({ key: patentNumber, word: '组织专利信息警告数据失败' });
+        continue;
+        // throw new BusinessError(ErrorCode.SERVICE_FAULT, '组织专利信息警告数据失败');
+      }
+      const tifPaths = filePaths.filter(f => f.includes('.tif'));
+      let lastResult;
+      if (tifPaths && tifPaths.length > 0) {
+        try {
+          lastResult = await this.toUploadTif(warningData, tifPaths);
+        } catch (error) {
+          errorList.push({ key: patentNumber, word: '存储数据失败' });
+          continue;
+        }
+      }
+      if (lastResult) {
+        try {
+          await this.dirDelete(`${uncompressFilePath}`);
+        } catch (error) {
+          errorList.push({ key: patentNumber, word: '清除缓存失败' });
+          continue;
+        }
+      }
     }
+    if (errorList.length > 0) return errorList;
     return 'ok';
   }