From da431c25dfe5122e4ed70372da36ede3e4eaec4a Mon Sep 17 00:00:00 2001 From: feiyu02 <risaku@163.com> Date: 星期五, 31 五月 2024 17:43:41 +0800 Subject: [PATCH] 1. 新增自动报告生成功能 --- src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt | 106 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 97 insertions(+), 9 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 efc4c36..ea62b09 100644 --- a/src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt +++ b/src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt @@ -1,8 +1,9 @@ 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.RealTimeData +import com.flightfeather.uav.domain.entity.RealTimeDataVehicle import com.flightfeather.uav.socket.bean.AirData import org.apache.poi.hssf.usermodel.HSSFWorkbook import org.apache.poi.xssf.streaming.SXSSFWorkbook @@ -28,11 +29,77 @@ private val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss") } - fun exchangeJinanData(file: File) { + /** + * 杞崲闈欏畨鍖鸿溅杞借蛋鑸暟鎹� + */ + 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 + } + + /** + * 杞崲杞﹁浇璧拌埅鏁版嵁 + */ + fun exchangeVehicleData(deviceCode: String, file: InputStream): List<RealTimeDataVehicle> { + val headers = listOf( + "data_time", "longitude", "latitude", - "data_time", "NO2", "CO", "H2S", @@ -48,8 +115,9 @@ "wind_speed", "wind_direction" ) + val result = mutableListOf<RealTimeDataVehicle>() try { - ExcelUtil.read(file, headerCheck = { + ExcelUtil.readXLXS(file, headerCheck = { val cellIterator = it.cellIterator() val headIterator = headers.iterator() while (headIterator.hasNext()) { @@ -57,21 +125,41 @@ 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 }, onRow = { - it.cellIterator().forEach { - it.numericCellValue + val data = RealTimeDataVehicle().apply { + this.deviceCode = deviceCode + dataTime = it.getCell(0).dateCellValue + longitude = it.getCell(1).numericCellValue.toBigDecimal() + latitude = it.getCell(2).numericCellValue.toBigDecimal() + no2 = it.getCell(3).numericCellValue.toFloat() + co = it.getCell(4).numericCellValue.toFloat() + h2s = it.getCell(5).numericCellValue.toFloat() + so2 = it.getCell(6).numericCellValue.toFloat() + o3 = it.getCell(7).numericCellValue.toFloat() + pm25 = it.getCell(8).numericCellValue.toFloat() + pm10 = it.getCell(9).numericCellValue.toFloat() + temperature = it.getCell(10).numericCellValue.toFloat() + humidity = it.getCell(11).numericCellValue.toFloat() + voc = it.getCell(12).numericCellValue.toFloat() + noi = it.getCell(13).numericCellValue.toFloat() + velocity = it.getCell(14).numericCellValue.toFloat() + windSpeed = it.getCell(15).numericCellValue.toFloat() + windDirection = it.getCell(16).numericCellValue.toFloat() } + result.add(data) }) } catch (e: Exception) { - throw ResponseErrorException("excel鏂囦欢鍐呭閿欒锛屾暟鎹浆鎹㈠け璐ワ紒", e) + e.printStackTrace() + throw BizException("excel鏂囦欢鍐呭閿欒锛屾暟鎹浆鎹㈠け璐ワ紒", e) } + return result } /** -- Gitblit v1.9.3