houzhongyi
2024-07-11 e7c1260db32209a078a962aaa0ad5492c35774fb
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.system.controller.admin.user;
H 2
3 import cn.hutool.core.collection.CollUtil;
4 import com.iailab.framework.apilog.core.annotation.ApiAccessLog;
5 import com.iailab.framework.common.enums.CommonStatusEnum;
6 import com.iailab.framework.common.pojo.CommonResult;
7 import com.iailab.framework.common.pojo.PageParam;
8 import com.iailab.framework.common.pojo.PageResult;
9 import com.iailab.framework.excel.core.util.ExcelUtils;
10 import com.iailab.module.system.controller.admin.user.vo.user.*;
11 import com.iailab.module.system.convert.user.UserConvert;
12 import com.iailab.module.system.dal.dataobject.dept.DeptDO;
13 import com.iailab.module.system.dal.dataobject.user.AdminUserDO;
14 import com.iailab.module.system.enums.common.SexEnum;
15 import com.iailab.module.system.service.dept.DeptService;
16 import com.iailab.module.system.service.user.AdminUserService;
17 import io.swagger.v3.oas.annotations.Operation;
18 import io.swagger.v3.oas.annotations.Parameter;
19 import io.swagger.v3.oas.annotations.Parameters;
20 import io.swagger.v3.oas.annotations.tags.Tag;
21 import org.springframework.security.access.prepost.PreAuthorize;
22 import org.springframework.validation.annotation.Validated;
23 import org.springframework.web.bind.annotation.*;
24 import org.springframework.web.multipart.MultipartFile;
25
26 import javax.annotation.Resource;
27 import javax.servlet.http.HttpServletResponse;
28 import javax.validation.Valid;
29 import java.io.IOException;
30 import java.util.Arrays;
31 import java.util.List;
32 import java.util.Map;
33
34 import static com.iailab.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
35 import static com.iailab.framework.common.pojo.CommonResult.success;
36 import static com.iailab.framework.common.util.collection.CollectionUtils.convertList;
37
38 @Tag(name = "管理后台 - 用户")
39 @RestController
40 @RequestMapping("/system/user")
41 @Validated
42 public class UserController {
43
44     @Resource
45     private AdminUserService userService;
46     @Resource
47     private DeptService deptService;
48
49     @PostMapping("/create")
50     @Operation(summary = "新增用户")
51     @PreAuthorize("@ss.hasPermission('system:user:create')")
52     public CommonResult<Long> createUser(@Valid @RequestBody UserSaveReqVO reqVO) {
53         Long id = userService.createUser(reqVO);
54         return success(id);
55     }
56
57     @PutMapping("update")
58     @Operation(summary = "修改用户")
59     @PreAuthorize("@ss.hasPermission('system:user:update')")
60     public CommonResult<Boolean> updateUser(@Valid @RequestBody UserSaveReqVO reqVO) {
61         userService.updateUser(reqVO);
62         return success(true);
63     }
64
65     @DeleteMapping("/delete")
66     @Operation(summary = "删除用户")
67     @Parameter(name = "id", description = "编号", required = true, example = "1024")
68     @PreAuthorize("@ss.hasPermission('system:user:delete')")
69     public CommonResult<Boolean> deleteUser(@RequestParam("id") Long id) {
70         userService.deleteUser(id);
71         return success(true);
72     }
73
74     @PutMapping("/update-password")
75     @Operation(summary = "重置用户密码")
76     @PreAuthorize("@ss.hasPermission('system:user:update-password')")
77     public CommonResult<Boolean> updateUserPassword(@Valid @RequestBody UserUpdatePasswordReqVO reqVO) {
78         userService.updateUserPassword(reqVO.getId(), reqVO.getPassword());
79         return success(true);
80     }
81
82     @PutMapping("/update-status")
83     @Operation(summary = "修改用户状态")
84     @PreAuthorize("@ss.hasPermission('system:user:update')")
85     public CommonResult<Boolean> updateUserStatus(@Valid @RequestBody UserUpdateStatusReqVO reqVO) {
86         userService.updateUserStatus(reqVO.getId(), reqVO.getStatus());
87         return success(true);
88     }
89
90     @GetMapping("/page")
91     @Operation(summary = "获得用户分页列表")
92     @PreAuthorize("@ss.hasPermission('system:user:list')")
93     public CommonResult<PageResult<UserRespVO>> getUserPage(@Valid UserPageReqVO pageReqVO) {
94         // 获得用户分页列表
95         PageResult<AdminUserDO> pageResult = userService.getUserPage(pageReqVO);
96         if (CollUtil.isEmpty(pageResult.getList())) {
97             return success(new PageResult<>(pageResult.getTotal()));
98         }
99         // 拼接数据
100         Map<Long, DeptDO> deptMap = deptService.getDeptMap(
101                 convertList(pageResult.getList(), AdminUserDO::getDeptId));
102         return success(new PageResult<>(UserConvert.INSTANCE.convertList(pageResult.getList(), deptMap),
103                 pageResult.getTotal()));
104     }
105
106     @GetMapping({"/list-all-simple", "/simple-list"})
107     @Operation(summary = "获取用户精简信息列表", description = "只包含被开启的用户,主要用于前端的下拉选项")
108     public CommonResult<List<UserSimpleRespVO>> getSimpleUserList() {
109         List<AdminUserDO> list = userService.getUserListByStatus(CommonStatusEnum.ENABLE.getStatus());
110         // 拼接数据
111         Map<Long, DeptDO> deptMap = deptService.getDeptMap(
112                 convertList(list, AdminUserDO::getDeptId));
113         return success(UserConvert.INSTANCE.convertSimpleList(list, deptMap));
114     }
115
116     @GetMapping("/get")
117     @Operation(summary = "获得用户详情")
118     @Parameter(name = "id", description = "编号", required = true, example = "1024")
119     @PreAuthorize("@ss.hasPermission('system:user:query')")
120     public CommonResult<UserRespVO> getUser(@RequestParam("id") Long id) {
121         AdminUserDO user = userService.getUser(id);
122         if (user == null) {
123             return success(null);
124         }
125         // 拼接数据
126         DeptDO dept = deptService.getDept(user.getDeptId());
127         return success(UserConvert.INSTANCE.convert(user, dept));
128     }
129
130     @GetMapping("/export")
131     @Operation(summary = "导出用户")
132     @PreAuthorize("@ss.hasPermission('system:user:export')")
133     @ApiAccessLog(operateType = EXPORT)
134     public void exportUserList(@Validated UserPageReqVO exportReqVO,
135                                HttpServletResponse response) throws IOException {
136         exportReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
137         List<AdminUserDO> list = userService.getUserPage(exportReqVO).getList();
138         // 输出 Excel
139         Map<Long, DeptDO> deptMap = deptService.getDeptMap(
140                 convertList(list, AdminUserDO::getDeptId));
141         ExcelUtils.write(response, "用户数据.xls", "数据", UserRespVO.class,
142                 UserConvert.INSTANCE.convertList(list, deptMap));
143     }
144
145     @GetMapping("/get-import-template")
146     @Operation(summary = "获得导入用户模板")
147     public void importTemplate(HttpServletResponse response) throws IOException {
148         // 手动创建导出 demo
149         List<UserImportExcelVO> list = Arrays.asList(
150                 UserImportExcelVO.builder().username("yunai").deptId(1L).email("yunai@iocoder.cn").mobile("15601691300")
151                         .nickname("平台").status(CommonStatusEnum.ENABLE.getStatus()).sex(SexEnum.MALE.getSex()).build(),
152                 UserImportExcelVO.builder().username("yuanma").deptId(2L).email("yuanma@iocoder.cn").mobile("15601701300")
153                         .nickname("源码").status(CommonStatusEnum.DISABLE.getStatus()).sex(SexEnum.FEMALE.getSex()).build()
154         );
155         // 输出
156         ExcelUtils.write(response, "用户导入模板.xls", "用户列表", UserImportExcelVO.class, list);
157     }
158
159     @PostMapping("/import")
160     @Operation(summary = "导入用户")
161     @Parameters({
162             @Parameter(name = "file", description = "Excel 文件", required = true),
163             @Parameter(name = "updateSupport", description = "是否支持更新,默认为 false", example = "true")
164     })
165     @PreAuthorize("@ss.hasPermission('system:user:import')")
166     public CommonResult<UserImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
167                                                       @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
168         List<UserImportExcelVO> list = ExcelUtils.read(file, UserImportExcelVO.class);
169         return success(userService.importUserList(list, updateSupport));
170     }
171
172 }