feiyu02
2025-08-14 b10c22af595bd995e56946bff63b8f2f984b13e8
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
package com.flightfeather.uav.domain.repository
 
import com.flightfeather.uav.common.exception.BizException
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 com.github.pagehelper.PageHelper
import org.springframework.stereotype.Repository
import tk.mybatis.mapper.entity.Example
import java.time.LocalDateTime
import java.time.ZoneId
import java.util.*
 
/**
 * 实时走航数据数据库相关操作
 */
@Repository
class RealTimeDataRep(
    private val realTimeDataVehicleMapper: RealTimeDataVehicleMapper,
    private val realTimeDataUavMapper: RealTimeDataUavMapper,
    private val realTimeDataGridMapper: RealTimeDataGridMapper,
    private val realTimeDataGridMinMapper: RealTimeDataGridMinMapper,
) {
 
    private val delegate = RealTimeDataRepDelegate(realTimeDataVehicleMapper, realTimeDataUavMapper,
        realTimeDataGridMapper, realTimeDataGridMinMapper)
 
    fun fetchData(
        deviceType: UWDeviceType?, deviceCode: String, sTime: Date? = null, eTime: Date? = null, type: Int? = 0,
        page: Int? = null, perPage: Int? = null,
    ): List<BaseRealTimeData> {
        if (page != null && perPage != null) {
            var pageInfo = PageHelper.startPage<BaseRealTimeData>(page, perPage)
        }
        return delegate.selectByDeviceType(deviceType, type) { example ->
            example.createCriteria().apply {
                andEqualTo("deviceCode", deviceCode)
                sTime?.let { andGreaterThanOrEqualTo("dataTime", it) }
                eTime?.let { andLessThanOrEqualTo("dataTime", it) }
            }
            example.orderBy("dataTime").apply {
                // 当请求接口不传递起始时间,默认获取最新的数据
                if (sTime == null && eTime == null) {
                    desc()
                }
            }
        }
    }
 
    fun fetchData(mission: Mission) =
        fetchData(UWDeviceType.fromValue(mission.deviceType), mission.deviceCode, mission.startTime, mission.endTime)
 
 
    fun saveData(deviceType: UWDeviceType?, data: List<BaseRealTimeData>, type: Int? = 0): Int {
        return delegate.insertByDeviceType(deviceType, type, data)
    }
 
    fun deleteData(mission: Mission, type: Int? = 0): Int {
        if (mission.deviceCode == null || mission.startTime == null || mission.endTime == null) {
            throw BizException("要删除的走航任务缺失设备编号或采样时间范围,无法删除对应监测数据")
        }
        return deleteData(UWDeviceType.fromValue(mission.deviceType),
            mission.deviceCode,
            mission.startTime,
            mission.endTime, type)
    }
 
    fun deleteData(deviceType: UWDeviceType?, deviceCode: String, sTime: Date?, eTime: Date?, type: Int? = 0): Int {
        return delegate.deleteByDeviceType(deviceType, type) {
            it.createCriteria().apply {
                andEqualTo("deviceCode", deviceCode)
                andGreaterThanOrEqualTo("dataTime", sTime)
                andLessThanOrEqualTo("dataTime", eTime)
            }
        }
    }
 
    fun deleteData(
        deviceType: UWDeviceType?, deviceCode: String, sTime: LocalDateTime?, eTime: LocalDateTime?, type: Int? = 0,
    ): Int = deleteData(deviceType, deviceCode,
        Date.from(sTime?.atZone(ZoneId.systemDefault())?.toInstant()),
        Date.from(eTime?.atZone(ZoneId.systemDefault())?.toInstant()),
        type
    )
 
    fun deleteData(deviceType: UWDeviceType?, data: List<BaseRealTimeData>, type: Int? = 0) {
        delegate.deleteByDeviceType(deviceType, type, data)
    }
}