|
|
|
@ -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<Translation> 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<String, Object> 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<Translation> parseExcelFile(MultipartFile file) throws IOException { |
|
|
|
List<Translation> 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; |
|
|
|
} |
|
|
|
} |
|
|
|
} |