package com.flightfeather.uav.domain.repository
|
|
import com.flightfeather.uav.domain.entity.*
|
import com.flightfeather.uav.domain.mapper.RealTimeDataGridMapper
|
import com.flightfeather.uav.domain.mapper.RealTimeDataGridMinMapper
|
import com.flightfeather.uav.domain.mapper.RealTimeDataUavMapper
|
import com.flightfeather.uav.domain.mapper.RealTimeDataVehicleMapper
|
import com.flightfeather.uav.socket.eunm.UWDeviceType
|
import tk.mybatis.mapper.entity.Example
|
import java.util.*
|
|
/**
|
* 实时走航数据数据库相关操作代理类
|
* @date 2024/8/27
|
* @author feiyu02
|
*/
|
class RealTimeDataRepDelegate(
|
private val realTimeDataVehicleMapper: RealTimeDataVehicleMapper,
|
private val realTimeDataUavMapper: RealTimeDataUavMapper,
|
private val realTimeDataGridMapper: RealTimeDataGridMapper,
|
private val realTimeDataGridMinMapper: RealTimeDataGridMinMapper,
|
) {
|
/**
|
* 根据设备类型执行不同数据库操作步骤
|
* 各类设备的监测数据虽分不同数据表存储,但数据表结构相同
|
* @param deviceType 设备类型
|
* @param type 数据统计周期,0:秒级值;1:分钟值
|
* @param callbacks 针对不同类型的设备对应的不同回调函数
|
*/
|
private fun <T> byDeviceType(deviceType: UWDeviceType?, type: Int? = 0, vararg callbacks: () -> T): T? {
|
return when (deviceType) {
|
UWDeviceType.VEHICLE -> {
|
if (callbacks.isNotEmpty()) callbacks[0].invoke() else null
|
}
|
UWDeviceType.UAV -> {
|
if (callbacks.size > 1) callbacks[1].invoke() else null
|
}
|
UWDeviceType.GRID -> {
|
// 网格化监测秒级值
|
if (type == null || type == 0) {
|
if (callbacks.size > 2) callbacks[2].invoke() else null
|
}
|
// 网格化监测分钟值
|
else {
|
if (callbacks.size > 3) callbacks[3].invoke() else null
|
}
|
}
|
else -> null
|
}
|
}
|
|
/**
|
* 统一的select方法
|
* @param deviceType 设备类型
|
* @param type 数据统计周期,0:秒级值;1:分钟值
|
* @param condition 查询条件
|
*/
|
fun selectByDeviceType(
|
deviceType: UWDeviceType?, type: Int? = 0,
|
condition: (example: Example) -> Unit,
|
): List<BaseRealTimeData> {
|
return byDeviceType(deviceType, type, {
|
realTimeDataVehicleMapper.selectByExample(Example(RealTimeDataVehicle::class.java).apply { condition(this) })
|
}, {
|
realTimeDataUavMapper.selectByExample(Example(RealTimeDataUav::class.java).apply { condition(this) })
|
}, {
|
realTimeDataGridMapper.selectByExample(Example(RealTimeDataGrid::class.java).apply { condition(this) })
|
}, {
|
realTimeDataGridMinMapper.selectByExample(Example(RealTimeDataGridMin::class.java).apply { condition(this) })
|
}) ?: emptyList()
|
}
|
|
/**
|
* 统一的insert方法
|
* @param deviceType 设备类型
|
* @param type 数据统计周期,0:秒级值;1:分钟值
|
* @param data 数据
|
*/
|
fun insertByDeviceType(deviceType: UWDeviceType?, type: Int? = 0, data: List<BaseRealTimeData>): Int {
|
return byDeviceType(deviceType, type, {
|
realTimeDataVehicleMapper.insertList(data as List<RealTimeDataVehicle>)
|
}, {
|
realTimeDataUavMapper.insertList(data as List<RealTimeDataUav>)
|
}, {
|
realTimeDataGridMapper.insertList(data as List<RealTimeDataGrid>)
|
}, {
|
realTimeDataGridMinMapper.insertList(data as List<RealTimeDataGridMin>)
|
}) ?: 0
|
}
|
|
/**
|
* 统一的delete方法
|
* @param deviceType 设备类型
|
* @param type 数据统计周期,0:秒级值;1:分钟值
|
* @param data 数据
|
*/
|
fun deleteByDeviceType(deviceType: UWDeviceType?, type: Int? = 0, data: List<BaseRealTimeData>): Int {
|
return byDeviceType(deviceType, type, {
|
var count = 0
|
data.forEach { count += realTimeDataVehicleMapper.delete(it as RealTimeDataVehicle) }
|
count
|
}, {
|
var count = 0
|
data.forEach { count += realTimeDataUavMapper.delete(it as RealTimeDataUav) }
|
count
|
}, {
|
var count = 0
|
data.forEach { count += realTimeDataGridMapper.delete(it as RealTimeDataGrid) }
|
count
|
}, {
|
var count = 0
|
data.forEach { count += realTimeDataGridMinMapper.delete(it as RealTimeDataGridMin) }
|
count
|
}) ?: 0
|
}
|
|
/**
|
* 统一的delete方法
|
* @param deviceType 设备类型
|
* @param type 数据统计周期,0:秒级值;1:分钟值
|
* @param condition 查询条件
|
*/
|
fun deleteByDeviceType(deviceType: UWDeviceType?, type: Int? = 0, condition: (example: Example) -> Unit,): Int {
|
return byDeviceType(deviceType, type, {
|
realTimeDataVehicleMapper.deleteByExample(Example(RealTimeDataVehicle::class.java).apply { condition(this) })
|
}, {
|
realTimeDataUavMapper.deleteByExample(Example(RealTimeDataUav::class.java).apply { condition(this) })
|
}, {
|
realTimeDataGridMapper.deleteByExample(Example(RealTimeDataGrid::class.java).apply { condition(this) })
|
}, {
|
realTimeDataGridMinMapper.deleteByExample(Example(RealTimeDataGridMin::class.java).apply { condition(this) })
|
}) ?: 0
|
}
|
}
|