//package cn.iocoder.yudao.module.knowledge.controller.api;
//
//import cn.iocoder.yudao.framework.common.pojo.CommonResult;
//import cn.iocoder.yudao.module.knowledge.constants.Result;
//import cn.iocoder.yudao.module.system.api.dept.DeptApi;
//import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
//import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
//import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
//import cn.iocoder.yudao.module.knowledge.common.StringUtils;
//import cn.iocoder.yudao.module.knowledge.entity.DeptTree;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.*;
//
//import java.util.*;
//
//@RestController
//@RequestMapping("/commonApi")
//public class CommonApi {
//    @Autowired
//    private DeptApi deptApi;
//    @Autowired
//    private AdminUserApi userApi;
//    @GetMapping("/getDeptData")
//    public Result getDeptData(){
//        List<DeptRespDTO> deptList = deptApi.getDeptList(null);
//        List<DeptTree> tempList =new ArrayList<>();
//        for (DeptRespDTO deptRespDTO : deptList) {
//            DeptTree deptTree =new DeptTree();
//            deptTree.setId(deptRespDTO.getId());
//            deptTree.setParentId(deptRespDTO.getParentId());
//            deptTree.setLabel(deptRespDTO.getName());
//            tempList.add(deptTree);
//        }
//        //循环递归处理返回树结构
//        List<DeptTree> deptTrees = buildTree(tempList);
//        return Result.success(deptTrees);
//    }
////    @GetMapping("/getUserData")
////    public Result getUserData(@RequestParam(value = "deptIds") Collection<Long> deptIds,
////                              @RequestParam(value = "nickName",required = false) String nickName){
////        CommonResult<List<AdminUserRespDTO>> userListByDeptIds = userApi.getUserByUsername(deptIds,false,nickName);
////        return Result.success(userListByDeptIds.getData());
////    }
//
////    public List<DeptTree> buildTree(List<DeptTree> depts)
////    {
////        List<DeptTree> returnList = new ArrayList<DeptTree>();
////        List<Long> tempList = depts.stream().map(DeptTree::getId).toList();
////        for (DeptTree dept : depts)
////        {
////            // 如果是顶级节点, 遍历该父节点的所有子节点
////            if (!tempList.contains(dept.getParentId()))
////            {
////                recursionFn(depts, dept);
////                returnList.add(dept);
////            }
////        }
////        if (returnList.isEmpty())
////        {
////            returnList = depts;
////        }
////        return returnList;
////    }
//
//    private void recursionFn(List<DeptTree> list, DeptTree t)
//    {
//        // 得到子节点列表
//        List<DeptTree> childList = getChildList(list, t);
//        t.setChildren(childList);
//        for (DeptTree tChild : childList)
//        {
//            if (hasChild(list, tChild))
//            {
//                recursionFn(list, tChild);
//            }
//        }
//    }
//    private List<DeptTree> getChildList(List<DeptTree> list, DeptTree t)
//    {
//        List<DeptTree> tlist = new ArrayList<DeptTree>();
//        Iterator<DeptTree> it = list.iterator();
//        while (it.hasNext())
//        {
//            DeptTree n = (DeptTree) it.next();
//            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue())
//            {
//                tlist.add(n);
//            }
//        }
//        return tlist;
//    }
//
//    /**
//     * 判断是否有子节点
//     */
//    private boolean hasChild(List<DeptTree> list, DeptTree t)
//    {
//        return !getChildList(list, t).isEmpty();
//    }
//
//}
