You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
153 lines
4.9 KiB
153 lines
4.9 KiB
//定义包名
|
|
package com.yanzhen.controller;
|
|
|
|
import com.github.pagehelper.PageInfo; // 导入分页插件
|
|
import com.yanzhen.entity.Org; // 导入组织实体类
|
|
import com.yanzhen.service.OrgService; // 导入组织服务接口
|
|
import com.yanzhen.utils.Result; // 导入结果工具类
|
|
import org.springframework.beans.factory.annotation.Autowired; // 导入自动装配注解
|
|
import org.springframework.web.bind.annotation.*; // 导入Spring MVC相关注解
|
|
|
|
import java.util.ArrayList; // 导入ArrayList类
|
|
import java.util.HashMap; // 导入HashMap类
|
|
import java.util.List; // 导入List接口
|
|
import java.util.Map; // 导入Map接口
|
|
//组织管理的控制器
|
|
@RestController
|
|
@RequestMapping("/org")
|
|
public class OrgController {
|
|
//注入实例
|
|
//注入组织服务类
|
|
@Autowired
|
|
private OrgService orgService;
|
|
//获取组织信息存入tree
|
|
@GetMapping("tree")
|
|
public Result tree(){
|
|
//查询所有组织信息
|
|
PageInfo<Org> pageInfo = orgService.query(null);
|
|
//所有的树形信息
|
|
List<Org> list = pageInfo.getList();
|
|
//要构建的树形结构
|
|
List<Map<String,Object>> trees = new ArrayList<>();
|
|
//嵌套查询组织信息
|
|
for (Org entity : list) {
|
|
//判断是否为顶级节点
|
|
if(entity.getParentId() == 0){
|
|
//创建Map对象
|
|
Map<String,Object> map = new HashMap<>();
|
|
//组织ID存入Map
|
|
map.put("id",entity.getId());
|
|
//组织名称存入Map
|
|
map.put("name",entity.getName());
|
|
//判断是否有子节点
|
|
if(entity.getType()<4){
|
|
//标记为父节点
|
|
map.put("isParent",true);
|
|
map.put("open",true);
|
|
//递归获取子节点信息存入Map
|
|
map.put("children",getChild(entity,list));
|
|
}else{
|
|
//标记为叶子节点
|
|
map.put("isParent",false);
|
|
}
|
|
//将Map加到树形结构中
|
|
trees.add(map);
|
|
}
|
|
}
|
|
//返回结果
|
|
return Result.ok(trees);
|
|
}
|
|
|
|
//自己调自己、有出口
|
|
public List<Map<String,Object>> getChild(Org parent,List<Org> list){
|
|
//初始化
|
|
List<Map<String,Object>> child = new ArrayList<>();
|
|
//遍历查询组织信息
|
|
for (Org org : list) {
|
|
//判断父节点信息
|
|
if(parent.getId() == org.getParentId()){
|
|
//创建Map对象
|
|
Map<String,Object> map = new HashMap<>();
|
|
//组织ID存入Map
|
|
map.put("id",org.getId());
|
|
//将组织名称放入Map
|
|
map.put("name",org.getName());
|
|
//判断是否有子节点
|
|
if(org.getType()<4){
|
|
//标记为父节点
|
|
map.put("isParent",true);
|
|
//获取子节点存入Map
|
|
map.put("children",getChild(org,list));
|
|
}else{
|
|
//标记为叶子节点
|
|
map.put("isParent",false);
|
|
}
|
|
//Map加到子节点中
|
|
child.add(map);
|
|
}
|
|
}
|
|
//返回结果
|
|
return child;
|
|
}
|
|
//创建新的实例
|
|
@PostMapping("create")
|
|
public Result create(@RequestBody Org org){
|
|
//未设置则为一级栏目
|
|
//判断是否为顶节点
|
|
if(org.getParentId() == null){
|
|
org.setParentId(0);
|
|
}
|
|
//在组织管理服务类中引用create方法
|
|
int flag = orgService.create(org);
|
|
//判断是否创建成功
|
|
if(flag>0){
|
|
//成功
|
|
return Result.ok();
|
|
}else{
|
|
//失败
|
|
return Result.fail();
|
|
}
|
|
}
|
|
//删除组织信息
|
|
@GetMapping("delete")
|
|
public Result delete(String id){
|
|
//在组织管理服务类中引用delete方法
|
|
int flag = orgService.delete(id);
|
|
//判断是否删除成功
|
|
if(flag>0){
|
|
//成功
|
|
return Result.ok();
|
|
}else{
|
|
//失败
|
|
return Result.fail();
|
|
}
|
|
}
|
|
//更新组织信息
|
|
@PostMapping("update")
|
|
public Result update(@RequestBody Org org){
|
|
//在组织管理服务类中引用update方法
|
|
int flag = orgService.update(org);
|
|
//判断是否更新成功
|
|
if(flag>0){
|
|
//成功
|
|
return Result.ok();
|
|
}else{
|
|
//失败
|
|
return Result.fail();
|
|
}
|
|
}
|
|
//获取组织详细信息
|
|
@GetMapping("detail")
|
|
public Org detail(Integer id){
|
|
//返回结果
|
|
return orgService.detail(id);
|
|
}
|
|
//查询组织信息
|
|
@PostMapping("query")
|
|
public Map<String,Object> query(@RequestBody Org org){
|
|
//在组织管理服务类中引用查询方法
|
|
PageInfo<Org> pageInfo = orgService.query(org);
|
|
//返回结果
|
|
return Result.ok(pageInfo);
|
|
}
|
|
} |