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()
                }
            }
        }
    }
}