Преглед изворни кода

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

guhongwei пре 3 година
родитељ
комит
89591288df
3 измењених фајлова са 189 додато и 175 уклоњено
  1. 0 1
      app/service/patent/patentinfo.js
  2. 102 87
      app/service/patent/patentwarning.js
  3. 87 87
      app/service/users/personal.js

+ 0 - 1
app/service/patent/patentinfo.js

@@ -83,7 +83,6 @@ class PatentinfoService extends CrudService {
     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))

+ 102 - 87
app/service/patent/patentwarning.js

@@ -36,92 +36,94 @@ class PatentwarningService extends CrudService {
       throw new BusinessError(ErrorCode.FILE_FAULT, '解压失败');
     }
     // 修改,多个专利:需要扫描下解压后的文件夹,然后循环处理下面所有的内容
-    const tempDirRootPath = `${uncompressFilePath}${sep}${fileName}`;
+    const tempDirRootPath = `${uncompressFilePath}${sep}`;
     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;
+        // 找到
+        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;
+        }
+        let xmlData;
+        try {
+          xmlData = await this.dealXml(xmlPath);
+        } catch (error) {
+          errorList.push({ key: patentNumber, word: '解析xml数据中的 申请号 和 专利名称 失败' });
+          continue;
         // throw new BusinessError(ErrorCode.SERVICE_FAULT, '解析申请号失败');
-      }
-      if (!create_number) {
-        errorList.push({ key: patentNumber, word: '未找到申请号' });
-        continue;
+        }
+        if (!xmlData) {
+          errorList.push({ key: patentNumber, word: '未找到xml的数据' });
+          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) {
+        }
+        let result;
         try {
-          result = await this.getPatentFromApply(create_number);
+          result = await this.getPatent(xmlData);
         } catch (error) {
-          errorList.push({ key: patentNumber, word: '查询申请信息失败' });
+          errorList.push({ key: patentNumber, word: '查询专利信息失败' });
           continue;
+        // throw new BusinessError(ErrorCode.SERVICE_FAULT, '查询专利信息失败');
         }
-      }
-      // 找不到就算了,弹回去了
-      if (!result) {
-        errorList.push({ key: patentNumber, word: '未通过申请号找到指定专利' });
-        continue;
+        // 找不到就算了,弹回去了
+        if (!result || !_.get(result, 'patent')) {
+          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) {
+        }
+        const patent = _.get(result, 'patent');
+        let warningData;
         try {
-          lastResult = await this.toUploadTif(warningData, tifPaths);
+          warningData = await this.getWarningData(patent);
         } catch (error) {
-          errorList.push({ key: patentNumber, word: '存储数据失败' });
+          errorList.push({ key: patentNumber, word: '组织专利信息警告数据失败' });
           continue;
+        // throw new BusinessError(ErrorCode.SERVICE_FAULT, '组织专利信息警告数据失败');
         }
-      }
-      if (lastResult) {
+        const tifPaths = filePaths.filter(f => f.includes('.tif'));
+        if (tifPaths && tifPaths.length > 0) {
+          try {
+            await this.toUploadTif(warningData, tifPaths);
+          } catch (error) {
+            errorList.push({ key: patentNumber, word: '存储数据失败' });
+            continue;
+          }
+        }
+        // 查看是否需要更改申请号
+        const needChange = _.get(result, 'needChange');
+        if (needChange) {
+          try {
+            await this.patentApply.updateOne({ _id: patent._id }, patent);
+          } catch (error) {
+            errorList.push({ key: patentNumber, word: 'xml申请号同步至专利申请号失败' });
+            continue;
+          }
+        }
+      } finally {
         try {
-          await this.dirDelete(`${uncompressFilePath}`);
+          await this.dirDelete(`${thisDirPath}`);
         } catch (error) {
-          errorList.push({ key: patentNumber, word: '清除缓存失败' });
-          continue;
+          this.ctx.logger.error('清除缓存失败');
         }
       }
+
     }
     if (errorList.length > 0) return errorList;
     return 'ok';
