mirror of
https://gitee.com/youlaitech/youlai-mall.git
synced 2024-12-23 05:00:25 +08:00
refactor(DeptController.java): 部门接口重构优化
This commit is contained in:
parent
16bcb9c87b
commit
1d529afb7c
@ -1,129 +1,80 @@
|
||||
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.vo.DeptVO;
|
||||
import com.youlai.admin.pojo.vo.TreeVO;
|
||||
import com.youlai.admin.service.ISysDeptService;
|
||||
import com.youlai.common.enums.QueryModeEnum;
|
||||
import com.youlai.common.result.Result;
|
||||
import com.youlai.common.result.ResultCode;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
import io.swagger.annotations.ApiImplicitParams;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.logging.log4j.util.Strings;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
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 = "部门接口")
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/depts")
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class DeptController {
|
||||
|
||||
@Autowired
|
||||
private ISysDeptService iSysDeptService;
|
||||
private final ISysDeptService deptService;
|
||||
|
||||
@ApiOperation(value = "列表分页")
|
||||
@ApiOperation(value = "部门表格(Table)层级列表")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "name", value = "部门名称", paramType = "query", dataType = "String"),
|
||||
@ApiImplicitParam(name = "status", value = "部门状态", paramType = "query", dataType = "Long"),
|
||||
@ApiImplicitParam(name = "queryMode", value = "查询模式", paramType = "query", dataType = "QueryModeEnum")
|
||||
})
|
||||
@GetMapping
|
||||
public Result list(String queryMode,
|
||||
Integer status,
|
||||
String name) {
|
||||
@GetMapping("/table")
|
||||
public Result getTableList(Integer status, String name) {
|
||||
List<DeptVO> deptTableList = deptService.listTable(status, name);
|
||||
return Result.success(deptTableList);
|
||||
}
|
||||
|
||||
LambdaQueryWrapper<SysDept> baseQuery = new LambdaQueryWrapper<SysDept>()
|
||||
.orderByAsc(SysDept::getSort)
|
||||
.orderByDesc(SysDept::getGmtModified)
|
||||
.orderByDesc(SysDept::getGmtCreate);
|
||||
QueryModeEnum queryModeEnum = QueryModeEnum.getByCode(queryMode);
|
||||
|
||||
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 = "部门下拉(Select)层级列表")
|
||||
@GetMapping("/select")
|
||||
public Result getSelectList() {
|
||||
List<TreeVO> deptSelectList = deptService.listSelect();
|
||||
return Result.success(deptSelectList);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "部门详情")
|
||||
@ApiImplicitParam(name = "id", value = "部门id", required = true, paramType = "path", dataType = "Long")
|
||||
@GetMapping("/{id}")
|
||||
public Result detail(@PathVariable Integer id) {
|
||||
SysDept sysDept = iSysDeptService.getById(id);
|
||||
public Result detail(@PathVariable Long id) {
|
||||
SysDept sysDept = deptService.getById(id);
|
||||
return Result.success(sysDept);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "新增部门")
|
||||
@ApiImplicitParam(name = "sysDept", value = "实体JSON对象", required = true, paramType = "body", dataType = "SysDept")
|
||||
@PostMapping
|
||||
public Result add(@RequestBody SysDept sysDept) {
|
||||
String treePath = getDeptTreePath(sysDept);
|
||||
sysDept.setTreePath(treePath);
|
||||
boolean status = iSysDeptService.save(sysDept);
|
||||
return Result.judge(status);
|
||||
public Result add(@RequestBody SysDept dept) {
|
||||
Long id = deptService.saveDept(dept);
|
||||
return Result.success(id);
|
||||
}
|
||||
|
||||
@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}")
|
||||
public Result update(
|
||||
@PathVariable Integer id,
|
||||
@RequestBody SysDept sysDept) {
|
||||
|
||||
String treePath = getDeptTreePath(sysDept);
|
||||
sysDept.setTreePath(treePath);
|
||||
boolean status = iSysDeptService.updateById(sysDept);
|
||||
return Result.judge(status);
|
||||
public Result update(@PathVariable Long id, @RequestBody SysDept dept) {
|
||||
dept.setId(id);
|
||||
Long deptId = deptService.saveDept(dept);
|
||||
return Result.success(deptId);
|
||||
}
|
||||
|
||||
@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}")
|
||||
public Result delete(@PathVariable("ids") String ids) {
|
||||
AtomicBoolean result = new AtomicBoolean(true);
|
||||
List<String> idList = Arrays.asList(ids.split(","));
|
||||
// 删除部门以及子部门
|
||||
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;
|
||||
boolean status= deptService.deleteByIds(ids);
|
||||
return Result.judge(status);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,17 +1,47 @@
|
||||
package com.youlai.admin.service;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.youlai.admin.pojo.entity.SysDept;
|
||||
import com.youlai.admin.pojo.vo.DeptVO;
|
||||
import com.youlai.admin.pojo.vo.TreeVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 菜单控制器
|
||||
*
|
||||
* @author <a href="mailto:xianrui0365@163.com">xianrui</a>
|
||||
* @date 2021-08-22
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
@ -9,59 +9,88 @@ import com.youlai.admin.pojo.vo.DeptVO;
|
||||
import com.youlai.admin.mapper.SysDeptMapper;
|
||||
import com.youlai.admin.pojo.vo.TreeVO;
|
||||
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 java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* 部门业务类
|
||||
*
|
||||
* @author <a href="mailto:xianrui0365@163.com">xianrui</a>
|
||||
* @date 2021-08-22
|
||||
*/
|
||||
@Service
|
||||
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
|
||||
public List<TreeVO> listTreeVO(LambdaQueryWrapper<SysDept> baseQuery) {
|
||||
List<SysDept> deptList = this.baseMapper.selectList(baseQuery);
|
||||
List<TreeVO> list = recursionForTreeSelect(SystemConstants.ROOT_DEPT_ID, deptList);
|
||||
return list;
|
||||
public List<DeptVO> listTable(Integer status, String name) {
|
||||
List<SysDept> deptList = this.list(new LambdaQueryWrapper<SysDept>()
|
||||
.orderByAsc(SysDept::getSort));
|
||||
List<DeptVO> deptTableList = recursionTableList(SystemConstants.ROOT_DEPT_ID, deptList);
|
||||
return deptTableList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归生成部门表格数据
|
||||
* 递归生成部门表格层级列表
|
||||
*
|
||||
* @param parentId
|
||||
* @param deptList
|
||||
* @return
|
||||
*/
|
||||
public static List<DeptVO> recursionForTree(Long parentId, List<SysDept> deptList) {
|
||||
List<DeptVO> list = new ArrayList<>();
|
||||
public static List<DeptVO> recursionTableList(Long parentId, List<SysDept> deptList) {
|
||||
List<DeptVO> deptTableList = new ArrayList<>();
|
||||
Optional.ofNullable(deptList).orElse(new ArrayList<>())
|
||||
.stream()
|
||||
.filter(dept -> dept.getParentId().equals(parentId))
|
||||
.forEach(dept -> {
|
||||
DeptVO deptVO = new DeptVO();
|
||||
BeanUtil.copyProperties(dept, deptVO);
|
||||
List<DeptVO> children = recursionForTree(dept.getId(), deptList);
|
||||
List<DeptVO> children = recursionTableList(dept.getId(), deptList);
|
||||
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 deptList
|
||||
* @return
|
||||
*/
|
||||
public static List<TreeVO> recursionForTreeSelect(long parentId, List<SysDept> deptList) {
|
||||
List<TreeVO> list = new ArrayList<>();
|
||||
public static List<TreeVO> recursionSelectList(long parentId, List<SysDept> deptList) {
|
||||
List<TreeVO> deptSelectList = new ArrayList<>();
|
||||
Optional.ofNullable(deptList).orElse(new ArrayList<>())
|
||||
.stream()
|
||||
.filter(dept -> dept.getParentId().equals(parentId))
|
||||
@ -69,11 +98,66 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
||||
TreeVO treeVO = new TreeVO();
|
||||
treeVO.setId(dept.getId());
|
||||
treeVO.setLabel(dept.getName());
|
||||
List<TreeVO> children = recursionForTreeSelect(dept.getId(), deptList);
|
||||
List<TreeVO> children = recursionSelectList(dept.getId(), deptList);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user