refactor(DeptController.java): 部门接口重构优化

This commit is contained in:
有来技术 2021-08-22 21:54:08 +08:00
parent 16bcb9c87b
commit 1d529afb7c
3 changed files with 173 additions and 108 deletions

View File

@ -1,129 +1,80 @@
package com.youlai.admin.controller; package com.youlai.admin.controller;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.youlai.admin.common.constant.SystemConstants;
import com.youlai.admin.pojo.entity.SysDept; import com.youlai.admin.pojo.entity.SysDept;
import com.youlai.admin.pojo.vo.DeptVO; import com.youlai.admin.pojo.vo.DeptVO;
import com.youlai.admin.pojo.vo.TreeVO; import com.youlai.admin.pojo.vo.TreeVO;
import com.youlai.admin.service.ISysDeptService; import com.youlai.admin.service.ISysDeptService;
import com.youlai.common.enums.QueryModeEnum;
import com.youlai.common.result.Result; import com.youlai.common.result.Result;
import com.youlai.common.result.ResultCode;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; import lombok.RequiredArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* 部门控制器
*
* @author <a href="mailto:xianrui0365@163.com">xianrui</a>
* @date 2020-11-06
*/
@Api(tags = "部门接口") @Api(tags = "部门接口")
@RestController @RestController
@RequestMapping("/api/v1/depts") @RequestMapping("/api/v1/depts")
@Slf4j @RequiredArgsConstructor
public class DeptController { public class DeptController {
@Autowired private final ISysDeptService deptService;
private ISysDeptService iSysDeptService;
@ApiOperation(value = "列表分页") @ApiOperation(value = "部门表格Table层级列表")
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = "name", value = "部门名称", paramType = "query", dataType = "String"), @ApiImplicitParam(name = "name", value = "部门名称", paramType = "query", dataType = "String"),
@ApiImplicitParam(name = "status", value = "部门状态", paramType = "query", dataType = "Long"), @ApiImplicitParam(name = "status", value = "部门状态", paramType = "query", dataType = "Long"),
@ApiImplicitParam(name = "queryMode", value = "查询模式", paramType = "query", dataType = "QueryModeEnum")
}) })
@GetMapping @GetMapping("/table")
public Result list(String queryMode, public Result getTableList(Integer status, String name) {
Integer status, List<DeptVO> deptTableList = deptService.listTable(status, name);
String name) { return Result.success(deptTableList);
}
LambdaQueryWrapper<SysDept> baseQuery = new LambdaQueryWrapper<SysDept>() @ApiOperation(value = "部门下拉Select层级列表")
.orderByAsc(SysDept::getSort) @GetMapping("/select")
.orderByDesc(SysDept::getGmtModified) public Result getSelectList() {
.orderByDesc(SysDept::getGmtCreate); List<TreeVO> deptSelectList = deptService.listSelect();
QueryModeEnum queryModeEnum = QueryModeEnum.getByCode(queryMode); return Result.success(deptSelectList);
switch (queryModeEnum) {
case LIST:
baseQuery = baseQuery
.like(StrUtil.isNotBlank(name), SysDept::getName, name)
.eq(status != null, SysDept::getStatus, status);
List<DeptVO> list = iSysDeptService.listDeptVO(baseQuery);
return Result.success(list);
case TREE:
List<TreeVO> treeList = iSysDeptService.listTreeVO(baseQuery);
return Result.success(treeList);
default:
return Result.failed(ResultCode.QUERY_MODE_IS_NULL);
}
} }
@ApiOperation(value = "部门详情") @ApiOperation(value = "部门详情")
@ApiImplicitParam(name = "id", value = "部门id", required = true, paramType = "path", dataType = "Long") @ApiImplicitParam(name = "id", value = "部门id", required = true, paramType = "path", dataType = "Long")
@GetMapping("/{id}") @GetMapping("/{id}")
public Result detail(@PathVariable Integer id) { public Result detail(@PathVariable Long id) {
SysDept sysDept = iSysDeptService.getById(id); SysDept sysDept = deptService.getById(id);
return Result.success(sysDept); return Result.success(sysDept);
} }
@ApiOperation(value = "新增部门") @ApiOperation(value = "新增部门")
@ApiImplicitParam(name = "sysDept", value = "实体JSON对象", required = true, paramType = "body", dataType = "SysDept")
@PostMapping @PostMapping
public Result add(@RequestBody SysDept sysDept) { public Result add(@RequestBody SysDept dept) {
String treePath = getDeptTreePath(sysDept); Long id = deptService.saveDept(dept);
sysDept.setTreePath(treePath); return Result.success(id);
boolean status = iSysDeptService.save(sysDept);
return Result.judge(status);
} }
@ApiOperation(value = "修改部门") @ApiOperation(value = "修改部门")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "部门id", required = true, paramType = "path", dataType = "Long"),
@ApiImplicitParam(name = "sysDept", value = "实体JSON对象", required = true, paramType = "body", dataType = "SysDept")
})
@PutMapping(value = "/{id}") @PutMapping(value = "/{id}")
public Result update( public Result update(@PathVariable Long id, @RequestBody SysDept dept) {
@PathVariable Integer id, dept.setId(id);
@RequestBody SysDept sysDept) { Long deptId = deptService.saveDept(dept);
return Result.success(deptId);
String treePath = getDeptTreePath(sysDept);
sysDept.setTreePath(treePath);
boolean status = iSysDeptService.updateById(sysDept);
return Result.judge(status);
} }
@ApiOperation(value = "删除部门") @ApiOperation(value = "删除部门")
@ApiImplicitParam(name = "ids", value = "id集合", required = true, paramType = "query", dataType = "String") @ApiImplicitParam(name = "ids", value = "部门ID多个以英文逗号,分割拼接", required = true, paramType = "query", dataType = "String")
@DeleteMapping("/{ids}") @DeleteMapping("/{ids}")
public Result delete(@PathVariable("ids") String ids) { public Result delete(@PathVariable("ids") String ids) {
AtomicBoolean result = new AtomicBoolean(true); boolean status= deptService.deleteByIds(ids);
List<String> idList = Arrays.asList(ids.split(",")); return Result.judge(status);
// 删除部门以及子部门
Optional.ofNullable(idList).orElse(new ArrayList<>()).forEach(id ->
result.set(iSysDeptService.remove(new LambdaQueryWrapper<SysDept>().eq(SysDept::getId, id)
.or().apply("concat (',',tree_path,',') like concat('%,',{0},',%')", id)))
);
return Result.judge(result.get());
}
private String getDeptTreePath(SysDept sysDept) {
Long parentId = sysDept.getParentId();
String treePath;
if (parentId.equals(SystemConstants.ROOT_DEPT_ID)) {
treePath = String.valueOf(SystemConstants.ROOT_DEPT_ID);
} else {
SysDept parentDept = iSysDeptService.getById(parentId);
treePath = Optional.ofNullable(parentDept).map(dept -> dept.getTreePath() + "," + dept.getId()).orElse(Strings.EMPTY);
}
return treePath;
} }
} }

