| | |
| | | import java.io.IOException; |
| | | |
| | | import java.time.LocalDateTime; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.*; |
| | | import java.util.Objects; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | if (!StringUtils.hasText(platform.getDomain())) { |
| | | return ResponseResult.error("平台域名不能为空"); |
| | | } |
| | | platform.setCreate_time(LocalDateTime.now()); |
| | | |
| | | boolean success = platformService.save(platform); |
| | | if (success) { |
| | |
| | | return ResponseResult.error("上传文件不能为空"); |
| | | } |
| | | |
| | | // 获取数据库中的旧数据 |
| | | List<Platform> oldPlatforms = platformService.list(); |
| | | |
| | | // 使用自定义监听器读取数据(包含行号) |
| | | PlatformExcelListener listener = new PlatformExcelListener(); |
| | | EasyExcel.read(file.getInputStream()) |
| | |
| | | List<Platform> platforms = new ArrayList<>(); |
| | | List<String> errorMessages = new ArrayList<>(); |
| | | |
| | | // 遍历数据并验证(逻辑同方案一) |
| | | // 遍历数据并验证 |
| | | for (ExcelDataWithRow<PlatformExcel> excelData : excelList) { |
| | | int rowNum = excelData.getRowNumber(); |
| | | PlatformExcel excel = excelData.getData(); |
| | | List<String> rowErrors = new ArrayList<>(); |
| | | |
| | | // 验证逻辑 |
| | | if (!StringUtils.hasText(excel.getPlatform_name())) { |
| | | rowErrors.add("平台名称不能为空"); |
| | |
| | | if (!StringUtils.hasText(excel.getDomain())) { |
| | | rowErrors.add("平台域名不能为空"); |
| | | } |
| | | // ... 其他验证 |
| | | |
| | | // 查找类型(仅当类型名称不为空时检查,避免空指针) |
| | | Integer typeId = null; |
| | | if (StringUtils.hasText(excel.getType_name())) { |
| | | Type typeByName = typeService.getTypeByName(excel.getType_name()); |
| | | if (typeByName == null) { |
| | | rowErrors.add("未知的平台类型: " + excel.getType_name()); |
| | | } else { |
| | | typeId = typeByName.getType_id(); |
| | | } |
| | | typeId = typeByName.getType_id(); |
| | | |
| | | }else { |
| | | } else { |
| | | rowErrors.add("平台类型不能为空"); |
| | | } |
| | | |
| | | if (!rowErrors.isEmpty()) { |
| | | errorMessages.add(String.format("第%d行错误: %s", rowNum, String.join(";", rowErrors))); |
| | | } else { |
| | | // 构建Platform对象... |
| | | // 构建Platform对象 |
| | | Platform platform = new Platform(); |
| | | platform.setPlatform_name(excel.getPlatform_name()); |
| | | platform.setDomain(excel.getDomain()); |
| | | platform.setType_id(typeId); |
| | | platform.setCreate_time(LocalDateTime.now()); |
| | | platforms.add(platform); |
| | | |
| | | // 检查重复性 |
| | | boolean isDuplicate = false; |
| | | for (Platform oldPlatform : oldPlatforms) { |
| | | // 判断重复的条件,这里假设平台名称和域名都相同才算重复 |
| | | if (oldPlatform.getPlatform_name().equals(platform.getPlatform_name()) && |
| | | oldPlatform.getDomain().equals(platform.getDomain())) { |
| | | isDuplicate = true; |
| | | break; |
| | | } |
| | | } |
| | | |
| | | // 如果不重复才添加到导入列表 |
| | | if (!isDuplicate) { |
| | | platforms.add(platform); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 错误处理和导入逻辑(同方案一) |
| | | // 错误处理 |
| | | if (!errorMessages.isEmpty()) { |
| | | // 构建清晰的错误信息 |
| | | StringBuilder errorMsg = new StringBuilder(); |
| | | errorMsg.append("导入失败,共").append(errorMessages.size()).append("条数据存在错误:\n"); |
| | | errorMessages.forEach(msg -> errorMsg.append(msg).append("\n")); |
| | | |
| | | return ResponseResult.error(400, errorMsg.toString()); |
| | | } |
| | | |
| | | // 无错误时导入... |
| | | return ResponseResult.success("导入成功,共" + platforms.size() + "条数据"); |
| | | // 无错误时导入 |
| | | if (!platforms.isEmpty()) { |
| | | platformService.saveBatch(platforms); |
| | | return ResponseResult.success("导入成功,新增" + platforms.size() + "条数据"); |
| | | } else { |
| | | return ResponseResult.success("导入完成,没有新增数据(所有数据均已存在)"); |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage()); |
| | | log.error("文件解析失败", e); |
| | | return ResponseResult.error("文件解析失败:" + e.getMessage()); |
| | | } |
| | | } |