feiyu02
2025-09-02 8c6e742562d0c8647e0ee8deff01a3eb176d677b
src/main/kotlin/com/flightfeather/uav/domain/repository/RealTimeDataRep.kt
@@ -1,5 +1,6 @@
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
@@ -9,6 +10,8 @@
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.*
/**
@@ -22,56 +25,68 @@
    private val realTimeDataGridMinMapper: RealTimeDataGridMinMapper,
) {
    private fun getSecondDataExample(example: Example, deviceCode: String?, sTime: Date?, eTime: Date?) {
        example.createCriteria().apply {
            deviceCode?.let { andEqualTo("deviceCode", it) }
            sTime?.let { andGreaterThanOrEqualTo("dataTime", it) }
            eTime?.let { andLessThanOrEqualTo("dataTime", it) }
        }
        example.orderBy("dataTime").apply {
            // 当请求接口不传递起始时间,默认获取最新的数据
            if (sTime == null && eTime == null) {
                desc()
            }
        }
    }
    private val delegate = RealTimeDataRepDelegate(realTimeDataVehicleMapper, realTimeDataUavMapper,
        realTimeDataGridMapper, realTimeDataGridMinMapper)
    fun fetchData(
        deviceCode: String,
        sTime: Date?,
        eTime: Date?,
        type: Int? = 0,
        deviceType: UWDeviceType?, deviceCode: String, sTime: Date? = null, eTime: Date? = null, type: Int? = 0,
        page: Int? = null, perPage: Int? = null,
    ): List<BaseRealTimeData> {
        var result = listOf<BaseRealTimeData>()
        when (UWDeviceType.getType(deviceCode)) {
            UWDeviceType.VEHICLE -> {
                result = realTimeDataVehicleMapper.selectByExample(Example(RealTimeDataVehicle::class.java).apply {
                    getSecondDataExample(this, deviceCode, sTime, eTime)
                })
            }
            UWDeviceType.UAV -> {
                result = realTimeDataUavMapper.selectByExample(Example(RealTimeDataUav::class.java).apply {
                    getSecondDataExample(this, deviceCode, sTime, eTime)
                })
            }
            UWDeviceType.GRID -> {
                // 网格化监测秒级值
                result = if (type == null || type == 0) {
                    realTimeDataGridMapper.selectByExample(Example(RealTimeDataGrid::class.java).apply {
                        getSecondDataExample(this, deviceCode, sTime, eTime)
                    })
                }
                // 网格化监测分钟值
                else {
                    realTimeDataGridMinMapper.selectByExample(Example(RealTimeDataGridMin::class.java).apply {
                        getSecondDataExample(this, deviceCode, sTime, eTime)
                    })
                }
            }
            else -> Unit
        if (page != null && perPage != null) {
            var pageInfo = PageHelper.startPage<BaseRealTimeData>(page, perPage)
        }
        return result
        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(mission.deviceCode, mission.startTime, mission.endTime)
    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)
    }
}