Browse Source

上传返回错误excel。

master
刘忱 2 years ago
parent
commit
36cd61eb80
  1. 1
      win-framework/win-spring-boot-starter-excel/src/main/java/com/win/framework/excel/core/util/ExcelUtils.java
  2. 5
      win-module-system/win-module-system-biz/src/main/java/com/win/module/system/controller/user/UserController.java
  3. 2
      win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/user/AdminUserService.java
  4. 12
      win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/user/AdminUserServiceImpl.java

1
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); .sheet(sheetName).doWrite(data);
// 设置 header 和 contentType。写在最后的原因是,避免报错时,响应 contentType 已经被修改了 // 设置 header 和 contentType。写在最后的原因是,避免报错时,响应 contentType 已经被修改了
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8")); 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"); response.setContentType("application/vnd.ms-excel;charset=UTF-8");
} }

5
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") @Parameter(name = "updatePart", description = "部分更新,默认为 true", example = "true")
}) })
@PreAuthorize("@ss.hasPermission('system:user:import')") @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 { public void importExcel(HttpServletResponse response, @RequestParam("file") MultipartFile file, @RequestParam(value = "mode") Integer mode, @RequestParam(value = "updatePart") Boolean updatePart) throws Exception {
List<UserImportExcelVO> list = ExcelUtils.read(file, UserImportExcelVO.class); List<UserImportExcelVO> list = ExcelUtils.read(file, UserImportExcelVO.class);
return success(userService.importUserList(list, mode, updatePart)); List<UserImportExcelVO> errorList = userService.importUserList(list, mode, updatePart);
ExcelUtils.write(response, "用户导入错误数据.xls", "错误列表", UserImportExcelVO.class, errorList);
} }
} }

2
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 是否部分更新 * @param updatePart 是否部分更新
* @return 导入结果 * @return 导入结果
*/ */
UserImportRespVO importUserList(List<UserImportExcelVO> importUsers, Integer mode, boolean updatePart); List<UserImportExcelVO> importUserList(List<UserImportExcelVO> importUsers, Integer mode, boolean updatePart);
/** /**
* 获得指定状态的用户们 * 获得指定状态的用户们

12
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 @Override
@Transactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入 @Transactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入
public UserImportRespVO importUserList(List<UserImportExcelVO> importUsers, Integer mode, boolean updatePart) { public List<UserImportExcelVO> importUserList(List<UserImportExcelVO> importUsers, Integer mode, boolean updatePart) {
if (CollUtil.isEmpty(importUsers)) { if (CollUtil.isEmpty(importUsers)) {
throw exception(USER_IMPORT_LIST_IS_EMPTY); throw exception(USER_IMPORT_LIST_IS_EMPTY);
} }
UserImportRespVO respVO = UserImportRespVO.builder().createUsernames(new ArrayList<>()).updateUsernames(new ArrayList<>()).failureUsernames(new LinkedHashMap<>()).build(); List<UserImportExcelVO> errorList = new ArrayList<>();
importUsers.forEach(importUser -> { importUsers.forEach(importUser -> {
// 校验,判断是否有不符合的原因 // 校验,判断是否有不符合的原因
try { try {
validateUserForCreateOrUpdate(null, null, importUser.getMobile(), importUser.getEmail(), importUser.getDeptId(), null); validateUserForCreateOrUpdate(null, null, importUser.getMobile(), importUser.getEmail(), importUser.getDeptId(), null);
} catch (ServiceException ex) { } catch (ServiceException ex) {
respVO.getFailureUsernames().put(importUser.getUsername(), ex.getMessage()); errorList.add(importUser);
return; return;
} }
// 判断如果不存在,在进行插入 // 判断如果不存在,在进行插入
@ -416,19 +416,17 @@ public class AdminUserServiceImpl implements AdminUserService {
//不存在并非覆盖类型 //不存在并非覆盖类型
if (existUser == null && mode != 3) { if (existUser == null && mode != 3) {
userMapper.insert(UserConvert.INSTANCE.convert(importUser).setPassword(encodePassword(userInitPassword)).setPostIds(new HashSet<>())); // 设置默认密码及空岗位编号数组 userMapper.insert(UserConvert.INSTANCE.convert(importUser).setPassword(encodePassword(userInitPassword)).setPostIds(new HashSet<>())); // 设置默认密码及空岗位编号数组
respVO.getCreateUsernames().add(importUser.getUsername());
} else if (existUser != null && mode != 2) {// 如果存在,判断是否允许更新 } else if (existUser != null && mode != 2) {// 如果存在,判断是否允许更新
AdminUserDO updateUser = UserConvert.INSTANCE.convert(importUser); AdminUserDO updateUser = UserConvert.INSTANCE.convert(importUser);
updateUser.setId(existUser.getId()); updateUser.setId(existUser.getId());
userMapper.updateById(updateUser); userMapper.updateById(updateUser);
respVO.getUpdateUsernames().add(importUser.getUsername());
} }
}); });
//错误不为空并非部分更新,手工回滚 //错误不为空并非部分更新,手工回滚
if(respVO.getUpdateUsernames().isEmpty() && !updatePart) { if(!errorList.isEmpty() && !updatePart) {
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
} }
return respVO; return errorList;
} }
@Override @Override

Loading…
Cancel
Save