refactor: 部门增删改查接口优化

This commit is contained in:
horizon 2022-09-05 07:41:04 +08:00
parent a4c53439f6
commit f0ba8d48c9
6 changed files with 219 additions and 75 deletions

View File

@ -1,15 +1,19 @@
package com.youlai.admin.controller;
import com.youlai.admin.pojo.entity.SysDept;
import com.youlai.admin.pojo.form.DeptForm;
import com.youlai.admin.pojo.query.DeptQuery;
import com.youlai.admin.pojo.vo.dept.DeptDetailVO;
import com.youlai.admin.pojo.vo.dept.DeptVO;
import com.youlai.admin.service.SysDeptService;
import com.youlai.common.result.Result;
import com.youlai.common.web.domain.Option;
import io.swagger.annotations.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
/**
@ -26,44 +30,53 @@ public class SysDeptController {
private final SysDeptService deptService;
@ApiOperation(value = "部门列表")
@ApiOperation(value = "获取部门列表")
@GetMapping
public Result<List<DeptVO>> listDepts(DeptQuery queryParams) {
List<DeptVO> list = deptService.listDepts(queryParams);
return Result.success(list);
}
@ApiOperation(value = "部门下拉列表")
@GetMapping("/select_list")
public Result lisetDeptOptions() {
@ApiOperation(value = "获取部门下拉选项")
@GetMapping("/options")
public Result<List<Option>> lisetDeptOptions() {
List<Option> list = deptService.lisetDeptOptions();
return Result.success(list);
}
@ApiOperation(value = "部门表单数据")
@GetMapping("/{deptId}/form_data")
public Result getDeptDetail(@ApiParam("部门ID") @PathVariable Long deptId) {
SysDept sysDept = deptService.getById(deptId);
return Result.success(sysDept);
@ApiOperation(value = "获取部门详情")
@GetMapping("/{deptId}")
public Result<DeptDetailVO> getDeptDetail(
@ApiParam("部门ID") @PathVariable Long deptId
) {
DeptDetailVO deptDetail = deptService.getDeptDetail(deptId);
return Result.success(deptDetail);
}
@ApiOperation(value = "新增部门")
@PostMapping
public Result addDept(@RequestBody SysDept dept) {
Long id = deptService.saveDept(dept);
public Result saveDept(
@Valid @RequestBody DeptForm formData
) {
Long id = deptService.saveDept(formData);
return Result.success(id);
}
@ApiOperation(value = "修改部门")
@PutMapping(value = "/{deptId}")
public Result updateDept(@ApiParam("部门ID") @PathVariable Long deptId, @RequestBody SysDept dept) {
deptId = deptService.saveDept(dept);
public Result updateDept(
@PathVariable Long deptId,
@Valid @RequestBody DeptForm formData
) {
deptId = deptService.updateDept(deptId, formData);
return Result.success(deptId);
}
@ApiOperation(value = "删除部门")
@DeleteMapping("/{ids}")
public Result deleteDepartments(@ApiParam("部门ID多个以英文逗号(,)分割") @PathVariable("ids") String ids) {
public Result deleteDepartments(
@ApiParam("部门ID多个以英文逗号(,)分割") @PathVariable("ids") String ids
) {
boolean result = deptService.deleteByIds(ids);
return Result.judge(result);
}

View File

@ -0,0 +1,21 @@
package com.youlai.admin.converter;
import com.youlai.admin.pojo.entity.SysDept;
import com.youlai.admin.pojo.form.DeptForm;
import com.youlai.admin.pojo.vo.dept.DeptDetailVO;
import org.mapstruct.Mapper;
/**
* 部门对象转换器
*
* @author haoxr
* @date 2022/7/29
*/
@Mapper(componentModel = "spring")
public interface DeptConverter {
DeptDetailVO entity2DetailVO(SysDept entity);
SysDept form2Entity(DeptForm deptForm);
}

View File

@ -0,0 +1,27 @@
package com.youlai.admin.pojo.form;
import com.youlai.common.base.BaseEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
@ApiModel("部门表单对象")
@Data
public class DeptForm extends BaseEntity {
@ApiModelProperty("部门名称")
private String name;
@ApiModelProperty("父部门ID")
@NotNull(message = "父部门ID不能为空")
private Long parentId;
@ApiModelProperty("状态")
private Integer status;
@ApiModelProperty("排序")
private Integer sort;
}

