mitao
2 天以前 a9f2a7f4fac47c7a5b1302db2e0c363a55459f84
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
package com.ruoyi.web.controller.system;
 
 
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.TDept;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.vo.TDeptVO;
import com.ruoyi.system.dto.AddDeptDTO;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.TDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 通知设置 前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2025-05-28
 */
@Api(tags = "部门管理")
@RestController
@RequestMapping("/tDept")
public class TDeptController {
    @Resource
    private TDeptService deptService;
    @Resource
    private ISysUserService sysUserService;
    @Resource
    private SysUserMapper sysUserMapper;
    /**
     * 获取部门树列表
     */
    @ApiOperation("获取部门树列表")
    @GetMapping("/tree")
    public R<List<TDept>> tree() {
        List<TDept> deptTree = deptService.selectDeptTreeList();
        return R.ok(deptTree);
    }
    @Log(title = "新增部门", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增部门")
    @PostMapping(value = "/add")
    public R add(@RequestBody AddDeptDTO dto) {
        deptService.save(dto);
        return R.ok();
    }
    @Log(title = "编辑部门", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "编辑部门")
    @PostMapping(value = "/edit")
    public R edit(@RequestBody AddDeptDTO dto) {
        deptService.updateById(dto);
        return R.ok();
    }
    @ApiOperation(value = "部门详情")
    @GetMapping(value = "/detail")
    public R<TDept> edit(@RequestParam String id) {
        return R.ok(deptService.getById(id));
    }
    @ApiOperation(value = "部门删除")
    @DeleteMapping(value = "/delete")
    @Log(title = "删除部门", businessType = BusinessType.DELETE)
    public R<Boolean> delete(@RequestParam String id) {
        Long count = deptService.lambdaQuery().eq(TDept::getParentId, id).count();
        if (count>0){
            return R.fail("当前部门存在下级部门,不可删除!");
        }
        List<SysUser> users = sysUserMapper.selectAllList().stream().filter(e -> e.getDelFlag().equals("0") && e.getDeptId().equals(id)).collect(Collectors.toList());
        if (!users.isEmpty()){
            return R.fail("当前部门存在用户,不可删除!");
        }
        deptService.removeById(id);
        return R.ok();
    }
}