- 查看部门列表的树形结构
public TreeVo deptTreeVo() {
public TreeVo deptTreeVo() {
String parentDeptNo = SuperConstant.ROOT_DEPT_PARENT_ID;
DeptDto param = DeptDto.builder()
.dataState(SuperConstant.DATA_STATE_0)
.parentDeptNo(NoProcessing.processString(parentDeptNo))
.build();
List<Dept> deptList = deptMapper.getList(param);
if(EmptyUtil.isNullOrEmpty(deptList)){
throw new BaseException("部门数据没有定义");
}
Dept rootDept = deptList.stream().filter(d -> SuperConstant.ROOT_DEPT_PARENT_ID.
equals(d.getParentDeptNo())).collect(Collectors.toList()).get(0);
List<TreeItemVo> treeItemVoList = new ArrayList<>();
recursionTreeItem(treeItemVoList,rootDept,deptList);
return TreeVo.builder().items(treeItemVoList).build();
}
private void recursionTreeItem(List<TreeItemVo> treeItemVoList, Dept rootDept, List<Dept> deptList) {
TreeItemVo treeItemVo = TreeItemVo.builder().id(rootDept.getDeptNo()).label(rootDept.getDeptName()).build();
List<Dept> childrenDept = deptList.stream()
.filter(n -> n.getParentDeptNo().equals(rootDept.getDeptNo()))
.collect(Collectors.toList());
if(!EmptyUtil.isNullOrEmpty(childrenDept)){
ArrayList<TreeItemVo> listChildren = Lists.newArrayList();
childrenDept.forEach(dept -> {
this.recursionTreeItem(listChildren,dept,deptList);
});
treeItemVo.setChildren(listChildren);
}
treeItemVoList.add(treeItemVo);
}
<select id="getList" resultType="com.zzyl.vo.DeptVo">
select
d.id, d.parent_dept_no, d.dept_no, d.dept_name, d.sort_no, d.data_state, d.create_time, d.update_time,
d.create_by, d.update_by, d.remark, d.leader_id
, u.real_name as leader_name,DATE_FORMAT(d.create_time,'%Y-%m-%d') as create_day
from sys_dept d
left join sys_user u on u.id = leader_id
<where>
<if test="deptName!=null and deptName!=''">
and d.dept_name like concat('%',#{deptName},'%')
</if>
<if test="parentDeptNo!=null and parentDeptNo!=''">
and d.parent_dept_no like concat(#{parentDeptNo},'%')
</if>
<if test="dataState!=null and dataState!=''">
and d.data_state=#{dataState}
</if>
</where>
order by d.sort_no asc, d.create_time desc
</select>
- 新增部门,需要生成新的部门编号
public void createDept(DeptDto deptDto) {
Dept dept = BeanUtil.toBean(deptDto, Dept.class);
String deptNo = createDeptNo(deptDto.getParentDeptNo());
dept.setDeptNo(deptNo);
int flag = deptMapper.insertDept(dept);
if(flag != 1){
throw new BaseException("保存部门失败");
}
}
private String createDeptNo(String parentDeptNo) {
if(NoProcessing.processString(parentDeptNo).length() /3 == 5){
throw new BaseException("部门创建不能超过4级");
}
DeptDto deptDto = DeptDto.builder().parentDeptNo(parentDeptNo).build();
List<DeptVo> deptVoList = getList(deptDto);
if(EmptyUtil.isNullOrEmpty(deptVoList)){
return NoProcessing.createNo(parentDeptNo,false);
}else {
Long deptNo = deptVoList.stream().map(dept -> {
return Long.valueOf(dept.getDeptNo());
}).max(Comparator.comparing(i -> i)).get();
return NoProcessing.createNo(String.valueOf(deptNo),true);
}
}