lrf402788946 3 éve
szülő
commit
73f4012caf

+ 3 - 0
app/controller/patent/.disclosure.js

@@ -83,4 +83,7 @@ module.exports = {
   import: {
     requestBody: ["uri"],
   },
+  cacheImport: {
+    requestBody: ["data", "temp_id"],
+  },
 };

+ 2 - 0
app/model/dock/patent.js

@@ -1,6 +1,7 @@
 'use strict';
 const Schema = require('mongoose').Schema;
 const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+const { ObjectId } = require('mongoose').Types;
 // 专利表
 const PatentSchema = {
   create_number: { type: String, required: false }, // 申请号
@@ -18,6 +19,7 @@ const PatentSchema = {
   abstract: { type: String, required: false }, // 摘要
   img_url: { type: String, required: false }, // 图片
   origin: { type: String }, // 数据来源(手写的)
+  user_id: { type: [ ObjectId ] },
 };
 
 const schema = new Schema(PatentSchema, { toJSON: { virtuals: true } });

+ 18 - 0
app/model/patent/import_temp.js

@@ -0,0 +1,18 @@
+'use strict';
+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 import_temp = {
+  data: { type: Array }, // 数据
+  remark: { type: String },
+};
+const schema = new Schema(import_temp, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ 'meta.createdAt': 1 });
+schema.plugin(metaPlugin);
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('ImportTemp', schema, 'import_temp');
+};

+ 1 - 0
app/router/patent/disclosure.js

@@ -14,4 +14,5 @@ module.exports = app => {
   router.post(target, `${profix}${vision}/${index}/${target}/update/:id`, mware, controller[index][target].update);
   router.post(target, `${profix}${vision}/${index}/${target}/check`, controller[index][target].check);
   router.post(target, `${profix}${vision}/${index}/${target}/import`, controller[index][target].import);
+  router.post(target, `${profix}${vision}/${index}/${target}/cacheImport`, controller[index][target].cacheImport);
 };

+ 71 - 3
app/service/patent/disclosure.js

@@ -15,6 +15,9 @@ class DisclosureService extends CrudService {
     this.model = this.ctx.model.Patent.Disclosure;
     this.notice = this.ctx.model.Patent.Notice;
     this.patent = this.ctx.model.Dock.Patent;
+    this.personalModel = this.ctx.model.Personal;
+    this.organizationModel = this.ctx.model.Organization;
+    this.itemp = this.ctx.model.Patent.ImportTemp;
   }
 
   /**
@@ -184,7 +187,7 @@ class DisclosureService extends CrudService {
 
   // 导入
   async import({ uri }) {
-    const domain = 'http://127.0.0.1';
+    const domain = process.env.NODE_ENV === 'development' ? 'http://broadcast.waityou24.cn/' : 'http://127.0.0.1';
     assert(uri, '未获取到文件地址');
     const file = await this.ctx.curl(`${domain}${uri}`);
     if (!(file && file.data)) {
@@ -240,6 +243,7 @@ class DisclosureService extends CrudService {
           type,
           img_url,
           number,
+          user_id: [],
         };
         // 此处添加判断条件,不限制则不需要加,直接放过即可
         const { result, notice } = this.tocheckData(obj);
@@ -250,8 +254,72 @@ class DisclosureService extends CrudService {
         }
       }
     });
-    if (allNotice.length > 0) return allNotice;
-    await this.patent.insertMany(arr);
+    if (allNotice.length > 0) return { data: allNotice, errcode: '1' };
+    // 信息合格后,将发明人分出来,然后在个人/企业查出来,如果不是一个人,那就返回给前端,让前端选择后进行存储
+    // 提取出所有人,分割,放到数组里(重不重复无所谓,这个地方无所谓);然后用人名去查出来所有的用户(企业/个人),如果有重复的,那就提出来返回给前端
+    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 });
+    // 查出来的所有人
+    const nList = [ ...l1, ...l2 ];
+    const groups = _.groupBy(nList, 'name');
+    // 重复的名单
+    const rList = {};
+    for (const key in groups) {
+      if (groups[key].length > 1) {
+        rList[key] = groups[key];
+      } else {
+        // 剩下的都是1个的,直接找所有项目里有没有这个人,有就扔进去
+        for (const i of arr) {
+          const { inventor } = i;
+          if (inventor.includes(key)) {
+            const acct = _.head(groups[key]);
+            if (acct) i.user_id.push(acct._id);
+          }
+        }
+      }
+    }
+    // 将有这些重复人名的专利找到,返回前端
+    const errorArr = [];
+    const dealList = [];
+    for (const i of arr) {
+      const { inventor } = i;
+      const key = Object.keys(rList).find(i => inventor.includes(i));
+      if (key) errorArr.push({ ...i, nameList: rList[key] });
+      else dealList.push(i);
+    }
+    // 不导入,缓存
+    if (errorArr.length > 0) {
+      const tempRes = await this.itemp.create({ data: dealList });
+      const { _id } = tempRes;
+      return { data: errorArr, errcode: '2', temp_id: _id };
+    }
+    await this.importAction(dealList);
+  }
+  // 继续导入
+  async cacheImport({ data, temp_id }) {
+    data = data.map(i => {
+      const acct = i.nameList.find(f => f.is_select);
+      if (acct) {
+        const { _id } = acct;
+        i.user_id.push(_id);
+      }
+      delete i.nameList;
+      return i;
+    });
+    const tempList = await this.itemp.findById(temp_id);
+    data = [ ...data, ...tempList.data ];
+    await this.importAction(data);
+    await this.itemp.deleteOne({ _id: temp_id });
+  }
+
+  async importAction(data) {
+    await this.patent.insertMany(data);
   }
 
   /**