From 68458fbfed0299d8029d3865ee1b268e286f39d9 Mon Sep 17 00:00:00 2001 From: zhenxianyimeng <1920405993@qq.com> Date: Sat, 22 Jun 2019 11:47:56 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E9=97=A8=E5=88=97=E8=A1=A8=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/admins/DeptmentController.java | 48 ++++++++++++++++-- .../application/convert/DeptmentConvert.java | 32 ++++++++++++ .../application/vo/deptment/DeptmentVO.java | 10 +++- .../mall/admin/api/DeptmentService.java | 10 ++++ .../admin/api/constant/DeptmentConstants.java | 16 ++++++ .../api/dto/depetment/DeptmentAddDTO.java | 2 + .../api/dto/depetment/DeptmentPageDTO.java | 19 +++++++ .../mall/admin/convert/DeptmentConvert.java | 10 ++++ .../mall/admin/dao/DeptmentMapper.java | 25 ++++++++++ .../admin/service/DeptmentServiceImpl.java | 49 ++++++++++++++----- 10 files changed, 204 insertions(+), 17 deletions(-) create mode 100644 system/system-application/src/main/java/cn/iocoder/mall/admin/application/convert/DeptmentConvert.java create mode 100644 system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/constant/DeptmentConstants.java create mode 100644 system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/dto/depetment/DeptmentPageDTO.java diff --git a/system/system-application/src/main/java/cn/iocoder/mall/admin/application/controller/admins/DeptmentController.java b/system/system-application/src/main/java/cn/iocoder/mall/admin/application/controller/admins/DeptmentController.java index 8a77b27cd..b18e32cdc 100644 --- a/system/system-application/src/main/java/cn/iocoder/mall/admin/application/controller/admins/DeptmentController.java +++ b/system/system-application/src/main/java/cn/iocoder/mall/admin/application/controller/admins/DeptmentController.java @@ -1,17 +1,27 @@ package cn.iocoder.mall.admin.application.controller.admins; import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.common.framework.vo.PageResult; import cn.iocoder.mall.admin.api.DeptmentService; import cn.iocoder.mall.admin.api.bo.deptment.DeptmentBO; +import cn.iocoder.mall.admin.api.constant.ResourceConstants; import cn.iocoder.mall.admin.api.dto.depetment.DeptmentAddDTO; +import cn.iocoder.mall.admin.api.dto.depetment.DeptmentPageDTO; +import cn.iocoder.mall.admin.application.convert.DeptmentConvert; +import cn.iocoder.mall.admin.application.vo.deptment.DeptmentVO; +import cn.iocoder.mall.admin.application.vo.resource.ResourceTreeNodeVO; import cn.iocoder.mall.admin.sdk.context.AdminSecurityContextHolder; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + import static cn.iocoder.common.framework.vo.CommonResult.success; /** @@ -29,6 +39,33 @@ public class DeptmentController { @Autowired private DeptmentService deptmentService; + @GetMapping("tree/page") + @ApiOperation(value = "根部门分页的部门树") + public CommonResult> treePage(DeptmentPageDTO deptmentPageDTO){ + PageResult pageResult = deptmentService.getPageRootDeptment(deptmentPageDTO); + PageResult voPageResult = DeptmentConvert.INSTANCE.convert(pageResult); + List list = deptmentService.getAllDeptments(); + List voList = DeptmentConvert.INSTANCE.convert(list); + Map nodeMap = voList.stream().collect(Collectors.toMap(e->e.getId(), e->e)); + + nodeMap.values().stream() + .filter(node -> !node.getPid().equals(ResourceConstants.PID_ROOT)) + .forEach((childNode) -> { + // 获得父节点 + DeptmentVO parentNode = nodeMap.get(childNode.getPid()); + if (parentNode.getChildren() == null) { // 初始化 children 数组 + parentNode.setChildren(new ArrayList<>()); + } + // 将自己添加到父节点中 + parentNode.getChildren().add(childNode); + }); + + voPageResult.getList().forEach(d->{ + d.setChildren(nodeMap.get(d.getId()).getChildren()); + }); + return success(voPageResult); + } + @PostMapping("add") @ApiOperation(value = "新增部门", notes = "选择部门名称,父级部门") public CommonResult add(@RequestBody DeptmentAddDTO deptmentAddDTO){ @@ -36,4 +73,7 @@ public class DeptmentController { AdminSecurityContextHolder.getContext().getAdminId(), deptmentAddDTO)); } + + + } diff --git a/system/system-application/src/main/java/cn/iocoder/mall/admin/application/convert/DeptmentConvert.java b/system/system-application/src/main/java/cn/iocoder/mall/admin/application/convert/DeptmentConvert.java new file mode 100644 index 000000000..61f0caeb3 --- /dev/null +++ b/system/system-application/src/main/java/cn/iocoder/mall/admin/application/convert/DeptmentConvert.java @@ -0,0 +1,32 @@ +package cn.iocoder.mall.admin.application.convert; + +import cn.iocoder.common.framework.vo.PageResult; +import cn.iocoder.mall.admin.api.bo.deptment.DeptmentBO; +import cn.iocoder.mall.admin.application.vo.deptment.DeptmentVO; +import cn.iocoder.mall.admin.dataobject.DeptmentDO; +import com.baomidou.mybatisplus.core.metadata.IPage; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.Mappings; +import org.mapstruct.factory.Mappers; + +import java.util.List; + +/** + * Description: + * + * @author: zhenxianyimeng + * @date: 2019-06-22 + * @time: 00:23 + */ +@Mapper +public interface DeptmentConvert { + + DeptmentConvert INSTANCE = Mappers.getMapper(DeptmentConvert.class); + + @Mappings({@Mapping(source = "list", target = "list")}) + PageResult convert(PageResult pageResult); + + @Mappings({}) + List convert(List list); +} diff --git a/system/system-application/src/main/java/cn/iocoder/mall/admin/application/vo/deptment/DeptmentVO.java b/system/system-application/src/main/java/cn/iocoder/mall/admin/application/vo/deptment/DeptmentVO.java index 929d5e153..ebf99bda9 100644 --- a/system/system-application/src/main/java/cn/iocoder/mall/admin/application/vo/deptment/DeptmentVO.java +++ b/system/system-application/src/main/java/cn/iocoder/mall/admin/application/vo/deptment/DeptmentVO.java @@ -1,6 +1,11 @@ package cn.iocoder.mall.admin.application.vo.deptment; import cn.iocoder.mall.admin.api.bo.deptment.DeptmentBO; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import java.util.List; /** * Description: @@ -9,6 +14,9 @@ import cn.iocoder.mall.admin.api.bo.deptment.DeptmentBO; * @date: 2019-06-15 * @time: 16:57 */ +@Data +@ApiModel("部门VO") public class DeptmentVO extends DeptmentBO { - + @ApiModelProperty("子部门数组") + private List children; } diff --git a/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/DeptmentService.java b/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/DeptmentService.java index e6b9f53e2..d1ab4ef80 100644 --- a/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/DeptmentService.java +++ b/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/DeptmentService.java @@ -1,7 +1,11 @@ package cn.iocoder.mall.admin.api; +import cn.iocoder.common.framework.vo.PageResult; import cn.iocoder.mall.admin.api.bo.deptment.DeptmentBO; import cn.iocoder.mall.admin.api.dto.depetment.DeptmentAddDTO; +import cn.iocoder.mall.admin.api.dto.depetment.DeptmentPageDTO; + +import java.util.List; /** * Description: @@ -13,4 +17,10 @@ import cn.iocoder.mall.admin.api.dto.depetment.DeptmentAddDTO; public interface DeptmentService { DeptmentBO addDeptment(Integer adminId, DeptmentAddDTO deptmentAddDTO); + + PageResult getPageRootDeptment(DeptmentPageDTO deptmentPageDTO); + + List getAllDeptments(); + + List getAllNotRootDeptment(); } diff --git a/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/constant/DeptmentConstants.java b/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/constant/DeptmentConstants.java new file mode 100644 index 000000000..14a939885 --- /dev/null +++ b/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/constant/DeptmentConstants.java @@ -0,0 +1,16 @@ +package cn.iocoder.mall.admin.api.constant; + +/** + * Description: + * + * @author: zhenxianyimeng + * @date: 2019-06-16 + * @time: 23:15 + */ +public interface DeptmentConstants { + + /** + * 顶级部门的pid + */ + Integer PID_ROOT = 0; +} diff --git a/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/dto/depetment/DeptmentAddDTO.java b/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/dto/depetment/DeptmentAddDTO.java index f440aa140..ba6d9a2c6 100644 --- a/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/dto/depetment/DeptmentAddDTO.java +++ b/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/dto/depetment/DeptmentAddDTO.java @@ -30,4 +30,6 @@ public class DeptmentAddDTO { @NotNull(message = "可以为空,默认0,顶层") @Min(value = 0,message = "父id不能小于0") private Integer pid = 0; + + } diff --git a/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/dto/depetment/DeptmentPageDTO.java b/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/dto/depetment/DeptmentPageDTO.java new file mode 100644 index 000000000..cb12e1549 --- /dev/null +++ b/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/dto/depetment/DeptmentPageDTO.java @@ -0,0 +1,19 @@ +package cn.iocoder.mall.admin.api.dto.depetment; + +import cn.iocoder.common.framework.vo.PageParam; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + + +/** + * Description: + * + * @author: zhenxianyimeng + * @date: 2019-06-21 + * @time: 00:22 + */ +@Data +public class DeptmentPageDTO extends PageParam { + @ApiModelProperty(value = "根部门名字", example = "研发部") + private String name; +} diff --git a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/convert/DeptmentConvert.java b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/convert/DeptmentConvert.java index ac3cfd31c..885f53317 100644 --- a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/convert/DeptmentConvert.java +++ b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/convert/DeptmentConvert.java @@ -1,12 +1,17 @@ package cn.iocoder.mall.admin.convert; +import cn.iocoder.common.framework.vo.PageResult; import cn.iocoder.mall.admin.api.bo.deptment.DeptmentBO; import cn.iocoder.mall.admin.api.dto.depetment.DeptmentAddDTO; import cn.iocoder.mall.admin.dataobject.DeptmentDO; +import com.baomidou.mybatisplus.core.metadata.IPage; import org.mapstruct.Mapper; +import org.mapstruct.Mapping; import org.mapstruct.Mappings; import org.mapstruct.factory.Mappers; +import java.util.List; + /** * Description: * @@ -25,4 +30,9 @@ public interface DeptmentConvert { @Mappings({}) DeptmentBO convert(DeptmentDO deptmentDO); + @Mappings({@Mapping(source = "records", target = "list")}) + PageResult convert(IPage list); + + @Mappings({}) + List convert(List list); } diff --git a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/dao/DeptmentMapper.java b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/dao/DeptmentMapper.java index 99b6fa76c..040ab6175 100644 --- a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/dao/DeptmentMapper.java +++ b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/dao/DeptmentMapper.java @@ -1,11 +1,17 @@ package cn.iocoder.mall.admin.dao; +import cn.iocoder.common.framework.mybatis.QueryWrapperX; +import cn.iocoder.mall.admin.api.dto.depetment.DeptmentPageDTO; import cn.iocoder.mall.admin.dataobject.AdminDO; import cn.iocoder.mall.admin.dataobject.DeptmentDO; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.apache.ibatis.annotations.Param; +import java.util.List; + /** * Description: * @@ -23,5 +29,24 @@ public interface DeptmentMapper extends BaseMapper { ); } + default IPage selectDeptPage(DeptmentPageDTO deptmentPageDTO, Integer pid){ + return selectPage(new Page<>(deptmentPageDTO.getPageNo(), deptmentPageDTO.getPageSize()), + new QueryWrapperX() + .likeIfPresent("name", deptmentPageDTO.getName()) + .eqIfPresent("pid", pid) + .eq("deleted", false)); + } + + default List getDeptByPid(Integer pid){ + return selectList(new QueryWrapperX() + .eqIfPresent("pid", pid) + .eq("deleted", false)); + } + + default List getDeptExcudePid(Integer pid){ + return selectList(new QueryWrapper() + .ne("pid",pid) + .eq("deleted",false)); + } } diff --git a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/service/DeptmentServiceImpl.java b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/service/DeptmentServiceImpl.java index 067a83446..b56da932a 100644 --- a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/service/DeptmentServiceImpl.java +++ b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/service/DeptmentServiceImpl.java @@ -1,16 +1,22 @@ package cn.iocoder.mall.admin.service; import cn.iocoder.common.framework.util.ServiceExceptionUtil; +import cn.iocoder.common.framework.vo.PageResult; import cn.iocoder.mall.admin.api.DeptmentService; import cn.iocoder.mall.admin.api.bo.deptment.DeptmentBO; import cn.iocoder.mall.admin.api.constant.AdminErrorCodeEnum; +import cn.iocoder.mall.admin.api.constant.DeptmentConstants; import cn.iocoder.mall.admin.api.dto.depetment.DeptmentAddDTO; +import cn.iocoder.mall.admin.api.dto.depetment.DeptmentPageDTO; import cn.iocoder.mall.admin.convert.DeptmentConvert; import cn.iocoder.mall.admin.dao.DeptmentMapper; import cn.iocoder.mall.admin.dataobject.DeptmentDO; +import com.baomidou.mybatisplus.core.metadata.IPage; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.List; + /** * Description: * @@ -25,17 +31,36 @@ public class DeptmentServiceImpl implements DeptmentService { private DeptmentMapper deptmentMapper; @Override - public DeptmentBO addDeptment(Integer adminId, DeptmentAddDTO deptmentAddDTO) { - if(deptmentAddDTO.getPid() != 0 && - deptmentMapper.selectById(deptmentAddDTO.getPid()) == null){ - throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.DEPT_PARENT_NOT_EXITS.getCode()); - } - //不同的大部门下好像可以小部门名字一样,验证同级别部门名字 - if (null != deptmentMapper.findDeptByNameAndPid(deptmentAddDTO.getName(), deptmentAddDTO.getPid())) { - throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.DEPT_SAME_LEVEL_NAME_EXITS.getCode()); - } - DeptmentDO deptmentDO = DeptmentConvert.INSTANCE.convert(deptmentAddDTO); - deptmentMapper.insert(deptmentDO); - return DeptmentConvert.INSTANCE.convert(deptmentDO); + public DeptmentBO addDeptment(Integer adminId, DeptmentAddDTO deptmentAddDTO) { + if (deptmentAddDTO.getPid() != 0 && + deptmentMapper.selectById(deptmentAddDTO.getPid()) == null) { + throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.DEPT_PARENT_NOT_EXITS.getCode()); } + //不同的大部门下好像可以小部门名字一样,验证同级别部门名字 + if (null != deptmentMapper.findDeptByNameAndPid(deptmentAddDTO.getName(), deptmentAddDTO.getPid())) { + throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.DEPT_SAME_LEVEL_NAME_EXITS.getCode()); + } + DeptmentDO deptmentDO = DeptmentConvert.INSTANCE.convert(deptmentAddDTO); + deptmentMapper.insert(deptmentDO); + return DeptmentConvert.INSTANCE.convert(deptmentDO); + } + + @Override + public PageResult getPageRootDeptment(DeptmentPageDTO deptmentPageDTO) { + IPage page = deptmentMapper.selectDeptPage(deptmentPageDTO, DeptmentConstants.PID_ROOT); + return DeptmentConvert.INSTANCE.convert(page); + } + + @Override + public List getAllDeptments() { + List list = deptmentMapper.getDeptByPid(null); + return DeptmentConvert.INSTANCE.convert(list); + } + + @Override + public List getAllNotRootDeptment() { + List list = deptmentMapper.getDeptExcudePid(DeptmentConstants.PID_ROOT); + return DeptmentConvert.INSTANCE.convert(list); + } + }