|
@@ -22,6 +22,14 @@ module.exports = app => {
|
|
|
this.tableName = 'template_node';
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 查找父节点(根据编号),忽略大小写
|
|
|
+ * e.g. z1(z), z1-e(z1), z1-e-a(z1-e)
|
|
|
+ * @param code
|
|
|
+ * @param nodes
|
|
|
+ * @returns {*}
|
|
|
+ * @private
|
|
|
+ */
|
|
|
_findParentId(code, nodes) {
|
|
|
if (nodes.length === 0) { return -1; }
|
|
|
const codeList = code.split('-');
|
|
@@ -29,18 +37,25 @@ module.exports = app => {
|
|
|
codeList.splice(codeList.length - 1);
|
|
|
const parentCode = codeList.join('-');
|
|
|
for (const node of nodes) {
|
|
|
- if (parentCode === node.code) {
|
|
|
+ if (parentCode.toLowerCase() === node.code.toLowerCase()) {
|
|
|
return node.node_id;
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
for (const node of nodes) {
|
|
|
- if (code.search(node.code) === 0) {
|
|
|
+ if (code.toLowerCase().search(node.code.toLowerCase()) === 0) {
|
|
|
return node.node_id;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ /**
|
|
|
+ * 解析一个Excel工作表内的全部 指标节点 和 指标
|
|
|
+ * @param {Object} excelSheet
|
|
|
+ * @param {Array} nodes - 解析后的指标节点
|
|
|
+ * @param {Array} indexes - 解析后的指标
|
|
|
+ * @private
|
|
|
+ */
|
|
|
_parseSheetData(excelSheet, nodes, indexes) {
|
|
|
for (const row of excelSheet.data) {
|
|
|
if (!row[0]) { continue; }
|
|
@@ -49,7 +64,7 @@ module.exports = app => {
|
|
|
const node = {
|
|
|
template_id: 1,
|
|
|
node_id: nodes.length + 1,
|
|
|
- node_pid: this._findParentId(row[0], nodes),
|
|
|
+ node_pid: this._findParentId(row[0], nodes) || -1,
|
|
|
code: row[0],
|
|
|
name: row[1],
|
|
|
};
|
|
@@ -62,21 +77,29 @@ module.exports = app => {
|
|
|
unit1: row[2],
|
|
|
unit2: row[3],
|
|
|
node_id: nodes.length,
|
|
|
+ index_id: indexes.length + 1,
|
|
|
+ rule: row[9]
|
|
|
};
|
|
|
- if (row[5] === '√') {
|
|
|
- index.indexType = 1;
|
|
|
+ if (row[4] === '√') {
|
|
|
+ index.index_type = 1;
|
|
|
+ } else if (row[5] === '√') {
|
|
|
+ index.index_type = 2;
|
|
|
} else if (row[6] === '√') {
|
|
|
- index.indexType = 2;
|
|
|
+ index.index_type = 3;
|
|
|
} else if (row[7] === '√') {
|
|
|
- index.indexType = 3;
|
|
|
- } else if (row[8] === '√') {
|
|
|
- index.indexType = 4;
|
|
|
+ index.index_type = 4;
|
|
|
}
|
|
|
indexes.push(index);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 导入Excel数据
|
|
|
+ *
|
|
|
+ * @param {Array} excelSheets - Excel文件中的全部工作表
|
|
|
+ * @returns {Promise<boolean>}
|
|
|
+ */
|
|
|
async importData(excelSheets) {
|
|
|
let result = false;
|
|
|
const limit = 30000;
|
|
@@ -90,12 +113,16 @@ module.exports = app => {
|
|
|
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;
|
|
|
+ if (insertResult.affectedRows !== nodes.length) {
|
|
|
+ throw '导入指标节点错误';
|
|
|
+ }
|
|
|
+ await this.ctx.service.templateIndex.importData(indexes, transaction);
|
|
|
} else {
|
|
|
throw 'Excel文件中无标准的指标数据';
|
|
|
}
|
|
|
|
|
|
await transaction.commit();
|
|
|
+ result = true;
|
|
|
} catch(err) {
|
|
|
await transaction.rollback();
|
|
|
throw err;
|