|
|
|
@ -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<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); |
|
|
|
|
} |
|
|
|
|
//根据name查询数据条数,如果条数大于0,说明数据库中已经存在此部门名称
|
|
|
|
|
int countByName = this.count(new QueryWrapper<DeptEntity>().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<DeptEntity>() |
|
|
|
|
.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<DeptDao, DeptEntity> 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<DeptDao, DeptEntity> 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<DeptDao, DeptEntity> implements |
|
|
|
|
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()); |
|
|
|
|
} |
|
|
|
|
} |