guhongwei 3 سال پیش
والد
کامیت
9addf91705
1فایلهای تغییر یافته به همراه68 افزوده شده و 34 حذف شده
  1. 68 34
      app/service/patent/patenttrans.js

+ 68 - 34
app/service/patent/patenttrans.js

@@ -1,4 +1,5 @@
 'use strict';
+
 const assert = require('assert');
 const moment = require('moment');
 const Excel = require('exceljs');
@@ -9,8 +10,6 @@ const fs = require('fs');
 const { CrudService } = require('naf-framework-mongoose/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 const { trimData } = require('naf-core').Util;
-const { ObjectId } = require('mongoose').Types;
-const { has } = require('lodash');
 
 // 专利交易
 class Patent_transService extends CrudService {
@@ -18,10 +17,18 @@ class Patent_transService extends CrudService {
     super(ctx, 'patenttrans');
     this.model = this.ctx.model.Patent.Patenttrans;
     this.notice = this.ctx.model.Patent.Patentexamine;
-    this.domain = 'http://127.0.0.1';
+    this.root_path = _.get(this.ctx.app.config.export, 'root_path');
     if (process.env.NODE_ENV === 'development') {
-      this.domain = 'http://127.0.0.1:9999';
+      this.root_path = 'E:\\exportFile\\';
+    }
+    this.file_type = '';
+    if (!fs.existsSync(`${this.root_path}${this.file_type}`)) {
+      // 如果不存在文件夹,就创建
+      fs.mkdirSync(`${this.root_path}${this.file_type}`);
     }
+    this.excel_path = `${sep}excel${sep}`;
+    this.domain = 'http://127.0.0.1';
+    this.export_limit = 50;
   }
   /**
    * 专利交易审核
@@ -160,36 +167,63 @@ class Patent_transService extends CrudService {
     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 arr = [];
-    // const allNotice = [];
-    // sheet.eachRow(async (row, rindex) => {
-    //   if (rindex !== 1) {
-    //     // 组织数据
-    //     const create_number = row.getCell(3).value || undefined,
-    //     patent_name = row.getCell(2).value || undefined,
-    //     on_obligee = row.getCell(4).value || undefined,
-    //     transfer_date = moment(row.getCell(5).value).format('YYYY-MM-DD') || undefined,
-    //     on_afterobligee = row.getCell(6).value || undefined,
-    //     status = row.getCell(7).value || undefined,
-    //     const has_data = await this.model.count({ create_number });
-    //     if (!has_data) {
-    //       const obj = {patent_name,create_number,on_obligee,transfer_date,on_afterobligee,status };
-    //       // 此处添加判断条件,不限制则不需要加,直接放过即可
-    //       const { result, notice } = this.tocheckData(obj);
-    //       if (result) {
-    //         arr.push(obj);
-    //       } else {
-    //         allNotice.push(notice);
-    //       }
-    //     }
-    //   }
-    // });
-    // if (allNotice.length > 0) return allNotice;
-    // const res = await this.model.insertMany(arr);
-    // return res;
+    const workbook = new Excel.Workbook();
+    await workbook.xlsx.load(file.data);
+    const sheet = workbook.getWorksheet(1);
+    const arr = [];
+    const allNotice = [];
+    sheet.eachRow((row, rindex) => {
+      if (rindex !== 1) {
+        const create_number = row.getCell(3).value || undefined,
+          patent_name = row.getCell(2).value || undefined,
+          on_obligee = row.getCell(4).value || undefined,
+          transfer_date =
+            moment(row.getCell(5).value).format('YYYY-MM-DD') || undefined,
+          on_afterobligee = row.getCell(6).value || undefined,
+          status = row.getCell(7).value || undefined;
+        const obj = {
+          patent_name,
+          create_number,
+          on_obligee,
+          transfer_date,
+          on_afterobligee,
+          status,
+        };
+        // 此处添加判断条件,不限制则不需要加,直接放过即可
+        const { result, notice } = this.tocheckData(obj);
+        if (result) {
+          arr.push(obj);
+        } else {
+          allNotice.push(notice);
+        }
+      }
+    });
+    if (allNotice.length > 0) return allNotice;
+    await this.model.insertMany(arr);
+  }
+  /**
+   * 检查数据是否没填 必填项
+   * @param {Object} object 每行数据,已转换成model的字段名
+   */
+  tocheckData(object) {
+    let result = true;
+    const { number } = object;
+    let notice;
+    const arr = [
+      { column: 'create_number', zh: '专利号' },
+    ];
+    const word = [];
+    for (const o of arr) {
+      const { column, zh } = o;
+      if (!_.get(object, column)) {
+        result = false;
+        word.push(`${zh}`);
+      }
+    }
+    if (!result) {
+      notice = `序号${number}缺少:${word.join(';')}`;
+    }
+    return { result, notice };
   }
 }