ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/RequestParamGlobalFilter.java
@@ -61,6 +61,10 @@ if(method != HttpMethod.POST){ return chain.filter(exchange); } MediaType contentType = request.getHeaders().getContentType(); if (contentType == null || !MediaType.APPLICATION_JSON.isCompatibleWith(contentType)) { return chain.filter(exchange); } ServerHttpRequestDecorator httpRequestDecorator = requestDecorator(exchange); return chain.filter(exchange.mutate().request(httpRequestDecorator).build()); } ruoyi-service/ruoyi-chargingPile/src/main/java/com/ruoyi/chargingPile/service/impl/SiteServiceImpl.java
@@ -42,6 +42,7 @@ import com.ruoyi.system.api.feignClient.SysUserClient; import com.ruoyi.system.api.feignClient.SysUserRoleClient; import com.ruoyi.system.api.model.SysUserRoleVo; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; @@ -64,8 +65,11 @@ * @author zhibing.pu * @Date 2024/8/7 19:54 */ @Slf4j @Service public class SiteServiceImpl extends ServiceImpl<SiteMapper, Site> implements ISiteService { static final int SITE_IMPORT_TITLE_ROWS = 1; @Resource private TChargingGunService chargingGunService; @@ -182,10 +186,13 @@ ExcelUtil<SiteExcelDTO> util = new ExcelUtil<>(SiteExcelDTO.class); List<SiteExcelDTO> rows; try (InputStream inputStream = file.getInputStream()) { rows = util.importExcel(inputStream, 0); rows = util.importExcel(inputStream, SITE_IMPORT_TITLE_ROWS); } catch (Exception e) { throw new ServiceException("Excel文件已损坏或格式不正确,请重新下载站点导入模板后填写") log.error("站点Excel解析失败,fileName={},size={}", file.getOriginalFilename(), file.getSize(), e); ServiceException exception = new ServiceException("Excel文件已损坏或格式不正确,请重新下载站点导入模板后填写") .setDetailMessage(e.getMessage()); exception.initCause(e); throw exception; } if (rows == null || rows.isEmpty()) { throw new ServiceException("导入文件没有数据"); @@ -216,7 +223,7 @@ Set<String> duplicateCodes = new LinkedHashSet<>(); for (int i = 0; i < rows.size(); i++) { SiteExcelDTO row = rows.get(i); int rowNum = i + 2; int rowNum = i + SITE_IMPORT_TITLE_ROWS + 2; List<String> errors = validateSiteImportRow(row, partnerMap, strategyMap, regionByCode, regionById, roleType, objectId, fileCodes); String code = row == null ? null : normalizeImportValue(row.getCode()); ruoyi-service/ruoyi-chargingPile/src/test/java/com/ruoyi/chargingPile/service/impl/SiteImportExcelTest.java
@@ -3,6 +3,8 @@ import com.ruoyi.chargingPile.dto.SiteExcelDTO; import com.ruoyi.common.core.exception.ServiceException; import com.ruoyi.common.core.utils.poi.ExcelUtil; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.junit.jupiter.api.Test; @@ -10,9 +12,12 @@ import org.springframework.mock.web.MockMultipartFile; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.util.List; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -37,6 +42,40 @@ } @Test void generatedTemplateWithSecondRowHeaderCanBeImported() throws Exception { MockHttpServletResponse response = new MockHttpServletResponse(); ExcelUtil<SiteExcelDTO> exportUtil = new ExcelUtil<>(SiteExcelDTO.class); exportUtil.importTemplateExcel(response, "站点导入模板", "填写说明"); byte[] filledTemplate; try (Workbook workbook = WorkbookFactory.create( new ByteArrayInputStream(response.getContentAsByteArray())); ByteArrayOutputStream output = new ByteArrayOutputStream()) { Sheet sheet = workbook.getSheetAt(0); Row header = sheet.getRow(1); Row data = sheet.createRow(2); for (int column = 0; column < header.getPhysicalNumberOfCells(); column++) { String title = header.getCell(column).getStringCellValue(); if ("*站点编号".equals(title)) { data.createCell(column).setCellValue("SITE-IMPORT-001"); } else if ("*站点名称".equals(title)) { data.createCell(column).setCellValue("站点导入测试"); } } workbook.write(output); filledTemplate = output.toByteArray(); } ExcelUtil<SiteExcelDTO> importUtil = new ExcelUtil<>(SiteExcelDTO.class); List<SiteExcelDTO> rows = importUtil.importExcel( new ByteArrayInputStream(filledTemplate), SiteServiceImpl.SITE_IMPORT_TITLE_ROWS); assertEquals(1, rows.size()); assertEquals("SITE-IMPORT-001", rows.get(0).getCode()); assertEquals("站点导入测试", rows.get(0).getName()); } @Test void corruptWorkbookReturnsReadableBusinessError() { SiteServiceImpl service = new SiteServiceImpl(); MockMultipartFile file = new MockMultipartFile( @@ -48,5 +87,6 @@ () -> service.importSites(file, false)); assertEquals("Excel文件已损坏或格式不正确,请重新下载站点导入模板后填写", error.getMessage()); assertNotNull(error.getCause()); } }