From b2fc99a49fe560ebd7b5e238d7a9dd9ef2f9b53f Mon Sep 17 00:00:00 2001 From: wangguorui <2069821375@qq.com> Date: Wed, 10 Dec 2025 15:32:54 +0800 Subject: [PATCH] =?UTF-8?q?12=E6=9C=8810=E6=97=A5=E7=8E=B0=E9=87=91?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=A4=9A=E8=AF=AD=E8=A8=80=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E6=89=B9=E9=87=8F=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E7=BF=BB=E8=AF=91=E6=95=B0=E6=8D=AE=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/demo/Util/LanguageTranslationUtil.java | 19 ++- .../controller/coin/TranslationController.java | 134 ++++++++++++++++++++- 2 files changed, 145 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/example/demo/Util/LanguageTranslationUtil.java b/src/main/java/com/example/demo/Util/LanguageTranslationUtil.java index 4789031..b10fed0 100644 --- a/src/main/java/com/example/demo/Util/LanguageTranslationUtil.java +++ b/src/main/java/com/example/demo/Util/LanguageTranslationUtil.java @@ -39,19 +39,19 @@ public class LanguageTranslationUtil { String result; switch (language.toLowerCase()) { case "en": - result = translation.getEnglish() != null ? translation.getEnglish() : chineseSimplified; + result = isNotEmpty(translation.getEnglish()) ? translation.getEnglish() : chineseSimplified; break; case "th": - result = translation.getThai() != null ? translation.getThai() : chineseSimplified; + result = isNotEmpty(translation.getThai()) ? translation.getThai() : chineseSimplified; break; case "zh_tw": - result = translation.getChineseTraditional() != null ? translation.getChineseTraditional() : chineseSimplified; + result = isNotEmpty(translation.getChineseTraditional()) ? translation.getChineseTraditional() : chineseSimplified; break; case "ms": - result = translation.getMalay() != null ? translation.getMalay() : chineseSimplified; + result = isNotEmpty(translation.getMalay()) ? translation.getMalay() : chineseSimplified; break; case "vi": - result = translation.getVietnamese() != null ? translation.getVietnamese() : chineseSimplified; + result = isNotEmpty(translation.getVietnamese()) ? translation.getVietnamese() : chineseSimplified; break; default: result = chineseSimplified; @@ -61,6 +61,15 @@ public class LanguageTranslationUtil { return result; } + /** + * 检查字符串是否非空且非空白 + * + * @param str 待检查字符串 + * @return true表示字符串有效,false表示字符串为空或空白 + */ + private boolean isNotEmpty(String str) { + return str != null && !str.trim().isEmpty(); + } /** * 根据中文简体查找翻译对象 diff --git a/src/main/java/com/example/demo/controller/coin/TranslationController.java b/src/main/java/com/example/demo/controller/coin/TranslationController.java index 6a796b9..65c0a07 100644 --- a/src/main/java/com/example/demo/controller/coin/TranslationController.java +++ b/src/main/java/com/example/demo/controller/coin/TranslationController.java @@ -10,11 +10,13 @@ import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.io.IOException; +import java.util.*; @RestController @RequestMapping("/language") @@ -150,4 +152,130 @@ public class TranslationController { } } + //批量导入翻译数据 + @Log("批量导入翻译数据") + @PostMapping("/batchImport") + public Result batchImport(@RequestParam("file") MultipartFile file) { + try { + if (file.isEmpty()) { + return Result.error("上传文件不能为空"); + } + + // 解析Excel文件 + List translations = parseExcelFile(file); + + // 批量保存数据 + int successCount = 0; + int failCount = 0; + StringBuilder errorMsg = new StringBuilder(); + + for (int i = 0; i < translations.size(); i++) { + Translation translation = translations.get(i); + try { + // 检查必填字段 + if (translation.getChineseSimplified() == null || translation.getChineseSimplified().trim().isEmpty()) { + errorMsg.append("第").append(i + 2).append("行: 中文简体不能为空\n"); + failCount++; + continue; + } + + // 检查是否已存在 + Translation existing = translationService.findByChineseSimplified(translation.getChineseSimplified()); + if (existing != null) { + // 更新现有记录 + translation.setId(existing.getId()); + translationService.update(translation); + } else { + // 新增记录 + translationService.add(translation); + } + successCount++; + } catch (Exception e) { + errorMsg.append("第").append(i + 2).append("行: ").append(e.getMessage()).append("\n"); + failCount++; + } + } + + // 构建返回结果 + Map result = new HashMap<>(); + result.put("successCount", successCount); + result.put("failCount", failCount); + result.put("total", translations.size()); + result.put("errorMsg", errorMsg.toString()); + + if (failCount > 0) { + return Result.success("部分数据导入完成", result); + } else { + // 刷新缓存 + languageTranslationUtil.refreshAllCache(); + return Result.success("全部数据导入成功", result); + } + } catch (Exception e) { + log.error("批量导入失败", e); + return Result.error("批量导入失败: " + e.getMessage()); + } + } + + /** + * 解析Excel文件 + * @param file Excel文件 + * @return 翻译记录列表 + * @throws IOException IO异常 + */ + private List parseExcelFile(MultipartFile file) throws IOException { + List translations = new ArrayList<>(); + + Workbook workbook = new XSSFWorkbook(file.getInputStream()); + Sheet sheet = workbook.getSheetAt(0); + + // 从第二行开始读取数据(跳过表头) + for (int i = 1; i <= sheet.getLastRowNum(); i++) { + Row row = sheet.getRow(i); + if (row == null) continue; + + Translation translation = new Translation(); + + // 读取各列数据 + translation.setChineseSimplified(getCellValue(row.getCell(0))); + translation.setEnglish(getCellValue(row.getCell(1))); + translation.setThai(getCellValue(row.getCell(2))); + translation.setChineseTraditional(getCellValue(row.getCell(3))); + translation.setMalay(getCellValue(row.getCell(4))); + translation.setVietnamese(getCellValue(row.getCell(5))); + + // 如果中文简体字段不为空,则添加到列表中 + if (translation.getChineseSimplified() != null && !translation.getChineseSimplified().trim().isEmpty()) { + translations.add(translation); + } + } + + workbook.close(); + return translations; + } + + /** + * 获取单元格的值 + * @param cell 单元格 + * @return 单元格值 + */ + private String getCellValue(Cell cell) { + if (cell == null) return null; + + switch (cell.getCellType()) { + case STRING: + return cell.getStringCellValue(); + case NUMERIC: + if (DateUtil.isCellDateFormatted(cell)) { + return cell.getDateCellValue().toString(); + } else { + return String.valueOf(cell.getNumericCellValue()); + } + case BOOLEAN: + return String.valueOf(cell.getBooleanCellValue()); + case FORMULA: + return cell.getCellFormula(); + default: + return null; + } + } }