@@ -203,17 +205,13 @@ class PatentwarningService extends CrudService {
     if (_.get(patent, 'id'))object.patent_id = _.get(patent, 'id');
     if (_.get(patent, 'name'))object.patent_name = _.get(patent, 'name');
     // 找人的信息
-    let userIds = _.get(patent, 'user_id', _.get(patent, 'inventor'));
-    if (!userIds) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到专利的发明人');
-    if (!_.isArray(userIds)) userIds = [ userIds ];
-    userIds = userIds.map(i => ObjectId(i));
-    const users = await this.personalModel.find({ _id: userIds });
+    const users = _.get(patent, 'inventor');
     const arr = [];
     for (const user of users) {
-      if (!_.get(user, '_id') || !_.get(user, 'name')) continue;
-      const { _id, name } = user;
+      if (!_.get(user, 'user_id') || !_.get(user, 'name')) continue;
+      const { user_id, name } = user;
       const obj = _.cloneDeep(object);
-      obj.to_id = _id;
+      obj.to_id = user_id;
       obj.to_name = name;
       arr.push(obj);
     }
@@ -222,25 +220,40 @@ class PatentwarningService extends CrudService {
   }
 
   /**
-   * 在patentApply表中找指定的专利
-   * @param {String} create_number 专利的申请号
+   * 在patentApply/patentInfo表中找指定的专利
+   * @param {Object} object xml的数据
    */
-  async getPatentFromApply(create_number) {
-    const res = await this.patentApply.findOne({ create_number });
-    return res;
+  async getPatent(object) {
+    const { name, create_number } = object;
+    let res;
+    let needChange = false;
+    res = await this.patentApply.findOne({ create_number });
+    // 如果在申请中,使用名称找到,则之后需要将该数据的专利号改为xml中的专利号
+    if (!res) {
+      res = await this.patentApply.findOne({ name });
+      needChange = true;
+    }
+    if (!res) {
+      res = await this.patentInfo.findOne({ create_number });
+    }
+    if (!res) {
+      res = await this.patentInfo.findOne({ name });
+    }
+    const returnObject = {};
+    if (res) {
+      if (needChange) {
+        res.create_number = create_number;
+        returnObject.needChange = needChange;
+      }
+      returnObject.patent = res;
+      return returnObject;
+    }
+    return null;
   }
 
-  /**
-   * 在patentInfo表中找指定的专利
-   * @param {String} create_number 专利的申请号
-   */
-  async getPatentFromInfo(create_number) {
-    const res = await this.patentInfo.findOne({ create_number });
-    return res;
-  }
 
   /**
-   * 根据路径找到xml,再找到该xml的申请号
+   * 根据路径找到xml,再找到该xml的申请号和专利名称
    * @param {String} filePath xml的文件路径
    */
   async dealXml(filePath) {
@@ -250,7 +263,9 @@ class PatentwarningService extends CrudService {
     str = str.toString();
     const json = fxp.parse(str);
     const create_number = _.get(json, 'data-bus.TONGZHISXJ.SHUXINGXX.SHENQINGH');
-    return create_number;
+    const name = _.get(json, 'data-bus.TONGZHISXJ.SHUXINGXX.FAMINGMC');
+    if (!name) return null;
+    return { name, create_number };
   }
 
   /**

+ 87 - 87
app/service/users/personal.js

@@ -1,87 +1,87 @@
-'use strict';
-const { CrudService } = require('naf-framework-mongoose/lib/service');
-const { BusinessError, ErrorCode } = require('naf-core').Error;
-const { ObjectId } = require('mongoose').Types;
-const _ = require('lodash');
-const jwt = require('jsonwebtoken');
-const assert = require('assert');
-
-// 个人用户
-class PersonalService extends CrudService {
-  constructor(ctx) {
-    super(ctx, 'personal');
-    this.redis = this.app.redis;
-    this.model = this.ctx.model.Personal;
-  }
-  /**
-   * 创建用户
-   * @param {Object} params 用户信息
-   */
-  async create({ password, ...data }) {
-    data.password = { secret: password };
-    const { phone } = data;
-    // 检查手机号
-    const num = await this.model.count({ phone, isdel: '0' });
-    if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已有个人用户使用该手机号');
-    return await this.model.create(data);
-  }
-  /**
-   * 修改密码
-   * @param {Object} {id,password} 用户id和密码
-   */
-  async password({ id, password }) {
-    const object = await this.model.findById(id);
-    if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
-    object.password = { secret: password };
-    await object.save();
-  }
-  /**
-   * 登陆
-   * @param {Object} params 登陆信息
-   * @property phone 手机号
-   * @property password 密码
-   */
-  async login({ phone, password }) {
-    const object = await this.model.findOne({ phone, isdel: '0' }, '+password');
-    if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
-    const { password: op, status } = object;
-    const { secret } = op;
-    if (status !== '1') throw new BusinessError(ErrorCode.ACCESS_DENIED, '拒绝访问!');
-    if (secret !== password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
-    const data = _.omit(JSON.parse(JSON.stringify(object)), [ 'meta', 'password', '__v' ]);
-    const { secret: secrets } = this.config.jwt;
-    const token = jwt.sign(data, secrets);
-    // 记录登陆
-    let number = await this.redis.get('login_number') || 0;
-    number++;
-    await this.redis.set('login_number', number);
-    return token;
-  }
-
-  async delete({ id }) {
-    const object = await this.model.findById(id);
-    if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
-    object.isdel = '1';
-    await object.save();
-  }
-  /**
-   * 个人升变专家
-   * @param {Object} body 升变数据
-   * 升级的时候要把所属的信息(产品,需求等)换到升级后的数据上,一种数据迁移
-   */
-  async upgrade({ id, ...data }) {
-    assert(id, '缺少个人用户ID');
-    const user = await this.model.findOne({ _id: ObjectId(id) }, '+password');
-    if (!user) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
-    data = _.omit(data, [ 'meta', '__v' ]);
-    data.user_id = id;
-    // 创建专家
-    const is_expert = await this.ctx.model.Expert.count({ user_id: ObjectId(id) });
-    if (is_expert > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已升级为专家,无需再次升级');
-    await this.ctx.model.Expert.create(data);
-    user.is_expert = true;
-    await user.save();
-  }
-}
-
-module.exports = PersonalService;
+'use strict';
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const { ObjectId } = require('mongoose').Types;
+const _ = require('lodash');
+const jwt = require('jsonwebtoken');
+const assert = require('assert');
+
+// 个人用户
+class PersonalService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'personal');
+    this.redis = this.app.redis;
+    this.model = this.ctx.model.Personal;
+  }
+  /**
+   * 创建用户
+   * @param {Object} params 用户信息
+   */
+  async create({ password, ...data }) {
+    data.password = { secret: password };
+    const { phone } = data;
+    // 检查手机号
+    const num = await this.model.count({ phone, isdel: '0' });
+    if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已有个人用户使用该手机号');
+    return await this.model.create(data);
+  }
+  /**
+   * 修改密码
+   * @param {Object} {id,password} 用户id和密码
+   */
+  async password({ id, password }) {
+    const object = await this.model.findById(id);
+    if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
+    object.password = { secret: password };
+    await object.save();
+  }
+  /**
+   * 登陆
+   * @param {Object} params 登陆信息
+   * @property phone 手机号
+   * @property password 密码
+   */
+  async login({ phone, password }) {
+    const object = await this.model.findOne({ phone, isdel: '0' }, '+password');
+    if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
+    const { password: op, status } = object;
+    const { secret } = op;
+    if (status !== '1') throw new BusinessError(ErrorCode.ACCESS_DENIED, '拒绝访问!');
+    if (secret !== password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
+    const data = _.omit(JSON.parse(JSON.stringify(object)), [ 'meta', 'password', '__v' ]);
+    const { secret: secrets } = this.config.jwt;
+    const token = jwt.sign(data, secrets);
+    // 记录登陆
+    // let number = await this.redis.get('login_number') || 0;
+    // number++;
+    // await this.redis.set('login_number', number);
+    return token;
+  }
+
+  async delete({ id }) {
+    const object = await this.model.findById(id);
+    if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
+    object.isdel = '1';
+    await object.save();
+  }
+  /**
+   * 个人升变专家
+   * @param {Object} body 升变数据
+   * 升级的时候要把所属的信息(产品,需求等)换到升级后的数据上,一种数据迁移
+   */
+  async upgrade({ id, ...data }) {
+    assert(id, '缺少个人用户ID');
+    const user = await this.model.findOne({ _id: ObjectId(id) }, '+password');
+    if (!user) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
+    data = _.omit(data, [ 'meta', '__v' ]);
+    data.user_id = id;
+    // 创建专家
+    const is_expert = await this.ctx.model.Expert.count({ user_id: ObjectId(id) });
+    if (is_expert > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已升级为专家,无需再次升级');
+    await this.ctx.model.Expert.create(data);
+    user.is_expert = true;
+    await user.save();
+  }
+}
+
+module.exports = PersonalService;