From 36cd61eb80ea0d91765165dd1fe07dacf0253a2f Mon Sep 17 00:00:00 2001 From: liuchen864 <23082234@qq.com> Date: Thu, 12 Oct 2023 10:28:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E8=BF=94=E5=9B=9E=E9=94=99?= =?UTF-8?q?=E8=AF=AFexcel=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../win/framework/excel/core/util/ExcelUtils.java | 1 + .../system/controller/user/UserController.java | 5 +++-- .../module/system/service/user/AdminUserService.java | 2 +- .../system/service/user/AdminUserServiceImpl.java | 12 +++++------- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/win-framework/win-spring-boot-starter-excel/src/main/java/com/win/framework/excel/core/util/ExcelUtils.java b/win-framework/win-spring-boot-starter-excel/src/main/java/com/win/framework/excel/core/util/ExcelUtils.java index 183ce3a4..6c868c40 100644 --- a/win-framework/win-spring-boot-starter-excel/src/main/java/com/win/framework/excel/core/util/ExcelUtils.java +++ b/win-framework/win-spring-boot-starter-excel/src/main/java/com/win/framework/excel/core/util/ExcelUtils.java @@ -36,6 +36,7 @@ public class ExcelUtils { .sheet(sheetName).doWrite(data); // 设置 header 和 contentType。写在最后的原因是,避免报错时,响应 contentType 已经被修改了 response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8")); + response.addHeader("excel-count", String.valueOf(data.size())); response.setContentType("application/vnd.ms-excel;charset=UTF-8"); } 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 4289995b..b792e77d 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 @@ -184,9 +184,10 @@ public class UserController { @Parameter(name = "updatePart", description = "部分更新,默认为 true", example = "true") }) @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 { + public void importExcel(HttpServletResponse response, @RequestParam("file") MultipartFile file, @RequestParam(value = "mode") Integer mode, @RequestParam(value = "updatePart") Boolean updatePart) throws Exception { List list = ExcelUtils.read(file, UserImportExcelVO.class); - return success(userService.importUserList(list, mode, updatePart)); + List errorList = userService.importUserList(list, mode, updatePart); + ExcelUtils.write(response, "用户导入错误数据.xls", "错误列表", UserImportExcelVO.class, errorList); } } diff --git a/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/user/AdminUserService.java b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/user/AdminUserService.java index e47e7adf..27ea56ed 100644 --- a/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/user/AdminUserService.java +++ b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/user/AdminUserService.java @@ -191,7 +191,7 @@ public interface AdminUserService { * @param updatePart 是否部分更新 * @return 导入结果 */ - UserImportRespVO importUserList(List importUsers, Integer mode, boolean updatePart); + List importUserList(List importUsers, Integer mode, boolean updatePart); /** * 获得指定状态的用户们 diff --git a/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/user/AdminUserServiceImpl.java b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/user/AdminUserServiceImpl.java index 575439ca..f36e2e1c 100644 --- a/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/user/AdminUserServiceImpl.java +++ b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/user/AdminUserServiceImpl.java @@ -398,17 +398,17 @@ public class AdminUserServiceImpl implements AdminUserService { @Override @Transactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入 - public UserImportRespVO importUserList(List importUsers, Integer mode, boolean updatePart) { + public List importUserList(List importUsers, Integer mode, boolean updatePart) { if (CollUtil.isEmpty(importUsers)) { throw exception(USER_IMPORT_LIST_IS_EMPTY); } - UserImportRespVO respVO = UserImportRespVO.builder().createUsernames(new ArrayList<>()).updateUsernames(new ArrayList<>()).failureUsernames(new LinkedHashMap<>()).build(); + List errorList = new ArrayList<>(); importUsers.forEach(importUser -> { // 校验,判断是否有不符合的原因 try { validateUserForCreateOrUpdate(null, null, importUser.getMobile(), importUser.getEmail(), importUser.getDeptId(), null); } catch (ServiceException ex) { - respVO.getFailureUsernames().put(importUser.getUsername(), ex.getMessage()); + errorList.add(importUser); return; } // 判断如果不存在,在进行插入 @@ -416,19 +416,17 @@ public class AdminUserServiceImpl implements AdminUserService { //不存在并非覆盖类型 if (existUser == null && mode != 3) { userMapper.insert(UserConvert.INSTANCE.convert(importUser).setPassword(encodePassword(userInitPassword)).setPostIds(new HashSet<>())); // 设置默认密码及空岗位编号数组 - respVO.getCreateUsernames().add(importUser.getUsername()); } else if (existUser != null && mode != 2) {// 如果存在,判断是否允许更新 AdminUserDO updateUser = UserConvert.INSTANCE.convert(importUser); updateUser.setId(existUser.getId()); userMapper.updateById(updateUser); - respVO.getUpdateUsernames().add(importUser.getUsername()); } }); //错误不为空并非部分更新,手工回滚 - if(respVO.getUpdateUsernames().isEmpty() && !updatePart) { + if(!errorList.isEmpty() && !updatePart) { TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); } - return respVO; + return errorList; } @Override