无关风月
2024-08-20 f237a61c412870933f47316a011237cd538de9bc
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
package com.stylefeng.guns.modular.code.controller;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stylefeng.guns.core.common.annotion.BussinessLog;
import com.stylefeng.guns.core.common.constant.dictmap.DeptDict;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.mutidatasource.annotion.DataSource;
import com.stylefeng.guns.core.node.ZTreeNode;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dto.DeptQuery;
import com.stylefeng.guns.modular.system.model.Dept;
import com.stylefeng.guns.modular.system.model.User;
import com.stylefeng.guns.modular.system.service.IDeptService;
import com.stylefeng.guns.modular.system.service.IUserService;
import com.stylefeng.guns.modular.system.util.ResultUtil;
import com.stylefeng.guns.modular.system.vo.DeptVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
@Controller
@RequestMapping("/base/dept")
public class SysDeptController {
 
    @Autowired
    private IDeptService deptService;
 
 
    @DataSource(name = "dataSourceBiz")
    @GetMapping(value = "/list")
    @ApiOperation(value = "列表", tags = {"后台-部门管理"})
    @ResponseBody
    public ResultUtil<PageInfo<Dept>> getList(DeptQuery req) {
//        PageHelper.startPage(req.getPageNum(),req.getPageSize());
        List<Dept> depts = deptService.getList(req);
        PageInfo<Dept> info=new PageInfo<>(depts);
        return ResultUtil.success(info);
    }
 
    @DataSource(name = "dataSourceGuns")
    @ApiOperation(value = "添加部门", tags = {"后台-部门管理"})
    @PostMapping(value = "/add")
    @ResponseBody
 
    public ResultUtil add(Dept dept) {
        //完善pids,根据pid拿到pid的pids
        dept.setPid(0);
        deptSetPids(dept);
        dept.setInsertTime(new Date());
        dept.setSimplename(dept.getFullname());
        this.deptService.insert(dept);
        return ResultUtil.success("添加成功");
    }
 
    @DataSource(name = "dataSourceBiz")
    @GetMapping(value = "/tree")
    @ApiOperation(value = "获取部下拉框", tags = {"后台-部门管理"})
    @ResponseBody
    public List<Dept> tree() {
        List<ZTreeNode> tree = this.deptService.tree();
        tree.add(ZTreeNode.createParent());
    return     deptService.selectList(null);
//        return tree;
    }
    @Autowired
    private IUserService userService;
    @DeleteMapping ("/delete")
    @ResponseBody
    @ApiOperation(value = "删除部门", tags = {"后台-部门管理"})
    public ResultUtil delete(String ids) {
        String[] split = ids.split(",");
        for (String id : split) {
            List<User> users = userService.selectList(new EntityWrapper<User>().eq("deptid",Integer.valueOf(id)));
            if (!users.isEmpty()){
                return ResultUtil.error("当前部门已绑定用户,无法删除");
            }
            deptService.deleteById(Integer.valueOf(id));
        }
       return ResultUtil.success("删除成功");
    }
 
    @GetMapping ("pre/edit/{deptId}")
    public Dept deptUpdate(@PathVariable Integer deptId, Model model) {
        Dept dept = deptService.selectById(deptId);
        return dept;
    }
 
 
    @DataSource(name = "dataSourceGuns")
    @ApiOperation(value = "修改部门", tags = {"后台-部门管理"})
    @PutMapping (value = "/update")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(value = "部门id", name = "id"),
            @ApiImplicitParam(value = "部门名称", name = "fullname"),
    })
    public ResultUtil update(Integer id,String fullname ) {
        Dept dept1 = deptService.selectById(id);
        dept1.setFullname(fullname);
        dept1.setSimplename(fullname);
        deptService.updateById(dept1);
        return ResultUtil.success("修改成功");
    }
 
 
    private void deptSetPids(Dept dept) {
        if (ToolUtil.isEmpty(dept.getPid()) || dept.getPid().equals(0)) {
            dept.setPid(0);
            dept.setPids("[0],");
        } else {
            int pid = dept.getPid();
            Dept temp = deptService.selectById(pid);
            String pids = temp.getPids();
            dept.setPid(pid);
            dept.setPids(pids + "[" + pid + "],");
        }
    }
}