feiyu02
2024-05-31 da431c25dfe5122e4ed70372da36ede3e4eaec4a
src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt
@@ -1,7 +1,7 @@
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
@@ -29,7 +29,73 @@
        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
    }
    /**
     * 转换车载走航数据
     */
    fun exchangeVehicleData(deviceCode: String, file: InputStream): List<RealTimeDataVehicle> {
        val headers = listOf(
            "data_time",
            "longitude",
@@ -51,7 +117,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 +125,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 +157,7 @@
            })
        } catch (e: Exception) {
            e.printStackTrace()
            throw ResponseErrorException("excel文件内容错误,数据转换失败!", e)
            throw BizException("excel文件内容错误,数据转换失败!", e)
        }
        return result
    }