部门信息树形展示

master
shijie 4 years ago
parent 19ab4f5aa6
commit 59a6836bd4
  1. 11
      dq-financial-api/src/main/java/com/daqing/financial/hrms/DeptControllerApi.java
  2. 2
      dq-financial-crms/src/main/java/com/daqing/financial/crms/controller/CustomerController.java
  3. 24
      dq-financial-hrms/src/main/java/com/daqing/financial/hrms/controller/DeptController.java
  4. 5
      dq-financial-hrms/src/main/java/com/daqing/financial/hrms/dao/DeptDao.java
  5. 9
      dq-financial-hrms/src/main/java/com/daqing/financial/hrms/service/DeptService.java
  6. 53
      dq-financial-hrms/src/main/java/com/daqing/financial/hrms/service/impl/DeptServiceImpl.java
  7. 4
      dq-financial-hrms/src/main/resources/mapper/hrms/DeptDao.xml
  8. 2
      dq-framework-common/src/main/java/com/daqing/framework/exception/CustomException.java
  9. 9
      dq-framework-common/src/main/java/com/daqing/framework/model/response/ResponseResult.java
  10. 9
      dq-framework-model/src/main/java/com/daqing/framework/domain/hrms/DeptEntity.java
  11. 4
      dq-framework-model/src/main/java/com/daqing/framework/domain/hrms/request/DeptAddRequest.java

@ -21,4 +21,15 @@ public interface DeptControllerApi {
*/ */
@ApiOperation(value = "新增一条部门信息", notes = "新增一条部门信息") @ApiOperation(value = "新增一条部门信息", notes = "新增一条部门信息")
ResponseResult save(DeptAddRequest deptAddRequest); ResponseResult save(DeptAddRequest deptAddRequest);
/**
* 部门信息树形结构展示
*/
@ApiOperation(value = "部门信息树形结构展示", notes = "部门信息树形结构展示")
ResponseResult tree();
/**
* 批量修改部门信息
*/
} }

@ -48,7 +48,7 @@ public class CustomerController implements CustomerControllerApi {
public ResponseResult info(@PathVariable("id") Long id) { public ResponseResult info(@PathVariable("id") Long id) {
CustomerEntity customer = customerService.getById(id); CustomerEntity customer = customerService.getById(id);
return ResponseResult.SUCCESS(); return ResponseResult.SUCCESS(customer);
} }
/** /**

@ -6,13 +6,12 @@ import com.daqing.financial.hrms.service.DeptService;
import com.daqing.framework.domain.hrms.DeptEntity; import com.daqing.framework.domain.hrms.DeptEntity;
import com.daqing.framework.domain.hrms.request.DeptAddRequest; import com.daqing.framework.domain.hrms.request.DeptAddRequest;
import com.daqing.framework.model.response.ResponseResult; import com.daqing.framework.model.response.ResponseResult;
import com.daqing.framework.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; 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(); return result ? ResponseResult.SUCCESS() : ResponseResult.FAIL();
} }
@Override
@GetMapping("/tree")
public ResponseResult tree() {
List<DeptEntity> list = deptService.listWithTree();
return ResponseResult.SUCCESS(list);
}
/** /**
* 测试远程调用 * 测试远程调用
*/ */
@ -54,18 +62,6 @@ public class DeptController implements DeptControllerApi {
return crmsFeignService.list(new HashMap<>()); return crmsFeignService.list(new HashMap<>());
} }
/**
* 列表
*/
@GetMapping("/list")
//@RequiresPermissions("hrms:dept:list")
public ResponseResult list(@RequestParam Map<String, Object> params){
PageUtils page = deptService.queryPage(params);
return ResponseResult.SUCCESS();
}
/** /**
* 信息 * 信息
*/ */

@ -1,8 +1,9 @@
package com.daqing.financial.hrms.dao; package com.daqing.financial.hrms.dao;
import com.daqing.framework.domain.hrms.DeptEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; 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.Mapper;
import org.apache.ibatis.annotations.Param;
/** /**
* 记录部门信息 * 记录部门信息
@ -13,5 +14,5 @@ import org.apache.ibatis.annotations.Mapper;
*/ */
@Mapper @Mapper
public interface DeptDao extends BaseMapper<DeptEntity> { public interface DeptDao extends BaseMapper<DeptEntity> {
Integer getMaxSort(@Param("parentId") Long parentId);
} }

