123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- 'use strict';
- /**
- * 指标模板控制器
- *
- * @author Mai
- * @data 2018/4/19
- * @version
- */
- const fs = require('fs');
- const streamToArray = require('stream-to-array');
- module.exports = {
- /**
- * 转换数据
- *
- * @param {Array} list - 数据库查找的数据
- * @param {Boolean} tree - 是否展示为树形结构
- * @param {String} id - id属性名
- * @param {String} pid - pid属性名
- * @return {Object} - 返回转换后的数据
- */
- convertData(list, tree = true, id, pid) {
- const rootData = [];
- const childData = {};
- let listData = [];
- for (const tmp of list) {
- if (tmp[pid] === 0 || tmp[pid] === -1) {
- rootData.push(tmp);
- continue;
- }
- if (childData[tmp[pid]] === undefined) {
- childData[tmp[pid]] = [];
- }
- childData[tmp[pid]].push(tmp);
- }
- // 递归组织数据
- if (tree) {
- this._recursionTreeData(rootData, childData, id);
- } else {
- this._recursionData(rootData, childData, listData, id);
- }
- return tree ? rootData : listData;
- },
- /**
- * 递归组织数组结构数据
- *
- * @param {Array} parent - 父节点
- * @param {Object} childData - 子元素(以pid为key的对象)
- * @param {Array} outputData - 处理后的数据
- * @param {String} id - id属性名
- * @return {void}
- */
- _recursionData(parent, childData, outputData = [], id) {
- for (const index in parent) {
- // 父节点先进数组
- outputData.push(parent[index]);
- // 判断是否存在子项
- if (childData[parent[index][id]] !== undefined) {
- // 子项作为父节点递归查找子项中是否还有子项
- const tmpParent = childData[parent[index][id]];
- // 已用的子项删除
- delete childData[parent[index][id]];
- // 递归元素
- this._recursionData(tmpParent, childData, outputData);
- }
- }
- },
- /**
- * 递归组织树结构数据
- *
- * @param {Array} parent - 父节点
- * @param {Object} childData - 子元素(以pid为key的对象)
- * @param {String} id - id属性名
- * @return {void}
- */
- _recursionTreeData(parent, childData = {}, id) {
- if (Object.keys(childData).length <= 0) {
- return;
- }
- for (const index in parent) {
- // 判断是否存在子项
- if (childData[parent[index][id]] !== undefined) {
- // 子项作为父节点递归查找子项中是否还有子项
- const tmpParent = childData[parent[index][id]];
- // 已用的子项删除
- delete childData[parent[index][id]];
- this._recursionTreeData(tmpParent, childData, id);
- // 递归完赋值回原数据
- parent[index].children = tmpParent;
- }
- }
- },
- /**
- * 将文件流的数据保存至本地文件
- * @param stream
- * @param fileName
- * @returns {Promise<void>}
- */
- async saveStreamFile(stream, fileName) {
- // 读取字节流
- const parts = await streamToArray(stream);
- // 转化为buffer
- const buffer = Buffer.concat(parts);
- // 写入文件
- await fs.writeFileSync(fileName, buffer);
- },
- /**
- * 检查code是否是指标模板数据
- * @param {String} code
- * @returns {boolean}
- */
- ValidTemplateCode(code) {
- const reg1 = /(^[a-z]+)([a-z0-9\-]*)/i;
- const reg2 = /([a-z0-9]+$)/i;
- return reg1.test(code) && reg2.test(code);
- },
- /**
- * 检查code 是否是 指标节点编号
- * @param {String} code
- * @returns {boolean}
- */
- ValidTemplateNodeCode(code) {
- const reg1 = /(^[a-z]+)([a-z0-9\-]*)/i;
- const reg2 = /([a-z0-9]+$)/i;
- const reg3 = /([\-][0-9]+$)/i;
- return reg1.test(code) && reg2.test(code) && (!reg3.test(code));
- },
- /**
- * 检查code 是否是 指标节点编号
- * @param {String} code
- * @returns {boolean}
- */
- ValidTemplateIndexCode(code) {
- const reg1 = /(^[a-z]+)([a-z0-9\-]*)/i;
- const reg2 = /([0-9]+$)/i;
- return reg1.test(code) && reg2.test(code);
- },
- findObj(arr, field, value) {
- if (arr.length === 0) { return undefined; }
- for (const a of arr) {
- if (a[field] && a[field] === value) {
- return a;
- }
- }
- return undefined;
- }
- }
|