| | |
| | | import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.*; |
| | | import cn.iocoder.yudao.module.system.convert.user.UserConvert; |
| | | import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO; |
| | | import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO; |
| | | import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; |
| | | import cn.iocoder.yudao.module.system.enums.common.SexEnum; |
| | | import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptListReqVO; |
| | | import cn.iocoder.yudao.module.system.service.dept.DeptService; |
| | | import cn.iocoder.yudao.module.system.service.permission.PermissionService; |
| | | import cn.iocoder.yudao.module.system.service.permission.RoleService; |
| | | import cn.iocoder.yudao.module.system.service.user.AdminUserService; |
| | | import io.swagger.v3.oas.annotations.Operation; |
| | | import io.swagger.v3.oas.annotations.Parameter; |
| | |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT; |
| | | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.USER_IMPORT_FILE_EMPTY; |
| | | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.USER_IMPORT_FILE_INVALID; |
| | | |
| | | @Tag(name = "管理后台 - 用户") |
| | | @RestController |
| | |
| | | private AdminUserService userService; |
| | | @Resource |
| | | private DeptService deptService; |
| | | @Resource |
| | | private RoleService roleService; |
| | | @Resource |
| | | private PermissionService permissionService; |
| | | |
| | | @PostMapping("/create") |
| | | @Operation(summary = "新增用户") |
| | |
| | | |
| | | @GetMapping("/page") |
| | | @Operation(summary = "获得用户分页列表") |
| | | @PreAuthorize("@ss.hasPermission('system:user:query')") |
| | | public CommonResult<PageResult<UserRespVO>> getUserPage(@Valid UserPageReqVO pageReqVO) { |
| | | // 获得用户分页列表 |
| | | PageResult<AdminUserDO> pageResult = userService.getUserPage(pageReqVO); |
| | |
| | | @GetMapping("/list") |
| | | @Operation(summary = "获得用户详情列表") |
| | | @Parameter(name = "ids", description = "编号列表", required = true, example = "[1024]") |
| | | @PreAuthorize("@ss.hasPermission('system:user:query')") |
| | | public CommonResult<List<UserRespVO>> getUserList(@RequestParam("ids") List<Long> ids) { |
| | | List<AdminUserDO> list = userService.getUserList(ids); |
| | | if (CollUtil.isEmpty(list)) { |
| | |
| | | return success(UserConvert.INSTANCE.convertSimpleList(list, deptMap)); |
| | | } |
| | | |
| | | @GetMapping("/list-by-role-code") |
| | | @Operation(summary = "获得指定角色编码的用户精简信息列表", description = "只包含被开启的用户,主要用于前端的下拉选项") |
| | | @Parameter(name = "code", description = "角色编码", required = true, example = "production_submit_work") |
| | | public CommonResult<List<UserSimpleRespVO>> getUserListByRoleCode(@RequestParam("code") String code) { |
| | | // 1. 查询角色 |
| | | RoleDO role = roleService.getRoleByCode(code); |
| | | if (role == null) { |
| | | return success(Collections.emptyList()); |
| | | } |
| | | // 2. 查询拥有该角色的用户编号 |
| | | Set<Long> userIds = permissionService.getUserRoleIdListByRoleId(Collections.singletonList(role.getId())); |
| | | if (CollUtil.isEmpty(userIds)) { |
| | | return success(Collections.emptyList()); |
| | | } |
| | | // 3. 查询用户,只保留开启状态 |
| | | List<AdminUserDO> list = userService.getUserList(userIds).stream() |
| | | .filter(user -> CommonStatusEnum.ENABLE.getStatus().equals(user.getStatus())) |
| | | .collect(Collectors.toList()); |
| | | if (CollUtil.isEmpty(list)) { |
| | | return success(Collections.emptyList()); |
| | | } |
| | | // 4. 拼接部门数据 |
| | | Map<Long, DeptDO> deptMap = deptService.getDeptMap(convertList(list, AdminUserDO::getDeptId)); |
| | | return success(UserConvert.INSTANCE.convertSimpleList(list, deptMap)); |
| | | } |
| | | |
| | | @GetMapping("/get") |
| | | @Operation(summary = "获得用户详情") |
| | | @Parameter(name = "id", description = "编号", required = true, example = "1024") |
| | | @PreAuthorize("@ss.hasPermission('system:user:query')") |
| | | public CommonResult<UserRespVO> getUser(@RequestParam("id") Long id) { |
| | | AdminUserDO user = userService.getUser(id); |
| | | if (user == null) { |
| | |
| | | } |
| | | |
| | | @GetMapping("/get-import-template") |
| | | @Operation(summary = "获得导入用户模板") |
| | | @Operation(summary = "获得导入用户模板", description = "部门按部门名称填写,示例取自当前系统真实部门") |
| | | public void importTemplate(HttpServletResponse response) throws IOException { |
| | | // 手动创建导出 demo |
| | | // 示例取当前真实部门名称,避免使用者照抄官方 demo 导致「部门不存在」 |
| | | List<DeptDO> depts = deptService.getDeptList(new DeptListReqVO()); |
| | | String sampleDept1 = CollUtil.isNotEmpty(depts) ? depts.get(0).getName() : "请填写部门名称"; |
| | | String sampleDept2 = depts.size() > 1 ? depts.get(1).getName() : sampleDept1; |
| | | |
| | | List<UserImportExcelVO> list = Arrays.asList( |
| | | UserImportExcelVO.builder().username("yunai").deptId(1L).email("yunai@iocoder.cn").mobile("15601691300") |
| | | .nickname("芋道").status(CommonStatusEnum.ENABLE.getStatus()).sex(SexEnum.MALE.getSex()).build(), |
| | | UserImportExcelVO.builder().username("yuanma").deptId(2L).email("yuanma@iocoder.cn").mobile("15601701300") |
| | | .nickname("源码").status(CommonStatusEnum.DISABLE.getStatus()).sex(SexEnum.FEMALE.getSex()).build() |
| | | UserImportExcelVO.builder().username("zhangsan").deptName(sampleDept1).email("zhangsan@example.com") |
| | | .mobile("13800000001").nickname("张三") |
| | | .status(CommonStatusEnum.ENABLE.getStatus()).sex(SexEnum.MALE.getSex()).build(), |
| | | UserImportExcelVO.builder().username("lisi").deptName(sampleDept2).email("lisi@example.com") |
| | | .mobile("13800000002").nickname("李四") |
| | | .status(CommonStatusEnum.ENABLE.getStatus()).sex(SexEnum.FEMALE.getSex()).build() |
| | | ); |
| | | // 输出 |
| | | ExcelUtils.write(response, "用户导入模板.xls", "用户列表", UserImportExcelVO.class, list); |
| | | // 日期列按文本输出,避免显示成 Excel 序列号 |
| | | ExcelUtils.writeTemplate(response, "用户导入模板.xls", "用户列表", UserImportExcelVO.class, list); |
| | | } |
| | | |
| | | @PostMapping("/import") |
| | |
| | | @PreAuthorize("@ss.hasPermission('system:user:import')") |
| | | public CommonResult<UserImportRespVO> importExcel(@RequestParam("file") MultipartFile file, |
| | | @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception { |
| | | List<UserImportExcelVO> list = ExcelUtils.read(file, UserImportExcelVO.class); |
| | | // 空文件 / 非法文件给出明确提示,避免底层异常变成「系统异常」 |
| | | if (file == null || file.isEmpty()) { |
| | | throw exception(USER_IMPORT_FILE_EMPTY); |
| | | } |
| | | List<UserImportExcelVO> list; |
| | | try { |
| | | list = ExcelUtils.read(file, UserImportExcelVO.class); |
| | | } catch (Exception ex) { |
| | | throw exception(USER_IMPORT_FILE_INVALID); |
| | | } |
| | | return success(userService.importUserList(list, updateSupport)); |
| | | } |
| | | |
| | |
| | | |
| | | @GetMapping("/list-by-nickname") |
| | | @Operation(summary = "按昵称模糊搜索用户精简信息", description = "用于加好友等场景;免鉴权;当前仅按昵称匹配") |
| | | @Parameter(name = "nickname", description = "昵称关键词", required = true, example = "芋道") |
| | | @Parameter(name = "nickname", description = "昵称关键词", required = true, example = "超级管理员") |
| | | public CommonResult<List<UserSimpleRespVO>> getSimpleUserListByNickname(@RequestParam("nickname") String nickname) { |
| | | if (StrUtil.isBlank(nickname)) { |
| | | return success(Collections.emptyList()); |