文件检索模块
This commit is contained in:
parent
303cb9104b
commit
23b6ca073c
|
@ -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.RetrieveFilesData;
|
||||||
|
import com.wxjw.service.RetrieveFilesService;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
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/retrieve")
|
||||||
|
public class RetrieveFilesController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ExcelInfoMapper excelInfoMapper;
|
||||||
|
|
||||||
|
@PostMapping("/files")
|
||||||
|
public ResponseEntity<BaseResponse<Object>> retrieveFiles(@RequestBody RetrieveFilesData requestBody) {
|
||||||
|
if ("search".equals(requestBody.getAction())) {
|
||||||
|
RetrieveFilesService retrieveFilesService = new RetrieveFilesService(excelInfoMapper);
|
||||||
|
retrieveFilesService.getSearchFile(requestBody);
|
||||||
|
return retrieveFilesService.getReturnResult();
|
||||||
|
} else {
|
||||||
|
return ResultUtil.error(ErrorCode.PARAMETER_ERROR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -18,6 +18,7 @@ public enum ErrorCode {
|
||||||
FILE_TYPE_IS_INCORRECT("FileTypeIncorrect", 40014, "文件类型错误", HttpCode.BAD_REQUEST),
|
FILE_TYPE_IS_INCORRECT("FileTypeIncorrect", 40014, "文件类型错误", HttpCode.BAD_REQUEST),
|
||||||
FILE_CREATION_FAILED("FileCreationFailed", 40015, "文件创建失败", HttpCode.BAD_REQUEST),
|
FILE_CREATION_FAILED("FileCreationFailed", 40015, "文件创建失败", HttpCode.BAD_REQUEST),
|
||||||
FILE_ALREADY_EXISTS("FileAlreadyExists", 40016, "文件已经存在", HttpCode.BAD_REQUEST),
|
FILE_ALREADY_EXISTS("FileAlreadyExists", 40016, "文件已经存在", HttpCode.BAD_REQUEST),
|
||||||
|
RETRIEVE_EMPTY("RetrieveEmpty", 40017, "文件检索为空", HttpCode.BAD_REQUEST),
|
||||||
THERE_IS_NO_SUCH_RECORD("ThereIsNoSuchRecord", 40017, "记录不存在", HttpCode.BAD_REQUEST);
|
THERE_IS_NO_SUCH_RECORD("ThereIsNoSuchRecord", 40017, "记录不存在", HttpCode.BAD_REQUEST);
|
||||||
|
|
||||||
private final String output;
|
private final String output;
|
||||||
|
|
14
src/main/java/com/wxjw/dal/pojo/data/RetrieveFilesData.java
Normal file
14
src/main/java/com/wxjw/dal/pojo/data/RetrieveFilesData.java
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
package com.wxjw.dal.pojo.data;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取前端文件列表数据
|
||||||
|
* @author 筱锋xiao_lfeng
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
public class RetrieveFilesData {
|
||||||
|
private String action;
|
||||||
|
private String searchConent;
|
||||||
|
private int nodeId;
|
||||||
|
}
|
139
src/main/java/com/wxjw/service/RetrieveFilesService.java
Normal file
139
src/main/java/com/wxjw/service/RetrieveFilesService.java
Normal file
|
@ -0,0 +1,139 @@
|
||||||
|
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.RetrieveFilesData;
|
||||||
|
import com.wxjw.dal.pojo.entity.ExcelInfoEntity;
|
||||||
|
import lombok.Getter;
|
||||||
|
import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
||||||
|
import org.apache.poi.ss.usermodel.Sheet;
|
||||||
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
|
import org.apache.poi.ss.util.CellRangeAddress;
|
||||||
|
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.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service 文件查找
|
||||||
|
*
|
||||||
|
* @author 筱锋xiao_lfeng
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class RetrieveFilesService {
|
||||||
|
|
||||||
|
private final ExcelInfoMapper excelInfoMapper;
|
||||||
|
@Getter
|
||||||
|
private ResponseEntity<BaseResponse<Object>> returnResult;
|
||||||
|
|
||||||
|
public RetrieveFilesService(ExcelInfoMapper excelInfoMapper) {
|
||||||
|
this.excelInfoMapper = excelInfoMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void getSearchFile(@NotNull RetrieveFilesData requestBody) {
|
||||||
|
// 检查 nodeId 是否存在
|
||||||
|
ExcelInfoEntity getExcel = excelInfoMapper.getExcelForId(requestBody.getNodeId());
|
||||||
|
if (getExcel != null && getExcel.getId() != null) {
|
||||||
|
// 获取数据
|
||||||
|
String getFileName = getExcel.getFileName();
|
||||||
|
String getSheetName = getExcel.getSheetName();
|
||||||
|
// 构建文件夹位置
|
||||||
|
String filePathForXlsx = "./src/main/resources/excel/" + getFileName + "/" + getSheetName + ".xlsx";
|
||||||
|
String filePathForXls = "./src/main/resources/excel/" + getFileName + "/" + getSheetName + ".xls";
|
||||||
|
// 获取文件
|
||||||
|
Workbook workbook;
|
||||||
|
try {
|
||||||
|
FileInputStream inputStream;
|
||||||
|
if (Files.exists(Paths.get(filePathForXlsx))) {
|
||||||
|
inputStream = new FileInputStream(filePathForXlsx);
|
||||||
|
} else {
|
||||||
|
inputStream = new FileInputStream(filePathForXls);
|
||||||
|
}
|
||||||
|
// 流输入工作目录
|
||||||
|
workbook = new XSSFWorkbook(inputStream);
|
||||||
|
// 关闭流
|
||||||
|
inputStream.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
// 进入循环配置
|
||||||
|
// 获取单元行Row内容
|
||||||
|
Sheet sheet = workbook.getSheetAt(0);
|
||||||
|
ArrayList<Object[]> searchList = new ArrayList<>();
|
||||||
|
boolean isEmpty = true;
|
||||||
|
for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
|
||||||
|
// 获取单元格内容
|
||||||
|
for (int j = 0; j < sheet.getRow(i).getPhysicalNumberOfCells(); j++) {
|
||||||
|
// 检索Cell
|
||||||
|
if (sheet.getRow(i).getCell(j).getStringCellValue().contains(requestBody.getSearchConent())) {
|
||||||
|
searchList.add(new Object[]{sheet.getRow(i).getCell(j).getStringCellValue(), i, j});
|
||||||
|
isEmpty = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (isEmpty) {
|
||||||
|
returnResult = ResultUtil.error(ErrorCode.RETRIEVE_EMPTY);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 生成新 表
|
||||||
|
Workbook newWorkbook = new XSSFWorkbook();
|
||||||
|
Sheet newSheet = newWorkbook.createSheet("searchSheetName");
|
||||||
|
newSheet.createRow(0).createCell(0).setCellValue("内容查找");
|
||||||
|
newSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 4));
|
||||||
|
for (int i = 1; i <= searchList.size() + 1; i++) {
|
||||||
|
if (i > 1) {
|
||||||
|
newSheet.createRow(i).createCell(0).setCellValue(i - 1);
|
||||||
|
newSheet.getRow(i).createCell(1).setCellValue(requestBody.getSearchConent());
|
||||||
|
newSheet.getRow(i).createCell(2).setCellValue(searchList.get(i - 2)[0].toString());
|
||||||
|
newSheet.getRow(i).createCell(3).setCellValue((int)searchList.get(i - 2)[1]);
|
||||||
|
newSheet.getRow(i).createCell(4).setCellValue((int)searchList.get(i - 2)[2]);
|
||||||
|
} else {
|
||||||
|
newSheet.createRow(1).createCell(0).setCellValue("序号");
|
||||||
|
newSheet.getRow(1).createCell(1).setCellValue("查找");
|
||||||
|
newSheet.getRow(1).createCell(2).setCellValue("找到");
|
||||||
|
newSheet.getRow(1).createCell(3).setCellValue("Row");
|
||||||
|
newSheet.getRow(1).createCell(4).setCellValue("Column");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 获取命名规则
|
||||||
|
long date = new Date().getTime();
|
||||||
|
String newSheetName = "sheet1";
|
||||||
|
File file = new File("./src/main/resources/excel/" + date);
|
||||||
|
if (file.mkdirs()) {
|
||||||
|
try (FileOutputStream outputStream = new FileOutputStream("./src/main/resources/excel/" + date + "/" + newSheetName + ".xlsx")) {
|
||||||
|
newWorkbook.write(outputStream);
|
||||||
|
// 插入数据库
|
||||||
|
ExcelInfoEntity excelInfoEntity = new ExcelInfoEntity();
|
||||||
|
excelInfoEntity
|
||||||
|
.setFileName(String.valueOf(date))
|
||||||
|
.setSheetName(newSheetName)
|
||||||
|
.setType(2)
|
||||||
|
.setParentId(getExcel.getId())
|
||||||
|
.setCreateBy(getExcel.getCreateBy())
|
||||||
|
.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
||||||
|
// 数据库写入
|
||||||
|
excelInfoMapper.insertExcelInfo(excelInfoEntity);
|
||||||
|
// 返回结果
|
||||||
|
returnResult = ResultUtil.success();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 为空操作
|
||||||
|
returnResult = ResultUtil.error(ErrorCode.THERE_IS_NO_SUCH_RECORD);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user