Skip to content

Commit

Permalink
部门列表接口
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenxianyimeng committed Jun 22, 2019
1 parent b3322bf commit 68458fb
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -29,11 +39,41 @@ public class DeptmentController {
@Autowired
private DeptmentService deptmentService;

@GetMapping("tree/page")
@ApiOperation(value = "根部门分页的部门树")
public CommonResult<PageResult<DeptmentVO>> treePage(DeptmentPageDTO deptmentPageDTO){
PageResult<DeptmentBO> pageResult = deptmentService.getPageRootDeptment(deptmentPageDTO);
PageResult<DeptmentVO> voPageResult = DeptmentConvert.INSTANCE.convert(pageResult);
List<DeptmentBO> list = deptmentService.getAllDeptments();
List<DeptmentVO> voList = DeptmentConvert.INSTANCE.convert(list);
Map<Integer, DeptmentVO> 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<DeptmentBO> add(@RequestBody DeptmentAddDTO deptmentAddDTO){
return success(deptmentService.addDeptment(
AdminSecurityContextHolder.getContext().getAdminId(), deptmentAddDTO));

}



}
Original file line number Diff line number Diff line change
@@ -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<DeptmentVO> convert(PageResult<DeptmentBO> pageResult);

@Mappings({})
List<DeptmentVO> convert(List<DeptmentBO> list);
}
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -9,6 +14,9 @@
* @date: 2019-06-15
* @time: 16:57
*/
@Data
@ApiModel("部门VO")
public class DeptmentVO extends DeptmentBO {

@ApiModelProperty("子部门数组")
private List<DeptmentVO> children;
}
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -13,4 +17,10 @@
public interface DeptmentService {

DeptmentBO addDeptment(Integer adminId, DeptmentAddDTO deptmentAddDTO);

PageResult<DeptmentBO> getPageRootDeptment(DeptmentPageDTO deptmentPageDTO);

List<DeptmentBO> getAllDeptments();

List<DeptmentBO> getAllNotRootDeptment();
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ public class DeptmentAddDTO {
@NotNull(message = "可以为空,默认0,顶层")
@Min(value = 0,message = "父id不能小于0")
private Integer pid = 0;


}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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:
*
Expand All @@ -25,4 +30,9 @@ public interface DeptmentConvert {
@Mappings({})
DeptmentBO convert(DeptmentDO deptmentDO);

@Mappings({@Mapping(source = "records", target = "list")})
PageResult<DeptmentBO> convert(IPage<DeptmentDO> list);

@Mappings({})
List<DeptmentBO> convert(List<DeptmentDO> list);
}
Original file line number Diff line number Diff line change
@@ -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:
*
Expand All @@ -23,5 +29,24 @@ default DeptmentDO findDeptByNameAndPid(String name, Integer pid){
);
}

default IPage<DeptmentDO> selectDeptPage(DeptmentPageDTO deptmentPageDTO, Integer pid){
return selectPage(new Page<>(deptmentPageDTO.getPageNo(), deptmentPageDTO.getPageSize()),
new QueryWrapperX<DeptmentDO>()
.likeIfPresent("name", deptmentPageDTO.getName())
.eqIfPresent("pid", pid)
.eq("deleted", false));

}

default List<DeptmentDO> getDeptByPid(Integer pid){
return selectList(new QueryWrapperX<DeptmentDO>()
.eqIfPresent("pid", pid)
.eq("deleted", false));
}

default List<DeptmentDO> getDeptExcudePid(Integer pid){
return selectList(new QueryWrapper<DeptmentDO>()
.ne("pid",pid)
.eq("deleted",false));
}
}
Original file line number Diff line number Diff line change
@@ -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:
*
Expand All @@ -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<DeptmentBO> getPageRootDeptment(DeptmentPageDTO deptmentPageDTO) {
IPage<DeptmentDO> page = deptmentMapper.selectDeptPage(deptmentPageDTO, DeptmentConstants.PID_ROOT);
return DeptmentConvert.INSTANCE.convert(page);
}

@Override
public List<DeptmentBO> getAllDeptments() {
List<DeptmentDO> list = deptmentMapper.getDeptByPid(null);
return DeptmentConvert.INSTANCE.convert(list);
}

@Override
public List<DeptmentBO> getAllNotRootDeptment() {
List<DeptmentDO> list = deptmentMapper.getDeptExcudePid(DeptmentConstants.PID_ROOT);
return DeptmentConvert.INSTANCE.convert(list);
}

}

0 comments on commit 68458fb

Please sign in to comment.