部门列表接口

This commit is contained in:
zhenxianyimeng 2019-06-22 11:47:56 +08:00
parent b3322bf217
commit 68458fbfed
10 changed files with 204 additions and 17 deletions

View File

@ -1,17 +1,27 @@
package cn.iocoder.mall.admin.application.controller.admins; package cn.iocoder.mall.admin.application.controller.admins;
import cn.iocoder.common.framework.vo.CommonResult; 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.DeptmentService;
import cn.iocoder.mall.admin.api.bo.deptment.DeptmentBO; 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.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 cn.iocoder.mall.admin.sdk.context.AdminSecurityContextHolder;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import java.util.ArrayList;
import org.springframework.web.bind.annotation.RestController; 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; import static cn.iocoder.common.framework.vo.CommonResult.success;
/** /**
@ -29,6 +39,33 @@ public class DeptmentController {
@Autowired @Autowired
private DeptmentService deptmentService; 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") @PostMapping("add")
@ApiOperation(value = "新增部门", notes = "选择部门名称,父级部门") @ApiOperation(value = "新增部门", notes = "选择部门名称,父级部门")
public CommonResult<DeptmentBO> add(@RequestBody DeptmentAddDTO deptmentAddDTO){ public CommonResult<DeptmentBO> add(@RequestBody DeptmentAddDTO deptmentAddDTO){
@ -36,4 +73,7 @@ public class DeptmentController {
AdminSecurityContextHolder.getContext().getAdminId(), deptmentAddDTO)); AdminSecurityContextHolder.getContext().getAdminId(), deptmentAddDTO));
} }
} }

View File

@ -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);
}

View File

@ -1,6 +1,11 @@
package cn.iocoder.mall.admin.application.vo.deptment; package cn.iocoder.mall.admin.application.vo.deptment;
import cn.iocoder.mall.admin.api.bo.deptment.DeptmentBO; 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: * Description:
@ -9,6 +14,9 @@ import cn.iocoder.mall.admin.api.bo.deptment.DeptmentBO;
* @date: 2019-06-15 * @date: 2019-06-15
* @time: 16:57 * @time: 16:57
*/ */
@Data
@ApiModel("部门VO")
public class DeptmentVO extends DeptmentBO { public class DeptmentVO extends DeptmentBO {
@ApiModelProperty("子部门数组")
private List<DeptmentVO> children;
} }

View File

@ -1,7 +1,11 @@
package cn.iocoder.mall.admin.api; 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.bo.deptment.DeptmentBO;
import cn.iocoder.mall.admin.api.dto.depetment.DeptmentAddDTO; import cn.iocoder.mall.admin.api.dto.depetment.DeptmentAddDTO;
import cn.iocoder.mall.admin.api.dto.depetment.DeptmentPageDTO;
import java.util.List;
/** /**
* Description: * Description:
@ -13,4 +17,10 @@ import cn.iocoder.mall.admin.api.dto.depetment.DeptmentAddDTO;
public interface DeptmentService { public interface DeptmentService {
DeptmentBO addDeptment(Integer adminId, DeptmentAddDTO deptmentAddDTO); DeptmentBO addDeptment(Integer adminId, DeptmentAddDTO deptmentAddDTO);
PageResult<DeptmentBO> getPageRootDeptment(DeptmentPageDTO deptmentPageDTO);
List<DeptmentBO> getAllDeptments();
List<DeptmentBO> getAllNotRootDeptment();
} }

View File

@ -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;
}

View File

@ -30,4 +30,6 @@ public class DeptmentAddDTO {
@NotNull(message = "可以为空默认0顶层") @NotNull(message = "可以为空默认0顶层")
@Min(value = 0,message = "父id不能小于0") @Min(value = 0,message = "父id不能小于0")
private Integer pid = 0; private Integer pid = 0;
} }

View File

@ -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;
}

View File

@ -1,12 +1,17 @@
package cn.iocoder.mall.admin.convert; 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.bo.deptment.DeptmentBO;
import cn.iocoder.mall.admin.api.dto.depetment.DeptmentAddDTO; import cn.iocoder.mall.admin.api.dto.depetment.DeptmentAddDTO;
import cn.iocoder.mall.admin.dataobject.DeptmentDO; import cn.iocoder.mall.admin.dataobject.DeptmentDO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings; import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
import java.util.List;
/** /**
* Description: * Description:
* *
@ -25,4 +30,9 @@ public interface DeptmentConvert {
@Mappings({}) @Mappings({})
DeptmentBO convert(DeptmentDO deptmentDO); DeptmentBO convert(DeptmentDO deptmentDO);
@Mappings({@Mapping(source = "records", target = "list")})
PageResult<DeptmentBO> convert(IPage<DeptmentDO> list);
@Mappings({})
List<DeptmentBO> convert(List<DeptmentDO> list);
} }

View File

@ -1,11 +1,17 @@
package cn.iocoder.mall.admin.dao; 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.AdminDO;
import cn.iocoder.mall.admin.dataobject.DeptmentDO; import cn.iocoder.mall.admin.dataobject.DeptmentDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; 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 org.apache.ibatis.annotations.Param;
import java.util.List;
/** /**
* Description: * Description:
* *
@ -23,5 +29,24 @@ public interface DeptmentMapper extends BaseMapper<DeptmentDO> {
); );
} }
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));
}
} }

View File

@ -1,16 +1,22 @@
package cn.iocoder.mall.admin.service; package cn.iocoder.mall.admin.service;
import cn.iocoder.common.framework.util.ServiceExceptionUtil; 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.DeptmentService;
import cn.iocoder.mall.admin.api.bo.deptment.DeptmentBO; import cn.iocoder.mall.admin.api.bo.deptment.DeptmentBO;
import cn.iocoder.mall.admin.api.constant.AdminErrorCodeEnum; 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.DeptmentAddDTO;
import cn.iocoder.mall.admin.api.dto.depetment.DeptmentPageDTO;
import cn.iocoder.mall.admin.convert.DeptmentConvert; import cn.iocoder.mall.admin.convert.DeptmentConvert;
import cn.iocoder.mall.admin.dao.DeptmentMapper; import cn.iocoder.mall.admin.dao.DeptmentMapper;
import cn.iocoder.mall.admin.dataobject.DeptmentDO; import cn.iocoder.mall.admin.dataobject.DeptmentDO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
/** /**
* Description: * Description:
* *
@ -25,17 +31,36 @@ public class DeptmentServiceImpl implements DeptmentService {
private DeptmentMapper deptmentMapper; private DeptmentMapper deptmentMapper;
@Override @Override
public DeptmentBO addDeptment(Integer adminId, DeptmentAddDTO deptmentAddDTO) { public DeptmentBO addDeptment(Integer adminId, DeptmentAddDTO deptmentAddDTO) {
if(deptmentAddDTO.getPid() != 0 && if (deptmentAddDTO.getPid() != 0 &&
deptmentMapper.selectById(deptmentAddDTO.getPid()) == null){ deptmentMapper.selectById(deptmentAddDTO.getPid()) == null) {
throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.DEPT_PARENT_NOT_EXITS.getCode()); 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);
} }
//不同的大部门下好像可以小部门名字一样验证同级别部门名字
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);
}
} }