riku
2020-12-11 7269d4a4755fa48c45e827bdc5b5ac56c6eca99c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.flightfeather.uav.repository.impl
 
import com.flightfeather.uav.domain.entity.RealTimeData
import com.flightfeather.uav.domain.mapper.RealTimeDataMapper
import com.flightfeather.uav.repository.AirDataRepository
import com.flightfeather.uav.socket.bean.AirData
import com.flightfeather.uav.socket.bean.AirDataPackage
import com.flightfeather.uav.socket.eunm.FactorType
import com.google.gson.Gson
import org.springframework.stereotype.Repository
import java.text.SimpleDateFormat
import java.util.*
 
/**
 * @author riku
 * Date: 2020/6/11
 */
@Repository
class AirDataRepositoryImpl(private val realTimeDataMapper: RealTimeDataMapper): AirDataRepository {
 
    override fun saveAirData(dataPackage: AirDataPackage): Int {
        val data = RealTimeData().apply {
            deviceCode = dataPackage.deviceCode
            latitude
            longitude
            altitude
            height
            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
    }
}