Compare commits
6 Commits
99ff3d8fde
...
e27cc9095c
Author | SHA1 | Date | |
---|---|---|---|
e27cc9095c | |||
b9a7fa2a7f | |||
02612ac95b | |||
3d4314e0dd | |||
3e80f8843c | |||
9477a107aa |
19
pom.xml
19
pom.xml
@ -47,6 +47,25 @@
|
||||
<version>23.0.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.walterjwhite.java.modules.csv.modules</groupId>
|
||||
<artifactId>apache-poi</artifactId>
|
||||
<version>0.0.17</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi</artifactId>
|
||||
<version>5.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi-ooxml</artifactId>
|
||||
<version>5.0.0</version>
|
||||
</dependency>
|
||||
<!--<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
|
54
src/main/java/com/wxjw/common/BaseResponse.java
Normal file
54
src/main/java/com/wxjw/common/BaseResponse.java
Normal file
@ -0,0 +1,54 @@
|
||||
package com.wxjw.common;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.wxjw.dal.pojo.ErrorCode;
|
||||
import com.wxjw.dal.pojo.HttpCode;
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* 响应体定义
|
||||
*
|
||||
* @author 筱锋xiao_lfeng
|
||||
*/
|
||||
@Data
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public class BaseResponse<T> {
|
||||
private int code;
|
||||
private T data;
|
||||
private String msg;
|
||||
|
||||
public BaseResponse(@NotNull HttpCode httpCode) {
|
||||
this(httpCode.getCode(), httpCode.getDescription(), null);
|
||||
}
|
||||
|
||||
public BaseResponse(@NotNull HttpCode httpCode, String message) {
|
||||
this(httpCode.getCode(), message, null);
|
||||
}
|
||||
|
||||
public BaseResponse(@NotNull ErrorCode errorCode) {
|
||||
this(errorCode.getCode(), errorCode.getMessage(), null);
|
||||
}
|
||||
|
||||
public BaseResponse(@NotNull ErrorCode errorCode, T data) {
|
||||
this(errorCode.getCode(), errorCode.getMessage(), data);
|
||||
}
|
||||
|
||||
public BaseResponse(@NotNull ErrorCode errorCode, String message) {
|
||||
this(errorCode.getCode(), message, null);
|
||||
}
|
||||
|
||||
public BaseResponse(@NotNull HttpCode httpCode, String message, T data) {
|
||||
this(httpCode.getCode(), message, data);
|
||||
}
|
||||
|
||||
public BaseResponse(int code, String message) {
|
||||
this(code, message, null);
|
||||
}
|
||||
|
||||
public BaseResponse(int code, String message, T data) {
|
||||
this.code = code;
|
||||
this.msg = message;
|
||||
this.data = data;
|
||||
}
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
package com.wxjw.common;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author 筱锋xiao_lfeng
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class Result<T> {
|
||||
private int code;
|
||||
private T data;
|
||||
private String msg;
|
||||
|
||||
public Result() {
|
||||
}
|
||||
}
|
66
src/main/java/com/wxjw/common/ResultUtil.java
Normal file
66
src/main/java/com/wxjw/common/ResultUtil.java
Normal file
@ -0,0 +1,66 @@
|
||||
package com.wxjw.common;
|
||||
|
||||
import com.wxjw.dal.pojo.ErrorCode;
|
||||
import com.wxjw.dal.pojo.HttpCode;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
/**
|
||||
* Utils 响应体
|
||||
*
|
||||
* @author 筱锋xiao_lfeng
|
||||
*/
|
||||
public class ResultUtil {
|
||||
public static ResponseEntity<BaseResponse<Object>> success() {
|
||||
return ResponseEntity
|
||||
.status(HttpCode.OK.getCode())
|
||||
.body(new BaseResponse<>(HttpCode.OK , "Success", "成功"));
|
||||
}
|
||||
|
||||
public static ResponseEntity<BaseResponse<Object>> success(String message) {
|
||||
return ResponseEntity
|
||||
.status(HttpCode.OK.getCode())
|
||||
.body(new BaseResponse<>(HttpCode.OK, "Success", message));
|
||||
}
|
||||
|
||||
public static ResponseEntity<BaseResponse<Object>> success(Object data) {
|
||||
return ResponseEntity
|
||||
.status(HttpCode.OK.getCode())
|
||||
.body(new BaseResponse<>(HttpCode.OK, "Success", data));
|
||||
}
|
||||
|
||||
public static ResponseEntity<BaseResponse<Object>> success(Object data, String message) {
|
||||
return ResponseEntity
|
||||
.status(HttpCode.OK.getCode())
|
||||
.body(new BaseResponse<>(HttpCode.OK, message, data));
|
||||
}
|
||||
|
||||
public static ResponseEntity<BaseResponse<Object>> error(HttpCode httpCode, String output, String message) {
|
||||
return ResponseEntity
|
||||
.status(httpCode.getCode())
|
||||
.body(new BaseResponse<>(httpCode, output, message));
|
||||
}
|
||||
|
||||
public static ResponseEntity<BaseResponse<Object>> error(HttpCode httpCode, String output, String message, Object data) {
|
||||
return ResponseEntity
|
||||
.status(httpCode.getCode())
|
||||
.body(new BaseResponse<>(httpCode, message, data));
|
||||
}
|
||||
|
||||
public static ResponseEntity<BaseResponse<Object>> error(ErrorCode errorCode) {
|
||||
return ResponseEntity
|
||||
.status(errorCode.getHttpCode().getCode())
|
||||
.body(new BaseResponse<>(errorCode));
|
||||
}
|
||||
|
||||
public static ResponseEntity<BaseResponse<Object>> error(ErrorCode errorCode, String message) {
|
||||
return ResponseEntity
|
||||
.status(errorCode.getHttpCode().getCode())
|
||||
.body(new BaseResponse<>(errorCode, message));
|
||||
}
|
||||
|
||||
public static ResponseEntity<BaseResponse<Object>> error(ErrorCode errorCode, Object data) {
|
||||
return ResponseEntity
|
||||
.status(errorCode.getHttpCode().getCode())
|
||||
.body(new BaseResponse<>(errorCode, data));
|
||||
}
|
||||
}
|
@ -1,16 +1,16 @@
|
||||
package com.wxjw.controller.openapi;
|
||||
|
||||
import com.wxjw.common.Result;
|
||||
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.getFileTree.GetFileTreeResultBody;
|
||||
import com.wxjw.service.GetFileTreeService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* Controller 获取文件树
|
||||
*
|
||||
@ -24,14 +24,11 @@ public class GetFileTreeController {
|
||||
ExcelInfoMapper excelInfoMapper;
|
||||
|
||||
@PostMapping("/files/tree")
|
||||
public ResponseEntity<Result<Object>> getFileTree(@RequestBody @NotNull HashMap<String, String> resultBody) {
|
||||
String getResult = resultBody.get("action");
|
||||
if ("getfiletree".equals(getResult)) {
|
||||
return ResponseEntity.ok()
|
||||
.body(new GetFileTreeService().getFileTreeService(excelInfoMapper));
|
||||
public ResponseEntity<BaseResponse<Object>> getFileTree(@RequestBody @NotNull GetFileTreeResultBody resultBody) {
|
||||
if ("getfiletree".equals(resultBody.getAction())) {
|
||||
return new GetFileTreeService().getFileTreeService(excelInfoMapper);
|
||||
} else {
|
||||
return ResponseEntity.badRequest()
|
||||
.body(new Result<>(403, null, "参数错误"));
|
||||
return ResultUtil.error(ErrorCode.PARAMETER_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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.GetUploadFile.GetUploadFileData;
|
||||
import com.wxjw.service.GetUploadFileService;
|
||||
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;
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @author 筱锋xiao_lfeng
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/openapi/files")
|
||||
public class GetUploadFileController {
|
||||
@Resource
|
||||
private ExcelInfoMapper excelInfoMapper;
|
||||
|
||||
@PostMapping("/upload")
|
||||
public ResponseEntity<BaseResponse<Object>> getUploadFile(@RequestBody @NotNull GetUploadFileData getUploadFileData) {
|
||||
if ("importfile".equals(getUploadFileData.getAction())) {
|
||||
GetUploadFileService getUploadFileService = new GetUploadFileService(excelInfoMapper);
|
||||
getUploadFileService.uploadFileService(getUploadFileData);
|
||||
return getUploadFileService.getReturnResult();
|
||||
} else {
|
||||
return ResultUtil.error(ErrorCode.PARAMETER_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
@ -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.insertTable.InsertTableData;
|
||||
import com.wxjw.service.InsertTableService;
|
||||
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/files")
|
||||
public class InsertTableController {
|
||||
@Resource
|
||||
private ExcelInfoMapper excelInfoMapper;
|
||||
|
||||
@PostMapping("/insert")
|
||||
public ResponseEntity<BaseResponse<Object>> insertFunction(@RequestBody @NotNull InsertTableData resultBody) {
|
||||
if ("insert".equals(resultBody.getAction())) {
|
||||
InsertTableService insertTableService = new InsertTableService();
|
||||
insertTableService.insertLogic(resultBody, excelInfoMapper);
|
||||
return insertTableService.getOperationStatus();
|
||||
} else {
|
||||
return ResultUtil.error(ErrorCode.PARAMETER_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
@ -26,23 +26,19 @@ public interface ExcelInfoMapper {
|
||||
/**
|
||||
* @return 返回全部的文件名称(去重)
|
||||
*/
|
||||
@Select("SELECT t1.* FROM excel_file_handling.excel_info t1 LEFT JOIN excel_file_handling.excel_info t2 ON t1.file_name = t2.file_name AND t1.id < t2.id WHERE t2.id")
|
||||
@Select("SELECT t1.* FROM excel_file_handling.excel_info t1 LEFT JOIN excel_file_handling.excel_info t2 ON t1.file_name = t2.file_name AND t1.id > t2.id WHERE t2.id IS NULL")
|
||||
List<ExcelInfoEntity> getAllExcelFilesName();
|
||||
|
||||
/**
|
||||
* @return 返回全部的文件名称(不去重)
|
||||
*/
|
||||
@Select("SELECT * FROM excel_file_handling.excel_info WHERE file_name = #{fileName}")
|
||||
List<ExcelInfoEntity> getAllExcelFilesNameNoRepetition(String fileName);
|
||||
|
||||
/**
|
||||
* @param id 主键
|
||||
* @param parentId 父级 ID
|
||||
* @param fileName 文件名称
|
||||
* @param sheetName sheet 名称
|
||||
* @param tableName 表名称
|
||||
* @param type 类型
|
||||
* @param createBy 创建者
|
||||
* @param createTime 创建时间
|
||||
* @param updateBy 更新者
|
||||
* @param updateTime 更新时间
|
||||
* @param excelInfoEntity excel
|
||||
* @return 是否插入成功
|
||||
*/
|
||||
@Insert("INSERT INTO excel_file_handling.excel_info (`id`,`parent_id`,`file_name`,`sheet_name`,`table_name`,`type`,`create_by`,`create_time`,`update_by`,`update_time`) VALUES (#{id}, #{parentId},#{fileName},#{sheetName},#{tableName},#{type},#{createBy}, #{createTime},#{updateBy},#{updateTime})")
|
||||
boolean insertExcelInfo(int id, int parentId, String fileName, String sheetName, String tableName, int type, String createBy, String createTime, String updateBy, String updateTime);
|
||||
@Insert("INSERT INTO excel_file_handling.excel_info (`parent_id`,`file_name`,`sheet_name`,`table_name`,`type`,`create_by`,`create_time`,`update_by`,`update_time`) VALUES (#{parentId}, #{fileName}, #{sheetName}, #{tableName}, #{type}, #{createBy}, #{createTime}, #{updateBy}, #{updateTime})")
|
||||
boolean insertExcelInfo(ExcelInfoEntity excelInfoEntity);
|
||||
}
|
||||
|
26
src/main/java/com/wxjw/dal/pojo/ErrorCode.java
Normal file
26
src/main/java/com/wxjw/dal/pojo/ErrorCode.java
Normal file
@ -0,0 +1,26 @@
|
||||
package com.wxjw.dal.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 错误码
|
||||
*
|
||||
* @author 筱锋xiao_lfeng
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public enum ErrorCode {
|
||||
DATA_IS_EMPTY("DataIsEmpty", 40010, "数据为空", HttpCode.BAD_REQUEST),
|
||||
PARAMETER_ERROR("ParameterError", 40011, "参数错误", HttpCode.BAD_REQUEST),
|
||||
DATA_DUPLICATION("DataDuplication", 40012, "数据重复", HttpCode.BAD_REQUEST),
|
||||
DATA_WRITE_FAILURE("DataWriteFailure", 40013, "数据写入失败", 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);
|
||||
|
||||
private final String output;
|
||||
private final int code;
|
||||
private final String message;
|
||||
private final HttpCode httpCode;
|
||||
}
|
70
src/main/java/com/wxjw/dal/pojo/HttpCode.java
Normal file
70
src/main/java/com/wxjw/dal/pojo/HttpCode.java
Normal file
@ -0,0 +1,70 @@
|
||||
package com.wxjw.dal.pojo;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 错误码枚举列表
|
||||
*
|
||||
* @author 筱锋xiao_lfeng
|
||||
*/
|
||||
@Getter
|
||||
public enum HttpCode {
|
||||
CONTINUE(100, "Continue", "继续。服务器已接收初始请求,客户端应继续发送其余部分。"),
|
||||
SWITCHING_PROTOCOLS(101, "Switching Protocols", "切换协议。服务器已理解并接受客户端请求,将切换到协议升级的新协议"),
|
||||
PROCESSING(102, "Processing", "请求处理中,客户端根据协议进行处理。"),
|
||||
EARLY_HINTS(103, "Early Hints", "提前提示。服务器已经发送一些响应头部,等待发送正式的响应体。"),
|
||||
OK(200, "OK", "成功"),
|
||||
CREATED(201, "Created", "请求已完成,客户端将接收新请求。"),
|
||||
ACCEPTED(202, "Accepted", "已接受。请求已被服务器接受,但尚未执行或处理。"),
|
||||
NON_AUTHORITATIVE_INFORMATION(203, "Non-Authoritative Information", "服务器认证失败,客户端将尝试新建连接。"),
|
||||
NO_CONTENT(204, "No Content", "当前请求没有实体数据,就是没有数据"),
|
||||
RESET_CONTENT(205, "Reset Content", "客户端将没有实体数据,直接返回一个Added Removes Content"),
|
||||
PARTIAL_CONTENT(206, "Partial Content", "部分内容。服务器成功处理了部分请求的范围。"),
|
||||
MULTI_STATUS(207, "Multi-Status", "多个状态返回,如果当前请求是非回应,但存在多个状态"),
|
||||
ALREADY_REPORTED(208, "Already Reported", "已多次返回,删除进度失败。"),
|
||||
IM_USED(226, "IMUsed", "已被使用了,由于连接池由于找不到空闲"),
|
||||
MULTIPLE_CHOICES(300, "Multiple Choices", "支持多种协议。"),
|
||||
MOVE_PERMANENTLY(301, "Moved Permanently", "永久移动,重定向"),
|
||||
FOUND(302, "Found", "找到"),
|
||||
SEE_OTHER(303, "See Other", "客户端缓存的资源将发生变更,访问其他服务器。"),
|
||||
NOT_MODIFIED(304, "Not Modified", "服务器没有发送变更,客户端缓存的资源未发生变更。"),
|
||||
USE_PROXY(305, "Use Proxy", "客户端直接使用代理,不需要重定向"),
|
||||
TEMPORARY_REDIRECT(307, "Temporary Redirect", "客户端将重定向到其他服务器"),
|
||||
PERMANENT_REDIRECT(308, "Permanent Redirect", "客户端将该资源转发给其他客户端"),
|
||||
BAD_REQUEST(400, "Bad Request", "请求数据格式不合法。"),
|
||||
UNAUTHORIZED(401, "Unauthorized ", "没有授权"),
|
||||
FORBIDDEN(403, "Forbidden", "没有权限"),
|
||||
NOT_FOUND(404, "Not Found", "资源未找到"),
|
||||
METHOD_NOT_ALLOW(405, "Method Not Allowed", "方法不被允许"),
|
||||
NOT_ACCEPTABLE(406, "Not Acceptable", "请求类型不合法"),
|
||||
PROXY_AUTHENTICATION_REQUIRED(407, "Proxy Authentication Required", "代理认证失败"),
|
||||
REQUEST_TIMEOUT(408, "Request Timeout", "请求超时,超过最大超时限制。"),
|
||||
CONFLICT(409, "Conflict", "资源类型冲突"),
|
||||
LENGTH_REQUIRED(411, "Length Required", "需要长度"),
|
||||
INTERNAL_SERVER_ERROR(500, "Internal Server Error", "服务器内部错误"),
|
||||
NOT_IMPLEMENTED(501, "Not Implemented", "服务器不支持该协议"),
|
||||
BAD_GATEWAY(502, "Bad Gateway", "错误的网关"),
|
||||
SERVICE_UNAVAILABLE(503, "Service Unavailable", "服务器不可用"),
|
||||
GATEWAY_TIMEOUT(504, "Gateway Timeout", "网关超时");
|
||||
|
||||
/**
|
||||
* Http 状态码
|
||||
*/
|
||||
private final int code;
|
||||
|
||||
/**
|
||||
* 状态码信息概要
|
||||
*/
|
||||
private final String message;
|
||||
|
||||
/**
|
||||
* 描述模块
|
||||
*/
|
||||
private final String description;
|
||||
|
||||
HttpCode(int code, String message, String description) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
this.description = description;
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.wxjw.dal.pojo.data.GetUploadFile;
|
||||
|
||||
import jakarta.annotation.Nullable;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author 筱锋xiao_lfeng
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class GetUploadFileData {
|
||||
private String action;
|
||||
private String userid;
|
||||
@Nullable
|
||||
private String filelib;
|
||||
private Object files;
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package com.wxjw.dal.pojo.data.getFileTree;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* Data 自定义 GetFileTreeController 实体类构造 父亲
|
||||
*
|
||||
* @author 筱锋xiao_lfeng
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class GetFileTreeChildren<T> {
|
||||
private String name;
|
||||
private int id;
|
||||
private T children;
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package com.wxjw.dal.pojo.data.getFileTree;
|
||||
|
||||
import com.wxjw.dal.dao.ExcelInfoMapper;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Data 自定义 GetFileTreeController 实体类构造 父亲
|
||||
*
|
||||
* @author 筱锋xiao_lfeng
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class GetFileTreeData {
|
||||
private String name;
|
||||
private boolean open;
|
||||
private ArrayList<Object> children;
|
||||
|
||||
public GetFileTreeData setData(@NotNull ExcelInfoMapper excelInfoMapper, int id) {
|
||||
ArrayList<Object> children = new ArrayList<>();
|
||||
excelInfoMapper.getAllExcelFilesName().forEach(name -> {
|
||||
if (id == name.getType()) {
|
||||
ArrayList<Object> childrenSheet = new ArrayList<>();
|
||||
excelInfoMapper.getAllExcelInfos().forEach(sheet -> {
|
||||
if (name.getFileName() != null && id == sheet.getType() && name.getFileName().equals(sheet.getFileName())) {
|
||||
childrenSheet.add(new Sheet(sheet.getSheetName(), sheet.getId()));
|
||||
}
|
||||
});
|
||||
children.add(new Children<>(name.getFileName(), name.getId(), childrenSheet));
|
||||
}
|
||||
});
|
||||
this.children = children;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
class Children<T> {
|
||||
private String name;
|
||||
private int id;
|
||||
private T children;
|
||||
}
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
class Sheet {
|
||||
private String name;
|
||||
private int id;
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package com.wxjw.dal.pojo.data.getFileTree;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* Data 自定义 GetFileTreeController 实体类构造 父亲
|
||||
*
|
||||
* @author 筱锋xiao_lfeng
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class GetFileTreeFather<T> {
|
||||
private String name;
|
||||
private boolean open;
|
||||
private T children;
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.wxjw.dal.pojo.data.getFileTree;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* GetFileTreeResultBody
|
||||
*
|
||||
* @author 筱锋xiao_lfeng
|
||||
*/
|
||||
@Getter
|
||||
public class GetFileTreeResultBody {
|
||||
private String action;
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
package com.wxjw.dal.pojo.data.getFileTree;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* Data 自定义 GetFileTreeController 实体类构造 父亲
|
||||
*
|
||||
* @author 筱锋xiao_lfeng
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class GetFileTreeSheet {
|
||||
private String name;
|
||||
private int id;
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.wxjw.dal.pojo.data.insertTable;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author lfeng
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class InsertTableData {
|
||||
private String action;
|
||||
private long id;
|
||||
private long paren_id;
|
||||
private String file_name;
|
||||
private String sheet_name;
|
||||
private String table_name;
|
||||
private int type;
|
||||
private String create_by;
|
||||
private String create_time;
|
||||
private String update_by;
|
||||
private String update_time;
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package com.wxjw.dal.pojo.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
@ -9,15 +10,25 @@ import org.springframework.lang.Nullable;
|
||||
* @author 筱锋xiao_lfeng
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class ExcelInfoEntity {
|
||||
private int id;
|
||||
private int parentId;
|
||||
@Nullable private String fileName;
|
||||
@Nullable private String sheetName;
|
||||
@Nullable private String tableName;
|
||||
@Nullable
|
||||
private Integer id;
|
||||
@Nullable
|
||||
private Integer parentId;
|
||||
@Nullable
|
||||
private String fileName;
|
||||
@Nullable
|
||||
private String sheetName;
|
||||
@Nullable
|
||||
private String tableName;
|
||||
private int type;
|
||||
@Nullable private String createBy;
|
||||
@Nullable private String createTime;
|
||||
@Nullable private String updateBy;
|
||||
@Nullable private String updateTime;
|
||||
@Nullable
|
||||
private String createBy;
|
||||
@Nullable
|
||||
private String createTime;
|
||||
@Nullable
|
||||
private String updateBy;
|
||||
@Nullable
|
||||
private String updateTime;
|
||||
}
|
||||
|
@ -1,12 +1,12 @@
|
||||
package com.wxjw.service;
|
||||
|
||||
import com.wxjw.common.Result;
|
||||
import com.wxjw.common.BaseResponse;
|
||||
import com.wxjw.common.ResultUtil;
|
||||
import com.wxjw.dal.dao.ExcelInfoMapper;
|
||||
import com.wxjw.dal.pojo.data.getFileTree.GetFileTreeChildren;
|
||||
import com.wxjw.dal.pojo.data.getFileTree.GetFileTreeFather;
|
||||
import com.wxjw.dal.pojo.data.getFileTree.GetFileTreeSheet;
|
||||
import com.wxjw.dal.pojo.ErrorCode;
|
||||
import com.wxjw.dal.pojo.data.getFileTree.GetFileTreeData;
|
||||
import com.wxjw.dal.pojo.entity.ExcelInfoEntity;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -20,39 +20,22 @@ import java.util.ArrayList;
|
||||
@Service
|
||||
public class GetFileTreeService {
|
||||
|
||||
public Result<Object> getFileTreeService(ExcelInfoMapper excelInfoMapper) {
|
||||
public ResponseEntity<BaseResponse<Object>> getFileTreeService(ExcelInfoMapper excelInfoMapper) {
|
||||
// 从数据库读取数据
|
||||
ArrayList<ExcelInfoEntity> allExcelFileName = (ArrayList<ExcelInfoEntity>) excelInfoMapper.getAllExcelFilesName();
|
||||
// 检查所得数据是否为空
|
||||
if (!allExcelFileName.isEmpty() && !excelInfoMapper.getAllExcelInfos().isEmpty()) {
|
||||
ArrayList<Object> data = new ArrayList<>();
|
||||
for (int i = 0; i < 3; i++) {
|
||||
// 循环创建
|
||||
ArrayList<Object> fatherList = new ArrayList<>();
|
||||
for (ExcelInfoEntity excelInfoEntityFather : allExcelFileName) {
|
||||
if (excelInfoEntityFather.getType() == i) {
|
||||
ArrayList<Object> childrenList = new ArrayList<>();
|
||||
for (ExcelInfoEntity excelInfoEntityChildren : excelInfoMapper.getAllExcelInfos()) {
|
||||
GetFileTreeSheet getFileTreeSheet;
|
||||
getFileTreeSheet = new GetFileTreeSheet(excelInfoEntityChildren.getSheetName(), excelInfoEntityChildren.getId());
|
||||
childrenList.add(getFileTreeSheet);
|
||||
}
|
||||
|
||||
|
||||
GetFileTreeChildren<Object> getFileTreeChildren;
|
||||
getFileTreeChildren = new GetFileTreeChildren<>(excelInfoEntityFather.getFileName(), excelInfoEntityFather.getId(), childrenList);
|
||||
fatherList.add(getFileTreeChildren);
|
||||
}
|
||||
}
|
||||
switch (i) {
|
||||
case 0 -> data.add(new GetFileTreeFather<>("公共库", true, fatherList));
|
||||
case 1 -> data.add(new GetFileTreeFather<>("高级库", true, fatherList));
|
||||
case 2 -> data.add(new GetFileTreeFather<>("个人库", true, fatherList));
|
||||
case 0 -> data.add(new GetFileTreeData("公共库", true, null).setData(excelInfoMapper, i));
|
||||
case 1 -> data.add(new GetFileTreeData("高级库", true, null).setData(excelInfoMapper, i));
|
||||
case 2 -> data.add(new GetFileTreeData("个人库", true, null).setData(excelInfoMapper, i));
|
||||
}
|
||||
}
|
||||
return new Result<>(200, data, "输出成功");
|
||||
return ResultUtil.success(data, "输出成功");
|
||||
} else {
|
||||
return new Result<>(403, null, "数据为空");
|
||||
return ResultUtil.error(ErrorCode.DATA_IS_EMPTY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
157
src/main/java/com/wxjw/service/GetUploadFileService.java
Normal file
157
src/main/java/com/wxjw/service/GetUploadFileService.java
Normal file
@ -0,0 +1,157 @@
|
||||
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.GetUploadFile.GetUploadFileData;
|
||||
import com.wxjw.dal.pojo.entity.ExcelInfoEntity;
|
||||
import lombok.Getter;
|
||||
import org.apache.poi.EncryptedDocumentException;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.*;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Base64;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author 筱锋xiao_lfeng
|
||||
*/
|
||||
@Service
|
||||
public class GetUploadFileService {
|
||||
|
||||
private final ExcelInfoMapper excelInfoMapper;
|
||||
@Getter
|
||||
private ResponseEntity<BaseResponse<Object>> returnResult;
|
||||
@Getter
|
||||
private boolean checkType;
|
||||
|
||||
public GetUploadFileService(ExcelInfoMapper excelInfoMapper) {
|
||||
this.excelInfoMapper = excelInfoMapper;
|
||||
}
|
||||
|
||||
private static boolean isSheetEmpty(@NotNull Sheet sheet) {
|
||||
for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
|
||||
Row row = sheet.getRow(i);
|
||||
if (row != null) {
|
||||
for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
|
||||
Cell cell = row.getCell(j);
|
||||
if (cell != null && cell.getCellType() != CellType.BLANK) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void copySheet(@NotNull Sheet sourceSheet, Sheet targetSheet) {
|
||||
for (int i = sourceSheet.getFirstRowNum(); i <= sourceSheet.getLastRowNum(); i++) {
|
||||
Row sourceRow = sourceSheet.getRow(i);
|
||||
if (sourceRow != null) {
|
||||
Row targetRow = targetSheet.createRow(i);
|
||||
|
||||
for (int j = sourceRow.getFirstCellNum(); j < sourceRow.getLastCellNum(); j++) {
|
||||
Cell sourceCell = sourceRow.getCell(j);
|
||||
if (sourceCell != null) {
|
||||
Cell targetCell = targetRow.createCell(j, sourceCell.getCellType());
|
||||
Object cellValue = getCellValue(sourceCell);
|
||||
targetCell.setCellValue(cellValue.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Object getCellValue(@NotNull Cell cell) {
|
||||
return switch (cell.getCellType()) {
|
||||
case NUMERIC -> cell.getNumericCellValue();
|
||||
case STRING -> cell.getStringCellValue();
|
||||
case BOOLEAN -> cell.getBooleanCellValue();
|
||||
case BLANK -> "";
|
||||
// Handle other cell types if needed
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
public void uploadFileService(@NotNull GetUploadFileData getUploadFileData) {
|
||||
// 获取文件
|
||||
if (getUploadFileData.getFiles() != null) {
|
||||
// 获取 base64 文件流
|
||||
byte[] base64 = Base64.getDecoder().decode(String.valueOf(getUploadFileData.getFiles()));
|
||||
// 检查文件尾缀是否正确
|
||||
InputStream inputStream = new ByteArrayInputStream(base64);
|
||||
try {
|
||||
Workbook workBook = WorkbookFactory.create(inputStream);
|
||||
String getFileType = workBook.getSpreadsheetVersion().toString().toLowerCase();
|
||||
if (getFileType.contains("excel2007") || getFileType.contains("excel97") || getFileType.contains("wps")) {
|
||||
// 文件类型正确(存储)
|
||||
long time = new Date().getTime();
|
||||
// 设置文件存储位置
|
||||
String path = "./src/main/resources/excel/" + time + "/";
|
||||
String fileName = null;
|
||||
if (getFileType.contains("excel2007")) {
|
||||
fileName = time + ".xlsx";
|
||||
} else if (getFileType.contains("excel97")) {
|
||||
fileName = time + ".xls";
|
||||
}
|
||||
// 创建文件
|
||||
File file = new File(path);
|
||||
if (!file.exists()) {
|
||||
if (file.mkdirs()) {
|
||||
// 成功
|
||||
for (int i = 0; i < workBook.getNumberOfSheets(); i++) {
|
||||
Sheet sheet = workBook.getSheetAt(i);
|
||||
// 检查 sheet 是否存在
|
||||
if (!isSheetEmpty(sheet)) {
|
||||
if (getFileType.contains("excel2007")) {
|
||||
fileName = sheet.getSheetName() + ".xlsx";
|
||||
} else if (getFileType.contains("excel97")) {
|
||||
fileName = sheet.getSheetName() + ".xls";
|
||||
}
|
||||
try {
|
||||
Workbook targetWorkbook = WorkbookFactory.create(true);
|
||||
|
||||
Sheet targetSheet = targetWorkbook.createSheet(sheet.getSheetName());
|
||||
copySheet(workBook.getSheet(sheet.getSheetName()), targetSheet);
|
||||
|
||||
try (FileOutputStream fileOutputStream = new FileOutputStream(file + "/" + fileName)) {
|
||||
targetWorkbook.write(fileOutputStream);
|
||||
// 载入数据库
|
||||
ExcelInfoEntity excelInfoEntity = new ExcelInfoEntity();
|
||||
excelInfoEntity
|
||||
.setFileName(String.valueOf(time))
|
||||
.setSheetName(sheet.getSheetName())
|
||||
.setType(0)
|
||||
.setCreateBy(getUploadFileData.getUserid())
|
||||
.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
||||
excelInfoMapper.insertExcelInfo(excelInfoEntity);
|
||||
returnResult = new GetFileTreeService().getFileTreeService(excelInfoMapper);
|
||||
}
|
||||
} catch (IOException | EncryptedDocumentException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 文件夹创建失败
|
||||
returnResult = ResultUtil.error(ErrorCode.FILE_CREATION_FAILED);
|
||||
}
|
||||
} else {
|
||||
// 文件存在
|
||||
returnResult = ResultUtil.error(ErrorCode.FILE_ALREADY_EXISTS);
|
||||
}
|
||||
} else {
|
||||
// 文件类型不正确
|
||||
returnResult = ResultUtil.error(ErrorCode.FILE_TYPE_IS_INCORRECT);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
65
src/main/java/com/wxjw/service/InsertTableService.java
Normal file
65
src/main/java/com/wxjw/service/InsertTableService.java
Normal file
@ -0,0 +1,65 @@
|
||||
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.insertTable.InsertTableData;
|
||||
import com.wxjw.dal.pojo.entity.ExcelInfoEntity;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Service 插入数据内容
|
||||
*
|
||||
* @author 筱锋xiao_lfeng
|
||||
*/
|
||||
@Getter
|
||||
@Service
|
||||
public class InsertTableService {
|
||||
private ResponseEntity<BaseResponse<Object>> operationStatus;
|
||||
// 暂未设计
|
||||
@Getter
|
||||
private String validationErrorMessage = null;
|
||||
|
||||
public void insertLogic(InsertTableData resultBody, ExcelInfoMapper excelInfoMapper) {
|
||||
// 搜索数据是否存在
|
||||
ArrayList<ExcelInfoEntity> excelInfoList = (ArrayList<ExcelInfoEntity>) excelInfoMapper.getAllExcelFilesNameNoRepetition(resultBody.getFile_name());
|
||||
if (!excelInfoList.isEmpty()) {
|
||||
// 检查数据是否匹配
|
||||
excelInfoList.forEach(excelInfoEntity -> {
|
||||
if (Objects.equals(excelInfoEntity.getSheetName(), resultBody.getSheet_name())) {
|
||||
validationErrorMessage = "数据重复";
|
||||
operationStatus = ResultUtil.error(ErrorCode.DATA_DUPLICATION);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
// 执行插入
|
||||
if (validationErrorMessage == null) {
|
||||
// 插入数据
|
||||
ExcelInfoEntity excelInfoEntity = new ExcelInfoEntity();
|
||||
excelInfoEntity
|
||||
.setFileName(resultBody.getFile_name())
|
||||
.setSheetName(resultBody.getSheet_name())
|
||||
.setTableName(resultBody.getTable_name())
|
||||
.setType(resultBody.getType())
|
||||
.setCreateBy(resultBody.getCreate_by())
|
||||
.setUpdateBy(resultBody.getUpdate_by())
|
||||
.setCreateTime(resultBody.getCreate_time())
|
||||
.setUpdateTime(resultBody.getUpdate_time());
|
||||
if (excelInfoMapper.insertExcelInfo(excelInfoEntity)) {
|
||||
operationStatus = ResultUtil.success();
|
||||
} else {
|
||||
operationStatus = ResultUtil.error(ErrorCode.DATA_WRITE_FAILURE);
|
||||
}
|
||||
} else {
|
||||
operationStatus = ResultUtil.error(ErrorCode.DATA_DUPLICATION);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user