View File

@ -0,0 +1,27 @@
package com.youlai.admin.pojo.vo.dept;
import com.youlai.common.base.BaseEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ApiModel("部门详情对象")
@Data
public class DeptDetailVO extends BaseEntity {
@ApiModelProperty("部门ID(编辑必填)")
private Long id;
@ApiModelProperty("部门名称")
private String name;
@ApiModelProperty("父部门ID")
private Long parentId;
@ApiModelProperty("状态")
private Integer status;
@ApiModelProperty("排序")
private Integer sort;
}

View File

@ -2,17 +2,19 @@ package com.youlai.admin.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.youlai.admin.pojo.entity.SysDept;
import com.youlai.admin.pojo.form.DeptForm;
import com.youlai.admin.pojo.query.DeptQuery;
import com.youlai.admin.pojo.vo.dept.DeptDetailVO;
import com.youlai.admin.pojo.vo.dept.DeptVO;
import com.youlai.common.web.domain.Option;
import java.util.List;
/**
* 菜单路由业务接口
* 部门业务接口
*
* @author haoxr
* @date 2021-08-22
* @date 2021/8/22
*/
public interface SysDeptService extends IService<SysDept> {
/**
@ -23,19 +25,28 @@ public interface SysDeptService extends IService<SysDept> {
List<DeptVO> listDepts(DeptQuery queryParams);
/**
* 部门树形下拉TreeSelect层级列表
* 部门树形下拉选项
*
* @return
*/
List<Option> lisetDeptOptions();
/**
* 新增/修改部门
* 新增部门
*
* @param dept
* @param formData
* @return
*/
Long saveDept(SysDept dept);
Long saveDept(DeptForm formData);
/**
* 修改部门
*
* @param deptId
* @param formData
* @return
*/
Long updateDept(Long deptId, DeptForm formData);
/**
* 删除部门
@ -44,4 +55,12 @@ public interface SysDeptService extends IService<SysDept> {
* @return
*/
boolean deleteByIds(String ids);
/**
* 获取部门详情
*
* @param deptId
* @return
*/
DeptDetailVO getDeptDetail(Long deptId);
}

View File

