44323
2024-05-16 80870e154d7755c36cdb345147532c131858e270
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
package com.ruoyi.management.controller;
 
 
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.management.domain.SysDept;
import com.ruoyi.management.dto.DeptQuery;
import com.ruoyi.management.service.ISysDeptService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
 
import java.util.Date;
import java.util.List;
 
/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author 无关风月
 * @since 2024-04-26
 */
@Controller
@RequestMapping("/base/dept")
public class SysDeptController {
    @Autowired
    private ISysDeptService deptService;
 
 
    @GetMapping(value = "/list")
    @ApiOperation(value = "列表", tags = {"后台-部门管理"})
    @ResponseBody
    public AjaxResult<PageInfo<SysDept>> getList(DeptQuery req) {
        PageInfo<SysDept> res = new PageInfo<>(req.getPageNumber(), req.getPageSize());
        List<SysDept> depts = deptService.getList(req);
        res.setRecords(depts);
        return AjaxResult.success(res);
    }
    @ApiOperation(value = "添加部门", tags = {"后台-部门管理"})
    @PostMapping(value = "/add")
    @ResponseBody
    public AjaxResult add(SysDept dept) {
        //完善pids,根据pid拿到pid的pids
        dept.setParentId(0L);
        deptSetPids(dept);
        dept.setCreateTime(new Date());
        dept.setDeptName(dept.getDeptName());
        deptService.save(dept);
        return AjaxResult.success("添加成功");
    }
    private void deptSetPids(SysDept dept) {
        if (dept.getParentId()== null || dept.getParentId() == 0L) {
            dept.setParentId(0L);
            dept.setAncestors("0");
        } else {
            long pid = dept.getParentId();
            SysDept temp = deptService.getById(pid);
            String pids = temp.getAncestors();
            dept.setParentId(pid);
            dept.setAncestors(pids + "" + pid + ",");
        }
    }
}