|  |  | 
 |  |  | import com.core.common.Cools; | 
 |  |  | import com.core.common.DateUtils; | 
 |  |  | import com.core.common.R; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.web.bind.annotation.*; | 
 |  |  | import com.zy.crm.common.utils.ListUtils; | 
 |  |  | import com.zy.crm.common.utils.TreeUtils; | 
 |  |  | import com.zy.crm.common.web.BaseController; | 
 |  |  | import com.zy.crm.manager.utils.NodeUtils; | 
 |  |  | import com.zy.crm.system.entity.Dept; | 
 |  |  | import com.zy.crm.system.service.DeptService; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.web.bind.annotation.*; | 
 |  |  |  | 
 |  |  | import java.io.IOException; | 
 |  |  | import java.util.*; | 
 |  |  |  | 
 |  |  | @RestController | 
 |  |  | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private DeptService deptService; | 
 |  |  |     @Autowired | 
 |  |  |     private TreeUtils treeUtils; | 
 |  |  |  | 
 |  |  |     @RequestMapping(value = "/dept/{id}/auth") | 
 |  |  |     @ManagerAuth | 
 |  |  | 
 |  |  |         EntityWrapper<Dept> wrapper = new EntityWrapper<>(); | 
 |  |  |         excludeTrash(param); | 
 |  |  |         convert(param, wrapper); | 
 |  |  |         hostEq(wrapper); | 
 |  |  |         if (!Cools.isEmpty(orderByField)){wrapper.orderBy(humpToLine(orderByField), "asc".equals(orderByType));} { | 
 |  |  |             wrapper.orderBy("sort"); | 
 |  |  |         } | 
 |  |  | 
 |  |  |     @RequestMapping(value = "/dept/add/auth") | 
 |  |  |     @ManagerAuth | 
 |  |  |     public R add(Dept dept) { | 
 |  |  |         dept.setLevel(1); | 
 |  |  |         if (dept.getParentId() == null || dept.getParentId() == 0) { | 
 |  |  |             dept.setParentId(getOriginDept().getId()); | 
 |  |  |         } | 
 |  |  |         Dept parent = deptService.selectById(dept.getParentId()); | 
 |  |  |         if (parent != null) { | 
 |  |  |             dept.setParentName(parent.getName()); | 
 |  |  |             dept.setLevel(parent.getLevel() + 1); | 
 |  |  |         } | 
 |  |  |         // path | 
 |  |  |         NodeUtils nodeUtils = new NodeUtils(); | 
 |  |  |         nodeUtils.executePath(dept); | 
 |  |  |         dept.setHostId(getHostId()); | 
 |  |  |         dept.setPath(nodeUtils.path.toString()); | 
 |  |  |         dept.setNamePath(nodeUtils.pathName.toString()); | 
 |  |  |  | 
 |  |  |         dept.setCreateBy(getUserId()); | 
 |  |  |         dept.setCreateTime(new Date()); | 
 |  |  |         dept.setUpdateBy(getUserId()); | 
 |  |  |         dept.setUpdateTime(new Date()); | 
 |  |  |         if (dept.getParentId() == null) { | 
 |  |  |             dept.setParentId(0L); | 
 |  |  |         } | 
 |  |  |         dept.setStatus(1); | 
 |  |  |         deptService.insert(dept); | 
 |  |  |         return R.ok(); | 
 |  |  |     } | 
 |  |  | 
 |  |  |         if (Cools.isEmpty(dept) || null==dept.getId()){ | 
 |  |  |             return R.error(); | 
 |  |  |         } | 
 |  |  |         if (dept.getParentId() != null && dept.getParentId() > 0) { | 
 |  |  |             if (dept.getParentId().equals(dept.getId())) { | 
 |  |  |                 return R.error("数据错误"); | 
 |  |  |             } | 
 |  |  |             Dept parent = deptService.selectById(dept.getParentId()); | 
 |  |  |             if (parent != null) { | 
 |  |  |                 dept.setParentName(parent.getName()); | 
 |  |  |                 dept.setLevel(parent.getLevel() + 1); | 
 |  |  |             } | 
 |  |  |         } else { | 
 |  |  |             dept.setUuid(dept.getName()); | 
 |  |  |             List<Dept> depts = deptService.selectList(new EntityWrapper<Dept>().eq("parent_id",dept.getId())); | 
 |  |  |             for(Dept one : depts){ | 
 |  |  |                 one.setParentName(dept.getName()); | 
 |  |  |                 one.setNamePath(dept.getName()); | 
 |  |  |                 one.setUpdateBy(getUserId()); | 
 |  |  |                 one.setUpdateTime(new Date()); | 
 |  |  | //                deptService.update(one,new EntityWrapper<Dept>().eq("parent_id",dept.getId())); | 
 |  |  |                 deptService.updateById(one); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         // path | 
 |  |  |         NodeUtils nodeUtils = new NodeUtils(); | 
 |  |  |         nodeUtils.executePath(dept); | 
 |  |  |         dept.setPath(nodeUtils.path.toString()); | 
 |  |  |         dept.setNamePath(nodeUtils.pathName.toString()); | 
 |  |  |         dept.setUpdateBy(getUserId()); | 
 |  |  |         dept.setUpdateTime(new Date()); | 
 |  |  |         deptService.updateById(dept); | 
 |  |  |         return R.ok(); | 
 |  |  |     } | 
 |  |  | 
 |  |  |         return R.ok(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @PostMapping(value = "/dept/tree/auth") | 
 |  |  |     @ManagerAuth | 
 |  |  |     public R tree(@RequestParam(required = false, defaultValue = "") String condition) throws IOException, ClassNotFoundException { | 
 |  |  |         ArrayList<Map<String, Object>> tree = treeUtils.getDeptTree(String.valueOf(getDeptId()), getHostId()); | 
 |  |  |         // 深拷贝 | 
 |  |  |         List<Map<String, Object>> result = ListUtils.deepCopy(tree); | 
 |  |  |         if (!Cools.isEmpty(condition)) { | 
 |  |  |             treeUtils.remove(condition, result); | 
 |  |  |             treeUtils.remove(condition, result); | 
 |  |  |         } | 
 |  |  |         return R.ok(result); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @PostMapping(value = "/dept/user/tree/auth") | 
 |  |  |     @ManagerAuth | 
 |  |  |     public R deptUserTree(@RequestParam(required = false, defaultValue = "") String condition) throws IOException, ClassNotFoundException { | 
 |  |  |         ArrayList<Map<String, Object>> tree = treeUtils.getDeptUserTree(String.valueOf(getDeptId()), getHostId(), getUserId()); | 
 |  |  |         // 深拷贝 | 
 |  |  |         List<Map<String, Object>> result = ListUtils.deepCopy(tree); | 
 |  |  |         if (!Cools.isEmpty(condition)) { | 
 |  |  |             treeUtils.remove(condition, result); | 
 |  |  |             treeUtils.remove(condition, result); | 
 |  |  |         } | 
 |  |  |         return R.ok(result); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | } |