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/common/utils/FileExchange.kt | 123 +++++++++++++++++++++++++++++++++++++++- 1 files changed, 118 insertions(+), 5 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 c2cde2f..c9c666a 100644 --- a/src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt +++ b/src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt @@ -1,7 +1,8 @@ package com.flightfeather.uav.common.utils import com.alibaba.fastjson.JSONObject -import com.flightfeather.uav.common.exception.ResponseErrorException +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 @@ -29,7 +30,119 @@ private val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss") } + /** + * 杞崲闈欏畨鍖鸿溅杞借蛋鑸暟鎹� + */ fun exchangeJinanData(deviceCode: String, file: InputStream): List<RealTimeDataVehicle> { + val headers = listOf( + "PostTime", + "Lon", + "Lat", + "PM25", + "PM10", + "Temperature", + "Humidity", + "Speed", + ) + val result = mutableListOf<RealTimeDataVehicle>() + 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 = RealTimeDataVehicle().apply { + this.deviceCode = deviceCode + dataTime = DateUtil.instance.StringToDate( + it.getCell(0)?.stringCellValue?.trim()?.split(".")?.get(0) + ) + longitude = it.getCell(1)?.stringCellValue?.trim()?.toBigDecimal() + latitude = it.getCell(2)?.stringCellValue?.trim()?.toBigDecimal() + pm25 = it.getCell(3)?.stringCellValue?.trim()?.toFloat()?: 0F + pm10 = it.getCell(4)?.stringCellValue?.trim()?.toFloat()?: 0F + temperature = it.getCell(5)?.stringCellValue?.trim()?.toFloat()?: 0F + humidity = it.getCell(6)?.stringCellValue?.trim()?.toFloat()?: 0F + velocity = it.getCell(7)?.stringCellValue?.trim()?.toFloat()?: 0F + no2 = 0F + co = 0F + h2s = 0F + so2 = 0F + o3 = 0F + voc = 0F + noi = 0F + windSpeed = 0F + windDirection = 0F + } + result.add(data) + }) + } catch (e: Exception) { + e.printStackTrace() + throw BizException("excel鏂囦欢鍐呭閿欒锛屾暟鎹浆鎹㈠け璐ワ紒", e) + } + return result + } + + /** + * 杞崲涓篜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> { val headers = listOf( "data_time", "longitude", @@ -51,7 +164,7 @@ ) val result = mutableListOf<RealTimeDataVehicle>() try { - ExcelUtil.read(file, headerCheck = { + ExcelUtil.readXLXS(file, headerCheck = { val cellIterator = it.cellIterator() val headIterator = headers.iterator() while (headIterator.hasNext()) { @@ -59,10 +172,10 @@ if (cellIterator.hasNext()) { val cellText = cellIterator.next().stringCellValue if (!cellText.equals(head)) { - throw ResponseErrorException("鏂囦欢鏍煎紡閿欒, 琛ㄥご[${head}]搴旇涓篬${cellText}]") + throw BizException("鏂囦欢鏍煎紡閿欒, 琛ㄥご[${head}]搴旇涓篬${cellText}]") } } else { - throw ResponseErrorException("鏂囦欢鏍煎紡閿欒, 琛ㄥご[${head}]缂哄け") + throw BizException("鏂囦欢鏍煎紡閿欒, 琛ㄥご[${head}]缂哄け") } } true @@ -91,7 +204,7 @@ }) } catch (e: Exception) { e.printStackTrace() - throw ResponseErrorException("excel鏂囦欢鍐呭閿欒锛屾暟鎹浆鎹㈠け璐ワ紒", e) + throw BizException("excel鏂囦欢鍐呭閿欒锛屾暟鎹浆鎹㈠け璐ワ紒", e) } return result } -- Gitblit v1.9.3