#
Junjie
2024-07-04 0c8c0da73efb15fc92c23c023acaed2d1860e0aa
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package com.zy.asrs.wms.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.asrs.framework.exception.CoolException;
import com.zy.asrs.wms.common.annotation.OperationLog;
import com.zy.asrs.wms.common.domain.BaseParam;
import com.zy.asrs.wms.common.domain.KeyValVo;
import com.zy.asrs.wms.common.domain.PageParam;
import com.zy.asrs.wms.system.entity.Host;
import com.zy.asrs.wms.system.entity.Menu;
import com.zy.asrs.wms.system.entity.RoleMenu;
import com.zy.asrs.wms.system.service.*;
import com.zy.asrs.wms.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletResponse;
import java.util.*;
 
@RestController
@RequestMapping("/api")
public class HostController extends BaseController {
 
    @Autowired
    private HostService hostService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private RoleMenuService roleMenuService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private UserService userService;
 
    @PreAuthorize("hasAuthority('system:host:list')")
    @PostMapping("/host/page")
    public R page(@RequestBody Map<String, Object> map) {
        BaseParam baseParam = buildParam(map, BaseParam.class);
        PageParam<Host, BaseParam> pageParam = new PageParam<>(baseParam, Host.class);
        return R.ok().add(hostService.page(pageParam, pageParam.buildWrapper(true)));
    }
 
    @PreAuthorize("hasAuthority('system:host:list')")
    @PostMapping("/host/list")
    public R list(@RequestBody Map<String, Object> map) {
        return R.ok().add(hostService.list());
    }
 
    @PreAuthorize("hasAuthority('system:host:list')")
    @GetMapping("/host/{id}")
    public R get(@PathVariable("id") Long id) {
        return R.ok().add(hostService.getById(id));
    }
 
    @PreAuthorize("hasAuthority('system:host:save')")
    @OperationLog("添加机构")
    @PostMapping("/host/save")
    @Transactional
    public R save(@RequestBody Host host) {
        host.setCreateTime(new Date());
        host.setUpdateTime(new Date());
        if (!hostService.save(host)) {
            return R.error("添加失败");
        }
 
        List<Menu> parentMenus = menuService.selectByParentId(0L, 0, 1L);
        for (Menu parentMenu : parentMenus) {
            List<Menu> menuList = menuService.selectByParentId(parentMenu.getId(), 0, parentMenu.getHostId());
 
            parentMenu.setId(null);
            parentMenu.setHostId(host.getId());
            boolean save1 = menuService.save(parentMenu);
            if (!save1) {
                throw new CoolException("添加失败");
            }
 
            for (Menu menu1 : menuList) {
                List<Menu> menuList2 = menuService.selectByParentId(menu1.getId(), 1, menu1.getHostId());
 
                menu1.setId(null);
                menu1.setHostId(host.getId());
                menu1.setParentId(parentMenu.getId());
                boolean save2 = menuService.save(menu1);
                if (!save2) {
                    throw new CoolException("添加失败");
                }
 
                for (Menu menu2 : menuList2) {
                    menu2.setId(null);
                    menu2.setHostId(host.getId());
                    menu2.setParentId(menu1.getId());
                    boolean save3 = menuService.save(menu2);
                    if (!save3) {
                        throw new CoolException("添加失败");
                    }
                }
            }
 
            //创建默认权限
            for (Menu menu1 : menuList) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(1L);
                roleMenu.setMenuId(menu1.getId());
                boolean save4 = roleMenuService.save(roleMenu);
                if (!save4) {
                    throw new CoolException("添加失败");
                }
            }
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(1L);
            roleMenu.setMenuId(parentMenu.getId());
            boolean save5 = roleMenuService.save(roleMenu);
            if (!save5) {
                throw new CoolException("添加失败");
            }
 
        }
 
//        //创建系统默认菜单
//        Menu menu = new Menu();
//        menu.setName("系统管理");
//        menu.setParentId(0L);
//        menu.setRoute("/system");
//        menu.setType(0);
//        menu.setIcon("UserOutlined");
//        menu.setSort(1);
//        menu.setLanguageId("system.system");
//        menu.setHostId(host.getId());
//        boolean save = menuService.save(menu);
//        if (!save) {
//            throw new CoolException("添加失败");
//        }
//        List<Menu> menuList = menuService.selectByPathName("系统管理", 0);
//        for (Menu menu1 : menuList) {
//            menu1.setId(null);
//            menu1.setHostId(host.getId());
//            menu1.setParentId(menu.getId());
//            boolean save1 = menuService.save(menu1);
//            if (!save1) {
//                throw new CoolException("添加失败");
//            }
//
//            List<Menu> menuList2 = menuService.selectByPathName(menu1.getName(), 1);
//            for (Menu menu2 : menuList2) {
//                menu2.setId(null);
//                menu2.setHostId(host.getId());
//                menu2.setParentId(menu1.getId());
//                boolean save2 = menuService.save(menu2);
//                if (!save2) {
//                    throw new CoolException("添加失败");
//                }
//            }
//        }
//
//        //创建默认权限
//        for (Menu menu1 : menuList) {
//            RoleMenu roleMenu = new RoleMenu();
//            roleMenu.setRoleId(1L);
//            roleMenu.setMenuId(menu1.getId());
//            boolean save1 = roleMenuService.save(roleMenu);
//            if (!save1) {
//                throw new CoolException("添加失败");
//            }
//        }
//        RoleMenu roleMenu = new RoleMenu();
//        roleMenu.setRoleId(1L);
//        roleMenu.setMenuId(menu.getId());
//        boolean save1 = roleMenuService.save(roleMenu);
//        if (!save1) {
//            throw new CoolException("添加失败");
//        }
 
