From c1ceab7d4a9281f9ef308b63edf145cea0f51f5c Mon Sep 17 00:00:00 2001 From: liuchen864 <23082234@qq.com> Date: Thu, 12 Oct 2023 08:45:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BB=E5=AD=90=E8=A1=A8=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=B0=81=E8=A3=85=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../excel/core/listener/ExcelListener.java | 22 ++++++++++--------- .../controller/user/UserController.java | 7 ++---- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/win-framework/win-spring-boot-starter-excel/src/main/java/com/win/framework/excel/core/listener/ExcelListener.java b/win-framework/win-spring-boot-starter-excel/src/main/java/com/win/framework/excel/core/listener/ExcelListener.java index 73749217..39c974b0 100644 --- a/win-framework/win-spring-boot-starter-excel/src/main/java/com/win/framework/excel/core/listener/ExcelListener.java +++ b/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 extends AnalysisEventListener { /** * excel数据 */ - private List dataList; + private List dataList; /** * 判断重复的属性方法 @@ -52,13 +52,14 @@ public class ExcelListener extends AnalysisEventListener { /** * 构造函数 */ - public ExcelListener(Class mainClass) { + @SuppressWarnings("unchecked") + public ExcelListener() { dataList = new ArrayList<>(); - this.mainClass = mainClass; + this.mainClass = (Class) ((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 extends AnalysisEventListener { /** * 判断数据是否存在 */ - 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 extends AnalysisEventListener { * 添加数据 * @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 extends AnalysisEventListener { 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); } diff --git a/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/controller/user/UserController.java b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/controller/user/UserController.java index 406b45f2..4289995b 100644 --- a/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/controller/user/UserController.java +++ b/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 importExcel(@RequestParam("file") MultipartFile file, @RequestParam(value = "mode") Integer mode, @RequestParam(value = "updatePart") Boolean updatePart) throws Exception { - ExcelListener listener = new ExcelListener<>(); - ExcelUtils.read(file, UserImportExcelVO.class, listener); - List dataList = listener.getDataList(); - return success(userService.importUserList(dataList, mode, updatePart)); + List list = ExcelUtils.read(file, UserImportExcelVO.class); + return success(userService.importUserList(list, mode, updatePart)); } }