View File

@ -1,17 +1,47 @@
package com.youlai.admin.service; package com.youlai.admin.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.youlai.admin.pojo.entity.SysDept; import com.youlai.admin.pojo.entity.SysDept;
import com.youlai.admin.pojo.vo.DeptVO; import com.youlai.admin.pojo.vo.DeptVO;
import com.youlai.admin.pojo.vo.TreeVO; import com.youlai.admin.pojo.vo.TreeVO;
import java.util.List; import java.util.List;
/**
* 菜单控制器
*
* @author <a href="mailto:xianrui0365@163.com">xianrui</a>
* @date 2021-08-22
*/
public interface ISysDeptService extends IService<SysDept> { public interface ISysDeptService extends IService<SysDept> {
/**
* 部门表格Table层级列表
*
* @param status 部门状态 1-开启 0-禁用
* @param name
* @return
*/
List<DeptVO> listTable(Integer status, String name);
List<DeptVO> listDeptVO(LambdaQueryWrapper<SysDept> baseQuery); /**
* 部门下拉Select层级列表
*
* @return
*/
List<TreeVO> listSelect();
List<TreeVO> listTreeVO(LambdaQueryWrapper<SysDept> baseQuery); /**
* 新增/修改部门
*
* @param dept
* @return
*/
Long saveDept(SysDept dept);
/**
* 删除部门
*
* @param ids 部门ID多个以英文逗号,拼接字符串
* @return
*/
boolean deleteByIds(String ids);
} }

View File

