明树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 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 || []; // 移除children属性(使用正确拼写)
for (let i = kids.length - 1; i >= 0; i--) { delete nodeCopy.children;
stack.push({ node: kids[i], parentId: node.id });
// 添加当前节点到结果集
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; return result;
} }
...@@ -33,8 +50,8 @@ const complexForest = [ ...@@ -33,8 +50,8 @@ const complexForest = [
children: [{ id: 'B1' }] children: [{ id: 'B1' }]
} }
]; ];
console.log(flattenTreeIterative(complexForest)); console.log(flattenTree(complexForest));
/* 输出: /* 输出:
[ [
{ id: 'A', name: 'Root A', parentId: null }, { id: 'A', name: 'Root A', parentId: null },
......
...@@ -13,14 +13,26 @@ async function createResource(req, res, next) { ...@@ -13,14 +13,26 @@ async function createResource(req, res, next) {
ret = ret.toJSON(); ret = ret.toJSON();
console.log(ret); console.log(ret);
if (body.resourceInfos && body.resourceInfos.length && ret.id) { if (body.resourceInfos && body.resourceInfos.length && ret.id) {
let ris = []; if(type == 1){
for (let index = 0; index < body.resourceInfos.length; index++) { let ris = [];
const element = body.resourceInfos[index]; for (let index = 0; index < body.resourceInfos.length; index++) {
element.resourceId = ret.id; const element = body.resourceInfos[index];
ris.push(element); element.resourceId = ret.id;
ris.push(element);
}
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);
} }
console.log(ris, "===")
await DB.ResourcesInfo.bulkCreate(ris);
} }
return res.sendData(ret); return res.sendData(ret);
} catch (error) { } catch (error) {
......
...@@ -255,6 +255,8 @@ async function listUser(req, res, next) { ...@@ -255,6 +255,8 @@ async function listUser(req, res, next) {
through: { attributes: [] }, through: { attributes: [] },
} }
]; ];
search.distinct = true, // ✅ 关键:去重
search.col = 'id'
search.attributes = req.body.attributes || { exclude: ['password', 'salt'] }; search.attributes = req.body.attributes || { exclude: ['password', 'salt'] };
let ret = await DB.User.findAndCountAll(search); let ret = await DB.User.findAndCountAll(search);
return res.sendData(ret); return res.sendData(ret);
......
...@@ -13,6 +13,10 @@ const Resources = sequelize.define('Resources', { ...@@ -13,6 +13,10 @@ const Resources = sequelize.define('Resources', {
type: DataTypes.STRING, type: DataTypes.STRING,
comment: "资源库key" comment: "资源库key"
}, },
parentkey: {
type: DataTypes.STRING,
comment: "资源库key"
},
name: { name: {
type: DataTypes.STRING, type: DataTypes.STRING,
comment: "资源库名称" comment: "资源库名称"
......
...@@ -86,19 +86,12 @@ function flattenTreeIterative(forest) { ...@@ -86,19 +86,12 @@ function flattenTreeIterative(forest) {
return result; 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 = { module.exports = {
saltHashPassword, saltHashPassword,
checkUserPassword, checkUserPassword,
buildTree, buildTree,
disTree, 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