Merge pull request 'feat: 更新属性结构appends情况 feat #620' (#621) from hot_collection_tree into dev

Reviewed-on: daoyoucloud/tachycode#621
This commit is contained in:
sealday 2024-04-08 12:15:19 +08:00
commit e87024f8d2
2 changed files with 57 additions and 41 deletions

View File

@ -0,0 +1,5 @@
---
"@hera/plugin-rental": patch
---
树形结构关联查询完善

View File

@ -2,7 +2,7 @@ import { assign } from '@nocobase/utils';
import { Context } from '..'; import { Context } from '..';
import { getRepositoryFromParams, pageArgsToLimitArgs } from '../utils'; import { getRepositoryFromParams, pageArgsToLimitArgs } from '../utils';
import { DEFAULT_PAGE, DEFAULT_PER_PAGE } from '../constants'; import { DEFAULT_PAGE, DEFAULT_PER_PAGE } from '../constants';
import { QueryTypes } from 'sequelize'; import { Op, QueryTypes } from 'sequelize';
function totalPage(total, pageSize): number { function totalPage(total, pageSize): number {
return Math.ceil(total / pageSize); return Math.ceil(total / pageSize);
@ -43,6 +43,7 @@ async function listWithPagination(ctx: Context) {
} }
}); });
let filterTreeData = []; let filterTreeData = [];
let filterTreeCount = 0;
if (ctx.action.params.tree) { if (ctx.action.params.tree) {
const foreignKey = collection.treeParentField?.foreignKey || 'parentId'; const foreignKey = collection.treeParentField?.foreignKey || 'parentId';
const params = Object.values(options.filter).flat()[0] || {}; const params = Object.values(options.filter).flat()[0] || {};
@ -55,48 +56,57 @@ async function listWithPagination(ctx: Context) {
dataId = data.id; dataId = data.id;
}; };
await getParent(params); await getParent(params);
const filterTreeDatas = await ctx.db.sequelize.query( const query = `
` WITH RECURSIVE tree1 AS (
WITH RECURSIVE tree1 AS ( SELECT id, "${foreignKey}"
SELECT * FROM ${collection.name}
FROM ${collection.name} WHERE id = :dataId
WHERE id = :dataId
UNION ALL
SELECT p.*
FROM tree1 up
JOIN ${collection.name} p ON up."${foreignKey}" = p.id
),
tree2 AS (
SELECT *
FROM ${collection.name}
WHERE id = :dataId
UNION ALL
SELECT p.*
FROM tree2 down
JOIN ${collection.name} p ON down.id = p."${foreignKey}"
)
SELECT DISTINCT *
FROM (
SELECT *
FROM tree1
UNION ALL UNION ALL
SELECT *
FROM tree2 SELECT p.id, p."${foreignKey}"
) FROM tree1 up
`, JOIN ${collection.name} p ON up."${foreignKey}" = p.id
{ ),
replacements: { tree2 AS (
dataId, SELECT id, "${foreignKey}"
}, FROM ${collection.name}
type: QueryTypes.SELECT, WHERE id = :dataId
UNION ALL
SELECT p.id, p."${foreignKey}"
FROM tree2 down
JOIN ${collection.name} p ON down.id = p."${foreignKey}"
)
SELECT DISTINCT *
FROM (
SELECT *
FROM tree1
UNION ALL
SELECT *
FROM tree2
);`;
const filterTreeDatas = await ctx.db.sequelize.query(query, {
replacements: {
dataId,
}, },
); type: QueryTypes.SELECT,
// logging: console.log
});
const newRows: any[] = filterTreeDatas; const newRows: any[] = filterTreeDatas;
const father = newRows.filter((parent) => parent.parentId === null); const filterIds = newRows.map((item) => item.id);
const where = {
id: {
[Op.in]: filterIds,
},
};
const [rows, count] = await repository.findAndCount({
filter: where,
appends: options.appends || [],
});
const _data = rows.map((item) => item.dataValues);
const father = _data.filter((parent) => parent.parentId === null);
const transTreeData = (father, allRows) => { const transTreeData = (father, allRows) => {
father.forEach((parent, index) => { father.forEach((parent, index) => {
@ -117,13 +127,14 @@ async function listWithPagination(ctx: Context) {
} }
}); });
}; };
transTreeData(father, newRows); transTreeData(father, _data);
filterTreeData = father; filterTreeData = father;
filterTreeCount = count;
} }
} }
const [rows, count] = await repository.findAndCount(options); const [rows, count] = await repository.findAndCount(options);
ctx.body = { ctx.body = {
count, count: filterTreeData.length ? filterTreeCount : count,
rows: filterTreeData.length ? filterTreeData : rows, rows: filterTreeData.length ? filterTreeData : rows,
page: Number(page), page: Number(page),
pageSize: Number(pageSize), pageSize: Number(pageSize),