完成匹配、跟价、权限部分
This commit is contained in:
@@ -1,13 +1,11 @@
|
||||
package com.nanri.aiimage.common.api;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Schema(description = "统一响应体")
|
||||
public class ApiResponse<T> {
|
||||
|
||||
@@ -20,6 +18,20 @@ public class ApiResponse<T> {
|
||||
@Schema(description = "响应数据")
|
||||
private T data;
|
||||
|
||||
@Schema(description = "业务错误码,成功时可为空")
|
||||
private Integer code;
|
||||
|
||||
public ApiResponse(boolean success, String message, T data) {
|
||||
this(success, message, data, null);
|
||||
}
|
||||
|
||||
public ApiResponse(boolean success, String message, T data, Integer code) {
|
||||
this.success = success;
|
||||
this.message = message;
|
||||
this.data = data;
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public static <T> ApiResponse<T> success(T data) {
|
||||
return new ApiResponse<>(true, "操作成功", data);
|
||||
}
|
||||
@@ -31,4 +43,8 @@ public class ApiResponse<T> {
|
||||
public static <T> ApiResponse<T> fail(String message) {
|
||||
return new ApiResponse<>(false, message, null);
|
||||
}
|
||||
|
||||
public static <T> ApiResponse<T> fail(Integer code, String message) {
|
||||
return new ApiResponse<>(false, message, null, code);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,18 @@ package com.nanri.aiimage.common.exception;
|
||||
|
||||
public class BusinessException extends RuntimeException {
|
||||
|
||||
private final Integer code;
|
||||
|
||||
public BusinessException(String message) {
|
||||
this(null, message);
|
||||
}
|
||||
|
||||
public BusinessException(Integer code, String message) {
|
||||
super(message);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,9 @@ public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(BusinessException.class)
|
||||
public ApiResponse<Void> handleBusinessException(BusinessException ex) {
|
||||
return ApiResponse.fail(ex.getMessage());
|
||||
return ex.getCode() == null
|
||||
? ApiResponse.fail(ex.getMessage())
|
||||
: ApiResponse.fail(ex.getCode(), ex.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
package com.nanri.aiimage.common.util;
|
||||
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import com.alibaba.excel.context.AnalysisContext;
|
||||
import com.alibaba.excel.event.AnalysisEventListener;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public final class ExcelStreamReader {
|
||||
|
||||
private ExcelStreamReader() {
|
||||
}
|
||||
|
||||
public static void readFirstSheet(File file, SheetRowHandler handler) throws IOException {
|
||||
try (InputStream inputStream = new FileInputStream(file)) {
|
||||
readFirstSheet(inputStream, handler);
|
||||
}
|
||||
}
|
||||
|
||||
public static void readFirstSheet(InputStream inputStream, SheetRowHandler handler) throws IOException {
|
||||
runRead(inputStream, false, handler);
|
||||
}
|
||||
|
||||
public static void readAllSheets(File file, SheetRowHandler handler) throws IOException {
|
||||
try (InputStream inputStream = new FileInputStream(file)) {
|
||||
readAllSheets(inputStream, handler);
|
||||
}
|
||||
}
|
||||
|
||||
public static void readAllSheets(InputStream inputStream, SheetRowHandler handler) throws IOException {
|
||||
runRead(inputStream, true, handler);
|
||||
}
|
||||
|
||||
private static void runRead(InputStream inputStream, boolean readAllSheets, SheetRowHandler handler) throws IOException {
|
||||
StreamingListener listener = new StreamingListener(handler);
|
||||
try {
|
||||
if (readAllSheets) {
|
||||
EasyExcel.read(inputStream, listener).headRowNumber(1).doReadAll();
|
||||
} else {
|
||||
EasyExcel.read(inputStream, listener).sheet(0).headRowNumber(1).doRead();
|
||||
}
|
||||
} catch (WrappedRuntimeException ex) {
|
||||
if (ex.getCause() instanceof IOException ioException) {
|
||||
throw ioException;
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
public interface SheetRowHandler {
|
||||
default void onHeader(String sheetName, Integer sheetNo, Map<Integer, String> headerMap) throws Exception {
|
||||
}
|
||||
|
||||
void onRow(String sheetName, Integer sheetNo, int rowIndex, Map<Integer, String> headerMap, Map<Integer, String> rowMap) throws Exception;
|
||||
}
|
||||
|
||||
private static final class StreamingListener extends AnalysisEventListener<Map<Integer, String>> {
|
||||
|
||||
private final SheetRowHandler handler;
|
||||
private Map<Integer, String> currentHeaderMap = Map.of();
|
||||
|
||||
private StreamingListener(SheetRowHandler handler) {
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
|
||||
Map<Integer, String> normalizedHeadMap = new LinkedHashMap<>();
|
||||
for (Map.Entry<Integer, String> entry : headMap.entrySet()) {
|
||||
normalizedHeadMap.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
currentHeaderMap = normalizedHeadMap;
|
||||
try {
|
||||
handler.onHeader(sheetName(context), sheetNo(context), currentHeaderMap);
|
||||
} catch (RuntimeException ex) {
|
||||
throw ex;
|
||||
} catch (Exception ex) {
|
||||
throw new WrappedRuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(Map<Integer, String> data, AnalysisContext context) {
|
||||
Map<Integer, String> normalizedRow = new LinkedHashMap<>();
|
||||
for (Map.Entry<Integer, String> entry : data.entrySet()) {
|
||||
normalizedRow.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
try {
|
||||
handler.onRow(
|
||||
sheetName(context),
|
||||
sheetNo(context),
|
||||
context.readRowHolder().getRowIndex(),
|
||||
currentHeaderMap,
|
||||
normalizedRow
|
||||
);
|
||||
} catch (RuntimeException ex) {
|
||||
throw ex;
|
||||
} catch (Exception ex) {
|
||||
throw new WrappedRuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doAfterAllAnalysed(AnalysisContext context) {
|
||||
}
|
||||
|
||||
private String sheetName(AnalysisContext context) {
|
||||
return context.readSheetHolder() == null ? "" : context.readSheetHolder().getSheetName();
|
||||
}
|
||||
|
||||
private Integer sheetNo(AnalysisContext context) {
|
||||
return context.readSheetHolder() == null ? null : context.readSheetHolder().getSheetNo();
|
||||
}
|
||||
}
|
||||
|
||||
private static final class WrappedRuntimeException extends RuntimeException {
|
||||
private WrappedRuntimeException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user