refactor: 用户导入优化
This commit is contained in:
24
src/main/java/com/youlai/system/common/util/ExcelUtils.java
Normal file
24
src/main/java/com/youlai/system/common/util/ExcelUtils.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package com.youlai.system.common.util;
|
||||
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import com.youlai.system.framework.easyexcel.ExcelResult;
|
||||
import com.youlai.system.framework.easyexcel.MyAnalysisEventListener;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* Excel 工具类
|
||||
*
|
||||
* @author: haoxr
|
||||
* @date: 2023/03/01
|
||||
*/
|
||||
public class ExcelUtils {
|
||||
|
||||
public static <T> ExcelResult importExcel(InputStream is, Class clazz, MyAnalysisEventListener<T> listener) {
|
||||
EasyExcel.read(is, clazz, listener).sheet().doRead();
|
||||
ExcelResult excelResult = listener.getResult();
|
||||
return excelResult;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.youlai.system.framework.easyexcel;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Excel 读取结果
|
||||
*
|
||||
* @author: haoxr
|
||||
* @date: 2023/03/01
|
||||
*/
|
||||
public interface ExcelResult<T> {
|
||||
|
||||
List<T> getList();
|
||||
|
||||
String getMsg();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.youlai.system.framework.easyexcel;
|
||||
|
||||
import com.alibaba.excel.event.AnalysisEventListener;
|
||||
|
||||
/**
|
||||
* 自定义解析结果监听器
|
||||
*
|
||||
* @author: haoxr
|
||||
* @date: 2023/03/01
|
||||
*/
|
||||
public abstract class MyAnalysisEventListener<T> extends AnalysisEventListener<T> {
|
||||
public abstract ExcelResult<T> getResult();
|
||||
}
|
||||
@@ -1,27 +1,90 @@
|
||||
package com.youlai.system.listener;
|
||||
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.alibaba.excel.context.AnalysisContext;
|
||||
import com.alibaba.excel.event.AnalysisEventListener;
|
||||
import com.youlai.system.pojo.dto.UserImportDTO;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Component;
|
||||
import com.alibaba.excel.util.ListUtils;
|
||||
import com.youlai.system.framework.easyexcel.ExcelResult;
|
||||
import com.youlai.system.framework.easyexcel.MyAnalysisEventListener;
|
||||
import com.youlai.system.pojo.vo.UserImportVO;
|
||||
import com.youlai.system.service.SysUserService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户导入监听器
|
||||
* <p>
|
||||
* https://easyexcel.opensource.alibaba.com/docs/current/quickstart/read
|
||||
*
|
||||
* @author haoxr
|
||||
* @date 2022/4/10 20:49
|
||||
*/
|
||||
@Component
|
||||
@Scope("prototype")
|
||||
public class UserImportListener extends AnalysisEventListener<UserImportDTO.UserItem> {
|
||||
@Slf4j
|
||||
public class UserImportListener extends MyAnalysisEventListener<UserImportVO> {
|
||||
|
||||
/**
|
||||
* 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收
|
||||
*/
|
||||
private static final int BATCH_COUNT = 100;
|
||||
|
||||
/**
|
||||
* 缓存的数据
|
||||
*/
|
||||
private List<UserImportVO> cachedUserList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
|
||||
|
||||
|
||||
/**
|
||||
* 部门ID
|
||||
*/
|
||||
private final Long deptId;
|
||||
|
||||
private final SysUserService userService;
|
||||
|
||||
public UserImportListener(Long deptId) {
|
||||
this.deptId = deptId;
|
||||
this.userService = SpringUtil.getBean(SysUserService.class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 每一条数据解析都会来调用
|
||||
*
|
||||
* @param userImportVO 一行数据,类似于 {@link AnalysisContext#readRowHolder()}
|
||||
* @param analysisContext
|
||||
*/
|
||||
@Override
|
||||
public void invoke(UserImportDTO.UserItem userItem, AnalysisContext analysisContext) {
|
||||
public void invoke(UserImportVO userImportVO, AnalysisContext analysisContext) {
|
||||
log.info("解析到一条用户数据:{}", JSONUtil.toJsonStr(userImportVO));
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 所有数据解析完成会来调用
|
||||
*
|
||||
* @param analysisContext
|
||||
*/
|
||||
@Override
|
||||
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExcelResult<UserImportVO> getResult() {
|
||||
return new ExcelResult<UserImportVO>() {
|
||||
@Override
|
||||
public List<UserImportVO> getList() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMsg() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
package com.youlai.system.pojo.dto;
|
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户导入对象
|
||||
*
|
||||
* @author haoxr
|
||||
* @date 2022/4/10
|
||||
*/
|
||||
@Data
|
||||
public class UserImportDTO {
|
||||
|
||||
/**
|
||||
* 部门ID
|
||||
*/
|
||||
private Long deptId;
|
||||
|
||||
/**
|
||||
* 角色ID
|
||||
*/
|
||||
private String roleIds;
|
||||
|
||||
private MultipartFile file;
|
||||
|
||||
/**
|
||||
* 导入的用户列表
|
||||
*/
|
||||
private List<UserItem> userList;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class UserItem {
|
||||
|
||||
@ExcelProperty(value = "用户名")
|
||||
private String username;
|
||||
|
||||
@ExcelProperty(value = "用户昵称")
|
||||
private String nickname;
|
||||
|
||||
@ExcelProperty(value = "性别")
|
||||
private String gender;
|
||||
|
||||
@ExcelProperty(value = "手机号码")
|
||||
private String mobile;
|
||||
|
||||
@ExcelProperty(value = "邮箱")
|
||||
private String email;
|
||||
}
|
||||
|
||||
}
|
||||
30
src/main/java/com/youlai/system/pojo/vo/UserImportVO.java
Normal file
30
src/main/java/com/youlai/system/pojo/vo/UserImportVO.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package com.youlai.system.pojo.vo;
|
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 用户导入对象
|
||||
*
|
||||
* @author haoxr
|
||||
* @date 2022/4/10
|
||||
*/
|
||||
@Data
|
||||
public class UserImportVO {
|
||||
|
||||
@ExcelProperty(value = "用户名")
|
||||
private String username;
|
||||
|
||||
@ExcelProperty(value = "用户昵称")
|
||||
private String nickname;
|
||||
|
||||
@ExcelProperty(value = "性别")
|
||||
private String gender;
|
||||
|
||||
@ExcelProperty(value = "手机号码")
|
||||
private String mobile;
|
||||
|
||||
@ExcelProperty(value = "邮箱")
|
||||
private String email;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user