From cf23c1281fbb9955296233e6726a010471c0482f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=9D=E5=85=88=E7=91=9E?= <1490493387@qq.com> Date: Tue, 12 Apr 2022 00:10:57 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../youlai/admin/common/enums/GenderEnum.java | 2 + .../admin/controller/UserController.java | 28 ++++++++---- .../youlai/admin/mapper/SysUserMapper.java | 11 ++++- .../admin/pojo/vo/user/UserExportVO.java | 45 +++++++++++++++++++ .../youlai/admin/pojo/vo/user/UserPageVO.java | 4 +- .../youlai/admin/service/ISysUserService.java | 10 +++++ .../service/impl/SysUserServiceImpl.java | 16 ++++++- .../main/resources/mapper/SysUserMapper.xml | 45 +++++++++++++++++-- 8 files changed, 143 insertions(+), 18 deletions(-) create mode 100644 youlai-admin/admin-boot/src/main/java/com/youlai/admin/pojo/vo/user/UserExportVO.java diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/common/enums/GenderEnum.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/common/enums/GenderEnum.java index 5b278d088..b32bdfc6e 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/common/enums/GenderEnum.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/common/enums/GenderEnum.java @@ -16,9 +16,11 @@ public enum GenderEnum implements IBaseEnum { UNKNOWN(0, "未知"); @Getter + // @EnumValue // Mybatis-Plus 提供注解表示插入数据库时插入该值 private Integer value; @Getter + // @JsonValue // 表示对枚举序列化时返回此字段 private String label; GenderEnum(Integer value, String label) { diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/UserController.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/UserController.java index 58fe51df5..2055f7896 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/UserController.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/UserController.java @@ -6,12 +6,12 @@ import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; -import com.youlai.admin.component.listener.excel.UserImportListener; import com.youlai.admin.dto.AuthUserDTO; import com.youlai.admin.pojo.entity.SysUser; import com.youlai.admin.pojo.form.UserImportForm; import com.youlai.admin.pojo.query.UserPageQuery; import com.youlai.admin.pojo.vo.user.LoginUserVO; +import com.youlai.admin.pojo.vo.user.UserExportVO; import com.youlai.admin.pojo.vo.user.UserFormVO; import com.youlai.admin.pojo.vo.user.UserPageVO; import com.youlai.admin.service.ISysPermissionService; @@ -23,7 +23,6 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import lombok.RequiredArgsConstructor; -import lombok.SneakyThrows; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; @@ -33,6 +32,7 @@ import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.net.URLEncoder; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @@ -47,8 +47,7 @@ import java.util.stream.Collectors; @RestController @RequestMapping("/api/v1/users") @RequiredArgsConstructor -public class UserController { - +public class UserController { private final ISysUserService iSysUserService; private final PasswordEncoder passwordEncoder; private final ISysPermissionService iSysPermissionService; @@ -146,7 +145,7 @@ public class UserController { public void downloadTemplate(HttpServletResponse response) throws IOException { String fileName = "用户导入模板.xlsx"; response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); - response.setHeader("Content-Disposition", "attachment; filename=" + fileName); + response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName,"UTF-8")); String fileClassPath = "excel-templates" + File.separator + fileName; InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(fileClassPath); @@ -157,19 +156,30 @@ public class UserController { excelWriter.finish(); } - @SneakyThrows @ApiOperation("导入用户") @PostMapping("/_import") - public Result importUsers(@RequestBody UserImportForm userImportForm, MultipartFile file) { + public Result importUsers(@RequestBody UserImportForm userImportForm, MultipartFile file) throws IOException { InputStream inputStream = file.getInputStream(); - - String errMsg = iSysUserService.importUsers(inputStream,userImportForm); + String errMsg = iSysUserService.importUsers(inputStream, userImportForm); if (StrUtil.isNotBlank(errMsg)) { return Result.failed(errMsg); } return Result.success(); + } + @ApiOperation("导出用户") + @GetMapping("/_export") + public void exportUsers(UserPageQuery queryParams, HttpServletResponse response) throws IOException { + String fileName = "用户列表.xlsx"; + response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); + response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName,"UTF-8")); + List exportUserList = iSysUserService.listExportUsers(queryParams); + + EasyExcel.write(response.getOutputStream(), UserExportVO.class) + .sheet("用户列表") + .doWrite(exportUserList); + } } diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/mapper/SysUserMapper.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/mapper/SysUserMapper.java index 7c2ecc71b..b6be8f178 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/mapper/SysUserMapper.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/mapper/SysUserMapper.java @@ -5,10 +5,12 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.youlai.admin.dto.AuthUserDTO; import com.youlai.admin.pojo.entity.SysUser; import com.youlai.admin.pojo.query.UserPageQuery; +import com.youlai.admin.pojo.vo.user.UserExportVO; import com.youlai.admin.pojo.vo.user.UserFormVO; import com.youlai.admin.pojo.vo.user.UserPageVO; import com.youlai.common.mybatis.annotation.DataPermission; import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; import java.util.List; @@ -47,5 +49,12 @@ public interface SysUserMapper extends BaseMapper { */ AuthUserDTO getAuthInfoByUsername(String username); - + /** + * 获取导出用户列表 + * + * @param queryParams + * @return + */ + @DataPermission(deptAlias = "d") + List listExportUsers(UserPageQuery queryParams); } diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/pojo/vo/user/UserExportVO.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/pojo/vo/user/UserExportVO.java new file mode 100644 index 000000000..d036ec8c8 --- /dev/null +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/pojo/vo/user/UserExportVO.java @@ -0,0 +1,45 @@ +package com.youlai.admin.pojo.vo.user; + +import com.alibaba.excel.annotation.ExcelProperty; +import com.alibaba.excel.annotation.format.DateTimeFormat; +import com.alibaba.excel.annotation.write.style.ColumnWidth; +import lombok.Data; + +import java.time.LocalDateTime; +import java.util.Date; + +/** + * 用户导出视图对象 + * + * @author haoxr + * @date 2022/4/11 8:46 + */ + +@Data +@ColumnWidth(20) +public class UserExportVO { + + @ExcelProperty(value = "用户名") + private String username; + + @ExcelProperty(value = "用户昵称") + private String nickname; + + @ExcelProperty(value = "部门") + private String deptName; + + @ExcelProperty(value = "性别") + private String gender; + + @ExcelProperty(value = "手机号码") + private String mobile; + + @ExcelProperty(value = "邮箱") + private String email; + + @ExcelProperty(value = "创建时间") + @DateTimeFormat("yyyy/MM/dd HH:mm:ss") + private LocalDateTime gmtCreate; + + +} diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/pojo/vo/user/UserPageVO.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/pojo/vo/user/UserPageVO.java index 98c5a3530..1ab2eca34 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/pojo/vo/user/UserPageVO.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/pojo/vo/user/UserPageVO.java @@ -29,8 +29,8 @@ public class UserPageVO { @ApiModelProperty("手机号") private String mobile; - @ApiModelProperty("性别(1:男;2:女)") - private Integer gender; + @ApiModelProperty("性别") + private String gender; @ApiModelProperty("用户头像地址") private String avatar; diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/ISysUserService.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/ISysUserService.java index da773dc30..9bd26ff82 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/ISysUserService.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/ISysUserService.java @@ -7,10 +7,12 @@ import com.youlai.admin.dto.AuthUserDTO; import com.youlai.admin.pojo.entity.SysUser; import com.youlai.admin.pojo.form.UserImportForm; import com.youlai.admin.pojo.query.UserPageQuery; +import com.youlai.admin.pojo.vo.user.UserExportVO; import com.youlai.admin.pojo.vo.user.UserFormVO; import com.youlai.admin.pojo.vo.user.UserPageVO; import java.io.InputStream; +import java.util.List; /** * 用户业务接口 @@ -66,4 +68,12 @@ public interface ISysUserService extends IService { * @return */ String importUsers(InputStream inputStream, UserImportForm userImportForm); + + /** + * 获取导出用户列表 + * + * @param queryParams + * @return + */ + List listExportUsers(UserPageQuery queryParams); } diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/impl/SysUserServiceImpl.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/impl/SysUserServiceImpl.java index 4e3b873db..09667388b 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/impl/SysUserServiceImpl.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/impl/SysUserServiceImpl.java @@ -19,6 +19,7 @@ import com.youlai.admin.mapper.SysUserMapper; import com.youlai.admin.pojo.entity.SysUserRole; import com.youlai.admin.pojo.form.UserImportForm; import com.youlai.admin.pojo.query.UserPageQuery; +import com.youlai.admin.pojo.vo.user.UserExportVO; import com.youlai.admin.pojo.vo.user.UserFormVO; import com.youlai.admin.pojo.vo.user.UserPageVO; import com.youlai.admin.service.ISysUserRoleService; @@ -169,8 +170,7 @@ public class SysUserServiceImpl extends ServiceImpl impl Assert.isTrue(CollectionUtil.isNotEmpty(list), "未检测到任何数据"); - - // 有效数据列表 + // 有效数据集合 List validDataList = list.stream() .filter(item -> StrUtil.isNotBlank(item.getUsername())) .collect(Collectors.toList()); @@ -235,4 +235,16 @@ public class SysUserServiceImpl extends ServiceImpl impl } + /** + * 获取导出用户列表 + * + * @param queryParams + * @return + */ + @Override + public List listExportUsers(UserPageQuery queryParams) { + List list = this.baseMapper.listExportUsers(queryParams); + return list; + } + } diff --git a/youlai-admin/admin-boot/src/main/resources/mapper/SysUserMapper.xml b/youlai-admin/admin-boot/src/main/resources/mapper/SysUserMapper.xml index e369f0370..9d905b2d6 100644 --- a/youlai-admin/admin-boot/src/main/resources/mapper/SysUserMapper.xml +++ b/youlai-admin/admin-boot/src/main/resources/mapper/SysUserMapper.xml @@ -11,7 +11,11 @@ u.username, u.nickname, u.mobile, - u.gender, + CASE u.gender + WHEN 1 THEN '男' + WHEN 2 THEN '女' + ELSE '未知' + END gender, u.avatar, u.STATUS, d.NAME AS dept_name, @@ -25,9 +29,9 @@ u.deleted = 0 - AND (u.username like concat('%',#{queryParams.keywords},'%') - OR u.nickname like concat('%',#{queryParams.keywords},'%') - OR u.mobile like concat('%',#{queryParams.keywords},'%')) + AND (u.username LIKE CONCAT('%',#{queryParams.keywords},'%') + OR u.nickname LIKE CONCAT('%',#{queryParams.keywords},'%') + OR u.mobile LIKE CONCAT('%',#{queryParams.keywords},'%')) AND u.status = #{queryParams.status} @@ -103,4 +107,37 @@ WHERE t1.username = #{username} and t1.deleted=0 + + +