feat: 添加用户导出接口

This commit is contained in:
郝先瑞 2022-04-12 00:10:57 +08:00
parent 74dbbf246b
commit cf23c1281f
8 changed files with 143 additions and 18 deletions

View File

@ -16,9 +16,11 @@ public enum GenderEnum implements IBaseEnum<Integer> {
UNKNOWN(0, "未知");
@Getter
// @EnumValue // Mybatis-Plus 提供注解表示插入数据库时插入该值
private Integer value;
@Getter
// @JsonValue // 表示对枚举序列化时返回此字段
private String label;
GenderEnum(Integer value, String label) {

View File

@ -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<UserExportVO> exportUserList = iSysUserService.listExportUsers(queryParams);
EasyExcel.write(response.getOutputStream(), UserExportVO.class)
.sheet("用户列表")
.doWrite(exportUserList);
}
}

View File

@ -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<SysUser> {
*/
AuthUserDTO getAuthInfoByUsername(String username);
/**
* 获取导出用户列表
*
* @param queryParams
* @return
*/
@DataPermission(deptAlias = "d")
List<UserExportVO> listExportUsers(UserPageQuery queryParams);
}

View File

@ -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 <a href="mailto:xianrui0365@163.com">haoxr</a>
* @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;
}

View File

@ -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;

View File

@ -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<SysUser> {
* @return
*/
String importUsers(InputStream inputStream, UserImportForm userImportForm);
/**
* 获取导出用户列表
*
* @param queryParams
* @return
*/
List<UserExportVO> listExportUsers(UserPageQuery queryParams);
}

View File

@ -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<SysUserMapper, SysUser> impl
Assert.isTrue(CollectionUtil.isNotEmpty(list), "未检测到任何数据");
// 有效数据列表
// 有效数据集合
List<UserImportForm.UserItem> validDataList = list.stream()
.filter(item -> StrUtil.isNotBlank(item.getUsername()))
.collect(Collectors.toList());
@ -235,4 +235,16 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
}
/**
* 获取导出用户列表
*
* @param queryParams
* @return
*/
@Override
public List<UserExportVO> listExportUsers(UserPageQuery queryParams) {
List<UserExportVO> list = this.baseMapper.listExportUsers(queryParams);
return list;
}
}

View File

@ -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 @@
<where>
u.deleted = 0
<if test='queryParams.keywords!=null and queryParams.keywords.trim() neq ""'>
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},'%'))
</if>
<if test='queryParams.status!=null'>
AND u.status = #{queryParams.status}
@ -103,4 +107,37 @@
WHERE
t1.username = #{username} and t1.deleted=0
</select>
<!-- 获取用户导出列表 -->
<select id="listExportUsers" resultType="com.youlai.admin.pojo.vo.user.UserExportVO">
SELECT
u.username,
u.nickname,
u.mobile,
CASE u.gender
WHEN 1 THEN '男'
WHEN 2 THEN '女'
ELSE '未知'
END gender,
d.NAME AS dept_name,
u.gmt_create
FROM
sys_user u
LEFT JOIN sys_dept d ON u.dept_id = d.id
<where>
u.deleted = 0
<if test='keywords!=null and keywords.trim() neq ""'>
AND (u.username LIKE CONCAT('%',#{keywords},'%')
OR u.nickname LIKE CONCAT('%',#{keywords},'%')
OR u.mobile LIKE CONCAT('%',#{keywords},'%'))
</if>
<if test='status!=null'>
AND u.status = #{status}
</if>
<if test='deptId!=null'>
AND concat(',',concat(d.tree_path,',',d.id),',') like concat('%,',#{deptId},',%')
</if>
</where>
GROUP BY u.id
</select>
</mapper>