feiyu02
2025-01-03 0ddfab15b32dc054464d75c695999fa76c3b9b78
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
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
    }
}