ServiceImpl层:
@Override
public List selectList(DeptDictDto exp) {
log.info("【DeptDictService-selectList】,exp【" + JSON.toJSONString(exp) + "】");
//查询设置
QueryWrapper queryWrapper = new QueryWrapper<>();
//添加查询列(默认查全字段)
//queryWrapper.select("");
//添加查询条件
if(exp != null){
//等于
if(exp.getDeptId() != null){
queryWrapper.eq("dept_id", exp.getDeptId());
}
if(exp.getDeptName() != null){
queryWrapper.eq("dept_name", exp.getDeptName());
}
if(exp.getParentId() != null){
queryWrapper.eq("parent_id", exp.getParentId());
}
if(exp.getDeleteFlag() != null){
queryWrapper.eq("delete_flag", exp.getDeleteFlag());
}
if(exp.getUpdateUser() != null){
queryWrapper.eq("update_user", exp.getUpdateUser());
}
if(exp.getUpdateDate() != null){
queryWrapper.eq("update_date", exp.getUpdateDate());
}
//发起查询
List pos = mapper.selectList(queryWrapper);
//返回
List dtos = null;
if(!CollectionUtils.isEmpty(pos)){
dtos = new ArrayList<>();
for(DeptDict po: pos){
DeptDictDto dto = new DeptDictDto();
BeanUtils.copyProperties(po, dto);
dtos.add(dto);
}
}
return dtos;
}
二级部门返回对象:SecondDeptVo.java
@Data
public class SecondDeptVo implements Serializable {
private static final long serialVersionUID=1L;
private String deptId;
private String deptName;
}
出参:QueryAllDeptsResp.java
@Data
public class QueryAllDeptsResp implements Serializable {
private static final long serialVersionUID=1L;
private String deptId;
private String deptName;
private List secondDepts;
}
查询所有部门接口实现:
@PostMapping(value = "/queryAllDepts")
public ResultJson queryAllDepts() {
ResultJson resultJson = new ResultJson();
long time = System.currentTimeMillis();
log.info("【查询所有部门】开始");
//1.业务逻辑处理
try {
List respList = new ArrayList<>();
//1.1查询
DeptDictDto deptDictDto = new DeptDictDto();
deptDictDto.setParentId("abc");//parentId为abc的均为一级部门
//查询一级部门集合
List deptDictDtos = deptDictService.selectList(deptDictDto);
if (!CollectionUtils.isEmpty(deptDictDtos)) {
log.info("返回所有一级部门条数【" + deptDictDtos.size() + "】");
for (DeptDictDto dto : deptDictDtos) {
List secondDepts = new ArrayList<>();
QueryAllDeptsResp queryAllDeptsResp = new QueryAllDeptsResp();
DeptDictDto exp = new DeptDictDto();
exp.setParentId(dto.getDeptId());
log.info("根据一级部门id查询所有一级部门入参parentId【" + dto.getDeptId() + "】");
//查询二级部门集合
List list = deptDictService.selectList(exp);
if (!CollectionUtils.isEmpty(list)) {
log.info("根据一级部门id返回二级部门条数【" + list.size() + "】");
for (DeptDictDto dictDto : list) {
SecondDeptVo secondDeptVo = new SecondDeptVo();
secondDeptVo.setDeptId(dictDto.getDeptId());//二级部门id
secondDeptVo.setDeptName(dictDto.getDeptName());//二级部门name
secondDepts.add(secondDeptVo);
}
}
queryAllDeptsResp.setDeptId(dto.getDeptId());//一级部门id
queryAllDeptsResp.setDeptName(deptDictApiService.getDeptNameById(dto.getDeptId()));//一级部门name
queryAllDeptsResp.setSecondDepts(secondDepts);
respList.add(queryAllDeptsResp);
log.info("查询所有部门返回结果为:【" + JSON.toJSONString(respList) + "】");
}
resultJson = new ResultJson().success(respList);
}
} catch (
Exception e) {
log.error("【查询所有部门】出错,错误详情【" + e.getMessage() + "】", e);
return new ResultJson(ResultEnum.SERVICE_ERR);
}
//1.2 返回
log.info("【查询所有部门】结束,耗时【" + (System.currentTimeMillis() - time) + "】");
return resultJson;
}
接口结果展示:
{
"code": "200",
"msg": "请求成功",
"data": [
{
"deptId": "106600",
"deptName": "专业中心",
"secondDepts": [
{
"deptId": "500_000191",
"deptName": "专业中心"
}
]
},
{
"deptId": "217568",
"deptName": "市场部",
"secondDepts": [
{
"deptId": "500_000197",
"deptName": "市场中心"
}
]
},
{
"deptId": "98036",
"deptName": "总裁办",
"secondDepts": [
{
"deptId": "500_000008",
"deptName": "决策部"
},
{
"deptId": "500_000139",
"deptName": "战略投资部"
},
{
"deptId": "500_000194",
"deptName": "科技项目"
}
]
},
{
"deptId": "98037",
"deptName": "产品中心",
"secondDepts": [
{
"deptId": "500_000052",
"deptName": "产品部"
}
]
},
{
"deptId": "98038",
"deptName": "技术中心",
"secondDepts": [
{
"deptId": "500_000012",
"deptName": "研发中心"
}
]
},
{
"deptId": "98040",
"deptName": "财务部",
"secondDepts": [
{
"deptId": "500_000024",
"deptName": "财务"
}
]
},
{
"deptId": "98041",
"deptName": "人力资源部",
"secondDepts": [
{
"deptId": "500_000028",
"deptName": "组织发展&人力资源部"
}
]
},
{
"deptId": "98043",
"deptName": "公共事务部",
"secondDepts": [
{
"deptId": "500_000187",
"deptName": "创新项目部"
},
{
"deptId": "500_000188",
"deptName": "老项目部"
}
]
}
]
}



