riku
2019-09-29 fb1dc85a9ae6a9b8426ec5e29eb0139933ebe233
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.flightfeather.obd.repository.impl
 
import com.flightfeather.obd.domain.entity.DataStream
import com.flightfeather.obd.domain.mapper.DataStreamMapper
import com.flightfeather.obd.lightshare.bean.DataStreamVo
import com.flightfeather.obd.lightshare.bean.LatLngVo
import com.flightfeather.obd.repository.DataStreamRepository
import com.flightfeather.obd.socket.bean.EngineDataStream
import com.flightfeather.obd.socket.bean.ObdPackageData
import com.flightfeather.obd.socket.bean.ReplacementData
import com.flightfeather.obd.socket.bean.SupplementDataStream
import com.flightfeather.obd.socket.eunm.ObdCommandUnit
import com.github.pagehelper.PageHelper
import org.springframework.beans.BeanUtils
import org.springframework.stereotype.Repository
import tk.mybatis.mapper.entity.Example
import java.text.SimpleDateFormat
 
/**
 * @author riku
 * Date: 2019/9/17
 */
@Repository
class DataStreamDaoImpl(val dataStreamMapper: DataStreamMapper): DataStreamRepository {
 
    override fun saveDataStream(packageData: ObdPackageData): Boolean {
 
        return if (packageData.commandUnit == ObdCommandUnit.RealTimeData.value
                || packageData.commandUnit == ObdCommandUnit.ReplacementData.value) {
 
            val dataStream = DataStream().apply {
                obdDeviceCode = packageData.deviceCode
                obdStatus = packageData.commandUnit == ObdCommandUnit.ReplacementData.value
            }
 
            packageData.dataUnit.forEach {
                dataStream.apply {
                    obdDataTime = it.time
                    obdSerialNum = it.serialNum
                }
                when (it) {
                    is EngineDataStream -> dataStream.apply {
                        //fixme 此处相同属性可用 [BeanUtil] 工具来快速赋值,但因为并不是全部属性都相同,可能容易产生误解
                        obdSpeed = it.obdSpeed
                        obdAirPressure = it.obdAirPressure
                        obdEngineTorque = it.obdEngineTorque
                        obdFrictionTorque = it.obdFrictionTorque
                        obdEngineRpm = it.obdEngineRpm
                        obdEngineFuelFlow = it.obdEngineFuelFlow
                        obdScrUpstreamNox = it.obdScrUpstreamNox
                        obdScrDownstreamNox = it.obdScrDownstreamNox
                        obdRemainReactant = it.obdRemainReactant
                        obdAirInput = it.obdAirInput
                        obdScrInputTemp = it.obdScrInputTemp
                        obdScrOutputTemp = it.obdScrOutputTemp
                        obdDpf = it.obdDpf
                        obdEngineCoolantTemp = it.obdEngineCoolantTemp
                        obdFuelLevel = it.obdFuelLevel
                        obdLocationStatus = it.obdLocationStatus?.toString(2)
                        obdLong = it.obdLong
                        obdLat = it.obdLat
                        obdTotalMileage = it.obdTotalMileage
                    }
                    is SupplementDataStream -> dataStream.apply {
                        obdEngineTorqueMode = it.obdEngineTorqueMode
                        obdAcceleratorPedal = it.obdAcceleratorPedal
                        obdTotalOilConsumption = it.obdTotalOilConsumption
                        obdUreaBoxTemp = it.obdUreaBoxTemp
                        obdUreaVolume = it.obdUreaVolume
                        obdTotalUreaConsume = it.obdTotalUreaConsume
                        obdDpfTemp = it.obdDpfTemp
                    }
                }
            }
 
            dataStreamMapper.insert(dataStream) == 1
        } else {
            false
        }
    }
 
    override fun getDataStream(deviceCode: String, pageNum: Int?, pageSize: Int?, startTime: String?, endTime: String?): List<DataStreamVo> {
        val sf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        val example = Example(DataStream::class.java).apply {
            createCriteria().andEqualTo("obdDeviceCode", deviceCode).run {
                startTime?.let {
                    val st = sf.parse(startTime)
                    andGreaterThanOrEqualTo("obdDataTime", st)
                }
                endTime?.let {
                    val et = sf.parse(endTime)
                    andLessThanOrEqualTo("obdDataTime", et)
                }
                orderBy("obdDataTime").desc()
            }
        }
 
        //分页
        val offset = (pageSize?.times(pageNum?.minus(1) ?: 0)) ?: 0
        PageHelper.offsetPage<DataStream>(offset, pageSize ?: 10)
        val result = dataStreamMapper.selectByExample(example)
 
        val resultList = mutableListOf<DataStreamVo>()
        result.forEach {
            val vo = DataStreamVo()
            BeanUtils.copyProperties(it, vo)
            resultList.add(vo)
        }
 
        return resultList
    }
 
    override fun getDataStreamCount(deviceCode: String, startTime: String?, endTime: String?): Int {
        val sf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        val example = Example(DataStream::class.java).apply {
            createCriteria().andEqualTo("obdDeviceCode", deviceCode).run {
                startTime?.let {
                    val st = sf.parse(startTime)
                    andGreaterThanOrEqualTo("obdDataTime", st)
                }
                endTime?.let {
                    val et = sf.parse(endTime)
                    andLessThanOrEqualTo("obdDataTime", et)
                }
                orderBy("obdDataTime").desc()
            }
        }
 
        return dataStreamMapper.selectCountByExample(example)
    }
 
    override fun getCoordinate(deviceCode: String): LatLngVo {
        val example = Example(DataStream::class.java).apply {
            createCriteria().andEqualTo("obdDeviceCode", deviceCode).run {
                orderBy("obdDataTime").desc()
            }
        }
 
        //获取最新的一个
        PageHelper.offsetPage<DataStream>(0, 1)
        val result = dataStreamMapper.selectByExample(example)
 
        val latLngVo = LatLngVo()
        if (result.isNotEmpty()) {
            result[0].let {
                latLngVo.apply {
                    this.deviceCode = it.obdDeviceCode
                    obdDataTime = it.obdDataTime
                    lat = it.obdLat
                    lng = it.obdLong
                }
            }
        }
 
        return latLngVo
    }
}