diff --git a/src/main/java/com/wxjw/controller/openapi/UploadSheetController.java b/src/main/java/com/wxjw/controller/openapi/UploadSheetController.java new file mode 100644 index 0000000..1328350 --- /dev/null +++ b/src/main/java/com/wxjw/controller/openapi/UploadSheetController.java @@ -0,0 +1,38 @@ +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.UploadSheet.UploadSheetData; +import com.wxjw.service.UploadSheetService; +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 更新 Sheet + * + * @author 筱锋xiao_lfeng + */ +@RestController +@RequestMapping("/openapi/update") +public class UploadSheetController { + @Resource + private ExcelInfoMapper excelInfoMapper; + + @PostMapping("/sheet") + public ResponseEntity> deleteFile(@RequestBody @NotNull UploadSheetData requestBody) { + if ("updatefile".equals(requestBody.getAction())) { + UploadSheetService updateSheetService = new UploadSheetService(excelInfoMapper); + updateSheetService.updateSheet(requestBody); + return updateSheetService.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 73a0afa..a781e73 100644 --- a/src/main/java/com/wxjw/dal/dao/ExcelInfoMapper.java +++ b/src/main/java/com/wxjw/dal/dao/ExcelInfoMapper.java @@ -1,10 +1,7 @@ package com.wxjw.dal.dao; import com.wxjw.dal.pojo.entity.ExcelInfoEntity; -import org.apache.ibatis.annotations.Delete; -import org.apache.ibatis.annotations.Insert; -import org.apache.ibatis.annotations.Mapper; -import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.annotations.*; import org.springframework.context.annotation.ComponentScan; import java.util.List; @@ -51,6 +48,9 @@ public interface ExcelInfoMapper { @Delete("DELETE FROM excel_file_handling.excel_info WHERE id = #{id}") boolean deleteExcelForId(int id); + @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); + /** * @param excelInfoEntity excel * @return 是否插入成功 diff --git a/src/main/java/com/wxjw/dal/pojo/data/UploadSheet/UploadSheetData.java b/src/main/java/com/wxjw/dal/pojo/data/UploadSheet/UploadSheetData.java new file mode 100644 index 0000000..db6f838 --- /dev/null +++ b/src/main/java/com/wxjw/dal/pojo/data/UploadSheet/UploadSheetData.java @@ -0,0 +1,16 @@ +package com.wxjw.dal.pojo.data.UploadSheet; + +import lombok.Getter; + +import java.util.ArrayList; + +/** + * Data 处理前端获取内容 + * @author 筱锋xiao_lfeng + */ +@Getter +public class UploadSheetData { + private String action; + private int nodeId; + private ArrayList> cellInfo; +} diff --git a/src/main/java/com/wxjw/service/UploadSheetService.java b/src/main/java/com/wxjw/service/UploadSheetService.java new file mode 100644 index 0000000..27b2346 --- /dev/null +++ b/src/main/java/com/wxjw/service/UploadSheetService.java @@ -0,0 +1,121 @@ +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.UploadSheet.UploadSheetData; +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.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * Service 更新 Sheet + * + * @author 筱锋xiao_lfeng + */ +@Service +public class UploadSheetService { + + private final ExcelInfoMapper excelInfoMapper; + @Getter + private ResponseEntity> returnResult; + + public UploadSheetService(ExcelInfoMapper excelInfoMapper) { + this.excelInfoMapper = excelInfoMapper; + } + + public void updateSheet(@NotNull UploadSheetData requestBody) { + // 获取文件 + ExcelInfoEntity excelInfo = excelInfoMapper.getExcelForId(requestBody.getNodeId()); + if (excelInfo != null && excelInfo.getId() != null) { + // 获取文件 + String fileName = excelInfo.getFileName(); + String sheetName = excelInfo.getSheetName(); + // 综合路径 + String filePathForXlsx = "./src/main/resources/excel/" + fileName + "/" + sheetName + ".xlsx"; + String filePathForXls = "./src/main/resources/excel/" + fileName + "/" + sheetName + ".xls"; + Workbook workbook; + Path path = Path.of(filePathForXlsx); + try { + FileInputStream fileInputStream; + if (Files.exists(path)) { + fileInputStream = new FileInputStream(filePathForXlsx); + } else { + fileInputStream = new FileInputStream(filePathForXls); + } + workbook = new XSSFWorkbook(fileInputStream); + // 关闭流 + fileInputStream.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + // 获取 sheet + Sheet sheet = workbook.getSheet(sheetName); + // 获取总行数 + int rowNum = sheet.getLastRowNum(); + int columnNum = 0; + for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) { + if (columnNum < sheet.getRow(i).getLastCellNum()) { + columnNum = sheet.getRow(i).getLastCellNum(); + } + } + // 获取更新单元格信息 + for (int i = 0; i < requestBody.getCellInfo().size(); i++) { + for (int j = 0; j < requestBody.getCellInfo().get(i).size(); j++) { + if (sheet.getPhysicalNumberOfRows() > i) { + if (sheet.getRow(i).getPhysicalNumberOfCells() > j) { + sheet.getRow(i).getCell(j).setCellValue(String.valueOf(requestBody.getCellInfo().get(i).get(j))); + } else { + sheet.getRow(i).createCell(j).setCellValue(String.valueOf(requestBody.getCellInfo().get(i).get(j))); + } + } else { + sheet.createRow(i).createCell(j).setCellValue(String.valueOf(requestBody.getCellInfo().get(i).get(j))); + } + } + } + // 保存数据表 + try { + FileOutputStream fileOutputStream; + if (Files.exists(path)) { + fileOutputStream = new FileOutputStream(filePathForXlsx); + } else { + fileOutputStream = new FileOutputStream(filePathForXls); + } + workbook.write(fileOutputStream); + // 关闭流 + fileOutputStream.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + // 数据修改 + ExcelInfoEntity excelInfoUpdate = new ExcelInfoEntity(); + excelInfoUpdate + .setFileName(excelInfo.getFileName()) + .setSheetName(excelInfo.getSheetName()) + .setType(excelInfo.getType()) + .setCreateBy(excelInfo.getCreateBy()) + .setParentId(excelInfo.getParentId()) + .setUpdateBy(excelInfo.getUpdateBy()) + .setCreateTime(excelInfo.getCreateTime()) + .setUpdateBy(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); + excelInfoMapper.updateExcelInfo(excelInfoUpdate); + returnResult = ResultUtil.success(); + } else { + returnResult = ResultUtil.error(ErrorCode.DATA_IS_EMPTY); + } + } +}