@ -9,59 +9,88 @@ import com.youlai.admin.pojo.vo.DeptVO;
import com.youlai.admin.mapper.SysDeptMapper; import com.youlai.admin.mapper.SysDeptMapper;
import com.youlai.admin.pojo.vo.TreeVO; import com.youlai.admin.pojo.vo.TreeVO;
import com.youlai.admin.service.ISysDeptService; import com.youlai.admin.service.ISysDeptService;
import com.youlai.common.constant.GlobalConstants;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* 部门业务类
*
* @author <a href="mailto:xianrui0365@163.com">xianrui</a>
* @date 2021-08-22
*/
@Service @Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements ISysDeptService { public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements ISysDeptService {
@Override
public List<DeptVO> listDeptVO(LambdaQueryWrapper<SysDept> baseQuery) {
List<SysDept> deptList = this.baseMapper.selectList(baseQuery);
List<DeptVO> list = recursionForTree(SystemConstants.ROOT_DEPT_ID, deptList);
return list;
}
/**
* 部门表格Table层级列表
*
* @param name 部门名称
* @return
*/
@Override @Override
public List<TreeVO> listTreeVO(LambdaQueryWrapper<SysDept> baseQuery) { public List<DeptVO> listTable(Integer status, String name) {
List<SysDept> deptList = this.baseMapper.selectList(baseQuery); List<SysDept> deptList = this.list(new LambdaQueryWrapper<SysDept>()
List<TreeVO> list = recursionForTreeSelect(SystemConstants.ROOT_DEPT_ID, deptList); .orderByAsc(SysDept::getSort));
return list; List<DeptVO> deptTableList = recursionTableList(SystemConstants.ROOT_DEPT_ID, deptList);
return deptTableList;
} }
/** /**
* 递归生成部门表格数据 * 递归生成部门表格层级列表
*
* @param parentId * @param parentId
* @param deptList * @param deptList
* @return * @return
*/ */
public static List<DeptVO> recursionForTree(Long parentId, List<SysDept> deptList) { public static List<DeptVO> recursionTableList(Long parentId, List<SysDept> deptList) {
List<DeptVO> list = new ArrayList<>(); List<DeptVO> deptTableList = new ArrayList<>();
Optional.ofNullable(deptList).orElse(new ArrayList<>()) Optional.ofNullable(deptList).orElse(new ArrayList<>())
.stream() .stream()
.filter(dept -> dept.getParentId().equals(parentId)) .filter(dept -> dept.getParentId().equals(parentId))
.forEach(dept -> { .forEach(dept -> {
DeptVO deptVO = new DeptVO(); DeptVO deptVO = new DeptVO();
BeanUtil.copyProperties(dept, deptVO); BeanUtil.copyProperties(dept, deptVO);
List<DeptVO> children = recursionForTree(dept.getId(), deptList); List<DeptVO> children = recursionTableList(dept.getId(), deptList);
deptVO.setChildren(children); deptVO.setChildren(children);
list.add(deptVO); deptTableList.add(deptVO);
}); });
return list; return deptTableList;
} }
/** /**
* 递归生成部门树形下拉数据 * 部门下拉Select层级列表
*
* @return
*/
@Override
public List<TreeVO> listSelect() {
List<SysDept> deptList = this.list(new LambdaQueryWrapper<SysDept>()
.eq(SysDept::getStatus, GlobalConstants.STATUS_YES)
.orderByAsc(SysDept::getSort)
);
List<TreeVO> deptSelectList = recursionSelectList(SystemConstants.ROOT_DEPT_ID, deptList);
return deptSelectList;
}
/**
* 递归生成部门表格层级列表
*
* @param parentId * @param parentId
* @param deptList * @param deptList
* @return * @return
*/ */
public static List<TreeVO> recursionForTreeSelect(long parentId, List<SysDept> deptList) { public static List<TreeVO> recursionSelectList(long parentId, List<SysDept> deptList) {
List<TreeVO> list = new ArrayList<>(); List<TreeVO> deptSelectList = new ArrayList<>();
Optional.ofNullable(deptList).orElse(new ArrayList<>()) Optional.ofNullable(deptList).orElse(new ArrayList<>())
.stream() .stream()
.filter(dept -> dept.getParentId().equals(parentId)) .filter(dept -> dept.getParentId().equals(parentId))
@ -69,11 +98,66 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
TreeVO treeVO = new TreeVO(); TreeVO treeVO = new TreeVO();
treeVO.setId(dept.getId()); treeVO.setId(dept.getId());
treeVO.setLabel(dept.getName()); treeVO.setLabel(dept.getName());
List<TreeVO> children = recursionForTreeSelect(dept.getId(), deptList); List<TreeVO> children = recursionSelectList(dept.getId(), deptList);
treeVO.setChildren(children); treeVO.setChildren(children);
list.add(treeVO); deptSelectList.add(treeVO);
}); });
return list; return deptSelectList;
} }
/**
* 保存新增/修改部门
*
* @param dept
* @return
*/
@Override
public Long saveDept(SysDept dept) {
String treePath = getDeptTreePath(dept);
dept.setTreePath(treePath);
this.saveOrUpdate(dept);
return dept.getId();
}
/**
* 删除部门
*
* @param ids 部门ID多个以英文逗号,拼接字符串
* @return
*/
@Override
public boolean deleteByIds(String ids) {
AtomicBoolean result = new AtomicBoolean(true);
List<String> idList = Arrays.asList(ids.split(","));
// 删除部门及子部门
Optional.ofNullable(idList).orElse(new ArrayList<>()).forEach(id ->
result.set(this.remove(new LambdaQueryWrapper<SysDept>()
.eq(SysDept::getId, id)
.or()
.apply("concat (',',tree_path,',') like concat('%,',{0},',%')", id)))
);
return result.get();
}
/**
* 获取部门级联路径
*
* @param dept
* @return
*/
private String getDeptTreePath(SysDept dept) {
Long parentId = dept.getParentId();
String treePath;
if (parentId.equals(SystemConstants.ROOT_DEPT_ID)) {
treePath = String.valueOf(SystemConstants.ROOT_DEPT_ID);
} else {
SysDept parentDept = this.getById(parentId);
treePath = Optional.ofNullable(parentDept).map(item -> item.getTreePath() + "," + item.getId()).orElse(Strings.EMPTY);
}
return treePath;
}
} }