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