luxiaotao1123
2024-06-21 569d2b6bc754b037ade5b4205a4d1cdb8339b26b
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.zy.ptc.kernel.system.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.asrs.framework.common.Cools;
import com.zy.asrs.framework.common.R;
import com.zy.ptc.kernel.common.annotation.OperationLog;
import com.zy.ptc.kernel.common.domain.BaseParam;
import com.zy.ptc.kernel.common.domain.KeyValVo;
import com.zy.ptc.kernel.common.domain.PageParam;
import com.zy.ptc.kernel.system.entity.Dept;
import com.zy.ptc.kernel.system.service.DeptService;
import com.zy.ptc.kernel.utils.ExcelUtil;
import com.zy.ptc.kernel.utils.NodeUtils;
import com.zy.ptc.kernel.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletResponse;
import java.util.*;
 
@RestController
@RequestMapping("/api")
public class DeptController extends BaseController {
 
    @Autowired
    private DeptService deptService;
 
    @PreAuthorize("hasAuthority('system:dept:list')")
    @PostMapping("/dept/page")
    public R page(@RequestBody Map<String, Object> map) {
        BaseParam baseParam = buildParam(map, BaseParam.class);
        PageParam<Dept, BaseParam> pageParam = new PageParam<>(baseParam, Dept.class);
        return R.ok().add(deptService.page(pageParam, pageParam.buildWrapper(true)));
    }
 
    @PreAuthorize("hasAuthority('system:dept:list')")
    @PostMapping("/dept/list")
    public R list(@RequestBody Map<String, Object> map) {
        return R.ok().add(deptService.list());
    }
 
    @PreAuthorize("hasAuthority('system:dept:list')")
    @PostMapping("/dept/tree")
    public R tree(@RequestBody Map<String, Object> map) {
//        PageParam<Dept, BaseParam> param = new PageParam<>(buildParam(map, BaseParam.class), Dept.class);
//        QueryWrapper<Dept> wrapper = param.buildWrapper(true, queryWrapper -> queryWrapper.orderByAsc("sort"));
        List<Dept> deptList = deptService.list(new LambdaQueryWrapper<Dept>().orderByAsc(Dept::getSort));
        List<Dept> treeData = Utils.toTreeData(deptList, 0L, Dept::getParentId, Dept::getId, Dept::setChildren);
        if (!Cools.isEmpty(map.get("condition"))) {
            Utils.treeRemove(treeData, String.valueOf(map.get("condition")), Dept::getName, Dept::getChildren);
            Utils.treeRemove(treeData, String.valueOf(map.get("condition")), Dept::getName, Dept::getChildren);
        }
        return R.ok().add(treeData);
    }
 
    @PreAuthorize("hasAuthority('system:dept:list')")
    @GetMapping("/dept/{id}")
    public R get(@PathVariable("id") Long id) {
        return R.ok().add(deptService.getById(id));
    }
 
    @PreAuthorize("hasAuthority('system:dept:save')")
    @OperationLog("添加部门")
    @PostMapping("/dept/save")
    public R save(@RequestBody Dept dept) {
        if (dept.getParentId() != null && dept.getParentId() > 0) {
            Dept parent = deptService.getById(dept.getParentId());
            if (parent != null) {
                dept.setParentName(parent.getName());
            }
        } else {
            dept.setParentId(0L);
        }
 
        NodeUtils nodeUtils = new NodeUtils();
        nodeUtils.generatePath0(item -> deptService.getById(item.getParentId()), dept, Dept::getId, Dept::getName, Dept::getParentId);
        dept.setPath(nodeUtils.path.toString());
        dept.setPathName(nodeUtils.pathName.toString());
 
        dept.setCreateBy(getLoginUserId());
        dept.setCreateTime(new Date());
        dept.setUpdateBy(getLoginUserId());
        dept.setUpdateTime(new Date());
        if (!deptService.save(dept)) {
            return R.error("添加失败");
        }
        return R.ok("添加成功");
    }
 
    @PreAuthorize("hasAuthority('system:dept:update')")
    @OperationLog("修改部门")
    @PostMapping("/dept/update")
    public R update(@RequestBody Dept dept) {
        if (dept.getParentId() != null && dept.getParentId() > 0) {
            Dept parent = deptService.getById(dept.getParentId());
            if (parent != null) {
                dept.setParentName(parent.getName());
            }
        } else {
            dept.setParentId(0L);
        }
 
        NodeUtils nodeUtils = new NodeUtils();
        nodeUtils.generatePath0(item -> deptService.getById(item.getParentId()), dept, Dept::getId, Dept::getName, Dept::getParentId);
        dept.setPath(nodeUtils.path.toString());
        dept.setPathName(nodeUtils.pathName.toString());
 
        dept.setUpdateBy(getLoginUserId());
        dept.setUpdateTime(new Date());
        if (!deptService.updateById(dept)) {
            return R.error("修改失败");
        }
        return R.ok("修改成功");
    }
 
    @PreAuthorize("hasAuthority('system:dept:remove')")
    @OperationLog("删除部门")
    @PostMapping("/dept/remove/{ids}")
    public R remove(@PathVariable Long[] ids) {
        if (!deptService.removeByIds(Arrays.asList(ids))) {
            return R.error("删除失败");
        }
        return R.ok("删除成功");
    }
 
    @PreAuthorize("hasAuthority('system:dept:list')")
    @PostMapping("/dept/query")
    public R query(@RequestParam(required = false) String condition) {
        List<KeyValVo> vos = new ArrayList<>();
        LambdaQueryWrapper<Dept> wrapper = new LambdaQueryWrapper<>();
        if (!Cools.isEmpty(condition)) {
            wrapper.like(Dept::getName, condition);
        }
        deptService.page(new Page<>(1, 30), wrapper).getRecords().forEach(
                item -> vos.add(new KeyValVo(item.getId(), item.getName()))
        );
        return R.ok().add(vos);
    }
 
    @PreAuthorize("hasAuthority('system:dept:list')")
    @PostMapping("/dept/export")
    public void export(@RequestBody Map<String, Object> map, HttpServletResponse response) throws Exception {
        ExcelUtil.build(ExcelUtil.create(deptService.list(), Dept.class), response);
    }
 
}