本文是基于层级设计以及算法,treetable的实现来说的。
新增:新增通过父节点计算自己是第几个孩子,上篇文章已有描述。
@RequiresPermissions(value = { "dep.save" })
@RequestMapping(value="/save")
@ResponseBody
public String save(Department department,HttpSession session){
Integer uid = (Integer)session.getAttribute("uid");
department.setCreater(uid);
department.setCreateTime(new Date());
department.setDelFlag("0");
int cnt = departmentService.countByPid(department.getPid() == null ? 0 : department.getPid());
Department pdep = departmentService.single(department.getPid()== null ? 0 : department.getPid());
String pseq = pdep == null ? "1" : pdep.getSeq();
department.setSeq(pseq + "." + convertCount(cnt));
departmentService.save(department);
return "SUCCESS";
}
修改:修改可以更改部门的从属关系,这里比较复杂,利用递归算法,每个层级都修改其父节点集和排序号。
@RequiresPermissions(value = { "dep.update" })
@RequestMapping(value="/update", produces="text/plain; charset=UTF-8")
@ResponseBody
public String update(Department department, Integer oldPid, HttpSession session){
if(department.getId().equals(department.getPid()))
return "不可以设置自己为自己的父级";
if(department.getPids().contains(String.valueOf(department.getId())))
return "不能将上级部门设置为本部门或本部门下的子部门";
Integer uid = (Integer)session.getAttribute("