Browse Source

主子表导入数据封装。

master
刘忱 2 years ago
parent
commit
c1ceab7d4a
  1. 22
      win-framework/win-spring-boot-starter-excel/src/main/java/com/win/framework/excel/core/listener/ExcelListener.java
  2. 7
      win-module-system/win-module-system-biz/src/main/java/com/win/module/system/controller/user/UserController.java

22
win-framework/win-spring-boot-starter-excel/src/main/java/com/win/framework/excel/core/listener/ExcelListener.java

@ -27,7 +27,7 @@ public class ExcelListener<T> extends AnalysisEventListener<T> {
/**
* excel数据
*/
private List<Object> dataList;
private List<T> dataList;
/**
* 判断重复的属性方法
@ -52,13 +52,14 @@ public class ExcelListener<T> extends AnalysisEventListener<T> {
/**
* 构造函数
*/
public ExcelListener(Class<?> mainClass) {
@SuppressWarnings("unchecked")
public ExcelListener() {
dataList = new ArrayList<>();
this.mainClass = mainClass;
this.mainClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
@Override
public void invoke(T object, AnalysisContext analysisContext) {
public void invoke(Object object, AnalysisContext analysisContext) {
if(this.methodName == null) {
String methodName = this.getOnlyOneAnnotation();
if(methodName == null) {
@ -84,9 +85,9 @@ public class ExcelListener<T> extends AnalysisEventListener<T> {
/**
* 判断数据是否存在
*/
private Object checkDataIsExist(T object) {
for(Object obj : dataList) {
Object methodValue1 = ReflectUtil.invoke(obj, "get" + this.methodName);
private T checkDataIsExist(Object object) {
for(T obj : dataList) {
T methodValue1 = ReflectUtil.invoke(obj, "get" + this.methodName);
Object methodValue2 = ReflectUtil.invoke(object, "get" + this.methodName);
if(methodValue1.equals(methodValue2)) {
return obj;
@ -99,8 +100,9 @@ public class ExcelListener<T> extends AnalysisEventListener<T> {
* 添加数据
* @param vo
*/
private void buildEntity(T vo) {
Object mainObject = this.checkDataIsExist(vo);
@SuppressWarnings("unchecked")
private void buildEntity(Object vo) {
T mainObject = this.checkDataIsExist(vo);
Object subObject;
try {//创建子数据对象
subObject = this.subClass.getDeclaredConstructors()[0].newInstance((Object) null);
@ -110,7 +112,7 @@ public class ExcelListener<T> extends AnalysisEventListener<T> {
BeanUtil.copyProperties(vo, subObject);
if(mainObject == null) {//list中不存在主数据
try {//创建主数据对象
mainObject = this.mainClass.getDeclaredConstructors()[0].newInstance((Object) null);
mainObject = (T) this.mainClass.getDeclaredConstructors()[0].newInstance((Object) null);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}

7
win-module-system/win-module-system-biz/src/main/java/com/win/module/system/controller/user/UserController.java

@ -5,7 +5,6 @@ import com.win.framework.common.enums.CommonStatusEnum;
import com.win.framework.common.pojo.CommonResult;
import com.win.framework.common.pojo.PageResult;
import com.win.framework.common.util.collection.MapUtils;
import com.win.framework.excel.core.listener.ExcelListener;
import com.win.framework.excel.core.util.ExcelUtils;
import com.win.framework.operatelog.core.annotations.OperateLog;
import com.win.module.system.controller.user.vo.user.*;
@ -186,10 +185,8 @@ public class UserController {
})
@PreAuthorize("@ss.hasPermission('system:user:import')")
public CommonResult<UserImportRespVO> importExcel(@RequestParam("file") MultipartFile file, @RequestParam(value = "mode") Integer mode, @RequestParam(value = "updatePart") Boolean updatePart) throws Exception {
ExcelListener<UserImportExcelVO> listener = new ExcelListener<>();
ExcelUtils.read(file, UserImportExcelVO.class, listener);
List<UserImportExcelVO> dataList = listener.getDataList();
return success(userService.importUserList(dataList, mode, updatePart));
List<UserImportExcelVO> list = ExcelUtils.read(file, UserImportExcelVO.class);
return success(userService.importUserList(list, mode, updatePart));
}
}

Loading…
Cancel
Save