部门树形递归查询

    /**
     * 查询部门列表树
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/findOrgTree")
    public SystemResult findOrgTree(long companyId) {
        log.info("查询部门列表树入参打印companyId={}", companyId);
        //用企业id查询企业信息,转为部门对象放在首位
        SysCompanyEntity sysCompany = pcSysCompanyDao.getOne(companyId);
        SysOrganizationEntity sysOrg = new SysOrganizationEntity();
        sysOrg.setName(sysCompany.getName());
        sysOrg.setId(-1);
        SysOrganizationTree sysOrganizationTree = new SysOrganizationTree();
        sysOrganizationTree.setSysOrganization(sysOrg);
        //初始父id为-1
        long pId = -1;
        SysOrganizationTree sysOrgTree = getOrgTreeByPid(sysOrganizationTree, pId, companyId);
        result = new SystemResult(Dict_transaction_result_code.SUCCESS, "查询成功", sysOrgTree);
        log.info("查询部门列表树结果响应result={}", JSONObject.toJSONString(result));
        return result;
    }

    /**
     * 递归查询下级List-部门
     *
     * @param sysOrganizationTree
     * @param pId                 父id
     */
    private SysOrganizationTree getOrgTreeByPid(SysOrganizationTree sysOrganizationTree, long pId, long sysCoPid) {
        //根据父Id查询list
        List sysOrganizationList = pcSysOrganizationDao.findListByPIdAndSysCoPid(pId, sysCoPid);


        if (sysOrganizationList != null) {
            List SysOrganizationTreeList = new ArrayList();
            for (SysOrganizationEntity sysOrganizationEntity :
                    sysOrganizationList) {
                SysOrganizationTree sysOrganizationTreeSon = new SysOrganizationTree();
                sysOrganizationTreeSon.setSysOrganization(sysOrganizationEntity);
                //此处进行递归查询
                SysOrganizationTree orgTreeByPid = getOrgTreeByPid(sysOrganizationTreeSon, sysOrganizationEntity.getId(), sysCoPid);
                SysOrganizationTreeList.add(orgTreeByPid);
            }
            sysOrganizationTree.setSysOrganizationTreeList(SysOrganizationTreeList);
        }
        return sysOrganizationTree;
    }

 

你可能感兴趣的:(java技术)