@ -7,20 +7,22 @@ import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.common.constant.SystemConstants;
import com.youlai.admin.converter.DeptConverter;
import com.youlai.admin.mapper.SysDeptMapper;
import com.youlai.admin.pojo.entity.SysDept;
import com.youlai.admin.pojo.form.DeptForm;
import com.youlai.admin.pojo.query.DeptQuery;
import com.youlai.admin.pojo.vo.dept.DeptDetailVO;
import com.youlai.admin.pojo.vo.dept.DeptVO;
import com.youlai.admin.service.SysDeptService;
import com.youlai.common.constant.GlobalConstants;
import com.youlai.common.constant.SystemConstants;
import com.youlai.common.web.domain.Option;
import lombok.RequiredArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
/**
@ -34,10 +36,10 @@ import java.util.stream.Collectors;
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {
private final DeptConverter deptConverter;
/**
* 部门列表
*
* @return
*/
@Override
public List<DeptVO> listDepts(DeptQuery queryParams) {
@ -112,7 +114,7 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
/**
* 部门下拉Select层级列表
* 部门下拉选项
*
* @return
*/
@ -120,12 +122,36 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
public List<Option> lisetDeptOptions() {
List<SysDept> deptList = this.list(new LambdaQueryWrapper<SysDept>()
.eq(SysDept::getStatus, GlobalConstants.STATUS_YES)
.select(SysDept::getId, SysDept::getParentId, SysDept::getName)
.orderByAsc(SysDept::getSort)
);
List<Option> deptSelectList = recursionTreeSelectList(SystemConstants.ROOT_DEPT_ID, deptList);
return deptSelectList;
List<Option> options = recurDeptTreeOptions(SystemConstants.ROOT_DEPT_ID, deptList);
return options;
}
@Override
public Long saveDept(DeptForm formData) {
SysDept entity = deptConverter.form2Entity(formData);
// 部门路径
String treePath = generateDeptTreePath(formData.getParentId());
entity.setTreePath(treePath);
// 保存部门并返回部门ID
this.save(entity);
return entity.getId();
}
@Override
public Long updateDept(Long deptId, DeptForm formData) {
// form->entity
SysDept entity = deptConverter.form2Entity(formData);
entity.setId(deptId);
// 部门路径
String treePath = generateDeptTreePath(formData.getParentId());
entity.setTreePath(treePath);
// 保存部门并返回部门ID
this.updateById(entity);
return entity.getId();
}
/**
* 递归生成部门表格层级列表
@ -134,40 +160,27 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
* @param deptList
* @return
*/
public static List<Option> recursionTreeSelectList(long parentId, List<SysDept> deptList) {
List<Option> deptTreeSelectList = new ArrayList<>();
Optional.ofNullable(deptList).orElse(new ArrayList<>())
.stream()
public static List<Option> recurDeptTreeOptions(long parentId, List<SysDept> deptList) {
if (CollectionUtil.isEmpty(deptList)) {
return Collections.EMPTY_LIST;
}
List<Option> list = deptList.stream()
.filter(dept -> dept.getParentId().equals(parentId))
.forEach(dept -> {
Option Option = new Option(dept.getId(), dept.getName());
List<Option> children = recursionTreeSelectList(dept.getId(), deptList);
.map(dept -> {
Option option = new Option(dept.getId(), dept.getName());
List<Option> children = recurDeptTreeOptions(dept.getId(), deptList);
if (CollectionUtil.isNotEmpty(children)) {
Option.setChildren(children);
option.setChildren(children);
}
deptTreeSelectList.add(Option);
});
return deptTreeSelectList;
return option;
})
.collect(Collectors.toList());
return list;
}
/**
* 保存新增/修改部门
*
* @param dept
* @return
*/
@Override
public Long saveDept(SysDept dept) {
// 生成部门树路径
String treePath = generateDeptTreePath(dept);
dept.setTreePath(treePath);
boolean result = this.saveOrUpdate(dept);
Assert.isTrue(result, "保存部门出错");
return dept.getId();
}
/**
* 删除部门
*
@ -176,33 +189,57 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
*/
@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();
Optional.ofNullable(Arrays.stream(ids.split(",")))
.ifPresent(deptIds -> deptIds.forEach(deptId ->
this.remove(new LambdaQueryWrapper<SysDept>()
.eq(SysDept::getId, deptId)
.or()
.apply("concat (',',tree_path,',') like concat('%,',{0},',%')", deptId))
));
return true;
}
/**
* 获取部门详情
*
* @param deptId
* @return
*/
@Override
public DeptDetailVO getDeptDetail(Long deptId) {
SysDept entity = this.getOne(new LambdaQueryWrapper<SysDept>()
.eq(SysDept::getId, deptId)
.select(
SysDept::getId,
SysDept::getName,
SysDept::getParentId,
SysDept::getStatus,
SysDept::getSort
));
DeptDetailVO detailVO = deptConverter.entity2DetailVO(entity);
return detailVO;
}
/**
* 生成部门路径
* 部门路径生成
*
* @param dept
* @param parentId
* @return
*/
private String generateDeptTreePath(SysDept dept) {
Long parentId = dept.getParentId();
String treePath;
if (parentId.equals(SystemConstants.ROOT_DEPT_ID)) {
treePath = String.valueOf(SystemConstants.ROOT_DEPT_ID);
private String generateDeptTreePath(Long parentId) {
String treePath = null;
if (SystemConstants.ROOT_DEPT_ID.equals(parentId)) {
treePath = parentId + "";
} else {
SysDept parentDept = this.getById(parentId);
treePath = Optional.ofNullable(parentDept).map(item -> item.getTreePath() + "," + item.getId()).orElse(Strings.EMPTY);
SysDept parent = this.getById(parentId);
if (parent != null) {
treePath = parent.getTreePath() + "," + parent.getId();
}
}
return treePath;
}