Java postgre根据组织ID获取组织结构树

1.查询所有组织controller

@AutoLog(value = "查询所有组织")
    @PostMapping(value = "/getAllWorkOrg")
    public Result<Object> getAllWorkOrg(@RequestBody Map<String, Object> params) {
        try {
            List<Map<String, Object>> resultMap= workOrgService.getAllWorkOrg(params);
            return Result.OK(resultMap);
        } catch (Exception e) {
            return Result.error("账期查询接口接口调用失败!!", e.getMessage());
        }
    }

查询所有组织实现类impl

853为根节点的编码
List<Map<String, Object>> workOrgs = workOrgMapper.getAllWorkOrg(params);
            Map<String, List<Map<String,Object>>> parentMap = workOrgs.stream().collect(Collectors.groupingBy(item->String.valueOf(item.get("parentId")), LinkedHashMap::new,Collectors.toList()));
            List<String> rootIds = new ArrayList<>();
            workOrgs.forEach(item->{
                List<Map<String, Object>> tempList = parentMap.get(item.get("id"));
                if (oConvertUtils.listIsEmpty(tempList)) {
                    tempList = new ArrayList<>();
                    rootIds.add(String.valueOf(item.get("id")));
                }
                item.put("children", tempList);
            });
            List<Map<String, Object>> result =workOrgs.stream().filter(item->"853".equalsIgnoreCase(String.valueOf(item.get("id")))).collect(Collectors.toList());
            return result;

查询所有组织sql即xml文件

 <select id="getAllWorkOrg" parameterType="java.util.Map" resultType="java.util.Map">
        SELECT DISTINCT * FROM (
            WITH RECURSIVE org_hierarchy AS (
            SELECT
                id as "id",
                org_type as "orgType",
                parent_id as "parentId",
                name as "name"
            FROM work_org
            <where>
                <if test="orgId!= null and orgId!= ''">
                    id =#{orgId}
                </if>
            </where>
            UNION ALL
            SELECT
                o.id as "id",
                o.org_type as "orgType",
                o.parent_id as "parentId",
                o.name as "name"
            FROM work_org o
            JOIN org_hierarchy oh ON o.parent_id = oh.id
            )
            SELECT * FROM org_hierarchy
            <where>
                <if test="dispatchTarget != null and dispatchTarget != '' and dispatchTarget=='1'.toString()">
                    id  like '15%' or  id like '16%' or id like '18%' or  id like '0001%' or id='853'
                </if>
                <if test="dispatchTarget != null and dispatchTarget != '' and dispatchTarget=='2'.toString()">
                    id  like '08%' or  id like '06%' or id='853'
                </if>
            </where>
        ) AS FOO
    </select>

 

posted @ 2025-07-08 11:29  krt-wanyi  阅读(7)  评论(0)    收藏  举报