|
@@ -1,7 +1,7 @@
|
|
|
'use strict';
|
|
|
|
|
|
/**
|
|
|
- * 用户管理业务类
|
|
|
+ * 指标节点业务类
|
|
|
*
|
|
|
* @author Mai
|
|
|
* @date 2018/4/19
|
|
@@ -22,6 +22,87 @@ module.exports = app => {
|
|
|
this.tableName = 'template_node';
|
|
|
}
|
|
|
|
|
|
+ _findParentId(code, nodes) {
|
|
|
+ if (nodes.length === 0) { return -1; }
|
|
|
+ const codeList = code.split('-');
|
|
|
+ if (codeList.length > 1) {
|
|
|
+ codeList.splice(codeList.length - 1);
|
|
|
+ const parentCode = codeList.join('-');
|
|
|
+ for (const node of nodes) {
|
|
|
+ if (parentCode === node.code) {
|
|
|
+ return node.node_id;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ for (const node of nodes) {
|
|
|
+ if (code.search(node.code) === 0) {
|
|
|
+ return node.node_id;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ _parseSheetData(excelSheet, nodes, indexes) {
|
|
|
+ for (const row of excelSheet.data) {
|
|
|
+ if (!row[0]) { continue; }
|
|
|
+ if (this.ctx.helper.ValidTemplateNodeCode(row[0])) {
|
|
|
+ if (!this.ctx.helper.findObj(nodes, 'code', row[0])) {
|
|
|
+ const node = {
|
|
|
+ template_id: 1,
|
|
|
+ node_id: nodes.length + 1,
|
|
|
+ node_pid: this._findParentId(row[0], nodes),
|
|
|
+ code: row[0],
|
|
|
+ name: row[1],
|
|
|
+ };
|
|
|
+ nodes.push(node);
|
|
|
+ }
|
|
|
+ } else if (this.ctx.helper.ValidTemplateIndexCode(row[0])) {
|
|
|
+ const index = {
|
|
|
+ code: row[0],
|
|
|
+ name: row[1],
|
|
|
+ unit1: row[2],
|
|
|
+ unit2: row[3],
|
|
|
+ node_id: nodes.length,
|
|
|
+ };
|
|
|
+ if (row[5] === '√') {
|
|
|
+ index.indexType = 1;
|
|
|
+ } else if (row[6] === '√') {
|
|
|
+ index.indexType = 2;
|
|
|
+ } else if (row[7] === '√') {
|
|
|
+ index.indexType = 3;
|
|
|
+ } else if (row[8] === '√') {
|
|
|
+ index.indexType = 4;
|
|
|
+ }
|
|
|
+ indexes.push(index);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async importData(excelSheets) {
|
|
|
+ let result = false;
|
|
|
+ const limit = 30000;
|
|
|
+ const transaction = await this.db.beginTransaction();
|
|
|
+ try {
|
|
|
+ const nodes = [], indexes = [];
|
|
|
+ for (const sheet of excelSheets) {
|
|
|
+ this._parseSheetData(sheet, nodes, indexes);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (nodes.length > 0) {
|
|
|
+ await transaction.delete(this.tableName, {template_id: 1});
|
|
|
+ const insertResult = await transaction.insert(this.tableName, nodes);
|
|
|
+ result = insertResult.affectedRows === nodes.length;
|
|
|
+ } else {
|
|
|
+ throw 'Excel文件中无标准的指标数据';
|
|
|
+ }
|
|
|
+
|
|
|
+ await transaction.commit();
|
|
|
+ } catch(err) {
|
|
|
+ await transaction.rollback();
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
return TemplateNode;
|