明树Git Lab

Commit 9d7324e3 authored by zfp1's avatar zfp1

update

parent 99a08ce9
Pipeline #103959 passed with stage
in 3 seconds
const _ = require('lodash');
let a = [
{
xjlr: 100,
xjlc: 200,
sdsqjxjll: 300,
tzsds: 400,
sdshjxjll: 500,
year: "合计",
},
{
xjlr: 100,
xjlc: 200,
sdsqjxjll: 300,
tzsds: 400,
sdshjxjll: 500,
year: 2022,
},
{
xjlr: 101,
xjlc: 201,
sdsqjxjll: 301,
tzsds: 401,
sdshjxjll: 501,
year: 2023,
},
{
xjlr: 1003,
xjlc: 2003,
sdsqjxjll: 3003,
tzsds: 4003,
sdshjxjll: 5003,
year: "2024",
},
{
xjlr: 1004,
xjlc: 2004,
sdsqjxjll: 3004,
tzsds: 4004,
sdshjxjll: 5004,
year: "2025",
}
]
const b = _.sortBy(a, 'year');
console.log(b);
function thvc(a) {
let xjlr = [], xjlc = [], sdsqjxjll = [], tzsds = [], sdshjxjll =[];
let retArr = [];
for (let index = 0; index < a.length; index++) {
const element = a[index];
xjlr.push(element.xjlr);
xjlc.push(element.xjlc);
sdsqjxjll.push(element.sdsqjxjll);
tzsds.push(element.tzsds);
sdshjxjll.push(element.sdshjxjll);
// year.push(element.year);
}
return [xjlr, xjlc, sdsqjxjll, tzsds, sdshjxjll];
}
console.log(thvc(a));
function cvht(obj) {
for (const key in obj) {
const element = obj[key];
}
}
function reverseTransform(transformedData) {
// 获取第一个字段的数组长度作为对象数量
const field = Object.keys(transformedData)[0];
const itemCount = transformedData[field].length;
function flattenTreeIterative(forest) {
const result = [];
const stack = [...forest.map(node => ({ node, parentId: null }))];
// 遍历每个索引位置构建对象
for (let i = 0; i < itemCount; i++) {
const item = {};
while (stack.length) {
const { node, parentId } = stack.pop();
const { chilrden, children, ...rest } = node;
// 为每个字段赋值
for (const key in transformedData) {
if (transformedData.hasOwnProperty(key)) {
item[key] = transformedData[key][i];
}
}
result.push({ ...rest, parentId });
result.push(item);
const kids = chilrden || children || [];
for (let i = kids.length - 1; i >= 0; i--) {
stack.push({ node: kids[i], parentId: node.id });
}
}
return result;
}
// 使用示例
const transformed = {
xjlr: [100, 100, 101, 1003, 1004],
xjlc: [200, 200, 201, 2003, 2004],
sdsqjxjll: [300, 300, 301, 3003, 3004],
tzsds: [400, 400, 401, 4003, 4004],
sdshjxjll: [500, 500, 501, 5003, 5004]
};
// console.log(reverseTransform(transformed));
// console.log(thvc(a));
// {
// xh: String,
// sjx: String,
// }
// {
// year: []
// }
\ No newline at end of file
// 扩展测试:包含额外字段和标准children字段
const complexForest = [
{
id: 'A',
name: 'Root A',
chilrden: [
{ id: 'A1', value: 100 },
{ id: 'A2', value: 200, children: [{ id: 'A2a' }] }
]
},
{
id: 'B',
name: 'Root B',
children: [{ id: 'B1' }]
}
];
console.log(flattenTreeIterative(complexForest));
/* 输出:
[
{ 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' }
]
*/
\ No newline at end of file
......@@ -158,7 +158,13 @@ async function listResourceInfo(req, res, next) {
}
};
/**
* 这个接口有点问题,只能处理平行数据,数据
* @param {*} req
* @param {*} res
* @param {*} next
* @returns
*/
async function saveAllResourceInfo(req, res, next) {
try {
let { resourceId, data = [] } = req.body;
......@@ -175,9 +181,6 @@ async function saveAllResourceInfo(req, res, next) {
if (!resource) {
return res.sendError(errorMessage.resourceNotFound);
}
if (resource && resource.type == 2) {
data = utils.disTree(data)
}
let toCreate = [], toKeep = [];
if (data.length) {
for (let index = 0; index < data.length; index++) {
......
......@@ -104,7 +104,6 @@ async function createUser(req, res, next) {
const { salt, passwordHash } = utils.saltHashPassword(req.body.password);
body.salt = salt;
body.password = passwordHash;
const ret = await DB.User.create(body);
if (!(ret && ret.id)) {
return res.sendError(errorMessage.databaseQueryError);
......
......@@ -13,7 +13,6 @@ const User = sequelize.define('User', {
mobile: {
type: DataTypes.STRING(11),
allowNull: false,
unique: true,
},
password: {
type: DataTypes.STRING(200),
......
......@@ -66,6 +66,26 @@ function disTree(tree) {
return parallel
}
// 迭代替代递归(万级以上节点)
function flattenTreeIterative(forest) {
const result = [];
const stack = [...forest.map(node => ({ node, parentId: null }))];
while (stack.length) {
const { node, parentId } = stack.pop();
const { chilrden, children, ...rest } = node;
result.push({ ...rest, parentId });
const kids = chilrden || children || [];
for (let i = kids.length - 1; i >= 0; i--) {
stack.push({ node: kids[i], parentId: node.id });
}
}
return result;
}
function genTracSourceCode({EnterpriseCode, type, date, batchNum, logisticsNum}) {
// 企业代码(9) MA4W271Y8 + 产品代码【种植、初加工、深加工】(6)+ 生产/出厂日期(8)) + 批次号(8)+ 物流码 + 校验码(8)
//MA4W271Y8
......
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