From affd7e9f1cc591be36102299cac45b35d9536a17 Mon Sep 17 00:00:00 2001 From: huliguo <2023611923@qq.com> Date: 星期六, 12 七月 2025 15:08:49 +0800 Subject: [PATCH] stream流改for --- src/main/java/com/linghu/controller/CollectController.java | 62 ++++++++++++++- src/main/java/com/linghu/controller/UserController.java | 153 ++++++++++++++++++++------------------ 2 files changed, 138 insertions(+), 77 deletions(-) diff --git a/src/main/java/com/linghu/controller/CollectController.java b/src/main/java/com/linghu/controller/CollectController.java index 3d87085..6bc388f 100644 --- a/src/main/java/com/linghu/controller/CollectController.java +++ b/src/main/java/com/linghu/controller/CollectController.java @@ -683,7 +683,7 @@ questionsToUpdate.add(question); - List<Reference> references = + /* List<Reference> references = Optional.ofNullable(questionResult.getReferences()) .orElse(Collections.emptyList()) .stream() @@ -727,8 +727,60 @@ } return reference; }) - .collect(Collectors.toList()); + .collect(Collectors.toList());*/ +// 初始化引用列表(避免null) + List<Reference> references = new ArrayList<>(); + List<TaskResultResponse.Reference> originalReferences = questionResult.getReferences(); + if (originalReferences == null) { + originalReferences = Collections.emptyList(); + } +// 遍历原始引用列表,转换为Reference对象 + for (TaskResultResponse.Reference ref : originalReferences) { // 注意:需将“原引用类型”替换为实际类型(如QuestionResult中的引用类型) + Reference reference = new Reference(); + // 设置基本字段 + reference.setQuestion_id(question.getQuestion_id()); + reference.setTitle(ref.getTitle()); + reference.setUrl(ref.getUrl()); + reference.setDomain(ref.getDomain()); + reference.setNum(keyword.getNum()); + reference.setTask_id(result.getTask_id()); + reference.setKeyword_id(keyword.getKeyword_id()); + reference.setCreate_time(LocalDateTime.now()); + + // 处理平台和类型关联 + Platform platform = platformService.getPlatformByDomain(reference.getDomain()); + if (platform == null) { + // 平台不存在,创建新平台(类型默认为“默认”) + Type type = typeService.getOne(new LambdaQueryWrapper<Type>().eq(Type::getType_name, "默认")); + if (type == null) { + Type newType = new Type(); + newType.setType_name("默认"); + typeService.save(newType); + type = newType; + } + Platform platform1 = new Platform(); + platform1.setDomain(reference.getDomain()); + platform1.setPlatform_name(reference.getDomain()); + platform1.setType_id(type.getType_id()); + platform1.setCreate_time(LocalDateTime.now()); + platformService.save(platform1); + + // 关联新平台和类型 + reference.setType_id(type.getType_id()); + reference.setPlatform_id(platform1.getPlatform_id()); + } else { + // 平台已存在,直接关联 + reference.setPlatform_id(platform.getPlatform_id()); + Type type = typeService.getById(platform.getType_id()); + if (type != null) { + reference.setType_id(type.getType_id()); + } + } + + // 添加到结果列表 + references.add(reference); + } // 添加到总引用列表 if (!references.isEmpty()) { allReferences.addAll(references); @@ -763,18 +815,18 @@ int repetitionCount = refGroup.size() - 1; // 3.3 决定最终保留的记录 - Reference recordToSave; + Reference recordToSave = new Reference(); if (existingRecord.isPresent()) { // 使用已有ID的记录并更新重复次数 recordToSave = existingRecord.get(); recordToSave.setRepetition_num( - (recordToSave.getRepetition_num() == null ? 0 : recordToSave.getRepetition_num()) + (recordToSave.getRepetition_num() == null ? 1 : recordToSave.getRepetition_num()) + repetitionCount ); } else { // 没有ID记录则取第一条并设置重复次数 recordToSave = refGroup.get(0); - recordToSave.setRepetition_num(repetitionCount); + recordToSave.setRepetition_num(1+repetitionCount); } resultList.add(recordToSave); diff --git a/src/main/java/com/linghu/controller/UserController.java b/src/main/java/com/linghu/controller/UserController.java index adb5599..619e913 100644 --- a/src/main/java/com/linghu/controller/UserController.java +++ b/src/main/java/com/linghu/controller/UserController.java @@ -38,10 +38,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Objects; +import java.util.*; import java.util.stream.Collectors; @RestController @@ -173,108 +170,120 @@ @PostMapping("/importUserExcel") @ApiOperation("导入用户数据") public ResponseResult importUserExcel(@RequestParam("file") MultipartFile file) { - //查找出用户列表 - List<User> userList = userService.list(); - //名称 - Map<String, User> nameMap = userList.stream() - .collect(Collectors.toMap( - User::getUser_name, // Key映射 - excel -> excel, // Value映射 - (oldValue, newValue) -> oldValue // 键冲突处理(保留旧值) - )); - - //查找出分组列表 - List<Sectionalization> list = sectionalizationService.list(); - //分组 - Map<String, Sectionalization> sectionalizationMap = list.stream() - .collect(Collectors.toMap( - Sectionalization::getSectionalization_name, // Key映射 - excel -> excel, // Value映射 - (oldValue, newValue) -> oldValue // 键冲突处理(保留旧值) - )); - try { - // 检查文件是否为空 + // 1. 检查文件是否为空 if (file.isEmpty()) { return ResponseResult.error("上传文件不能为空"); } - // 使用自定义监听器读取数据(包含行号) + + // 2. 读取数据库中已有的用户和分组,用于重复校验 + // 2.1 已存在的用户(按用户名和邮箱分组) + List<User> userList = userService.list(); + Map<String, User> userNameMap = userList.stream() + .collect(Collectors.toMap( + User::getUser_name, + user -> user, // 变量名修正为user(原excel变量名不合理) + (oldValue, newValue) -> oldValue // 重复用户名保留旧值 + )); + Map<String, User> userEmailMap = userList.stream() + .collect(Collectors.toMap( + User::getUser_email, + user -> user, + (oldValue, newValue) -> oldValue // 重复邮箱保留旧值 + )); + + // 2.2 已存在的分组(按分组名称映射) + List<Sectionalization> sectionalizationList = sectionalizationService.list(); + Map<String, Sectionalization> sectionMap = sectionalizationList.stream() + .collect(Collectors.toMap( + Sectionalization::getSectionalization_name, + section -> section, + (oldValue, newValue) -> oldValue + )); + + // 3. 读取Excel数据(使用自定义监听器) UserExcelListener listener = new UserExcelListener(); + EasyExcel.read(file.getInputStream(), UserExcel.class, listener) + .sheet() // 读取第一个sheet + .doRead(); // 执行读取 - // 读取Excel数据 - EasyExcel.read(file.getInputStream()) - .head(UserExcel.class) - .sheet() - .doReadSync(); - - // 数据转换与验证 - List<ExcelDataWithRow<UserExcel>> excelList = listener.getDataList(); + // 4. 校验Excel数据并转换为User对象 + List<ExcelDataWithRow<UserExcel>> excelDataList = listener.getDataList(); List<User> insertUserList = new ArrayList<>(); List<String> errorMessages = new ArrayList<>(); - List<String> emailList = new ArrayList<>(); - for (ExcelDataWithRow<UserExcel> excelData : excelList) { + Set<String> emailSet = new HashSet<>(); // 用于校验导入数据中的邮箱重复 + + for (ExcelDataWithRow<UserExcel> excelData : excelDataList) { int rowNum = excelData.getRowNumber(); - UserExcel excel = excelData.getData(); + UserExcel excelRowData = excelData.getData(); List<String> rowErrors = new ArrayList<>(); - // 检查必要字段 - if (!StringUtils.hasText(excel.getUser_name())) { - rowErrors.add("用户名称不能为空"); + + // 4.1 校验必填字段 + if (!StringUtils.hasText(excelRowData.getUser_name())) { + rowErrors.add("账户名不能为空"); + } else if (userNameMap.containsKey(excelRowData.getUser_name())) { + rowErrors.add("账户名已存在(数据库中重复)"); } - if (!StringUtils.hasText(excel.getUser_email())) { + + if (!StringUtils.hasText(excelRowData.getUser_email())) { rowErrors.add("邮箱不能为空"); - }else{ - if (emailList.contains(excel.getUser_email())) { - rowErrors.add("导入数据中邮箱重复"); + } else { + if (userEmailMap.containsKey(excelRowData.getUser_email())) { + rowErrors.add("邮箱已存在(数据库中重复)"); } - if (nameMap.containsKey(excel.getUser_email())){ - rowErrors.add("邮箱与数据库中重复"); + if (emailSet.contains(excelRowData.getUser_email())) { + rowErrors.add("邮箱重复(导入数据中重复)"); } } - if (!StringUtils.hasText(String.valueOf(excel.getPassword()))) { + + if (!StringUtils.hasText(excelRowData.getPassword())) { rowErrors.add("密码不能为空"); } - if (!StringUtils.hasText(excel.getSectionalization_name())) { - rowErrors.add("分组不能为空"); - }else if (!sectionalizationMap.containsKey(excel.getSectionalization_name())){ - rowErrors.add("分组不存在"); + if (!StringUtils.hasText(excelRowData.getSectionalization_name())) { + rowErrors.add("账户分组不能为空"); + } else if (!sectionMap.containsKey(excelRowData.getSectionalization_name())) { + rowErrors.add("账户分组不存在(数据库中无此分组)"); } + // 4.2 处理校验结果 if (!rowErrors.isEmpty()) { - errorMessages.add(String.format("第%d行错误: %s", rowNum, String.join(";", rowErrors))); - }else { - // 构建Platform对象... + // 记录错误信息 + errorMessages.add(String.format("第%d行:%s", rowNum, String.join(";", rowErrors))); + } else { + // 校验通过,转换为User对象 User user = new User(); - user.setUser_name(excel.getUser_name()); - user.setUser_email(excel.getUser_email()); - user.setPassword(excel.getPassword()); - user.setSectionalization_id(sectionalizationMap.get(excel.getSectionalization_name()).getSectionalization_id()); + user.setUser_name(excelRowData.getUser_name()); + user.setUser_email(excelRowData.getUser_email()); + user.setPassword(excelRowData.getPassword()); // 实际场景建议加密存储 + user.setSectionalization_id(sectionMap.get(excelRowData.getSectionalization_name()).getSectionalization_id()); user.setStatus("正常"); + insertUserList.add(user); - emailList.add(excel.getUser_email()); + emailSet.add(excelRowData.getUser_email()); // 记录已添加的邮箱,用于去重 } } - // 处理错误 + + // 5. 处理错误或执行批量插入 if (!errorMessages.isEmpty()) { - // 构建清晰的错误信息 + // 组装错误信息 StringBuilder errorMsg = new StringBuilder(); - errorMsg.append("导入失败,共").append(errorMessages.size()).append("条数据存在错误:\n"); + errorMsg.append("导入失败,共").append(errorMessages.size()).append("条数据有误:\n"); errorMessages.forEach(msg -> errorMsg.append(msg).append("\n")); - - return ResponseResult.error(400, errorMsg.toString()); + return ResponseResult.error( errorMsg.toString()); + } else { + // 批量插入数据库 + userService.saveBatch(insertUserList); + return ResponseResult.success("导入成功,共新增" + insertUserList.size() + "条用户数据"); } - // 开始添加 - userService.saveBatch(insertUserList); - // 返回信息 - return ResponseResult.success(); - } - catch (Exception e) { - // 记录详细异常信息 - return ResponseResult.error("文件解析失败:" + e.getMessage()); + } catch (Exception e) { + // 捕获异常(如文件格式错误、IO异常等) + return ResponseResult.error("导入失败:" + e.getMessage()); } } + } -- Gitblit v1.7.1