@ -1,11 +1,10 @@
package com.daqing.financial.hrms.service; package com.daqing.financial.hrms.service;
import com.baomidou.mybatisplus.extension.service.IService; 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.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<DeptEntity> { public interface DeptService extends IService<DeptEntity> {
PageUtils queryPage(Map<String, Object> params);
boolean saveDept(DeptAddRequest deptAddRequest); boolean saveDept(DeptAddRequest deptAddRequest);
List<DeptEntity> listWithTree();
} }

@ -16,23 +16,16 @@ import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
@Service("deptService") @Service("deptService")
public class DeptServiceImpl extends ServiceImpl<DeptDao, DeptEntity> implements DeptService { public class DeptServiceImpl extends ServiceImpl<DeptDao, DeptEntity> implements DeptService {
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<DeptEntity> page = this.page(
new Query<DeptEntity>().getPage(params),
new QueryWrapper<DeptEntity>()
);
return new PageUtils(page);
}
/** /**
* 保存一条部门信息 * 保存一条部门信息
* *
@ -47,8 +40,17 @@ public class DeptServiceImpl extends ServiceImpl<DeptDao, DeptEntity> implements
//抛出部门名称非法异常 //抛出部门名称非法异常
ExceptionCast.cast(DeptCode.DEPT_NAME_ILLEGAL); ExceptionCast.cast(DeptCode.DEPT_NAME_ILLEGAL);
} }
//根据name查询数据条数,如果条数大于0,说明数据库中已经存在此部门名称 //判断上级部门id是否为空
int countByName = this.count(new QueryWrapper<DeptEntity>().eq("name", deptName)); if (deptAddRequest.getParentId() == null) {
//如果上级部门id为空,赋值为0
deptAddRequest.setParentId(0L);
}
//得到上级部门id
Long parentId = deptAddRequest.getParentId();
//根据name和上级部门id查询数据条数,如果条数大于0,说明数据库中已经存在此部门名称
int countByName = this.count(new QueryWrapper<DeptEntity>()
.eq("name", deptName)
.eq("parent_id", parentId));
if (countByName > 0) { if (countByName > 0) {
//抛出部门名称非法异常 //抛出部门名称非法异常
ExceptionCast.cast(DeptCode.DEPT_NAME_ILLEGAL); ExceptionCast.cast(DeptCode.DEPT_NAME_ILLEGAL);
@ -57,17 +59,13 @@ public class DeptServiceImpl extends ServiceImpl<DeptDao, DeptEntity> implements
DeptEntity deptEntity = new DeptEntity(); DeptEntity deptEntity = new DeptEntity();
//设置id //设置id
deptEntity.setId(SnowflakeIdUtils.getRandomid()); deptEntity.setId(SnowflakeIdUtils.getRandomid());
//属性拷贝,将上级部门id(如果有)、部门名称、排序(如果有)设置到实体类 //属性拷贝,将上级部门id(如果有)、部门名称设置到实体类
BeanUtils.copyProperties(deptAddRequest, deptEntity); BeanUtils.copyProperties(deptAddRequest, deptEntity);
//2、判断部门上级id是否为空,为空则此部门是一级部门,否则查询并计算部门层级,并判断部门层级是否大于5级或小于1级 //2、判断部门上级id是否为空,为空则此部门是一级部门,否则查询并计算部门层级,并判断部门层级是否大于5级或小于1级
//得到上级部门id if (parentId == 0L) {
Long parentId = deptAddRequest.getParentId();
if (parentId == null) {
//说明是添加一级部门 //说明是添加一级部门
//设置层级为1 //设置层级为1
deptEntity.setLevel(1); deptEntity.setLevel(1);
//设置上级部门id为0
deptEntity.setParentId(0L);
} else { } else {
//添加非一级部门 //添加非一级部门
//根据上级部门id查询上级部门信息 //根据上级部门id查询上级部门信息
@ -79,10 +77,17 @@ public class DeptServiceImpl extends ServiceImpl<DeptDao, DeptEntity> implements
//根据上级部门层级计算当前部门层级 //根据上级部门层级计算当前部门层级
int level = byId.getLevel() + 1; int level = byId.getLevel() + 1;
if (level > 5 || level < 1) { if (level > 5 || level < 1) {
//部门层级大于5级或小于1级,抛出异常
ExceptionCast.cast(DeptCode.DEPT_LEVEL_ILLEGAL); ExceptionCast.cast(DeptCode.DEPT_LEVEL_ILLEGAL);
} }
deptEntity.setLevel(level); deptEntity.setLevel(level);
} }
//设置排序
//查询当前数据库当前parent_id下最大排序号
Integer maxSort = this.baseMapper.getMaxSort(parentId);
if (maxSort != null) {
deptEntity.setSort(maxSort + 1);
}
//设置创建时间和修改时间 //设置创建时间和修改时间
deptEntity.setCreateTime(new Date()); deptEntity.setCreateTime(new Date());
deptEntity.setMotifyTime(new Date()); deptEntity.setMotifyTime(new Date());
@ -90,4 +95,16 @@ public class DeptServiceImpl extends ServiceImpl<DeptDao, DeptEntity> implements
return this.save(deptEntity); return this.save(deptEntity);
} }
@Override
public List<DeptEntity> listWithTree() {
return getDeptTreeList(this.list(), 0L);
}
private List<DeptEntity> getDeptTreeList(List<DeptEntity> 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());
}
} }

@ -13,6 +13,8 @@
<result property="createTime" column="create_time"/> <result property="createTime" column="create_time"/>
<result property="motifyTime" column="motify_time"/> <result property="motifyTime" column="motify_time"/>
</resultMap> </resultMap>
<select id="getMaxSort" parameterType="java.lang.Long" resultType="java.lang.Integer">
SELECT MAX(sort) FROM hrms_dept WHERE parent_id = #{parentId}
</select>
</mapper> </mapper>

@ -13,7 +13,7 @@ public class CustomException extends RuntimeException {
public CustomException(ResultCode resultCode) { public CustomException(ResultCode resultCode) {
//异常信息为错误码+异常信息 //异常信息为错误码+异常信息
super("错误码:" + resultCode.code() + "错误信息:" + resultCode.message()); super("错误码:" + resultCode.code() + "错误信息:" + resultCode.message());
this.resultCode = resultCode; this.resultCode = resultCode;
} }

@ -15,7 +15,7 @@ import lombok.ToString;
@ToString @ToString
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public class ResponseResult<T> { public class ResponseResult {
/** /**
* 操作是否成功 * 操作是否成功
*/ */
@ -29,7 +29,7 @@ public class ResponseResult<T> {
/** /**
* 返回的数据 * 返回的数据
*/ */
private T data; private Object data;
/** /**
* 提示信息 * 提示信息
@ -52,7 +52,12 @@ public class ResponseResult<T> {
return new ResponseResult(CommonCode.SUCCESS); return new ResponseResult(CommonCode.SUCCESS);
} }
public static ResponseResult SUCCESS(Object obj) {
return new ResponseResult(true, 10000, obj, "操作成功!");
}
public static ResponseResult FAIL() { public static ResponseResult FAIL() {
return new ResponseResult(CommonCode.FAIL); return new ResponseResult(CommonCode.FAIL);
} }
} }

@ -1,12 +1,15 @@
package com.daqing.framework.domain.hrms; package com.daqing.framework.domain.hrms;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data; import lombok.Data;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date; import java.util.Date;
import java.util.List;
/** /**
* 记录部门信息 * 记录部门信息
@ -50,4 +53,10 @@ public class DeptEntity implements Serializable {
*/ */
private Date motifyTime; private Date motifyTime;
/**
* 子部门
*/
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@TableField(exist = false)
private List<DeptEntity> children;
} }

@ -25,8 +25,4 @@ public class DeptAddRequest implements Serializable {
* 上级部门id * 上级部门id
*/ */
private Long parentId; private Long parentId;
/**
* 排序
*/
private Integer sort;
} }

Loading…
Cancel
Save