        return R.ok("添加成功");
    }
 
    @PreAuthorize("hasAuthority('system:host:update')")
    @OperationLog("修改机构")
    @PostMapping("/host/update")
    public R update(@RequestBody Host host) {
        host.setUpdateTime(new Date());
        if (!hostService.updateById(host)) {
            return R.error("修改失败");
        }
        return R.ok("修改成功");
    }
 
    @PreAuthorize("hasAuthority('system:host:remove')")
    @OperationLog("删除机构")
    @PostMapping("/host/remove/{ids}")
    @Transactional
    public R remove(@PathVariable Long[] ids) {
        List<Host> list = hostService.list(new LambdaQueryWrapper<Host>().in(Host::getId, ids));
        for (Host host : list) {
            if (host.getId() == 1) {
                throw new CoolException("根节点不可被删除");
            }
 
            ArrayList<Long> menuIds = new ArrayList<>();
            for (Menu menu : menuService.selectByHostId(host.getId())) {
                menuIds.add(menu.getId());
            }
 
            if (!menuIds.isEmpty()) {
                roleMenuService.remove(new LambdaQueryWrapper<RoleMenu>().in(RoleMenu::getMenuId, menuIds));
                menuService.removeByHostId(host.getId());
                roleService.removeByHostId(host.getId());
                userService.removeByHostId(host.getId());
            }
        }
 
        if (!hostService.removeByIds(Arrays.asList(ids))) {
            return R.error("删除失败");
        }
        return R.ok("删除成功");
    }
 
    @PreAuthorize("hasAuthority('system:host:list')")
    @PostMapping("/host/query")
    public R query(@RequestParam(required = false) String condition) {
        List<KeyValVo> vos = new ArrayList<>();
        LambdaQueryWrapper<Host> wrapper = new LambdaQueryWrapper<>();
        if (!Cools.isEmpty(condition)) {
            wrapper.like(Host::getName, condition);
        }
        hostService.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:host:list')")
    @PostMapping("/host/export")
    public void export(@RequestBody Map<String, Object> map, HttpServletResponse response) throws Exception {
        ExcelUtil.build(ExcelUtil.create(hostService.list(), Host.class), response);
    }
 
}