核查系统api
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
4.4 KiB

  1. package com.iflytop.nuclear.service.impl;
  2. import com.iflytop.nuclear.model.Account;
  3. import com.iflytop.nuclear.model.NuclearCoreConfig;
  4. import com.iflytop.nuclear.service.NuclearCoreConfigService;
  5. import com.iflytop.nuclear.service.UploadService;
  6. import org.apache.poi.ss.usermodel.Cell;
  7. import org.apache.poi.ss.usermodel.Row;
  8. import org.apache.poi.ss.usermodel.Sheet;
  9. import org.apache.poi.ss.usermodel.Workbook;
  10. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Service;
  13. import org.springframework.web.multipart.MultipartFile;
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. /**
  19. * @author cool
  20. * @date 2023/6/28 14:21
  21. */
  22. @Service
  23. public class UploadServiceImpl implements UploadService {
  24. @Autowired
  25. NuclearCoreConfigService nuclearCoreConfigService;
  26. @Override
  27. public boolean verifyFileFormat(MultipartFile file) throws IOException {
  28. InputStream inputStream = file.getInputStream();
  29. Workbook wb = new XSSFWorkbook(inputStream);
  30. //默认会跳过第一行标题
  31. // 得到第一个shell
  32. Sheet sheet = wb.getSheetAt(0);
  33. // 得到Excel的行数
  34. int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
  35. int totalCells = 0;
  36. // 得到Excel的列数(前提是有行数)
  37. if (physicalNumberOfRows > 1 && sheet.getRow(1) != null) {
  38. totalCells = sheet.getRow(1).getPhysicalNumberOfCells();
  39. }
  40. if (physicalNumberOfRows == 41 && totalCells == 14) {
  41. return true;
  42. }
  43. return false;
  44. }
  45. @Override
  46. public boolean saveFileContentToDB(MultipartFile file) throws IOException {
  47. InputStream inputStream = file.getInputStream();
  48. Workbook wb = new XSSFWorkbook(inputStream);
  49. Sheet sheet = wb.getSheetAt(0);
  50. int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
  51. int physicalNumberOfColumns = sheet.getRow(1).getPhysicalNumberOfCells();
  52. // 通过传递参数获取当前excel所处的taskid并存储
  53. List<ArrayList> result = new ArrayList<>();
  54. for (int r = 2; r < physicalNumberOfRows; r += 3) {
  55. List<List<String>> list = new ArrayList<>();
  56. // 一次性对三行数据进行汇总并创建存储对象
  57. for(int f = r; f < r + 3; f++){
  58. List<String> line = new ArrayList<>();
  59. Row row = sheet.getRow(f);
  60. for(int c = 1; c < physicalNumberOfColumns; c++) {
  61. Cell cell = row.getCell(c);
  62. if (null != cell) {
  63. String stringCellValue = cell.getStringCellValue();
  64. line.add(stringCellValue);
  65. }
  66. }
  67. list.add(line);
  68. }
  69. result.add((ArrayList) list);
  70. }
  71. System.out.println(result);
  72. // 对result进行遍历组合成每条数据 插入数据库中
  73. List<NuclearCoreConfig> nuclearCoreConfigArrayList = new ArrayList<NuclearCoreConfig>();
  74. for(int i = 0; i < result.size(); i++) {
  75. int startIndex = i + 1;
  76. ArrayList rowData = result.get(i);
  77. for (int j = 0; j < 13; j++) {
  78. ArrayList line1 = (ArrayList) rowData.get(0);
  79. ArrayList line2 = (ArrayList) rowData.get(1);
  80. ArrayList line3 = (ArrayList) rowData.get(2);
  81. String s1 = (String) line1.get(j);
  82. String s2 = (String) line2.get(j);
  83. String s3 = (String) line3.get(j);
  84. String serialNumber = startIndex + "-" + (j + 1) ;
  85. // 创建存储数据库对象list
  86. // 临时taskid
  87. Long taskid = 1L;
  88. NuclearCoreConfig nuclearCoreConfig = NuclearCoreConfig.builder()
  89. .percent(s2)
  90. .firstSign(s1)
  91. .secondSign(s3)
  92. .taskId(taskid)
  93. .serialNumber(serialNumber)
  94. .build();
  95. nuclearCoreConfigArrayList.add(nuclearCoreConfig);
  96. }
  97. }
  98. // 批量插入 如果该taskid 存在数据 需要删除 在前端进行判断
  99. boolean b = nuclearCoreConfigService.saveBatch(nuclearCoreConfigArrayList);
  100. return b;
  101. }
  102. }