使用oracle做的数据上传系统后台
kongdeqiang
2026-03-21 b63977ec7120e8d5f8e5b7d1ac9b85be76ad62a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.example.controller;
 
import com.example.common.Result;
import com.example.entity.Department;
import com.example.security.UserContext;
import com.example.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.util.List;
 
@RestController
@RequestMapping("/api/department")
public class DepartmentController {
 
    @Autowired
    private DepartmentService departmentService;
 
    @GetMapping("/list")
    public Result<List<Department>> list() {
        String currentDeptCode = UserContext.getCurrentDeptCode();
        return Result.success(departmentService.getDepartmentTreeWithPermission(currentDeptCode));
    }
 
    @GetMapping("/tree")
    public Result<List<Department>> tree() {
        String currentDeptCode = UserContext.getCurrentDeptCode();
        return Result.success(departmentService.getDepartmentTreeWithPermission(currentDeptCode));
    }
 
    @GetMapping("/{id}")
    public Result<Department> getById(@PathVariable Long id) {
        Department dept = departmentService.getById(id);
        if (dept == null) {
            return Result.error("部门不存在");
        }
        String currentDeptCode = UserContext.getCurrentDeptCode();
        List<String> deptCodes = departmentService.getChildDeptCodes(currentDeptCode);
        if (!deptCodes.contains(dept.getDeptCode())) {
            return Result.error("无权限查看该部门");
        }
        return Result.success(dept);
    }
 
    @PostMapping
    public Result<Boolean> save(@RequestBody Department department) {
        String currentDeptCode = UserContext.getCurrentDeptCode();
        List<String> deptCodes = departmentService.getChildDeptCodes(currentDeptCode);
        if (!deptCodes.contains(department.getParentCode())) {
            return Result.error("无权限在该父部门下创建部门");
        }
        if (departmentService.checkDeptCodeExists(department.getDeptCode())) {
            return Result.error("部门编码已存在");
        }
        return Result.success(departmentService.save(department));
    }
 
    @PutMapping
    public Result<Boolean> update(@RequestBody Department department) {
        Department existDept = departmentService.getById(department.getId());
        if (existDept == null) {
            return Result.error("部门不存在");
        }
        String currentDeptCode = UserContext.getCurrentDeptCode();
        List<String> deptCodes = departmentService.getChildDeptCodes(currentDeptCode);
        if (!deptCodes.contains(existDept.getDeptCode())) {
            return Result.error("无权限修改该部门");
        }
        Department checkDept = departmentService.getByDeptCode(department.getDeptCode());
        if (checkDept != null && !checkDept.getId().equals(department.getId())) {
            return Result.error("部门编码已存在");
        }
        return Result.success(departmentService.updateById(department));
    }
 
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        Department dept = departmentService.getById(id);
        if (dept == null) {
            return Result.error("部门不存在");
        }
        String currentDeptCode = UserContext.getCurrentDeptCode();
        List<String> deptCodes = departmentService.getChildDeptCodes(currentDeptCode);
        if (!deptCodes.contains(dept.getDeptCode())) {
            return Result.error("无权限删除该部门");
        }
        List<String> childCodes = departmentService.getChildDeptCodes(dept.getDeptCode());
        if (childCodes.size() > 1) {
            return Result.error("该部门下存在子部门,无法删除");
        }
        return Result.success(departmentService.removeById(id));
    }
}