From fcefe41e05439f548a58c7e5b6aa6e58f7b80ada Mon Sep 17 00:00:00 2001 From: hcong <1050828145@qq.com> Date: 星期一, 23 十二月 2024 11:57:46 +0800 Subject: [PATCH] 1. 新增接口网格数据excel模板、网格数据excel导入 2. 接口解析网格数据抛出相关excel报错信息 3. FileExchange.kt新增excel转换到GridDataDetail列表方法 4. 新增导入excel的单元测试代码 --- src/main/kotlin/com/flightfeather/uav/lightshare/bean/GridDataImportResult.kt | 12 + src/test/resources/templates/GridData-1_type_error.xlsx | 0 src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt | 47 ++++++ src/test/resources/templates/GridData-index_short.xlsx | 0 src/main/kotlin/com/flightfeather/uav/domain/entity/GridData.java | 1 src/main/kotlin/com/flightfeather/uav/domain/repository/SatelliteGridRep.kt | 13 + src/test/kotlin/com/flightfeather/uav/common/utils/FileExchangeTest.java | 198 ++++++++++++++++++++++++++++ src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/SatelliteTelemetryServiceImpl.kt | 71 ++++++++++ src/main/resources/templates/GridData-PM2.5-Template.xlsx | 0 src/test/resources/templates/GridData-2_type_error.xlsx | 0 src/test/resources/templates/GridData-success-update.xlsx | 0 src/main/kotlin/com/flightfeather/uav/lightshare/service/SatelliteTelemetryService.kt | 9 + src/main/kotlin/com/flightfeather/uav/lightshare/web/SatelliteTelemetryController.kt | 22 +++ src/test/resources/templates/GridData-index_out.xlsx | 0 src/main/kotlin/com/flightfeather/uav/domain/mapper/GridDataDetailMapper.kt | 4 src/main/resources/mapper/GridDataDetailMapper.xml | 11 + src/test/resources/templates/GridData-success-insert.xlsx | 0 src/test/resources/templates/GridData-fileType-error.txt | 0 18 files changed, 387 insertions(+), 1 deletions(-) diff --git a/src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt b/src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt index ea62b09..c9c666a 100644 --- a/src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt +++ b/src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt @@ -2,6 +2,7 @@ import com.alibaba.fastjson.JSONObject import com.flightfeather.uav.common.exception.BizException +import com.flightfeather.uav.domain.entity.GridDataDetail import com.flightfeather.uav.domain.entity.RealTimeData import com.flightfeather.uav.domain.entity.RealTimeDataVehicle import com.flightfeather.uav.socket.bean.AirData @@ -93,6 +94,52 @@ } /** + * 杞崲涓篜M2.5琛ㄦ牸鏁版嵁 + */ + fun exchangeGridData(file: InputStream): List<GridDataDetail> { + val headers = listOf( + "pointid", + "PM2.5" + ) + val result = mutableListOf<GridDataDetail>() + try { + ExcelUtil.readXLXS(file, headerCheck = { + val cellIterator = it.cellIterator() + val headIterator = headers.iterator() + while (headIterator.hasNext()) { + val head = headIterator.next() + if (cellIterator.hasNext()) { + val cellText = cellIterator.next().stringCellValue + if (!cellText.equals(head)) { + throw BizException("鏂囦欢鏍煎紡閿欒, 琛ㄥご[${head}]搴旇涓篬${cellText}]") + } + } else { + throw BizException("鏂囦欢鏍煎紡閿欒, 琛ㄥご[${head}]缂哄け") + } + } + true + }, onRow = { + val data = GridDataDetail().apply { + try { + this.cellId = it.getCell(0)?.numericCellValue?.toInt() + } catch (e: Exception) { + throw BizException("鍗曞厓鏍糩${it.rowNum}${0}]涓嶆槸鏁板瓧绫诲瀷") + } + try { + this.pm25 = it.getCell(1)?.numericCellValue?.toFloat() + } catch (e: Exception) { + throw BizException("鍗曞厓鏍糩${it.rowNum}${1}]涓嶆槸鏁板瓧绫诲瀷") + } + } + result.add(data) + }) + } catch (e: BizException) { + throw e + } + return result + } + + /** * 杞崲杞﹁浇璧拌埅鏁版嵁 */ fun exchangeVehicleData(deviceCode: String, file: InputStream): List<RealTimeDataVehicle> { diff --git a/src/main/kotlin/com/flightfeather/uav/domain/entity/GridData.java b/src/main/kotlin/com/flightfeather/uav/domain/entity/GridData.java index e008649..2273b64 100644 --- a/src/main/kotlin/com/flightfeather/uav/domain/entity/GridData.java +++ b/src/main/kotlin/com/flightfeather/uav/domain/entity/GridData.java @@ -6,6 +6,7 @@ @Table(name = "grid_data") public class GridData { @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; /** diff --git a/src/main/kotlin/com/flightfeather/uav/domain/mapper/GridDataDetailMapper.kt b/src/main/kotlin/com/flightfeather/uav/domain/mapper/GridDataDetailMapper.kt index 1a6f152..cf4437e 100644 --- a/src/main/kotlin/com/flightfeather/uav/domain/mapper/GridDataDetailMapper.kt +++ b/src/main/kotlin/com/flightfeather/uav/domain/mapper/GridDataDetailMapper.kt @@ -5,4 +5,6 @@ import org.apache.ibatis.annotations.Mapper @Mapper -interface GridDataDetailMapper : MyMapper<GridDataDetail?> \ No newline at end of file +interface GridDataDetailMapper : MyMapper<GridDataDetail?> { + fun updatePM25Batch(gridDataDetails: List<GridDataDetail>) +} \ No newline at end of file diff --git a/src/main/kotlin/com/flightfeather/uav/domain/repository/SatelliteGridRep.kt b/src/main/kotlin/com/flightfeather/uav/domain/repository/SatelliteGridRep.kt index 2c0db63..84df65a 100644 --- a/src/main/kotlin/com/flightfeather/uav/domain/repository/SatelliteGridRep.kt +++ b/src/main/kotlin/com/flightfeather/uav/domain/repository/SatelliteGridRep.kt @@ -59,4 +59,17 @@ orderBy("cellId") }) } + + fun insertGridDataAndDetail(data: GridData, gridDataDetails: List<GridDataDetail>) { + gridDataMapper.insert(data) + gridDataDetails.forEach { + it.dataId = data.id + it.groupId = data.groupId + } + gridDataDetailMapper.insertList(gridDataDetails) + } + + fun updatePM25Batch(gridDataDetails: List<GridDataDetail>) { + gridDataDetailMapper.updatePM25Batch(gridDataDetails) + } } \ No newline at end of file diff --git a/src/main/kotlin/com/flightfeather/uav/lightshare/bean/GridDataImportResult.kt b/src/main/kotlin/com/flightfeather/uav/lightshare/bean/GridDataImportResult.kt new file mode 100644 index 0000000..7afad90 --- /dev/null +++ b/src/main/kotlin/com/flightfeather/uav/lightshare/bean/GridDataImportResult.kt @@ -0,0 +1,12 @@ +package com.flightfeather.uav.lightshare.bean + +import com.fasterxml.jackson.annotation.JsonInclude + +/** + * 缃戞牸鏁版嵁瀵煎叆澶勭悊缁撴灉 + */ +@JsonInclude(JsonInclude.Include.NON_NULL) +data class GridDataImportResult( + val success: Boolean, + val result: String +) \ No newline at end of file diff --git a/src/main/kotlin/com/flightfeather/uav/lightshare/service/SatelliteTelemetryService.kt b/src/main/kotlin/com/flightfeather/uav/lightshare/service/SatelliteTelemetryService.kt index 8263119..1f05f0d 100644 --- a/src/main/kotlin/com/flightfeather/uav/lightshare/service/SatelliteTelemetryService.kt +++ b/src/main/kotlin/com/flightfeather/uav/lightshare/service/SatelliteTelemetryService.kt @@ -1,12 +1,16 @@ package com.flightfeather.uav.lightshare.service +import com.flightfeather.uav.common.exception.BizException import com.flightfeather.uav.domain.entity.GridCell import com.flightfeather.uav.domain.entity.GridData import com.flightfeather.uav.domain.entity.GridDataDetail import com.flightfeather.uav.domain.entity.GridGroup import com.flightfeather.uav.lightshare.bean.AreaVo import com.flightfeather.uav.lightshare.bean.DataHead +import com.flightfeather.uav.lightshare.bean.GridDataImportResult +import org.springframework.web.multipart.MultipartFile import java.time.LocalDateTime +import javax.servlet.http.HttpServletResponse /** * @@ -22,4 +26,9 @@ fun fetchGridData(groupId: Int, dataTime: LocalDateTime?, type: Int?): List<GridData?> fun fetchGridDataDetail(dataId: Int, groupId: Int?, cellId: Int?): List<GridDataDetail?> + + @Throws(BizException::class) + fun importGridData(groupId: Int, type: Int?, dataTime: LocalDateTime?, update: Int?, file: MultipartFile): GridDataImportResult? + + fun downloadTemplate(response: HttpServletResponse): Boolean } \ No newline at end of file diff --git a/src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/SatelliteTelemetryServiceImpl.kt b/src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/SatelliteTelemetryServiceImpl.kt index 60ef723..c28b246 100644 --- a/src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/SatelliteTelemetryServiceImpl.kt +++ b/src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/SatelliteTelemetryServiceImpl.kt @@ -1,5 +1,7 @@ package com.flightfeather.uav.lightshare.service.impl +import com.flightfeather.uav.common.exception.BizException +import com.flightfeather.uav.common.utils.FileExchange import com.flightfeather.uav.domain.entity.GridCell import com.flightfeather.uav.domain.entity.GridData import com.flightfeather.uav.domain.entity.GridDataDetail @@ -7,10 +9,17 @@ import com.flightfeather.uav.domain.repository.SatelliteGridRep import com.flightfeather.uav.lightshare.bean.AreaVo import com.flightfeather.uav.lightshare.bean.DataHead +import com.flightfeather.uav.lightshare.bean.GridDataImportResult import com.flightfeather.uav.lightshare.service.SatelliteTelemetryService import com.github.pagehelper.PageHelper import org.springframework.stereotype.Service +import org.springframework.web.multipart.MultipartFile +import java.io.ByteArrayInputStream +import java.io.File import java.time.LocalDateTime +import java.time.ZoneId +import java.util.* +import javax.servlet.http.HttpServletResponse /** * @@ -20,6 +29,7 @@ @Service class SatelliteTelemetryServiceImpl(private val satelliteGridRep: SatelliteGridRep) : SatelliteTelemetryService { + private val fileExchange = FileExchange() override fun fetchGridGroup(areaVo: AreaVo, page: Int?, perPage: Int?): Pair<DataHead, List<GridGroup?>> { val pageInfo = PageHelper.startPage<GridGroup>(page ?: 1, perPage ?: 100) val res = satelliteGridRep.fetchGridGroup(areaVo) @@ -37,4 +47,65 @@ override fun fetchGridDataDetail(dataId: Int, groupId: Int?, cellId: Int?): List<GridDataDetail?> { return satelliteGridRep.fetchGridDataDetail(dataId, groupId, cellId) } + + override fun importGridData(groupId: Int, type: Int?, dataTime: LocalDateTime?, update: Int?, file: MultipartFile): GridDataImportResult? { + val gridData = satelliteGridRep.fetchGridData(groupId, dataTime, type) + if (!file.contentType!!.startsWith("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) { + throw BizException("鏂囦欢绫诲瀷閿欒锛岃涓婁紶xlsx绫诲瀷鏂囦欢") + } + if (gridData.isNotEmpty() && update == 0) { + return GridDataImportResult(false, "鏁版嵁搴撳搴旂綉鏍肩粍鍜屾棩鏈熶笅宸插瓨鍦ㄩ仴娴嬫暟鎹�") + } + val exchangeGridDataDetails + = fileExchange.exchangeGridData(ByteArrayInputStream(file.bytes)) + val gridCellsInDB = satelliteGridRep.fetchGridCell(groupId) + val setA = gridCellsInDB.asSequence().map { it?.id ?: -1 }.toSet() + val setB = exchangeGridDataDetails.asSequence().map { it.cellId ?: -1 }.toSet() + // excel 涓己灏戠殑鐨勫崟鍏冩牸 + val onlyInA = setA - setB + // excel 涓鍑虹殑鐨勫崟鍏冩牸 + val onlyInB = setB - setA + if (onlyInA.isNotEmpty()) { + throw BizException("瀵煎叆鏁版嵁涓己灏戜互涓嬬綉鏍煎崟鍏冩牸锛�${onlyInA.joinToString(",")}") + } + if (onlyInB.isNotEmpty()) { + throw BizException("瀵煎叆鏁版嵁涓湁澶氫綑缃戞牸鍗曞厓鏍硷細${onlyInB.joinToString(",")}") + } + if (update == 1) { + exchangeGridDataDetails.forEach { + it.dataId = gridData[0]?.id + it.groupId = gridData[0]?.groupId + } + satelliteGridRep.updatePM25Batch(exchangeGridDataDetails) + return GridDataImportResult(true, "瑕嗙洊鎴愬姛") + } + val gridDataEntity = GridData() + gridDataEntity.groupId = groupId + gridDataEntity.dataTime = dataTime?.atZone(ZoneId.systemDefault())?.toInstant()?.toEpochMilli() + ?.let { Date(it) } + gridDataEntity.type = type?.toByte() + satelliteGridRep.insertGridDataAndDetail(gridDataEntity, exchangeGridDataDetails) + return GridDataImportResult(true, "瀵煎叆鎴愬姛") + } + + override fun downloadTemplate(response: HttpServletResponse): Boolean { + val fileName = "GridData-PM2.5-Template.xlsx" + val path = (Thread.currentThread().contextClassLoader?.getResource("/")?.path + ?: "src/main/resources") + "/templates/" + fileName + val file = File(path) + if (file.exists()) { + val fName = Base64.getEncoder().encodeToString(fileName.toByteArray()) + response.apply { + setHeader("Content-Disposition", "attachment;filename=$fName") + setHeader("fileName", fName) + addHeader("Access-Control-Expose-Headers", "fileName") + contentType = "application/vnd.ms-excel;charset=UTF-8" + setHeader("Pragma", "no-cache") + setHeader("Cache-Control", "no-cache") + setDateHeader("Expires", 0) + } + response.outputStream.write(file.readBytes()) + } + return true + } } \ No newline at end of file diff --git a/src/main/kotlin/com/flightfeather/uav/lightshare/web/SatelliteTelemetryController.kt b/src/main/kotlin/com/flightfeather/uav/lightshare/web/SatelliteTelemetryController.kt index 2ac67d6..b1f9225 100644 --- a/src/main/kotlin/com/flightfeather/uav/lightshare/web/SatelliteTelemetryController.kt +++ b/src/main/kotlin/com/flightfeather/uav/lightshare/web/SatelliteTelemetryController.kt @@ -7,7 +7,11 @@ import io.swagger.annotations.ApiOperation import io.swagger.annotations.ApiParam import org.springframework.web.bind.annotation.* +import org.springframework.web.multipart.MultipartFile +import springfox.documentation.annotations.ApiIgnore import java.time.LocalDateTime +import java.time.format.DateTimeFormatter +import javax.servlet.http.HttpServletResponse /** * 鍗槦閬ユ祴 @@ -49,4 +53,22 @@ @ApiParam("缃戞牸缁刬d") @RequestParam(required = false) groupId: Int?, @ApiParam("缃戞牸鍗曞厓鏍糹d") @RequestParam(required = false) cellId: Int?, ) = resPack { satelliteTelemetryService.fetchGridDataDetail(dataId, groupId, cellId) } + + + @ApiOperation(value = "瀵煎叆鍗槦閬ユ祴PM2.5缁撴灉鏁版嵁") + @PostMapping("/import/grid/data") + fun importGridData( + @ApiParam("缃戞牸缁刬d") @RequestParam groupId: Int, + @ApiParam("閬ユ祴鏁版嵁绫诲瀷", allowableValues = "0锛氬師濮嬪崼鏄熼仴娴嬫暟鎹紱1锛氳瀺鍚堟暟鎹�") @RequestParam(required = false) type: Int?, + @ApiParam("閬ユ祴鏁版嵁鏃堕棿") + @RequestParam @JsonFormat(pattern = "YYYY-MM-DD HH:mm:ss") dateTime: String?, + @ApiParam("瑕嗙洊鏃ф暟鎹� 0: 涓嶈鐩� 1: 瑕嗙洊") @RequestParam update: Int?, + @RequestParam("excel") file: MultipartFile, + ) = resPack { + satelliteTelemetryService.importGridData(groupId, type, LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), update, file) + } + + @ApiOperation(value = "涓嬭浇鍗槦閬ユ祴PM2.5缁撴灉鏁版嵁瀵煎叆妯℃澘") + @GetMapping("/import/grid/data/download/template") + fun downloadTemplate(@ApiIgnore response: HttpServletResponse) = satelliteTelemetryService.downloadTemplate(response) } \ No newline at end of file diff --git a/src/main/resources/mapper/GridDataDetailMapper.xml b/src/main/resources/mapper/GridDataDetailMapper.xml index 6312fe3..7574d75 100644 --- a/src/main/resources/mapper/GridDataDetailMapper.xml +++ b/src/main/resources/mapper/GridDataDetailMapper.xml @@ -18,4 +18,15 @@ --> id, data_id, group_id, cell_id, PM25, rank </sql> + + <!-- 娣诲姞鎵归噺鏇存柊PM25鐨勫嚱鏁� --> + <update id="updatePM25Batch" parameterType="java.util.List"> + <foreach collection="list" item="item" separator=";"> + UPDATE grid_data_detail + SET PM25 = #{item.pm25} + WHERE data_id = #{item.dataId} + AND group_id = #{item.groupId} + AND cell_id = #{item.cellId} + </foreach> + </update> </mapper> \ No newline at end of file diff --git a/src/main/resources/templates/GridData-PM2.5-Template.xlsx b/src/main/resources/templates/GridData-PM2.5-Template.xlsx new file mode 100644 index 0000000..e502991 --- /dev/null +++ b/src/main/resources/templates/GridData-PM2.5-Template.xlsx Binary files differ diff --git a/src/test/kotlin/com/flightfeather/uav/common/utils/FileExchangeTest.java b/src/test/kotlin/com/flightfeather/uav/common/utils/FileExchangeTest.java new file mode 100644 index 0000000..ea696a6 --- /dev/null +++ b/src/test/kotlin/com/flightfeather/uav/common/utils/FileExchangeTest.java @@ -0,0 +1,198 @@ +package com.flightfeather.uav.common.utils; + +import com.flightfeather.uav.common.exception.BizException; +import com.flightfeather.uav.domain.entity.GridDataDetail; +import com.flightfeather.uav.lightshare.service.SatelliteTelemetryService; +import org.apache.commons.fileupload.disk.DiskFileItem; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.multipart.commons.CommonsMultipartFile; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.net.URL; +import java.time.LocalDateTime; +import java.util.List; +import java.util.Objects; +import java.util.function.Function; + +import static org.junit.Assert.assertEquals; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class FileExchangeTest { + + @Autowired + private SatelliteTelemetryService satelliteTelemetryService; + + public void start() throws BizException, IOException { + test1(); + test2(); + test3(); + test4(); + test5(); + test6(); + test7(); + } + +// 绗竴鍒楁暟瀛楃被鍨嬮敊璇� + @Test + public void test1() throws IOException, BizException { + URL resource = getClass().getClassLoader().getResource("templates/GridData-1_type_error.xlsx"); + if (resource != null) { + System.out.println(resource.getPath()); + // 鍒涘缓鏂囦欢杈撳叆娴� + FileInputStream inputStream = new FileInputStream(new File(resource.getPath())); + // 鍒涘缓MockMultipartFile瀵硅薄 + MockMultipartFile multipartFile = new MockMultipartFile("file", "111.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", inputStream); + try { + satelliteTelemetryService.importGridData(1, 1, LocalDateTime.of(2029, 7, 29, 0, 0), 0, multipartFile); + }catch (BizException e) { + // 楠岃瘉寮傚父娑堟伅鏄惁涓庨鏈熺浉绗� + if (!e.getMessage().contains("涓嶆槸鏁板瓧绫诲瀷")) { + System.out.println(e.getMessage()); + throw e; + } + } + } else { + System.out.println("璧勬簮鏂囦欢鏈壘鍒�"); + } + } + // 绗簩鍒楁暟瀛楃被鍨嬮敊璇� + @Test + public void test2() throws IOException, BizException { + URL resource = getClass().getClassLoader().getResource("templates/GridData-2_type_error.xlsx"); + if (resource != null) { + System.out.println(resource.getPath()); + // 鍒涘缓鏂囦欢杈撳叆娴� + FileInputStream inputStream = new FileInputStream(new File(resource.getPath())); + // 鍒涘缓MockMultipartFile瀵硅薄 + MockMultipartFile multipartFile = new MockMultipartFile("file", "111.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", inputStream); + try { + satelliteTelemetryService.importGridData(1, 1, LocalDateTime.of(2029, 7, 29, 0, 0), 0, multipartFile); + }catch (BizException e) { + // 楠岃瘉寮傚父娑堟伅鏄惁涓庨鏈熺浉绗� + if (!e.getMessage().contains("涓嶆槸鏁板瓧绫诲瀷")) { + System.out.println(e.getMessage()); + throw e; + } + } + } else { + System.out.println("璧勬簮鏂囦欢鏈壘鍒�"); + } + } + // 缃戞牸缂栧彿瓒婄晫閿欒 + @Test + public void test3() throws IOException, BizException { + URL resource = getClass().getClassLoader().getResource("templates/GridData-index_out.xlsx"); + if (resource != null) { + System.out.println(resource.getPath()); + // 鍒涘缓鏂囦欢杈撳叆娴� + FileInputStream inputStream = new FileInputStream(new File(resource.getPath())); + // 鍒涘缓MockMultipartFile瀵硅薄 + MockMultipartFile multipartFile = new MockMultipartFile("file", "111.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", inputStream); + try { + satelliteTelemetryService.importGridData(1, 1, LocalDateTime.of(2029, 7, 29, 0, 0), 0, multipartFile); + }catch (BizException e) { + // 楠岃瘉寮傚父娑堟伅鏄惁涓庨鏈熺浉绗� + if (!e.getMessage().contains("瀵煎叆鏁版嵁涓湁澶氫綑缃戞牸鍗曞厓鏍�")) { + System.out.println(e.getMessage()); + throw e; + } + } + } else { + System.out.println("璧勬簮鏂囦欢鏈壘鍒�"); + } + } + // 缃戞牸缂栧彿缂哄け閿欒 + @Test + public void test4() throws IOException, BizException { + URL resource = getClass().getClassLoader().getResource("templates/GridData-index_short.xlsx"); + if (resource != null) { + System.out.println(resource.getPath()); + // 鍒涘缓鏂囦欢杈撳叆娴� + FileInputStream inputStream = new FileInputStream(new File(resource.getPath())); + // 鍒涘缓MockMultipartFile瀵硅薄 + MockMultipartFile multipartFile = new MockMultipartFile("file", "111.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", inputStream); + try { + satelliteTelemetryService.importGridData(1, 1, LocalDateTime.of(2029, 7, 29, 0, 0), 0, multipartFile); + }catch (BizException e) { + // 楠岃瘉寮傚父娑堟伅鏄惁涓庨鏈熺浉绗� + if (!e.getMessage().contains("瀵煎叆鏁版嵁涓己灏戜互涓嬬綉鏍煎崟鍏冩牸")) { + System.out.println(e.getMessage()); + throw e; + } + } + } else { + System.out.println("璧勬簮鏂囦欢鏈壘鍒�"); + } + } + // 鏂囦欢绫诲瀷閿欒 + @Test + public void test5() throws IOException, BizException { + URL resource = getClass().getClassLoader().getResource("templates/GridData-fileType-error.txt"); + if (resource != null) { + System.out.println(resource.getPath()); + // 鍒涘缓鏂囦欢杈撳叆娴� + FileInputStream inputStream = new FileInputStream(new File(resource.getPath())); + // 鍒涘缓MockMultipartFile瀵硅薄 + MockMultipartFile multipartFile = new MockMultipartFile("file", "111.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", inputStream); + try { + satelliteTelemetryService.importGridData(1, 1, LocalDateTime.of(2029, 7, 29, 0, 0), 0, multipartFile); + }catch (BizException e) { + // 楠岃瘉寮傚父娑堟伅鏄惁涓庨鏈熺浉绗� + if (!e.getMessage().contains("瀵煎叆鏁版嵁涓己灏戜互涓嬬綉鏍煎崟鍏冩牸")) { + System.out.println(e.getMessage()); + throw e; + } + } + } else { + System.out.println("璧勬簮鏂囦欢鏈壘鍒�"); + } + } + // 鏂板鏁版嵁瀵煎叆鎴愬姛 + @Test + public void test6() throws IOException, BizException { + URL resource = getClass().getClassLoader().getResource("templates/GridData-success-insert.xlsx"); + if (resource != null) { + System.out.println(resource.getPath()); + // 鍒涘缓鏂囦欢杈撳叆娴� + FileInputStream inputStream = new FileInputStream(new File(resource.getPath())); + // 鍒涘缓MockMultipartFile瀵硅薄 + MockMultipartFile multipartFile = new MockMultipartFile("file", "111.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", inputStream); + try { + satelliteTelemetryService.importGridData(1, 1, + LocalDateTime.of(2029, 7, 29, 0, 0), 0, multipartFile); + }catch (BizException e) { + throw e; + } + } else { + System.out.println("璧勬簮鏂囦欢鏈壘鍒�"); + } + } + // 鏇存柊鏁版嵁瀵煎叆鎴愬姛 + @Test + public void test7() throws IOException, BizException { + URL resource = getClass().getClassLoader().getResource("templates/GridData-success-update.xlsx"); + if (resource != null) { + System.out.println(resource.getPath()); + // 鍒涘缓鏂囦欢杈撳叆娴� + FileInputStream inputStream = new FileInputStream(new File(resource.getPath())); + // 鍒涘缓MockMultipartFile瀵硅薄 + MockMultipartFile multipartFile = new MockMultipartFile("file", "111.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", inputStream); + try { + satelliteTelemetryService.importGridData(1, 1, + LocalDateTime.of(2029, 7, 29, 0, 0), 1, multipartFile); + }catch (BizException e) { + throw e; + } + } else { + System.out.println("璧勬簮鏂囦欢鏈壘鍒�"); + } + } +} diff --git a/src/test/resources/templates/GridData-1_type_error.xlsx b/src/test/resources/templates/GridData-1_type_error.xlsx new file mode 100644 index 0000000..2bdff3b --- /dev/null +++ b/src/test/resources/templates/GridData-1_type_error.xlsx Binary files differ diff --git a/src/test/resources/templates/GridData-2_type_error.xlsx b/src/test/resources/templates/GridData-2_type_error.xlsx new file mode 100644 index 0000000..de83859 --- /dev/null +++ b/src/test/resources/templates/GridData-2_type_error.xlsx Binary files differ diff --git a/src/test/resources/templates/GridData-fileType-error.txt b/src/test/resources/templates/GridData-fileType-error.txt new file mode 100644 index 0000000..e502991 --- /dev/null +++ b/src/test/resources/templates/GridData-fileType-error.txt Binary files differ diff --git a/src/test/resources/templates/GridData-index_out.xlsx b/src/test/resources/templates/GridData-index_out.xlsx new file mode 100644 index 0000000..e502991 --- /dev/null +++ b/src/test/resources/templates/GridData-index_out.xlsx Binary files differ diff --git a/src/test/resources/templates/GridData-index_short.xlsx b/src/test/resources/templates/GridData-index_short.xlsx new file mode 100644 index 0000000..e502991 --- /dev/null +++ b/src/test/resources/templates/GridData-index_short.xlsx Binary files differ diff --git a/src/test/resources/templates/GridData-success-insert.xlsx b/src/test/resources/templates/GridData-success-insert.xlsx new file mode 100644 index 0000000..e502991 --- /dev/null +++ b/src/test/resources/templates/GridData-success-insert.xlsx Binary files differ diff --git a/src/test/resources/templates/GridData-success-update.xlsx b/src/test/resources/templates/GridData-success-update.xlsx new file mode 100644 index 0000000..ba19bfb --- /dev/null +++ b/src/test/resources/templates/GridData-success-update.xlsx Binary files differ -- Gitblit v1.9.3