From c422864e436ba9f2b3839a4f10b4984447339a88 Mon Sep 17 00:00:00 2001 From: XiaoLFeng Date: Sat, 19 Aug 2023 19:24:23 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=94=E5=9B=9E=E6=89=80=E9=80=89=E6=8B=A9?= =?UTF-8?q?=E7=9A=84sheets=E7=9A=84=E8=A1=A8=E5=A4=B4=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../openapi/ListHeadArrayController.java | 39 ++++++++ .../com/wxjw/dal/dao/ExcelInfoMapper.java | 11 +++ .../java/com/wxjw/dal/pojo/ErrorCode.java | 2 + .../data/ListHeadArray/ListHeadArrayData.java | 16 ++++ .../wxjw/service/ListHeadArrayService.java | 93 +++++++++++++++++++ 5 files changed, 161 insertions(+) create mode 100644 src/main/java/com/wxjw/controller/openapi/ListHeadArrayController.java create mode 100644 src/main/java/com/wxjw/dal/pojo/data/ListHeadArray/ListHeadArrayData.java create mode 100644 src/main/java/com/wxjw/service/ListHeadArrayService.java diff --git a/src/main/java/com/wxjw/controller/openapi/ListHeadArrayController.java b/src/main/java/com/wxjw/controller/openapi/ListHeadArrayController.java new file mode 100644 index 0000000..2ad0887 --- /dev/null +++ b/src/main/java/com/wxjw/controller/openapi/ListHeadArrayController.java @@ -0,0 +1,39 @@ +package com.wxjw.controller.openapi; + +import com.wxjw.common.BaseResponse; +import com.wxjw.common.ResultUtil; +import com.wxjw.dal.dao.ExcelInfoMapper; +import com.wxjw.dal.pojo.ErrorCode; +import com.wxjw.dal.pojo.data.ListHeadArray.ListHeadArrayData; +import com.wxjw.dal.pojo.entity.ExcelInfoEntity; +import com.wxjw.service.ListHeadArrayService; +import jakarta.annotation.Resource; +import org.jetbrains.annotations.NotNull; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * Controller 表头数组控制 + * + * @author 筱锋xiao_lfeng + */ +@RestController +@RequestMapping("/openapi/header") +public class ListHeadArrayController { + @Resource + private ExcelInfoMapper excelInfoMapper; + + @PostMapping("/get") + public ResponseEntity> getListHeadArray(@RequestBody @NotNull ListHeadArrayData requestBody) { + if ("getfileIds".equals(requestBody.getAction())) { + ListHeadArrayService service = new ListHeadArrayService(excelInfoMapper); + service.getListHeadArray(requestBody); + return service.getReturnResult(); + } else { + return ResultUtil.error(ErrorCode.PARAMETER_ERROR); + } + } +} diff --git a/src/main/java/com/wxjw/dal/dao/ExcelInfoMapper.java b/src/main/java/com/wxjw/dal/dao/ExcelInfoMapper.java index a781e73..4124666 100644 --- a/src/main/java/com/wxjw/dal/dao/ExcelInfoMapper.java +++ b/src/main/java/com/wxjw/dal/dao/ExcelInfoMapper.java @@ -41,6 +41,13 @@ public interface ExcelInfoMapper { @Select("SELECT * FROM excel_file_handling.excel_info WHERE id = #{id}") ExcelInfoEntity getExcelForId(int id); + /** + * @param ids String类型多个id + * @return 返回类型内容 + */ + @Select("SELECT * FROM excel_file_handling.excel_info WHERE id IN (${ids})") + List getExcelForIds(String ids); + /** * @param id 序号 * @return 是否删除成功 @@ -48,6 +55,10 @@ public interface ExcelInfoMapper { @Delete("DELETE FROM excel_file_handling.excel_info WHERE id = #{id}") boolean deleteExcelForId(int id); + /** + * @param excelInfoEntity Excel自定义实体类 + * @return 是否更新成功 + */ @Update("UPDATE excel_file_handling.excel_info SET file_name = #{fileName}, sheet_name = #{sheetName}, table_name = #{tableName}, type = #{type}, create_by = #{createBy}, create_time = #{createTime}, update_by = #{updateBy}, update_time = #{updateTime} WHERE id = #{id}") boolean updateExcelInfo(ExcelInfoEntity excelInfoEntity); diff --git a/src/main/java/com/wxjw/dal/pojo/ErrorCode.java b/src/main/java/com/wxjw/dal/pojo/ErrorCode.java index aed615e..7981cb5 100644 --- a/src/main/java/com/wxjw/dal/pojo/ErrorCode.java +++ b/src/main/java/com/wxjw/dal/pojo/ErrorCode.java @@ -16,6 +16,8 @@ public enum ErrorCode { DATA_DUPLICATION("DataDuplication", 40012, "数据重复", HttpCode.BAD_REQUEST), DATA_WRITE_FAILURE("DataWriteFailure", 40013, "数据写入失败", HttpCode.BAD_REQUEST), DATA_DELETE_FAILURE("DataDeleteFailure", 40014, "数据删除失败", HttpCode.BAD_REQUEST), + DATA_UPDATE_FAILURE("DataUpdateFailure", 40015, "数据更新失败", HttpCode.BAD_REQUEST), + DATA_RETRIEVE_FAILURE("DataRetrieveFailure", 40016, "数据检索失败", HttpCode.BAD_REQUEST), FILE_TYPE_IS_INCORRECT("FileTypeIncorrect", 40014, "文件类型错误", HttpCode.BAD_REQUEST), FILE_CREATION_FAILED("FileCreationFailed", 40015, "文件创建失败", HttpCode.BAD_REQUEST), FILE_ALREADY_EXISTS("FileAlreadyExists", 40016, "文件已经存在", HttpCode.BAD_REQUEST), diff --git a/src/main/java/com/wxjw/dal/pojo/data/ListHeadArray/ListHeadArrayData.java b/src/main/java/com/wxjw/dal/pojo/data/ListHeadArray/ListHeadArrayData.java new file mode 100644 index 0000000..be4dc95 --- /dev/null +++ b/src/main/java/com/wxjw/dal/pojo/data/ListHeadArray/ListHeadArrayData.java @@ -0,0 +1,16 @@ +package com.wxjw.dal.pojo.data.ListHeadArray; + +import lombok.Getter; + +import java.util.stream.Collector; + +/** + * Data 获取前端数据 + * + * @author 筱锋xiao_lfeng + */ +@Getter +public class ListHeadArrayData { + private String action; + private int[] nodeId; +} diff --git a/src/main/java/com/wxjw/service/ListHeadArrayService.java b/src/main/java/com/wxjw/service/ListHeadArrayService.java new file mode 100644 index 0000000..e85c72b --- /dev/null +++ b/src/main/java/com/wxjw/service/ListHeadArrayService.java @@ -0,0 +1,93 @@ +package com.wxjw.service; + +import com.wxjw.common.BaseResponse; +import com.wxjw.common.ResultUtil; +import com.wxjw.dal.dao.ExcelInfoMapper; +import com.wxjw.dal.pojo.ErrorCode; +import com.wxjw.dal.pojo.data.ListHeadArray.ListHeadArrayData; +import com.wxjw.dal.pojo.entity.ExcelInfoEntity; +import lombok.Getter; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.jetbrains.annotations.NotNull; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; + +import java.io.FileInputStream; +import java.lang.reflect.Array; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.stream.Collectors; + +/** + * Service 列表获取控制器 + * + * @author 筱锋xiao_lfeng + */ +@Service +public class ListHeadArrayService { + + private final ExcelInfoMapper excelInfoMapper; + @Getter + private ResponseEntity> returnResult; + + public ListHeadArrayService(ExcelInfoMapper excelInfoMapper) { + this.excelInfoMapper = excelInfoMapper; + } + + public void getListHeadArray(@NotNull ListHeadArrayData requestBody) { + // 检查数据是否存在 + String number = Arrays.stream(requestBody.getNodeId()) + .mapToObj(String::valueOf) + .collect(Collectors.joining(",")); + ArrayList excelInfo = (ArrayList) excelInfoMapper.getExcelForIds(number); + ArrayList excelFileIds = new ArrayList<>(); + if (excelInfo != null && !excelInfo.isEmpty()) { + for (ExcelInfoEntity excelInfoEntity : excelInfo) { + // 获取文件信息 + String fileName = excelInfoEntity.getFileName(); + String sheetName = excelInfoEntity.getSheetName(); + // 获取文件路径 + String filePathForXlsx = "./src/main/resources/excel/" + fileName + "/" + sheetName + ".xlsx"; + String filePathForXls = "./src/main/resources/excel/" + fileName + "/" + sheetName + ".xls"; + // 读取文件 + Workbook workbook; + try { + FileInputStream fileInputStream; + if (Files.exists(Path.of(filePathForXlsx))) { + fileInputStream = new FileInputStream(filePathForXlsx); + } else { + fileInputStream = new FileInputStream(filePathForXls); + } + // 获取文件内容 + workbook = new XSSFWorkbook(fileInputStream); + // 关闭流 + fileInputStream.close(); + } catch (Exception e) { + throw new RuntimeException(e); + } + // 打开文件获取内容 + Sheet sheet = workbook.getSheetAt(0); + // 获取标题抬头 + ArrayList data = new ArrayList<>(); + for (int i = 0; i < sheet.getRow(1).getPhysicalNumberOfCells(); i++) { + switch (sheet.getRow(1).getCell(i).getCellType()) { + case NUMERIC -> data.add(sheet.getRow(1).getCell(i).getNumericCellValue()); + case BOOLEAN -> data.add(sheet.getRow(1).getCell(i).getBooleanCellValue()); + case FORMULA -> data.add(sheet.getRow(1).getCell(i).getCellFormula()); + default -> data.add(sheet.getRow(1).getCell(i).getStringCellValue()); + } + } + excelFileIds.add(data); + } + // 处理完成返回结果集 + returnResult = ResultUtil.success(excelFileIds,"输出成功"); + } else { + returnResult = ResultUtil.error(ErrorCode.DATA_IS_EMPTY); + } + } +}