From a549d6bbd7cb6f2a0dda1234c1b4df072a9e1d61 Mon Sep 17 00:00:00 2001
From: riku <risaku@163.com>
Date: 星期一, 18 十月 2021 09:34:05 +0800
Subject: [PATCH] 1. 新增车载、无人机和网格化三个单独的实时数据存储表 2. 新增数据转存方法,后续待补充转换中的数据预处理逻辑

---
 src/main/kotlin/com/flightfeather/uav/repository/impl/AirDataRepositoryImpl.kt |  156 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 148 insertions(+), 8 deletions(-)

diff --git a/src/main/kotlin/com/flightfeather/uav/repository/impl/AirDataRepositoryImpl.kt b/src/main/kotlin/com/flightfeather/uav/repository/impl/AirDataRepositoryImpl.kt
index 055349d..68975de 100644
--- a/src/main/kotlin/com/flightfeather/uav/repository/impl/AirDataRepositoryImpl.kt
+++ b/src/main/kotlin/com/flightfeather/uav/repository/impl/AirDataRepositoryImpl.kt
@@ -1,32 +1,172 @@
 package com.flightfeather.uav.repository.impl
 
-import com.flightfeather.uav.domain.entity.RealTimeData
+import com.flightfeather.uav.common.utils.GsonUtils
+import com.flightfeather.uav.domain.MyMapper
+import com.flightfeather.uav.domain.entity.*
+import com.flightfeather.uav.domain.mapper.RealTimeDataGridMapper
 import com.flightfeather.uav.domain.mapper.RealTimeDataMapper
+import com.flightfeather.uav.domain.mapper.RealTimeDataUavMapper
+import com.flightfeather.uav.domain.mapper.RealTimeDataVehicleMapper
+import com.flightfeather.uav.lightshare.bean.DataVo
 import com.flightfeather.uav.repository.AirDataRepository
-import com.flightfeather.uav.socket.bean.AirPackageData
+import com.flightfeather.uav.socket.bean.AirData
+import com.flightfeather.uav.socket.bean.AirDataPackage
+import com.flightfeather.uav.socket.eunm.FactorType
+import com.flightfeather.uav.socket.eunm.UWDeviceType
 import com.google.gson.Gson
 import org.springframework.stereotype.Repository
+import java.text.SimpleDateFormat
 
 /**
  * @author riku
  * Date: 2020/6/11
  */
 @Repository
