mirror of
http://192.168.1.205:9980/cf_devdept2/cf_imes_server.git
synced 2026-08-12 21:02:08 +08:00
文件导入错误信息返回
This commit is contained in:
+3
-24
@@ -4,21 +4,21 @@ import com.alibaba.excel.EasyExcelFactory;
|
|||||||
import com.alibaba.excel.ExcelWriter;
|
import com.alibaba.excel.ExcelWriter;
|
||||||
import com.alibaba.excel.write.metadata.WriteSheet;
|
import com.alibaba.excel.write.metadata.WriteSheet;
|
||||||
import com.alibaba.excel.write.metadata.fill.FillConfig;
|
import com.alibaba.excel.write.metadata.fill.FillConfig;
|
||||||
import com.cf.imes.module.executor.dal.dataobject.order.OrderDO;
|
|
||||||
import com.cf.imes.module.executor.util.fileConversion.admin.files.excel.OrderPlateImportExcelVO;
|
import com.cf.imes.module.executor.util.fileConversion.admin.files.excel.OrderPlateImportExcelVO;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.springframework.util.ResourceUtils;
|
import org.springframework.util.ResourceUtils;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.servlet.ServletOutputStream;
|
import javax.servlet.ServletOutputStream;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.time.format.DateTimeFormatter;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* excel 文件写入
|
* excel 文件写入
|
||||||
*/
|
*/
|
||||||
@@ -60,27 +60,6 @@ public class ExcelWriterUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 填充订单数据
|
|
||||||
private static Map<String, String> fillOrderData(OrderDO order) {
|
|
||||||
Map<String, String> orderMap = new HashMap<>();
|
|
||||||
orderMap.put("customOrderNo", order.getCustomOrderNo());
|
|
||||||
orderMap.put("orderId", order.getId().toString());
|
|
||||||
orderMap.put("orderData", order.getOrderDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
|
||||||
orderMap.put("customer", order.getCustomer());
|
|
||||||
orderMap.put("address", order.getAddress());
|
|
||||||
orderMap.put("phoneNumber", order.getPhoneNumber());
|
|
||||||
orderMap.put("remark", order.getRemark());
|
|
||||||
orderMap.put("splitter", order.getSplitter());
|
|
||||||
orderMap.put("salesman", order.getSalesman());
|
|
||||||
orderMap.put("dealerPhoneNumber", order.getDealerPhoneNumber());
|
|
||||||
orderMap.put("dealer", order.getDealer());
|
|
||||||
orderMap.put("deliveryDate", order.getDeliveryDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
|
||||||
return orderMap;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 填充配置
|
|
||||||
private static FillConfig fillConfig() {
|
|
||||||
return FillConfig.builder().forceNewRow(Boolean.TRUE).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+4
@@ -13,4 +13,8 @@ public class ErrorCodeConstants {
|
|||||||
public static final ErrorCode PARTS_UNIT_NOT_NULL = new ErrorCode(1_004_001_002, "配件-组件或五金或封边条,单位禁止为空");
|
public static final ErrorCode PARTS_UNIT_NOT_NULL = new ErrorCode(1_004_001_002, "配件-组件或五金或封边条,单位禁止为空");
|
||||||
public static final ErrorCode PARTS_EDGE_NOT_NULL = new ErrorCode(1_004_001_003, "配件-封边条颜色、宽厚禁止为空");
|
public static final ErrorCode PARTS_EDGE_NOT_NULL = new ErrorCode(1_004_001_003, "配件-封边条颜色、宽厚禁止为空");
|
||||||
|
|
||||||
|
public static final ErrorCode FILE_NULL = new ErrorCode(1_004_001_004, "上传文件为空");
|
||||||
|
public static final ErrorCode FILE_FORMAT_ERROR = new ErrorCode(1_004_001_005, "导入文件格式有误");
|
||||||
|
public static final ErrorCode FILE_EXCEED_SIZE = new ErrorCode(1_004_001_006, "文件大小超过 10M");
|
||||||
|
public static final ErrorCode FILE_CONTENT_NULL = new ErrorCode(1_004_001_007, "文件大小为空");
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-6
@@ -1,7 +1,8 @@
|
|||||||
package com.cf.imes.module.manage.controller.admin.plate;
|
package com.cf.imes.module.manage.controller.admin.plate;
|
||||||
|
|
||||||
import com.cf.imes.module.manage.controller.admin.plate.vo.plate.*;
|
import com.cf.imes.module.manage.controller.admin.plate.vo.plate.*;
|
||||||
import com.cf.imes.module.manage.service.plate.PlateExcelUtil;
|
import com.cf.imes.module.manage.service.plate.PlateExcelListener;
|
||||||
|
import com.cf.imes.module.manage.util.file.ExcelReadUtil;
|
||||||
import io.swagger.v3.oas.annotations.Parameters;
|
import io.swagger.v3.oas.annotations.Parameters;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
@@ -32,7 +33,6 @@ import org.springframework.web.multipart.MultipartFile;
|
|||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import javax.validation.constraints.NotEmpty;
|
|
||||||
|
|
||||||
@Tag(name = "管理后台 - 板材库管理")
|
@Tag(name = "管理后台 - 板材库管理")
|
||||||
@RestController
|
@RestController
|
||||||
@@ -149,10 +149,12 @@ public class PlateController {
|
|||||||
@RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport,
|
@RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport,
|
||||||
@RequestParam(value = "organId") Long organId,
|
@RequestParam(value = "organId") Long organId,
|
||||||
HttpServletResponse response) throws Exception {
|
HttpServletResponse response) throws Exception {
|
||||||
PlateExcelUtil plateExcelUtil = new PlateExcelUtil();
|
ExcelReadUtil excelReadUtil = new ExcelReadUtil();
|
||||||
List<PlateImportExcelVO> list = plateExcelUtil.read(file, PlateImportExcelVO.class);
|
PlateExcelListener listener = new PlateExcelListener();
|
||||||
|
List<PlateImportExcelVO> list = excelReadUtil.read(file, PlateImportExcelVO.class, listener);
|
||||||
|
|
||||||
// 判断文件是否导入成功
|
// 判断文件是否导入成功
|
||||||
if (!plateExcelUtil.getFlag())
|
if (!excelReadUtil.getFlag())
|
||||||
ExcelUtils.write(response, IMPORT_TEMPLATE, "板材列表", PlateImportExcelVO.class, list);
|
ExcelUtils.write(response, IMPORT_TEMPLATE, "板材列表", PlateImportExcelVO.class, list);
|
||||||
else {
|
else {
|
||||||
if (list.isEmpty()){
|
if (list.isEmpty()){
|
||||||
@@ -164,7 +166,7 @@ public class PlateController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
plateExcelUtil.clear();
|
// plateExcelUtil.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+2
-2
@@ -1,14 +1,14 @@
|
|||||||
package com.cf.imes.module.manage.service.plate;
|
package com.cf.imes.module.manage.service.plate;
|
||||||
|
|
||||||
import com.alibaba.excel.context.AnalysisContext;
|
import com.alibaba.excel.context.AnalysisContext;
|
||||||
import com.alibaba.excel.event.AnalysisEventListener;
|
|
||||||
import com.cf.imes.module.manage.controller.admin.plate.vo.plate.PlateImportExcelVO;
|
import com.cf.imes.module.manage.controller.admin.plate.vo.plate.PlateImportExcelVO;
|
||||||
|
import com.cf.imes.module.manage.util.file.ExcelListener;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public final class PlateExcelListener<T> extends AnalysisEventListener<PlateImportExcelVO> {
|
public final class PlateExcelListener<T> extends ExcelListener<PlateImportExcelVO> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自定义用于暂时存储data
|
* 自定义用于暂时存储data
|
||||||
|
|||||||
-59
@@ -1,59 +0,0 @@
|
|||||||
package com.cf.imes.module.manage.service.plate;
|
|
||||||
|
|
||||||
import com.alibaba.excel.EasyExcelFactory;
|
|
||||||
import com.cf.imes.module.manage.controller.admin.plate.vo.plate.PlateImportExcelVO;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
public class PlateExcelUtil {
|
|
||||||
|
|
||||||
private String value;
|
|
||||||
|
|
||||||
private PlateExcelListener<PlateImportExcelVO> excelListener = new PlateExcelListener<>();
|
|
||||||
|
|
||||||
public List<PlateImportExcelVO> read(MultipartFile file, Class<PlateImportExcelVO> head) throws IOException {
|
|
||||||
|
|
||||||
if (file.isEmpty()) {
|
|
||||||
value = "文件为空";
|
|
||||||
throw new IOException("文件为空");
|
|
||||||
} else if (!(file.getOriginalFilename().endsWith(".xlsx") || file.getOriginalFilename().endsWith(".xls"))) {
|
|
||||||
value = "文件格式不正确";
|
|
||||||
throw new IOException("文件格式不正确");
|
|
||||||
} else if (file.getSize() > 1024 * 1024 * 10) {
|
|
||||||
value = "文件大小超过 10M";
|
|
||||||
throw new IOException("文件大小超过 10M");
|
|
||||||
} else if (file.getSize() == 0) {
|
|
||||||
value = "文件大小为 0";
|
|
||||||
throw new IOException("文件大小为 0");
|
|
||||||
} else {
|
|
||||||
EasyExcelFactory.read(file.getInputStream(), PlateImportExcelVO.class, excelListener)
|
|
||||||
.autoCloseStream(true) // 不要自动关闭,交给 Servlet 自己处理
|
|
||||||
.headRowNumber(1).sheet(0).doRead();
|
|
||||||
//获取读取的数据
|
|
||||||
List<PlateImportExcelVO> list = excelListener.getDatas();
|
|
||||||
|
|
||||||
if (excelListener.getValueMap().size() > 0)
|
|
||||||
excelListener.getValueMap().forEach((k, v) -> {
|
|
||||||
list.get(k - 1).setResult(v);
|
|
||||||
});
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean getFlag() {
|
|
||||||
return excelListener.getFlag();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clear() {
|
|
||||||
excelListener.getDatas().clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<Integer, String> getValueMap() {
|
|
||||||
return excelListener.getValueMap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+6
-18
@@ -5,6 +5,8 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -18,25 +20,10 @@ public final class ExcelReadUtil{
|
|||||||
|
|
||||||
public <T> List<T> read(MultipartFile file, Class<T> head, ExcelListener<T> listener) throws IOException {
|
public <T> List<T> read(MultipartFile file, Class<T> head, ExcelListener<T> listener) throws IOException {
|
||||||
value = "";
|
value = "";
|
||||||
flag = true;
|
Long fileSize = 1024 * 1024 * 10L; // 10 MB
|
||||||
|
flag = FileHelperUtil.checkFile(file, fileSize);
|
||||||
|
|
||||||
if (file.isEmpty()) {
|
if (flag) {
|
||||||
value = "文件为空";
|
|
||||||
flag = false;
|
|
||||||
throw new IOException("文件为空");
|
|
||||||
} else if (!(file.getOriginalFilename().endsWith(".xlsx") || file.getOriginalFilename().endsWith(".xls"))) {
|
|
||||||
value = "文件格式不正确";
|
|
||||||
flag = false;
|
|
||||||
throw new IOException("文件格式不正确");
|
|
||||||
} else if (file.getSize() > 1024 * 1024 * 10) {
|
|
||||||
value = "文件大小超过 10M";
|
|
||||||
flag = false;
|
|
||||||
throw new IOException("文件大小超过 10M");
|
|
||||||
} else if (file.getSize() == 0) {
|
|
||||||
value = "文件大小为 0";
|
|
||||||
flag = false;
|
|
||||||
throw new IOException("文件大小为 0");
|
|
||||||
} else {
|
|
||||||
EasyExcelFactory.read(file.getInputStream(), head, listener)
|
EasyExcelFactory.read(file.getInputStream(), head, listener)
|
||||||
.autoCloseStream(true) // 不要自动关闭,交给 Servlet 自己处理
|
.autoCloseStream(true) // 不要自动关闭,交给 Servlet 自己处理
|
||||||
.headRowNumber(1).sheet(0).doRead();
|
.headRowNumber(1).sheet(0).doRead();
|
||||||
@@ -48,6 +35,7 @@ public final class ExcelReadUtil{
|
|||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getValue() {
|
public String getValue() {
|
||||||
|
|||||||
+22
-2
@@ -1,6 +1,6 @@
|
|||||||
package com.cf.imes.module.manage.util.file;
|
package com.cf.imes.module.manage.util.file;
|
||||||
|
|
||||||
import org.springframework.boot.system.ApplicationHome;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
@@ -8,9 +8,10 @@ import java.net.URLEncoder;
|
|||||||
|
|
||||||
import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.exception;
|
import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||||
import static com.cf.imes.module.infra.enums.ErrorCodeConstants.FILE_NOT_EXISTS;
|
import static com.cf.imes.module.infra.enums.ErrorCodeConstants.FILE_NOT_EXISTS;
|
||||||
|
import static com.cf.imes.module.manage.enums.ErrorCodeConstants.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件上传下载相工具类
|
* 文件相关工具类
|
||||||
*/
|
*/
|
||||||
public class FileHelperUtil {
|
public class FileHelperUtil {
|
||||||
|
|
||||||
@@ -38,4 +39,23 @@ public class FileHelperUtil {
|
|||||||
throw exception(FILE_NOT_EXISTS);
|
throw exception(FILE_NOT_EXISTS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 文件条件判断
|
||||||
|
public static boolean checkFile(MultipartFile file,
|
||||||
|
Long fileSize) {
|
||||||
|
if (file.isEmpty()) {
|
||||||
|
throw exception(FILE_NULL);
|
||||||
|
}
|
||||||
|
if (!(file.getOriginalFilename().endsWith(".xlsx") || file.getOriginalFilename().endsWith(".xls"))) {
|
||||||
|
throw exception(FILE_FORMAT_ERROR);
|
||||||
|
}
|
||||||
|
if (file.getSize() > fileSize) {
|
||||||
|
throw exception(FILE_EXCEED_SIZE);
|
||||||
|
}
|
||||||
|
if (file.getSize() == 0) {
|
||||||
|
throw exception(FILE_CONTENT_NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user