From 59a6836bd4ac4111e98010ced2d4d09ba1323553 Mon Sep 17 00:00:00 2001 From: shijie <648688341@qq.com> Date: Thu, 10 Sep 2020 15:11:14 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E9=97=A8=E4=BF=A1=E6=81=AF=E6=A0=91?= =?UTF-8?q?=E5=BD=A2=E5=B1=95=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../financial/hrms/DeptControllerApi.java | 11 ++++ .../crms/controller/CustomerController.java | 2 +- .../hrms/controller/DeptController.java | 24 ++++----- .../daqing/financial/hrms/dao/DeptDao.java | 5 +- .../financial/hrms/service/DeptService.java | 9 ++-- .../hrms/service/impl/DeptServiceImpl.java | 53 ++++++++++++------- .../main/resources/mapper/hrms/DeptDao.xml | 4 +- .../framework/exception/CustomException.java | 2 +- .../model/response/ResponseResult.java | 9 +++- .../framework/domain/hrms/DeptEntity.java | 9 ++++ .../domain/hrms/request/DeptAddRequest.java | 4 -- 11 files changed, 84 insertions(+), 48 deletions(-) diff --git a/dq-financial-api/src/main/java/com/daqing/financial/hrms/DeptControllerApi.java b/dq-financial-api/src/main/java/com/daqing/financial/hrms/DeptControllerApi.java index 764b3d7d..adf43540 100644 --- a/dq-financial-api/src/main/java/com/daqing/financial/hrms/DeptControllerApi.java +++ b/dq-financial-api/src/main/java/com/daqing/financial/hrms/DeptControllerApi.java @@ -21,4 +21,15 @@ public interface DeptControllerApi { */ @ApiOperation(value = "新增一条部门信息", notes = "新增一条部门信息") ResponseResult save(DeptAddRequest deptAddRequest); + + /** + * 部门信息树形结构展示 + */ + @ApiOperation(value = "部门信息树形结构展示", notes = "部门信息树形结构展示") + ResponseResult tree(); + + /** + * 批量修改部门信息 + */ + } diff --git a/dq-financial-crms/src/main/java/com/daqing/financial/crms/controller/CustomerController.java b/dq-financial-crms/src/main/java/com/daqing/financial/crms/controller/CustomerController.java index 9ecfef1c..fdaa784f 100644 --- a/dq-financial-crms/src/main/java/com/daqing/financial/crms/controller/CustomerController.java +++ b/dq-financial-crms/src/main/java/com/daqing/financial/crms/controller/CustomerController.java @@ -48,7 +48,7 @@ public class CustomerController implements CustomerControllerApi { public ResponseResult info(@PathVariable("id") Long id) { CustomerEntity customer = customerService.getById(id); - return ResponseResult.SUCCESS(); + return ResponseResult.SUCCESS(customer); } /** diff --git a/dq-financial-hrms/src/main/java/com/daqing/financial/hrms/controller/DeptController.java b/dq-financial-hrms/src/main/java/com/daqing/financial/hrms/controller/DeptController.java index bb6582b2..15bcb95d 100644 --- a/dq-financial-hrms/src/main/java/com/daqing/financial/hrms/controller/DeptController.java +++ b/dq-financial-hrms/src/main/java/com/daqing/financial/hrms/controller/DeptController.java @@ -6,13 +6,12 @@ import com.daqing.financial.hrms.service.DeptService; import com.daqing.framework.domain.hrms.DeptEntity; import com.daqing.framework.domain.hrms.request.DeptAddRequest; import com.daqing.framework.model.response.ResponseResult; -import com.daqing.framework.utils.PageUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.Arrays; import java.util.HashMap; -import java.util.Map; +import java.util.List; /** * 记录部门信息 @@ -45,6 +44,15 @@ public class DeptController implements DeptControllerApi { return result ? ResponseResult.SUCCESS() : ResponseResult.FAIL(); } + @Override + @GetMapping("/tree") + public ResponseResult tree() { + List list = deptService.listWithTree(); + return ResponseResult.SUCCESS(list); + } + + + /** * 测试远程调用 */ @@ -54,18 +62,6 @@ public class DeptController implements DeptControllerApi { return crmsFeignService.list(new HashMap<>()); } - /** - * 列表 - */ - @GetMapping("/list") - //@RequiresPermissions("hrms:dept:list") - public ResponseResult list(@RequestParam Map params){ - PageUtils page = deptService.queryPage(params); - - return ResponseResult.SUCCESS(); - } - - /** * 信息 */ diff --git a/dq-financial-hrms/src/main/java/com/daqing/financial/hrms/dao/DeptDao.java b/dq-financial-hrms/src/main/java/com/daqing/financial/hrms/dao/DeptDao.java index d76dab25..4eb84c7f 100644 --- a/dq-financial-hrms/src/main/java/com/daqing/financial/hrms/dao/DeptDao.java +++ b/dq-financial-hrms/src/main/java/com/daqing/financial/hrms/dao/DeptDao.java @@ -1,8 +1,9 @@ package com.daqing.financial.hrms.dao; -import com.daqing.framework.domain.hrms.DeptEntity; import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.daqing.framework.domain.hrms.DeptEntity; import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; /** * 记录部门信息 @@ -13,5 +14,5 @@ import org.apache.ibatis.annotations.Mapper; */ @Mapper public interface DeptDao extends BaseMapper { - + Integer getMaxSort(@Param("parentId") Long parentId); } diff --git a/dq-financial-hrms/src/main/java/com/daqing/financial/hrms/service/DeptService.java b/dq-financial-hrms/src/main/java/com/daqing/financial/hrms/service/DeptService.java index 49ea4034..23bc6c80 100644 --- a/dq-financial-hrms/src/main/java/com/daqing/financial/hrms/service/DeptService.java +++ b/dq-financial-hrms/src/main/java/com/daqing/financial/hrms/service/DeptService.java @@ -1,11 +1,10 @@ package com.daqing.financial.hrms.service; import com.baomidou.mybatisplus.extension.service.IService; -import com.daqing.framework.domain.hrms.request.DeptAddRequest; -import com.daqing.framework.utils.PageUtils; import com.daqing.framework.domain.hrms.DeptEntity; +import com.daqing.framework.domain.hrms.request.DeptAddRequest; -import java.util.Map; +import java.util.List; /** * 记录部门信息 @@ -16,8 +15,8 @@ import java.util.Map; */ public interface DeptService extends IService { - PageUtils queryPage(Map params); - boolean saveDept(DeptAddRequest deptAddRequest); + + List listWithTree(); } diff --git a/dq-financial-hrms/src/main/java/com/daqing/financial/hrms/service/impl/DeptServiceImpl.java b/dq-financial-hrms/src/main/java/com/daqing/financial/hrms/service/impl/DeptServiceImpl.java index c75ad899..c622cc91 100644 --- a/dq-financial-hrms/src/main/java/com/daqing/financial/hrms/service/impl/DeptServiceImpl.java +++ b/dq-financial-hrms/src/main/java/com/daqing/financial/hrms/service/impl/DeptServiceImpl.java @@ -16,23 +16,16 @@ import org.apache.commons.lang.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; +import java.util.Comparator; import java.util.Date; +import java.util.List; import java.util.Map; +import java.util.stream.Collectors; @Service("deptService") public class DeptServiceImpl extends ServiceImpl implements DeptService { - @Override - public PageUtils queryPage(Map params) { - IPage page = this.page( - new Query().getPage(params), - new QueryWrapper() - ); - - return new PageUtils(page); - } - /** * 保存一条部门信息 * @@ -47,8 +40,17 @@ public class DeptServiceImpl extends ServiceImpl implements //抛出部门名称非法异常 ExceptionCast.cast(DeptCode.DEPT_NAME_ILLEGAL); } - //根据name查询数据条数,如果条数大于0,说明数据库中已经存在此部门名称 - int countByName = this.count(new QueryWrapper().eq("name", deptName)); + //判断上级部门id是否为空 + if (deptAddRequest.getParentId() == null) { + //如果上级部门id为空,赋值为0 + deptAddRequest.setParentId(0L); + } + //得到上级部门id + Long parentId = deptAddRequest.getParentId(); + //根据name和上级部门id查询数据条数,如果条数大于0,说明数据库中已经存在此部门名称 + int countByName = this.count(new QueryWrapper() + .eq("name", deptName) + .eq("parent_id", parentId)); if (countByName > 0) { //抛出部门名称非法异常 ExceptionCast.cast(DeptCode.DEPT_NAME_ILLEGAL); @@ -57,17 +59,13 @@ public class DeptServiceImpl extends ServiceImpl implements DeptEntity deptEntity = new DeptEntity(); //设置id deptEntity.setId(SnowflakeIdUtils.getRandomid()); - //属性拷贝,将上级部门id(如果有)、部门名称、排序(如果有)设置到实体类 + //属性拷贝,将上级部门id(如果有)、部门名称设置到实体类 BeanUtils.copyProperties(deptAddRequest, deptEntity); //2、判断部门上级id是否为空,为空则此部门是一级部门,否则查询并计算部门层级,并判断部门层级是否大于5级或小于1级 - //得到上级部门id - Long parentId = deptAddRequest.getParentId(); - if (parentId == null) { + if (parentId == 0L) { //说明是添加一级部门 //设置层级为1 deptEntity.setLevel(1); - //设置上级部门id为0 - deptEntity.setParentId(0L); } else { //添加非一级部门 //根据上级部门id查询上级部门信息 @@ -79,10 +77,17 @@ public class DeptServiceImpl extends ServiceImpl implements //根据上级部门层级计算当前部门层级 int level = byId.getLevel() + 1; if (level > 5 || level < 1) { + //部门层级大于5级或小于1级,抛出异常 ExceptionCast.cast(DeptCode.DEPT_LEVEL_ILLEGAL); } deptEntity.setLevel(level); } + //设置排序 + //查询当前数据库当前parent_id下最大排序号 + Integer maxSort = this.baseMapper.getMaxSort(parentId); + if (maxSort != null) { + deptEntity.setSort(maxSort + 1); + } //设置创建时间和修改时间 deptEntity.setCreateTime(new Date()); deptEntity.setMotifyTime(new Date()); @@ -90,4 +95,16 @@ public class DeptServiceImpl extends ServiceImpl implements return this.save(deptEntity); } + @Override + public List listWithTree() { + return getDeptTreeList(this.list(), 0L); + } + + private List getDeptTreeList(List list, Long parentId) { + return list.stream() + .filter(dept -> parentId.equals(dept.getParentId())) + .peek(dept -> dept.setChildren(getDeptTreeList(list, dept.getId()))) + .sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort()))) + .collect(Collectors.toList()); + } } \ No newline at end of file diff --git a/dq-financial-hrms/src/main/resources/mapper/hrms/DeptDao.xml b/dq-financial-hrms/src/main/resources/mapper/hrms/DeptDao.xml index e345cb1f..37ef3832 100644 --- a/dq-financial-hrms/src/main/resources/mapper/hrms/DeptDao.xml +++ b/dq-financial-hrms/src/main/resources/mapper/hrms/DeptDao.xml @@ -13,6 +13,8 @@ - + \ No newline at end of file diff --git a/dq-framework-common/src/main/java/com/daqing/framework/exception/CustomException.java b/dq-framework-common/src/main/java/com/daqing/framework/exception/CustomException.java index 90710156..7f7090dd 100644 --- a/dq-framework-common/src/main/java/com/daqing/framework/exception/CustomException.java +++ b/dq-framework-common/src/main/java/com/daqing/framework/exception/CustomException.java @@ -13,7 +13,7 @@ public class CustomException extends RuntimeException { public CustomException(ResultCode resultCode) { //异常信息为错误码+异常信息 - super("错误码:" + resultCode.code() + "错误信息:" + resultCode.message()); + super("错误码:" + resultCode.code() + ",错误信息:" + resultCode.message()); this.resultCode = resultCode; } diff --git a/dq-framework-common/src/main/java/com/daqing/framework/model/response/ResponseResult.java b/dq-framework-common/src/main/java/com/daqing/framework/model/response/ResponseResult.java index 2f37e4a9..b765068a 100644 --- a/dq-framework-common/src/main/java/com/daqing/framework/model/response/ResponseResult.java +++ b/dq-framework-common/src/main/java/com/daqing/framework/model/response/ResponseResult.java @@ -15,7 +15,7 @@ import lombok.ToString; @ToString @NoArgsConstructor @AllArgsConstructor -public class ResponseResult { +public class ResponseResult { /** * 操作是否成功 */ @@ -29,7 +29,7 @@ public class ResponseResult { /** * 返回的数据 */ - private T data; + private Object data; /** * 提示信息 @@ -52,7 +52,12 @@ public class ResponseResult { return new ResponseResult(CommonCode.SUCCESS); } + public static ResponseResult SUCCESS(Object obj) { + return new ResponseResult(true, 10000, obj, "操作成功!"); + } + public static ResponseResult FAIL() { return new ResponseResult(CommonCode.FAIL); } + } diff --git a/dq-framework-model/src/main/java/com/daqing/framework/domain/hrms/DeptEntity.java b/dq-framework-model/src/main/java/com/daqing/framework/domain/hrms/DeptEntity.java index 628b3bf5..813f5807 100644 --- a/dq-framework-model/src/main/java/com/daqing/framework/domain/hrms/DeptEntity.java +++ b/dq-framework-model/src/main/java/com/daqing/framework/domain/hrms/DeptEntity.java @@ -1,12 +1,15 @@ package com.daqing.framework.domain.hrms; import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; +import com.fasterxml.jackson.annotation.JsonInclude; import lombok.Data; import java.io.Serializable; import java.util.Date; +import java.util.List; /** * 记录部门信息 @@ -50,4 +53,10 @@ public class DeptEntity implements Serializable { */ private Date motifyTime; + /** + * 子部门 + */ + @JsonInclude(JsonInclude.Include.NON_EMPTY) + @TableField(exist = false) + private List children; } diff --git a/dq-framework-model/src/main/java/com/daqing/framework/domain/hrms/request/DeptAddRequest.java b/dq-framework-model/src/main/java/com/daqing/framework/domain/hrms/request/DeptAddRequest.java index 121775f0..08bac371 100644 --- a/dq-framework-model/src/main/java/com/daqing/framework/domain/hrms/request/DeptAddRequest.java +++ b/dq-framework-model/src/main/java/com/daqing/framework/domain/hrms/request/DeptAddRequest.java @@ -25,8 +25,4 @@ public class DeptAddRequest implements Serializable { * 上级部门id */ private Long parentId; - /** - * 排序 - */ - private Integer sort; }