双重预防项目-国泰新华二开定制版
SZH
2022-08-20 f9f0687195e0fe349185437d22c495d74c8d4a20
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package com.ruoyi.project.system.user.controller;
 
import java.util.ArrayList;
import java.util.List;
 
import com.ruoyi.common.utils.html.EscapeUtil;
import com.ruoyi.project.system.company.domain.Company;
import com.ruoyi.project.system.company.service.ICompanyService;
import com.ruoyi.project.system.dept.domain.Dept;
import com.ruoyi.project.system.dept.service.IDeptService;
import com.ruoyi.project.system.post.domain.Post;
import com.ruoyi.project.system.role.domain.Role;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.post.service.IPostService;
import com.ruoyi.project.system.role.service.IRoleService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.domain.UserRole;
import com.ruoyi.project.system.user.service.IUserService;
 
/**
 * 用户信息
 *
 * @author ruoyi
 */
@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {
    private String prefix = "system/user";
 
    @Autowired
    private IUserService userService;
 
    @Autowired
    private IRoleService roleService;
 
    @Autowired
    private IPostService postService;
 
    @Autowired
    private ICompanyService companyService;
 
    @Autowired
    private IDeptService deptService;
 
 
 
    @RequiresPermissions("system:user:view")
    @GetMapping()
    public String user() {
        return prefix + "/user";
    }
 
    @RequiresPermissions("system:user:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(User user) {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }
 
    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:user:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(User user) {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        return util.exportExcel(list, "用户数据");
    }
 
    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:user:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        List<User> userList = util.importExcel(file.getInputStream());
        String message = userService.importUser(userList, updateSupport);
        return AjaxResult.success(message);
    }
 
    @RequiresPermissions("system:user:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        return util.importTemplateExcel("用户数据");
    }
 
    /**
     * 新增用户
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        mmap.put("roles", roleService.selectRoleAll());
        mmap.put("posts", postService.selectPostAll());
        return prefix + "/add";
    }
 
    /**
     * 新增保存用户
     */
    @RequiresPermissions("system:user:add")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated User user) {
        if (UserConstants.USER_NAME_NOT_UNIQUE.equals(userService.checkLoginNameUnique(user.getLoginName()))) {
            return error("新增用户'" + user.getLoginName() + "'失败,登录账号已存在");
        } else if (UserConstants.USER_PHONE_NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
            return error("新增用户'" + user.getLoginName() + "'失败,手机号码已存在");
        }
//        else if (UserConstants.USER_EMAIL_NOT_UNIQUE.equals(userService.checkEmailUnique(user)))
//        {
//            return error("新增用户'" + user.getLoginName() + "'失败,邮箱账号已存在");
//        }
        return toAjax(userService.insertUser(user));
    }
 
    /**
     * 修改用户
     */
    @GetMapping("/edit/{userId}")
    public String edit(@PathVariable("userId") Long userId, ModelMap mmap) {
        mmap.put("user", userService.selectUserById(userId));
        mmap.put("roles", roleService.selectRolesByUserId(userId));
        mmap.put("posts", postService.selectPostsByUserId(userId));
        return prefix + "/edit";
    }
 
    /**
     * 修改保存用户
     */
    @RequiresPermissions("system:user:edit")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated User user) {
        userService.checkUserAllowed(user);
        if (UserConstants.USER_PHONE_NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
            return error("修改用户'" + user.getLoginName() + "'失败,手机号码已存在");
        }
//        else if (UserConstants.USER_EMAIL_NOT_UNIQUE.equals(userService.checkEmailUnique(user)))
//        {
//            return error("修改用户'" + user.getLoginName() + "'失败,邮箱账号已存在");
//        }
        return toAjax(userService.updateUser(user));
    }
 
    @RequiresPermissions("system:user:resetPwd")
    @Log(title = "重置密码", businessType = BusinessType.UPDATE)
    @GetMapping("/resetPwd/{userId}")
    public String resetPwd(@PathVariable("userId") Long userId, ModelMap mmap) {
        mmap.put("user", userService.selectUserById(userId));
        return prefix + "/resetPwd";
    }
 
    @RequiresPermissions("system:user:resetPwd")
    @Log(title = "重置密码", businessType = BusinessType.UPDATE)
    @PostMapping("/resetPwd")
    @ResponseBody
    public AjaxResult resetPwdSave(User user) {
        userService.checkUserAllowed(user);
        if (userService.resetUserPwd(user) > 0) {
            if (ShiroUtils.getUserId() == user.getUserId()) {
                setSysUser(userService.selectUserById(user.getUserId()));
            }
            return success();
        }
        return error();
    }
 
    /**
     * 进入授权角色页
     */
    @GetMapping("/authRole/{userId}")
    public String authRole(@PathVariable("userId") Long userId, ModelMap mmap) {
        User user = userService.selectUserById(userId);
        // 获取用户所属的角色列表
        List<UserRole> userRoles = userService.selectUserRoleByUserId(userId);
        mmap.put("user", user);
        mmap.put("userRoles", userRoles);
        return prefix + "/authRole";
    }
 
    /**
     * 用户授权角色
     */
    @RequiresPermissions("system:user:add")
    @Log(title = "用户管理", businessType = BusinessType.GRANT)
    @PostMapping("/authRole/insertAuthRole")
    @ResponseBody
    public AjaxResult insertAuthRole(Long userId, Long[] roleIds) {
        userService.insertUserAuth(userId, roleIds);
        return success();
    }
 
    @RequiresPermissions("system:user:remove")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(userService.deleteUserByIdsByPhysics(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }
 
    /**
     * 校验用户名
     */
    @PostMapping("/checkLoginNameUnique")
    @ResponseBody
    public String checkLoginNameUnique(User user) {
        return userService.checkLoginNameUnique(user.getLoginName());
    }
 
    /**
     * 校验手机号码
     */
    @PostMapping("/checkPhoneUnique")
    @ResponseBody
    public String checkPhoneUnique(User user) {
        return userService.checkPhoneUnique(user);
    }
 
    /**
     * 校验email邮箱
     */
    @PostMapping("/checkEmailUnique")
    @ResponseBody
    public String checkEmailUnique(User user) {
        return userService.checkEmailUnique(user);
    }
 
    /**
     * 用户状态修改
     */
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:user:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(User user) {
        userService.checkUserAllowed(user);
        return toAjax(userService.changeStatus(user));
    }
 
 
    @GetMapping("/userByCompanyId")
    public String userByCompanyId(ModelMap modelMap) {
        modelMap.put("userId", ShiroUtils.getUserId());
        modelMap.put("companyId", ShiroUtils.getSysUser().getCompanyId());
        return prefix + "/userByCompanyId";
    }
 
 
    /**
     * 新增用户
     */
    @GetMapping("/addByCompanyId")
    public String addByCompanyId(ModelMap mmap) {
        Long companyId = ShiroUtils.getSysUser().getCompanyId();
 
        mmap.put("companyId", ShiroUtils.getSysUser().getCompanyId());
        Role role = new Role();
        role.setCompanyId(companyId);
        mmap.put("roles", roleService.selectRoleList(role));
        Post post = new Post();
        post.setCompanyId(companyId);
        mmap.put("posts", postService.selectPostList(post));
        return prefix + "/addByCompanyId";
    }
 
 
    /**
     * 修改用户
     */
    @GetMapping("/editByCompanyId/{userId}")
    public String editByCompanyId(@PathVariable("userId") Long userId, ModelMap mmap) {
        mmap.put("companyId", ShiroUtils.getSysUser().getCompanyId());
        mmap.put("user", userService.selectUserById(userId));
        mmap.put("roles", roleService.selectRolesByUserIdAndCompanyId(userId, ShiroUtils.getSysUser().getCompanyId()));
        mmap.put("posts", postService.selectPostsByUserId(userId));
        return prefix + "/editByCompanyId";
    }
 
 
    //跳转至用户选择页面
    @GetMapping("selectUserByCompanyId")
    public String selectUserByCompanyId() {
        return prefix + "/selectUserByCompanyId";
    }
 
    @PostMapping("/listByCompanyId")
    @ResponseBody
    public TableDataInfo listByCompanyId(User user) {
        user.setCompanyId(ShiroUtils.getSysUser().getCompanyId());
        //只查询已经启用的用户
        user.setStatus("0");
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }
 
 
    //查询用户(如果是总公司或者分公司账号,查询所有user)
    @GetMapping("selectUserListByParentSonCompany")
    public String selectUserListByParentSonCompany(ModelMap mmap) {
        Long companyId = ShiroUtils.getSysUser().getCompanyId();
        Company company = companyService.selectCompanyById(companyId);
        List<Company> companyList = new ArrayList<>();
        if (companyId != null && companyId != 0L && companyId != -1L && company != null) {
            if (company.getParentId() == 0L) {
                companyList.add(company);
                //查询是否存在子公司
                Company sonQuery = new Company();
                sonQuery.setParentId(companyId);
                List<Company> sonList = companyService.selectCompanyList(sonQuery);
                if (sonList.size() == 0) {
 
                } else {
                    companyList.addAll(sonList);
                }
            } else {
                Company companyParent = companyService.selectCompanyById(company.getParentId());
                companyList.add(companyParent);
                //查询是否存在子公司
                Company sonQuery = new Company();
                sonQuery.setParentId(companyParent.getCompanyId());
                List<Company> sonList = companyService.selectCompanyList(sonQuery);
                companyList.addAll(sonList);
            }
        }
        mmap.put("companyList", companyList);
 
 
        //部门列表,(查询时选择)
        List<Dept> deptList = new ArrayList<>();
        List<Long> companyIdList = new ArrayList<>();
        for (int i = 0; i < companyList.size(); i++)
        {
            companyIdList.add(companyList.get(i).getCompanyId());
        }
        if(companyIdList.size()>0) {
            Dept queryDept = new Dept();
            queryDept.setCompanyIdList(companyIdList);
            deptList = deptService.selectDeptList(queryDept);
        }
        mmap.put("deptList", deptList);
 
        return prefix + "/selectUserListByParentSonCompany";
    }
 
 
 
    //查询用户(如果是总公司或者分公司账号,查询所有user)
    @GetMapping("selectUserListByParentSonCompanyConfirm")
    public String selectUserListByParentSonCompanyConfirm(ModelMap mmap) {
        Long companyId = ShiroUtils.getSysUser().getCompanyId();
        Company company = companyService.selectCompanyById(companyId);
        List<Company> companyList = new ArrayList<>();
        if (companyId != null && companyId != 0L && companyId != -1L && company != null) {
            if (company.getParentId() == 0L) {
                companyList.add(company);
                //查询是否存在子公司
                Company sonQuery = new Company();
                sonQuery.setParentId(companyId);
                List<Company> sonList = companyService.selectCompanyList(sonQuery);
                if (sonList.size() == 0) {
 
                } else {
                    companyList.addAll(sonList);
                }
            } else {
                Company companyParent = companyService.selectCompanyById(company.getParentId());
                companyList.add(companyParent);
                //查询是否存在子公司
                Company sonQuery = new Company();
                sonQuery.setParentId(companyParent.getCompanyId());
                List<Company> sonList = companyService.selectCompanyList(sonQuery);
                companyList.addAll(sonList);
            }
        }
        mmap.put("companyList", companyList);
 
        //部门列表,(查询时选择)
        List<Dept> deptList = new ArrayList<>();
        List<Long> companyIdList = new ArrayList<>();
        for (int i = 0; i < companyList.size(); i++)
        {
            companyIdList.add(companyList.get(i).getCompanyId());
        }
        if(companyIdList.size()>0) {
            Dept queryDept = new Dept();
            queryDept.setCompanyIdList(companyIdList);
            deptList = deptService.selectDeptList(queryDept);
        }
        mmap.put("deptList", deptList);
 
        return prefix + "/selectUserListByParentSonCompanyConfirm";
    }
 
 
    @PostMapping("/selectUserListByParentSonCompany")
    @ResponseBody
    public TableDataInfo selectUserListByParentSonCompany(User user) {
        Long companyId = ShiroUtils.getSysUser().getCompanyId();
        Company company = companyService.selectCompanyById(companyId);
        if (companyId != null && companyId != 0L && companyId != -1L && company != null) {
            //只查询已经启用的用户
            user.setStatus("0");
            startPage();
            List<User> list = userService.selectUserList(user);
            return getDataTable(list);
        }
        return getDataTable(new ArrayList<User>());
    }
 
 
    //查询用户(根据用户所在公司)
    @GetMapping("selectUserListByCompany")
    public String selectUserListByCompany(ModelMap mmap) {
        Long companyId = ShiroUtils.getSysUser().getCompanyId();
        mmap.put("companyId", companyId);
        return prefix + "/selectUserListByCompany";
    }
 
    @PostMapping("/selectUserListByCompany")
    @ResponseBody
    public TableDataInfo selectUserListByCompany(User user) {
        Long companyId = user.getCompanyId();
        if (companyId != null && companyId != 0L && companyId != -1L) {
            //只查询已经启用的用户
            user.setStatus("0");
            startPage();
            List<User> list = userService.selectUserList(user);
            return getDataTable(list);
        }
        return getDataTable(new ArrayList<User>());
    }
 
}