明树Git Lab

Commit 83f40631 authored by zfp1's avatar zfp1

update

parent 34260035
Pipeline #104238 passed with stage
in 3 seconds
function flattenTree(forest, idDes = 'id', parentIdDes = 'parentId') {
const result = [];
/**
* 递归处理节点
* @param {Object} node - 当前节点
* @param {string|null} parentId - 父节点ID
*/
function traverse(node, parentId) {
// 创建节点副本避免修改原数据
const nodeCopy = { ...node };
// 添加parentId属性
nodeCopy[parentIdDes] = parentId;
// 移除children属性(使用正确拼写)
delete nodeCopy.children;
// 添加当前节点到结果集
result.push(nodeCopy);
// 处理子节点(兼容chilrden拼写错误)
const children = node.chilrden || node.children || [];
for (const child of children) {
traverse(child, node[idDes]);
}
}
// 遍历森林中的每棵树
for (const tree of forest) {
traverse(tree, null); // 根节点parentId为null
}
return result;
}
// 扩展测试:包含额外字段和标准children字段
let complexForest =[
{
"xh": "1",
"zb": "项目资本金",
"dw": "万元",
"children": [
{
"xh": "1.1",
"zb": "能建方出资",
"dw": "万元",
"rmbjj": "2"
},
{
"xh": "1.2",
"zb": "外部股东",
"dw": "万元",
"rmbjj": "3"
}
],
"rmbjj": "1"
},
{
"xh": "2",
"zb": "贷款",
"dw": "万元",
"children": [
{
"xh": "2.1",
"zb": "其中:并非表项目我方贷款/投保额",
"dw": "万元",
"rmbjj": "5"
}
],
"rmbjj": "4"
},
{
"xh": "3",
"zb": "其他出资",
"dw": "万元",
"children": [
{
"xh": "3.1",
"zb": "其中:能建方出资",
"dw": "万元",
"rmbjj": "7"
}
],
"rmbjj": "6"
},
{
"xh": "",
"zb": "批复总出资",
"dw": "万元",
"rmbjj": "8"
}
]
console.log(flattenTree(complexForest, 'xh', 'parentXh'));
/* 输出:
[
{ id: 'A', name: 'Root A', parentId: null },
{ id: 'A1', value: 100, parentId: 'A' },
{ id: 'A2', value: 200, parentId: 'A' },
{ id: 'A2a', parentId: 'A2' },
{ id: 'B', name: 'Root B', parentId: null },
{ id: 'B1', parentId: 'B' }
]
*/
// const _ = require('lodash');
// function buildTree(nodes, keyid = 'id', keyParentId = 'parentId') {
// // 创建一个映射,将节点ID映射到节点对象
// const nodeMap = new Map(nodes.map(node => [String(node[keyid]), { ...node, children: [] }]));
// // 遍历所有节点,并将它们添加到对应父节点的children数组中
// nodes.forEach(node => {
// const parentId = String(node[keyParentId]);
// if (parentId !== null) {
// // 确保父节点在映射中存在
// if (nodeMap.has(parentId)) {
// // 将当前节点添加到父节点的children数组中
// nodeMap.get(parentId).children.push(nodeMap.get(String(node[keyid])));
// nodeMap.get(parentId).children = _.orderBy(nodeMap.get(parentId).children, 'order')
// }
// }
// });
// // 从映射中提取顶级节点(parentId为null)
// return _.orderBy(Array.from(nodeMap.values()).filter(node => node[keyParentId] == null), 'order');
// }
// console.log(JSON.stringify(buildTree([
// { xh: '1', zb: '项目资本金', dw: '万元', parentXh: null },
// { xh: '1.1', zb: '能建方出资', dw: '万元', parentXh: '1' },
// { xh: '1.2', zb: '外部股东', dw: '万元', parentXh: '1' },
// { xh: '2', zb: '贷款', dw: '万元', parentXh: null },
// { xh: '2.1', zb: '其中:并非表项目我方贷款/投保额', dw: '万元', parentXh: '2' },
// { xh: '3', zb: '其他出资', dw: '万元', parentXh: null },
// { xh: '3.1', zb: '其中:能建方出资', dw: '万元', parentXh: '3' },
// { xh: '', zb: '批复总出资', dw: '万元', parentXh: null }
// ], 'xh', 'parentXh')))
// [{ "xh": "1", "zb": "项目资本金", "dw": "万元", "parentXh": null, "children": [{ "xh": "1.1", "zb": "能建方出资", "dw": "万元", "parentXh": "1", "children": [] }, { "xh": "1.2", "zb": "外部股东", "dw": "万元", "parentXh": "1", "children": [] }] }, { "xh": "2", "zb": "贷款", "dw": "万元", "parentXh": null, "children": [{ "xh": "2.1", "zb": "其中:并非表项目我方贷款/投保额", "dw": "万元", "parentXh": "2", "children": [] }] }, { "xh": "3", "zb": "其他出资", "dw": "万元", "parentXh": null, "children": [{ "xh": "3.1", "zb": "其中:能建方出资", "dw": "万元", "parentXh": "3", "children": [] }] }, { "xh": "", "zb": "批复总出资", "dw": "万元", "parentXh": null, "children": [] }]
function disTree(tree) {
let parallel = [];
function addToParallel(nodes) {
nodes.forEach(node => {
parallel.push({
...node,
children: undefined
});
if (node.children && node.children.length) {
addToParallel(node.children);
}
});
}
addToParallel(tree)
return parallel
}
console.log(disTree( [
1,2,3
]))
\ No newline at end of file
......@@ -87,7 +87,7 @@ async function getXmtzzjllTem({ startYear, endYear, tampName, projectId }) {
// 遍历所有单元格添加边框
worksheet.eachRow((row, rowNumber) => {
row.eachCell((cell, colNumber) => {
if (rowNumber >= 4 && rowNumber <= 8 && colNumber >= 4 && colNumber <= (3 + columns.length)) {
if (rowNumber >= 4 && rowNumber <= worksheet.actualRowCount && colNumber >= 4 && colNumber <= (3 + columns.length)) {
cell.numFmt = '#,##0.00'; //数字格式
}
cell.alignment = {
......@@ -186,7 +186,11 @@ async function importExcelTempData(req, res, next) {
} else if (cell.type === ExcelJS.ValueType.String) {
rowData.push(cell.text + "_suffix");
} else if (cell.value === null || cell.value === undefined) {
rowData.push("");
if([4, 14, 25, 32].includes(ri) && tampName == "tjjh") {
rowData.push("");
} else {
rowData.push(0);
}
} else {
rowData.push(cell.value);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment