From 9d4ca2e7bdfc0d634b0cc27c7fbe23740f4bb398 Mon Sep 17 00:00:00 2001 From: hcong <1050828145@qq.com> Date: 星期五, 27 十二月 2024 15:43:30 +0800 Subject: [PATCH] 1. 新增aod数据索引,aod详细数据实体类,mapper,service 2. 新增卫星遥测数据网格数据pm2.5、aod数据导入,模板下载 3. FileExchange.kt 新增转换pm2.5和aod数据方法 --- src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt | 381 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 374 insertions(+), 7 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 c6dd971..423644f 100644 --- a/src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt +++ b/src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt @@ -1,9 +1,16 @@ package com.flightfeather.uav.common.utils import com.alibaba.fastjson.JSONObject +import com.flightfeather.uav.common.exception.BizException +import com.flightfeather.uav.domain.entity.GridAodDetail +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 import org.apache.poi.hssf.usermodel.HSSFWorkbook +import org.apache.poi.ss.usermodel.Cell +import org.apache.poi.ss.usermodel.CellType +import org.apache.poi.ss.util.CellAddress import org.apache.poi.xssf.streaming.SXSSFWorkbook import java.io.File import java.io.FileInputStream @@ -13,7 +20,7 @@ import java.util.* /** - * 鏃犱汉鑸归噰闆嗘暟鎹牸寮忚浆鎹� + * 閲囬泦鏁版嵁鏍煎紡杞崲 */ class FileExchange { @@ -27,11 +34,335 @@ 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("鏂囦欢鏍煎紡閿欒, 琛ㄥご[${cellText}]搴旇涓篬${head}]") + } + } 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 + } + + /** + * 杞崲PM2.5琛ㄦ牸鏁版嵁 + */ + fun exchangeGridData(file: InputStream, correctRangeGridCells: Set<Int>): List<GridDataDetail> { + // 妯′豢宸叉湁浠g爜鍒ゆ柇琛ㄥご姝g‘鎬� 骞跺湪閿欒鏃舵姏鍑哄紓甯� + 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("鏂囦欢鏍煎紡閿欒, 琛ㄥご[${cellText}]搴旇涓篬${head}]") + } + } else { + throw BizException("鏂囦欢鏍煎紡閿欒, 琛ㄥご[${head}]缂哄け") + } + } + true + }, + // 鍒ゆ柇鍗曞厓鏍兼暟鎹纭�э紝骞跺湪鎶ラ敊鍚庢姏鍑哄紓甯� + onRow = { + val data = GridDataDetail().apply { + // 杞崲缃戞牸缁勫崟鍏冩牸id + // 寰楀埌褰撳墠鍗曞厓鏍肩殑瀹氫綅淇℃伅 锛坋g: B3,BA3...锛� + val cellIdCellPosStr = CellAddress(it.rowNum, 0).formatAsString() + // 寰楀埌褰撳墠鍗曞厓鏍兼暟鎹被鍨� + val cellIdCellType = it.getCell(0).cellType + when (cellIdCellType) { + CellType.NUMERIC -> { + // 褰撳�间负鏁板瓧绫诲瀷鏃惰浆鍖栦负缃戞牸鍗曞厓鏍糹d + this.cellId = it.getCell(0)?.numericCellValue?.toInt() + } + else -> { + throw BizException("鍗曞厓鏍糩${cellIdCellPosStr}]涓嶆槸鏁板瓧绫诲瀷") + } + } + + // 杞崲PM2.5 + // 寰楀埌褰撳墠鍗曞厓鏍肩殑瀹氫綅淇℃伅 锛坋g: B3,BA3...锛� + val pm25CellPosStr = CellAddress(it.rowNum, 1).formatAsString() + // 寰楀埌褰撳墠鍗曞厓鏍兼暟鎹被鍨� + val pm25CellType = it.getCell(1).cellType + when (pm25CellType) { + CellType.STRING -> { + // 褰撳�间负瀛楃涓叉椂鏈変袱绉嶆儏鍐� + // 绌虹櫧鎴栬�呮槸鈥�/鈥欏瓧绗� 杞寲涓虹┖鍊� + // 鍚﹀垯鎶ラ敊 + val stringValue = it.getCell(1)?.stringCellValue?.trim() + if (stringValue != null) { + if (stringValue == "/" || stringValue.isBlank()) { + this.pm25 = null + } + }else { + throw BizException("鍗曞厓鏍糩${pm25CellPosStr}]涓嶆槸鏁板瓧绫诲瀷鎴栬�呮枃鏈被鍨�") + } + } + CellType.NUMERIC -> { + // 褰撳�间负鏁板瓧绫诲瀷鏃惰浆鍖栦负pm2.5鏁版嵁 + this.pm25 = it.getCell(1)?.numericCellValue?.toFloat() + } + else -> { + throw BizException("鍗曞厓鏍糩${pm25CellPosStr}]涓嶆槸鏁板瓧绫诲瀷") + } + } + } + result.add(data) + }) + } catch (e: BizException) { + throw e + } + // 鍒ゆ柇缃戞牸鍗曞厓鏍肩紪鍙锋槸鍚﹀湪姝g‘鐨勮寖鍥村唴 + val resultCellIdSet = result.asSequence().map { it.cellId ?: -1 }.toSet() + // 鐢ㄦ埛瀵煎叆涓己灏戠殑鐨勫崟鍏冩牸 + val shortCells = correctRangeGridCells - resultCellIdSet + // excel 涓鍑虹殑鐨勫崟鍏冩牸 + val outCells = resultCellIdSet - correctRangeGridCells + if (shortCells.isNotEmpty()) { + throw BizException("瀵煎叆鏁版嵁涓己灏戜互涓嬬綉鏍煎崟鍏冩牸锛�${shortCells.joinToString(",")}") + } + if (outCells.isNotEmpty()) { + throw BizException("瀵煎叆鏁版嵁涓湁澶氫綑缃戞牸鍗曞厓鏍硷細${outCells.joinToString(",")}") + } + return result + } + + /** + * 杞崲AOD琛ㄦ牸鏁版嵁 + */ + fun exchangeGridAod(file: InputStream, correctRangeGridCells: Set<Int>): List<GridAodDetail> { + // 妯′豢宸叉湁浠g爜鍒ゆ柇琛ㄥご姝g‘鎬� 骞跺湪閿欒鏃舵姏鍑哄紓甯� + val headers = listOf( + "pointid", + "AOD" + ) + val result = mutableListOf<GridAodDetail>() + 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("鏂囦欢鏍煎紡閿欒, 琛ㄥご[${cellText}]搴旇涓篬${head}]") + } + } else { + throw BizException("鏂囦欢鏍煎紡閿欒, 琛ㄥご[${head}]缂哄け") + } + } + true + }, + // 鍒ゆ柇鍗曞厓鏍兼暟鎹纭�э紝骞跺湪鎶ラ敊鍚庢姏鍑哄紓甯� + onRow = { + val data = GridAodDetail().apply { + // 杞崲缃戞牸缁勫崟鍏冩牸id + // 寰楀埌褰撳墠鍗曞厓鏍肩殑瀹氫綅淇℃伅 锛坋g: B3,BA3...锛� + val cellIdCellPosStr = CellAddress(it.rowNum, 0).formatAsString() + // 寰楀埌褰撳墠鍗曞厓鏍兼暟鎹被鍨� + val cellIdCellType = it.getCell(0).cellType + when (cellIdCellType) { + CellType.NUMERIC -> { + // 褰撳�间负鏁板瓧绫诲瀷鏃惰浆鍖栦负缃戞牸鍗曞厓鏍糹d + this.cellId = it.getCell(0)?.numericCellValue?.toInt() + } + else -> { + throw BizException("鍗曞厓鏍糩${cellIdCellPosStr}]涓嶆槸鏁板瓧绫诲瀷") + } + } + + // 杞崲AOD + // 寰楀埌褰撳墠鍗曞厓鏍肩殑瀹氫綅淇℃伅 锛坋g: B3,BA3...锛� + val aodCellPosStr = CellAddress(it.rowNum, 1).formatAsString() + // 寰楀埌褰撳墠鍗曞厓鏍兼暟鎹被鍨� + val aodCellType = it.getCell(1).cellType + when (aodCellType) { + CellType.STRING -> { + // 褰撳�间负瀛楃涓叉椂鏈変袱绉嶆儏鍐� + // 绌虹櫧鎴栬�呮槸鈥�/鈥欏瓧绗� 杞寲涓虹┖鍊� + // 鍚﹀垯鎶ラ敊 + val stringValue = it.getCell(1)?.stringCellValue?.trim() + if (stringValue != null) { + if (stringValue == "/" || stringValue.isBlank()) { + this.aod = null + } + }else { + throw BizException("鍗曞厓鏍糩${aodCellPosStr}]涓嶆槸鏁板瓧绫诲瀷鎴栬�呮枃鏈被鍨�") + } + } + CellType.NUMERIC -> { + // 褰撳�间负鏁板瓧绫诲瀷鏃惰浆鍖栦负pm2.5鏁版嵁 + this.aod = it.getCell(1)?.numericCellValue?.toFloat() + } + else -> { + throw BizException("鍗曞厓鏍糩${aodCellPosStr}]涓嶆槸鏁板瓧绫诲瀷") + } + } + } + result.add(data) + }) + } catch (e: BizException) { + throw e + } + // 鍒ゆ柇缃戞牸鍗曞厓鏍肩紪鍙锋槸鍚﹀湪姝g‘鐨勮寖鍥村唴 + val resultCellIdSet = result.asSequence().map { it.cellId ?: -1 }.toSet() + // 鐢ㄦ埛瀵煎叆涓己灏戠殑鐨勫崟鍏冩牸 + val shortCells = correctRangeGridCells - resultCellIdSet + // excel 涓鍑虹殑鐨勫崟鍏冩牸 + val outCells = resultCellIdSet - correctRangeGridCells + if (shortCells.isNotEmpty()) { + throw BizException("瀵煎叆鏁版嵁涓己灏戜互涓嬬綉鏍煎崟鍏冩牸锛�${shortCells.joinToString(",")}") + } + if (outCells.isNotEmpty()) { + throw BizException("瀵煎叆鏁版嵁涓湁澶氫綑缃戞牸鍗曞厓鏍硷細${outCells.joinToString(",")}") + } + return result + } + + /** + * 杞崲杞﹁浇璧拌埅鏁版嵁 + */ + fun exchangeVehicleData(deviceCode: String, file: InputStream): List<RealTimeDataVehicle> { + val headers = listOf( + "data_time", + "longitude", + "latitude", + "NO2", + "CO", + "H2S", + "SO2", + "O3", + "PM25", + "PM10", + "temperature", + "humidity", + "VOC", + "NOI", + "velocity", + "wind_speed", + "wind_direction" + ) + 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 = 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) { + e.printStackTrace() + throw BizException("excel鏂囦欢鍐呭閿欒锛屾暟鎹浆鎹㈠け璐ワ紒", e) + } + return result + } + + /** + * 杞崲鏃犱汉鑸圭殑姘磋川鐩戞祴鏁版嵁 + */ fun exchangeBoatData(deviceCode: String, file: InputStream): List<RealTimeData> { val workbook = HSSFWorkbook(file) val sheet = workbook.getSheetAt(0) val dataList = mutableListOf<RealTimeData>() + + val lastData = mutableListOf<Double>() for (i in 1 until sheet.lastRowNum) { val row = sheet.getRow(i) @@ -44,11 +375,27 @@ val datetime = Date(time) //鐩戞祴鍥犲瓙 val jO = JSONObject.parseObject(value) - val tmp = jO.getDoubleValue(TMP) - val spC = jO.getDoubleValue(SPCOND) - val tur = jO.getDoubleValue(TUR) - val dO = jO.getDoubleValue(DO) - val ph = jO.getDoubleValue(PH) + var tmp = jO.getDoubleValue(TMP) + var spC = jO.getDoubleValue(SPCOND) + var tur = jO.getDoubleValue(TUR) + var dO = jO.getDoubleValue(DO) + var ph = jO.getDoubleValue(PH) + + if (lastData.isEmpty()) { + lastData.addAll(listOf(tmp, spC, tur, dO, ph)) + } else { + if (tmp == .0) tmp = lastData[0] + if (spC == .0) spC = lastData[1] + if (tur == .0) tur = lastData[2] + if (dO == .0) dO = lastData[3] + if (ph == .0) ph = lastData[4] + } + + lastData[0] = tmp + lastData[1] = spC + lastData[2] = tur + lastData[3] = dO + lastData[4] = ph val factorsList = mutableListOf<AirData>() @@ -81,6 +428,18 @@ factorId = "5" factorName = "PH" factorData = ph + physicalQuantity = 0.0 + }) + add(AirData().apply { + factorId = "12" + factorName = "LNG" + factorData = lng + physicalQuantity = 0.0 + }) + add(AirData().apply { + factorId = "13" + factorName = "LAT" + factorData = lat physicalQuantity = 0.0 }) } @@ -175,7 +534,15 @@ val filePath = "e:/$fileName" val out = FileOutputStream(File(filePath)) - val heads = listOf("id", "device_code", "latitude", "longitude", "altitude", "height", "factors", "data_time", "create_time") + val heads = listOf("id", + "device_code", + "latitude", + "longitude", + "altitude", + "height", + "factors", + "data_time", + "create_time") val contents = mutableListOf<Array<Any>>() for (i in 1..sheet.lastRowNum) { -- Gitblit v1.9.3