Reviewed-on: daoyoucloud/tachycode#621
This commit is contained in:
commit
e87024f8d2
5
.changeset/pretty-scissors-knock.md
Normal file
5
.changeset/pretty-scissors-knock.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@hera/plugin-rental": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
树形结构关联查询完善
|
@ -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,27 +56,26 @@ 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 *
|
SELECT id, "${foreignKey}"
|
||||||
FROM ${collection.name}
|
FROM ${collection.name}
|
||||||
WHERE id = :dataId
|
WHERE id = :dataId
|
||||||
|
|
||||||
UNION ALL
|
UNION ALL
|
||||||
|
|
||||||
SELECT p.*
|
SELECT p.id, p."${foreignKey}"
|
||||||
FROM tree1 up
|
FROM tree1 up
|
||||||
JOIN ${collection.name} p ON up."${foreignKey}" = p.id
|
JOIN ${collection.name} p ON up."${foreignKey}" = p.id
|
||||||
),
|
),
|
||||||
tree2 AS (
|
tree2 AS (
|
||||||
SELECT *
|
SELECT id, "${foreignKey}"
|
||||||
FROM ${collection.name}
|
FROM ${collection.name}
|
||||||
WHERE id = :dataId
|
WHERE id = :dataId
|
||||||
|
|
||||||
UNION ALL
|
UNION ALL
|
||||||
|
|
||||||
SELECT p.*
|
SELECT p.id, p."${foreignKey}"
|
||||||
FROM tree2 down
|
FROM tree2 down
|
||||||
JOIN ${collection.name} p ON down.id = p."${foreignKey}"
|
JOIN ${collection.name} p ON down.id = p."${foreignKey}"
|
||||||
)
|
)
|
||||||
@ -86,17 +86,27 @@ async function listWithPagination(ctx: Context) {
|
|||||||
UNION ALL
|
UNION ALL
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM tree2
|
FROM tree2
|
||||||
)
|
);`;
|
||||||
`,
|
const filterTreeDatas = await ctx.db.sequelize.query(query, {
|
||||||
{
|
|
||||||
replacements: {
|
replacements: {
|
||||||
dataId,
|
dataId,
|
||||||
},
|
},
|
||||||
type: QueryTypes.SELECT,
|
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),
|
||||||
|
Loading…
Reference in New Issue
Block a user