明树Git Lab

Commit 6ce89f36 authored by zfp1's avatar zfp1

update

parent b6248355
Pipeline #104028 passed with stage
in 3 seconds
function flattenTree(forest) {
function flattenTree(forest, idDes = 'id', parentIdDes = 'parentId') {
const result = [];
/**
......@@ -11,7 +11,7 @@ function flattenTree(forest) {
const nodeCopy = { ...node };
// 添加parentId属性
nodeCopy.parentId = parentId;
nodeCopy[parentIdDes] = parentId;
// 移除children属性(使用正确拼写)
delete nodeCopy.children;
......@@ -22,7 +22,7 @@ function flattenTree(forest) {
// 处理子节点(兼容chilrden拼写错误)
const children = node.chilrden || node.children || [];
for (const child of children) {
traverse(child, node.id);
traverse(child, node[idDes]);
}
}
......@@ -35,23 +35,56 @@ function flattenTree(forest) {
}
// 扩展测试:包含额外字段和标准children字段
const complexForest = [
let complexForest = [
{
id: 'A',
name: 'Root A',
chilrden: [
{ id: 'A1', value: 100 },
{ id: 'A2', value: 200, children: [{ id: 'A2a' }] }
xh: "1",
zb: "项目资本金",
dw: "万元",
children: [
{
xh: "1.1",
zb: "能建方出资",
dw: "万元"
},
{
xh: "1.2",
zb: "外部股东",
dw: "万元"
}
]
},
{
xh: "2",
zb: "贷款",
dw: "万元",
children: [
{
xh: "2.1",
zb: "其中:并非表项目我方贷款/投保额",
dw: "万元"
}
]
},
{
xh: "3",
zb: "其他出资",
dw: "万元",
children: [
{
xh: "3.1",
zb: "其中:能建方出资",
dw: "万元"
}
]
},
{
id: 'B',
name: 'Root B',
children: [{ id: 'B1' }]
xh: "",
zb: "批复总出资",
dw: "万元"
}
];
]
console.log(flattenTree(complexForest));
console.log(flattenTree(complexForest, 'xh', 'parentXh'));
/* 输出:
[
{ id: 'A', name: 'Root A', parentId: null },
......@@ -62,3 +95,37 @@ console.log(flattenTree(complexForest));
{ 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": [] }]
\ No newline at end of file
......@@ -92,6 +92,7 @@ async function createProject(req, res, next) {
await DB.ProjectGdxx.bulkCreate(projectGdxxs);
// 处理 projectXmtzzes
let projectXmtzzes = body.projectXmtzzes || [];
projectXmtzzes = utils.flattenTree(projectXmtzzes, 'xh', 'parentXh');
projectXmtzzes = projectXmtzzes.map(o => { o.projectId = project.id; return o });
await DB.ProjectXmtzze.bulkCreate(projectXmtzzes);
// 处理 projectBjtjs
......@@ -212,6 +213,7 @@ async function updateProject(req, res, next) {
await DB.ProjectGdxx.bulkCreate(newprojectGdxxs);
// 处理 projectXmtzzes
let projectXmtzzes = body.projectXmtzzes || [];
projectXmtzzes = utils.flattenTree(projectXmtzzes, 'xh', 'parentXh');
let pnids3 = [], newprojectXmtzzes = [];
projectXmtzzes.map(o => { if (!o.id) { o.projectId = project.id; newprojectXmtzzes.push(o); } else { pnids3.push(o.id) } return o });
await DB.ProjectXmtzze.bulkCreate(newprojectXmtzzes);
......@@ -505,6 +507,7 @@ async function getProjectInfo(req, res, next) {
// raw: true
});
project = project.toJSON();
project.projectXmtzzes = utils.buildTree2(project.projectXmtzzes, 'xh', 'parentXh');
//处理文件 还原成项目各个字段拥有的文件
let files = project.files || [];
......
......@@ -1769,9 +1769,9 @@ const Project = sequelize.define('Project', {
// 同步模型到数据库(创建表)
Project.sync({
// force: false,
force: false,
// force: true ,//会删除已存在表并重新创建
alter: true
// alter: true
})
.then(() => {
console.log('Project 表同步成功');
......
......@@ -29,10 +29,10 @@ const ProjectXmtzze = sequelize.define('ProjectXmtzze', {
type: DataTypes.DECIMAL(20, 4),
comment: "人民币计价"
},
// parentId: {
// type: DataTypes.INTEGER,
// comment: "上级ID"
// },
parentXh: {
type: DataTypes.STRING,
comment: "上级ID"
},
projectId: {
type: DataTypes.INTEGER,
......@@ -67,9 +67,9 @@ const ProjectXmtzze = sequelize.define('ProjectXmtzze', {
// 同步模型到数据库(创建表)
ProjectXmtzze.sync({
force: false,
// force: false,
// force: true ,//会删除已存在表并重新创建
// alter: true
alter: true
})
.then(() => {
console.log('ProjectXmtzze 表同步成功');
......
......@@ -86,12 +86,78 @@ function flattenTreeIterative(forest) {
return result;
}
/**
* 把带有chilrden的推平
* @param {*} forest
* @param {*} idDes
* @param {*} parentIdDes
* @returns
*/
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;
}
//
function buildTree2(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');
}
module.exports = {
saltHashPassword,
checkUserPassword,
buildTree,
disTree,
flattenTreeIterative
flattenTreeIterative,
flattenTree,
buildTree2,
}
\ No newline at end of file
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