puzhibing
2023-12-04 3ad6b6ba2ba56fc0bcd2130e47190779c6e15acc
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.dsh.guns.modular.system.controller.system;
 
import com.dsh.course.model.node.ZTreeNode;
import com.dsh.course.util.StringUtil;
import com.dsh.guns.config.UserExt;
import com.dsh.guns.core.base.controller.BaseController;
import com.dsh.guns.core.common.annotion.BussinessLog;
import com.dsh.guns.core.common.annotion.Permission;
import com.dsh.guns.core.common.constant.dictmap.DeptDict;
import com.dsh.guns.core.common.constant.factory.ConstantFactory;
import com.dsh.guns.core.common.exception.BizExceptionEnum;
import com.dsh.guns.core.exception.GunsException;
import com.dsh.guns.core.log.LogObjectHolder;
import com.dsh.guns.modular.system.model.Dept;
import com.dsh.guns.modular.system.service.IDeptService;
import com.dsh.guns.modular.system.warpper.DeptWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
 
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * 部门控制器
 *
 * @author fengshuonan
 * @Date 2017年2月17日20:27:22
 */
@Controller
@RequestMapping("/dept")
public class DeptController extends BaseController {
 
    private String PREFIX = "/system/dept/";
 
    @Autowired
    private IDeptService deptService;
 
 
    /**
     * 跳转到部门管理首页
     */
    @RequestMapping("")
    public String index(Model model) {
        model.addAttribute("language", UserExt.getLanguage());
        return PREFIX + "dept.html";
    }
 
    /**
     * 跳转到添加部门
     */
    @RequestMapping("/dept_add")
    public String deptAdd(Model model) {
        model.addAttribute("language",UserExt.getLanguage());
        return PREFIX + "dept_add.html";
    }
 
    /**
     * 跳转到修改部门
     */
    @Permission
    @RequestMapping("/dept_update/{deptId}")
    public String deptUpdate(@PathVariable Integer deptId, Model model) {
        Dept dept = deptService.getById(deptId);
        model.addAttribute(dept);
        model.addAttribute("pName", ConstantFactory.me().getDeptName(dept.getPid()));
        LogObjectHolder.me().set(dept);
        model.addAttribute("language",UserExt.getLanguage());
        return PREFIX + "dept_edit.html";
    }
 
    /**
     * 获取部门的tree列表
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        List<ZTreeNode> tree = this.deptService.tree();
 
        tree.add(ZTreeNode.createParent());
        return tree;
    }
 
    /**
     * 新增部门
     */
    @BussinessLog(value = "添加部门", key = "simplename", dict = DeptDict.class)
    @RequestMapping(value = "/add")
    @Permission
    @ResponseBody
    public Object add(Dept dept) {
        if (StringUtil.isOneEmpty(dept, dept.getSimplename())) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        //完善pids,根据pid拿到pid的pids
        deptSetPids(dept);
        dept.setObjectType(UserExt.getUser().getObjectType());
        dept.setObjectId(UserExt.getUser().getObjectId());
        dept.setVersion(UserExt.getUser().getId());
        return this.deptService.save(dept);
    }
 
    /**
     * 获取所有部门列表
     */
    @RequestMapping(value = "/list")
    @Permission
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> list = this.deptService.list(condition);
        if (UserExt.getUser().getObjectType()==2){
            List<Map<String, Object>> filteredRoles = list.stream()
                    .filter(role -> role.containsKey("version") && role.get("version").equals(1))
                    .collect(Collectors.toList());
            return filteredRoles;
        }
 
        return super.warpObject(new DeptWarpper(list));
    }
 
    /**
     * 部门详情
     */
    @RequestMapping(value = "/detail/{deptId}")
    @Permission
    @ResponseBody
    public Object detail(@PathVariable("deptId") Integer deptId) {
        return deptService.getById(deptId);
    }
 
    /**
     * 修改部门
     */
    @BussinessLog(value = "修改部门", key = "simplename", dict = DeptDict.class)
    @RequestMapping(value = "/update")
    @Permission
    @ResponseBody
    public Object update(Dept dept) {
        if (Objects.isNull(dept) || dept.getId() == null) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        deptSetPids(dept);
        deptService.updateById(dept);
 
        return SUCCESS_TIP;
    }
 
    /**
     * 删除部门
     */
    @BussinessLog(value = "删除部门", key = "deptId", dict = DeptDict.class)
    @RequestMapping(value = "/delete")
    @Permission
    @ResponseBody
    public Object delete(@RequestParam Integer deptId) {
 
 
        //缓存被删除的部门名称
        //Department name that was deleted from cache
        LogObjectHolder.me().set(ConstantFactory.me().getDeptName(deptId));
 
        deptService.deleteDept(deptId);
 
        return SUCCESS_TIP;
    }
 
    private void deptSetPids(Dept dept) {
        if (Objects.isNull(dept.getPid()) || dept.getPid().equals(0)) {
            dept.setPid(0);
            dept.setPids("[0],");
        } else {
            int pid = dept.getPid();
            Dept temp = deptService.getById(pid);
            String pids = temp.getPids();
            dept.setPid(pid);
            dept.setPids(pids + "[" + pid + "],");
        }
    }
}