明树Git Lab

Commit d8d782be authored by zfp1's avatar zfp1

update

parent 9d7324e3
Pipeline #103960 passed with stage
in 4 seconds
function flattenTreeIterative(forest) {
function flattenTree(forest) {
const result = [];
const stack = [...forest.map(node => ({ node, parentId: null }))];
while (stack.length) {
const { node, parentId } = stack.pop();
const { chilrden, children, ...rest } = node;
/**
* 递归处理节点
* @param {Object} node - 当前节点
* @param {string|null} parentId - 父节点ID
*/
function traverse(node, parentId) {
// 创建节点副本避免修改原数据
const nodeCopy = { ...node };
result.push({ ...rest, parentId });
// 添加parentId属性
nodeCopy.parentId = parentId;
const kids = chilrden || children || [];
for (let i = kids.length - 1; i >= 0; i--) {
stack.push({ node: kids[i], parentId: node.id });
// 移除children属性(使用正确拼写)
delete nodeCopy.children;
// 添加当前节点到结果集
result.push(nodeCopy);
// 处理子节点(兼容chilrden拼写错误)
const children = node.chilrden || node.children || [];
for (const child of children) {
traverse(child, node.id);
}
}
// 遍历森林中的每棵树
for (const tree of forest) {
traverse(tree, null); // 根节点parentId为null
}
return result;
}
......@@ -34,7 +51,7 @@ const complexForest = [
}
];
console.log(flattenTreeIterative(complexForest));
console.log(flattenTree(complexForest));
/* 输出:
[
{ id: 'A', name: 'Root A', parentId: null },
......
......@@ -13,6 +13,7 @@ async function createResource(req, res, next) {
ret = ret.toJSON();
console.log(ret);
if (body.resourceInfos && body.resourceInfos.length && ret.id) {
if(type == 1){
let ris = [];
for (let index = 0; index < body.resourceInfos.length; index++) {
const element = body.resourceInfos[index];
......@@ -21,6 +22,17 @@ async function createResource(req, res, next) {
}
console.log(ris, "===")
await DB.ResourcesInfo.bulkCreate(ris);
} else if(type == 2) {
//树结构
let ris = [];
let data = utils.flattenTreeIterative(body.resourceInfos);
for (let index = 0; index < data.length; index++) {
const element = data[index];
element.resourceId = ret.id;
ris.push(element);
}
await DB.ResourcesInfo.bulkCreate(ris);
}
}
return res.sendData(ret);
} catch (error) {
......
......@@ -255,6 +255,8 @@ async function listUser(req, res, next) {
through: { attributes: [] },
}
];
search.distinct = true, // ✅ 关键:去重
search.col = 'id'
search.attributes = req.body.attributes || { exclude: ['password', 'salt'] };
let ret = await DB.User.findAndCountAll(search);
return res.sendData(ret);
......
......@@ -13,6 +13,10 @@ const Resources = sequelize.define('Resources', {
type: DataTypes.STRING,
comment: "资源库key"
},
parentkey: {
type: DataTypes.STRING,
comment: "资源库key"
},
name: {
type: DataTypes.STRING,
comment: "资源库名称"
......
......@@ -86,19 +86,12 @@ function flattenTreeIterative(forest) {
return result;
}
function genTracSourceCode({EnterpriseCode, type, date, batchNum, logisticsNum}) {
// 企业代码(9) MA4W271Y8 + 产品代码【种植、初加工、深加工】(6)+ 生产/出厂日期(8)) + 批次号(8)+ 物流码 + 校验码(8)
//MA4W271Y8
date = date && moment().format('YYYYMMDD');
return `${EnterpriseCode}${type}${date}${batchNum}${logisticsNum}`;
}
module.exports = {
saltHashPassword,
checkUserPassword,
buildTree,
disTree,
flattenTreeIterative
}
\ 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