-class AirDataRepositoryImpl(private val realTimeDataMapper: RealTimeDataMapper): AirDataRepository {
+class AirDataRepositoryImpl(
+    private val realTimeDataMapper: RealTimeDataMapper,
+    private val realTimeDataVehicleMapper: RealTimeDataVehicleMapper,
+    private val realTimeDataUavMapper: RealTimeDataUavMapper,
+    private val realTimeDataGridMapper: RealTimeDataGridMapper
+): AirDataRepository {
 
-    override fun saveAirData(packageData: AirPackageData): Int {
-
+    override fun saveAirData(dataPackage: AirDataPackage): Int {
         val data = RealTimeData().apply {
-            deviceCode = packageData.deviceCode
+            deviceCode = dataPackage.deviceCode
             latitude
             longitude
             altitude
             height
-            factors = Gson().toJson(packageData.dataUnit)
-            dataTime = packageData.dataTime
+            factors = Gson().toJson(dataPackage.dataUnit)
+            dataTime = dataPackage.dataTime
+        }
+        dataPackage.dataUnit.forEach {
+            if (it is AirData) {
+                when (it.factorId?.toInt()) {
+                    FactorType.LAT.value -> {
+                        data.latitude = it.factorData?.toBigDecimal()
+                    }
+                    FactorType.LNG.value -> {
+                        data.longitude = it.factorData?.toBigDecimal()
+                    }
+                    FactorType.TIME.value -> {
+                        it.statusList?.takeIf {l-> l.isNotEmpty() }?.get(0)?.let {d ->
+                            data.dataTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(d)
+                        }
+                    }
+                }
+            }
         }
         realTimeDataMapper.insert(data)
 
         return 0
     }
+
+    override fun savePrepData(dataPackage: AirDataPackage): Int {
+        var mapper: MyMapper<out BaseRealTimeData?>? = null
+        when (UWDeviceType.getType(dataPackage.deviceCode)) {
+            UWDeviceType.VEHICLE -> {
+                mapper = realTimeDataVehicleMapper
+                RealTimeDataVehicle()
+            }
+            UWDeviceType.UAV -> {
+                mapper = realTimeDataUavMapper
+                RealTimeDataUav()
+            }
+            UWDeviceType.GRID -> {
+                mapper = realTimeDataGridMapper
+                RealTimeDataGrid()
+            }
+            else -> null
+        }?.run {
+            deviceCode = dataPackage.deviceCode
+            dataPackage.dataUnit.forEach {
+                if (it is AirData) {
+                    when (it.factorId?.toInt()) {
+                        FactorType.NO2.value -> no2 = it.factorData?.toFloat()
+                        FactorType.CO.value -> co = it.factorData?.toFloat()
+                        FactorType.H2S.value -> h2s = it.factorData?.toFloat()
+                        FactorType.SO2.value -> so2 = it.factorData?.toFloat()
+                        FactorType.O3.value -> o3 = it.factorData?.toFloat()
+                        FactorType.PM25.value -> pm25 = it.factorData?.toFloat()
+                        FactorType.PM10.value -> pm10 = it.factorData?.toFloat()
+                        FactorType.TEMPERATURE.value -> temperature = it.factorData?.toFloat()
+                        FactorType.HUMIDITY.value -> humidity = it.factorData?.toFloat()
+                        FactorType.VOC.value -> voc = it.factorData?.toFloat()
+                        FactorType.NOI.value -> noi = it.factorData?.toFloat()
+                        FactorType.VELOCITY.value -> velocity = it.factorData?.toFloat()
+                        FactorType.WIND_SPEED.value -> windSpeed = it.factorData?.toFloat()
+                        FactorType.WIND_DIRECTION.value -> windDirection = it.factorData?.toFloat()
+                        FactorType.HEIGHT.value -> height = it.factorData?.toFloat()
+
+                        FactorType.LAT.value -> latitude = it.factorData?.toBigDecimal()
+                        FactorType.LNG.value -> longitude = it.factorData?.toBigDecimal()
+                        FactorType.TIME.value -> it.statusList?.takeIf { l -> l.isNotEmpty() }?.get(0)?.let { d ->
+                            dataTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(d)
+                        }
+                    }
+                }
+            }
+
+//            mapper?.insert(this)
+            return 1
+        }
+        return 0
+    }
+
+    override fun savePrepData(dataList: List<RealTimeData>): Int {
+        var count = 0
+        dataList.forEach {vo ->
+            when (UWDeviceType.getType(vo.deviceCode)) {
+                UWDeviceType.VEHICLE -> {
+                    val d = RealTimeDataVehicle()
+                    dataTransform(vo, d)
+                    realTimeDataVehicleMapper.insert(d)
+                    count++
+                }
+                UWDeviceType.UAV -> {
+                    val d = RealTimeDataUav()
+                    dataTransform(vo, d)
+                    realTimeDataUavMapper.insert(d)
+                    count++
+                }
+                UWDeviceType.GRID -> {
+                    val d = RealTimeDataGrid()
+                    dataTransform(vo, d)
+                    realTimeDataGridMapper.insert(d)
+                    count++
+                }
+            }
+        }
+        return count
+    }
+
+    private fun dataTransform(vo: RealTimeData, bean: BaseRealTimeData) {
+        bean.apply {
+            deviceCode = vo.deviceCode
+            latitude = vo.latitude
+            longitude = vo.longitude
+            dataTime = vo.dataTime
+            createTime = vo.createTime
+            GsonUtils.parserJsonToArrayBeans(vo.factors, AirData::class.java).forEach {
+                when (it.factorId?.toInt()) {
+                    FactorType.NO2.value -> no2 = it.factorData?.toFloat()
+                    FactorType.CO.value -> co = it.factorData?.toFloat()
+                    FactorType.H2S.value -> h2s = it.factorData?.toFloat()
+                    FactorType.SO2.value -> so2 = it.factorData?.toFloat()
+                    FactorType.O3.value -> o3 = it.factorData?.toFloat()
+                    FactorType.PM25.value -> pm25 = it.factorData?.toFloat()
+                    FactorType.PM10.value -> pm10 = it.factorData?.toFloat()
+                    FactorType.TEMPERATURE.value -> temperature = it.factorData?.toFloat()
+                    FactorType.HUMIDITY.value -> humidity = it.factorData?.toFloat()
+                    FactorType.VOC.value -> voc = it.factorData?.toFloat()
+                    FactorType.NOI.value -> noi = it.factorData?.toFloat()
+                    FactorType.VELOCITY.value -> velocity = it.factorData?.toFloat()
+                    FactorType.WIND_SPEED.value -> windSpeed = it.factorData?.toFloat()
+                    FactorType.WIND_DIRECTION.value -> windDirection = it.factorData?.toFloat()
+                    FactorType.HEIGHT.value -> height = it.factorData?.toFloat()
+                }
+            }
+
+        }
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.3