|
@@ -5,13 +5,16 @@ const _ = require('lodash');
|
|
|
const moment = require('moment');
|
|
|
const assert = require('assert');
|
|
|
const { ObjectId } = require('mongoose').Types;
|
|
|
-
|
|
|
+const Excel = require('exceljs');
|
|
|
+const { sep } = require('path');
|
|
|
+const fs = require('fs');
|
|
|
// 交底书
|
|
|
class DisclosureService extends CrudService {
|
|
|
constructor(ctx) {
|
|
|
super(ctx, 'disclosure');
|
|
|
this.model = this.ctx.model.Patent.Disclosure;
|
|
|
this.notice = this.ctx.model.Patent.Notice;
|
|
|
+ this.patent = this.ctx.model.Dock.Patent;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -178,6 +181,125 @@ class DisclosureService extends CrudService {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ // 导入
|
|
|
+ async import({ uri }) {
|
|
|
+ const domain = 'http://127.0.0.1';
|
|
|
+ assert(uri, '未获取到文件地址');
|
|
|
+ const file = await this.ctx.curl(`${domain}${uri}`);
|
|
|
+ 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 = [];
|
|
|
+ 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 };
|
|
|
+ }));
|
|
|
+ sheet.eachRow((row, rindex) => {
|
|
|
+ if (rindex !== 1) {
|
|
|
+ // 组织数据,图片的索引和行索引不一致,准确的说是:图片索引比行索引少1
|
|
|
+ // 原因:图片在工作簿中获取,按照1,2,3...顺序排序,但是行的第一行是表头(当前文件),所以当前行数需要减掉表头那一行
|
|
|
+ const imgid = imgids.find(f => f.row === rindex - 1);
|
|
|
+ let img_url;
|
|
|
+ if (imgid) { img_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,
|
|
|
+ success_number = row.getCell(4).value || undefined,
|
|
|
+ success_date =
|
|
|
+ moment(row.getCell(5).value).format('YYYY-MM-DD') || undefined,
|
|
|
+ inventor = row.getCell(6).value || undefined,
|
|
|
+ agent = row.getCell(7).value || undefined,
|
|
|
+ agent_personal = row.getCell(8).value || undefined,
|
|
|
+ abstract = row.getCell(9).value || undefined,
|
|
|
+ address = row.getCell(10).value || undefined,
|
|
|
+ name = row.getCell(11).value || undefined,
|
|
|
+ apply_personal = row.getCell(12).value || undefined,
|
|
|
+ term = row.getCell(13).value || undefined,
|
|
|
+ type = row.getCell(14).value || undefined,
|
|
|
+ number = row.getCell(1).value || undefined;
|
|
|
+ const obj = {
|
|
|
+ create_number,
|
|
|
+ create_date,
|
|
|
+ success_number,
|
|
|
+ success_date,
|
|
|
+ inventor,
|
|
|
+ agent,
|
|
|
+ agent_personal,
|
|
|
+ abstract,
|
|
|
+ address,
|
|
|
+ name,
|
|
|
+ apply_personal,
|
|
|
+ term,
|
|
|
+ type,
|
|
|
+ img_url,
|
|
|
+ number,
|
|
|
+ };
|
|
|
+ // 此处添加判断条件,不限制则不需要加,直接放过即可
|
|
|
+ const { result, notice } = this.tocheckData(obj);
|
|
|
+ if (result) {
|
|
|
+ arr.push(obj);
|
|
|
+ } else {
|
|
|
+ allNotice.push(notice);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ if (allNotice.length > 0) return allNotice;
|
|
|
+ await this.patent.insertMany(arr);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查数据是否没填 必填项
|
|
|
+ * @param {Object} object 每行数据,已转换成model的字段名
|
|
|
+ */
|
|
|
+ tocheckData(object) {
|
|
|
+ let result = true;
|
|
|
+ const { number } = object;
|
|
|
+ let notice;
|
|
|
+ const arr = [
|
|
|
+ { column: 'create_number', zh: '申请号' },
|
|
|
+ { column: 'create_date', zh: '申请日' },
|
|
|
+ { column: 'success_number', zh: '公开(公告)号' },
|
|
|
+ { column: 'success_date', zh: '公开(公告)日' },
|
|
|
+ { column: 'inventor', zh: '发明人' },
|
|
|
+ { column: 'apply_personal', zh: '申请人' },
|
|
|
+ { column: 'name', 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 };
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转换图片为base64
|
|
|
+ * @param {Object} object excel获取的图片object
|
|
|
+ * @property extension 后缀,文件类型
|
|
|
+ * @property buffer 图片内容,不含头部信息的,
|
|
|
+ */
|
|
|
+ turnImageToBase64(object = {}) {
|
|
|
+ const { extension, buffer } = object;
|
|
|
+ if (extension && buffer) {
|
|
|
+ const suffix = object.extension;
|
|
|
+ const ib = object.buffer.toString('base64');
|
|
|
+ const base64 = `data:image/${suffix};base64,${ib}`;
|
|
|
+ return base64;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
module.exports = DisclosureService;
|