package com.flightfeather.uav.domain.repository
|
|
import com.flightfeather.uav.biz.dataanalysis.BaseExceptionResult
|
import com.flightfeather.uav.biz.sourcetrace.model.AnalysisResult
|
import com.flightfeather.uav.biz.sourcetrace.model.BasePollutedMsg
|
import com.flightfeather.uav.biz.sourcetrace.model.PollutedClue
|
import com.flightfeather.uav.biz.sourcetrace.model.PollutedSummary
|
import com.flightfeather.uav.common.utils.GsonUtils
|
import com.flightfeather.uav.domain.entity.Mission
|
import com.flightfeather.uav.domain.entity.SourceTraceMsg
|
import com.flightfeather.uav.domain.mapper.SourceTraceMsgMapper
|
import com.flightfeather.uav.socket.sender.MsgType
|
import org.springframework.stereotype.Repository
|
import tk.mybatis.mapper.entity.Example
|
import java.util.*
|
|
/**
|
* 动态污染溯源数据库操作
|
* @date 2025/7/4
|
* @author feiyu02
|
*/
|
@Repository
|
class SourceTraceRep(private val sourceTraceMsgMapper: SourceTraceMsgMapper) {
|
|
/**
|
* 插入溯源信息和提醒信息
|
*/
|
fun insert(msgType: MsgType, obj: PollutedClue): Int {
|
val stm = SourceTraceMsg().apply {
|
deviceCode = obj.deviceCode
|
factorName = obj.pollutedData?.toFactorNames()
|
exceptionType = obj.pollutedData?.exceptionType
|
startTime = obj.pollutedData?.startTime
|
endTime = obj.pollutedData?.endTime
|
this.msgType = msgType.value
|
content = GsonUtils.gson.toJson(obj)
|
createTime = Date()
|
}
|
return if (fetchOneExist(stm) == null) {
|
sourceTraceMsgMapper.insert(stm)
|
} else {
|
0
|
}
|
}
|
|
fun insertList(msgType: MsgType, objList: List<PollutedClue>): Int {
|
var res = 0
|
objList.forEach {
|
res += insert(msgType, it)
|
}
|
return res
|
}
|
|
/**
|
* 插入线索信息
|
*/
|
fun insert(res: AnalysisResult): Int {
|
val stm = SourceTraceMsg().apply {
|
deviceCode = res.deviceCode
|
startTime = res.time
|
endTime = res.time
|
this.msgType = MsgType.AnaResult.value
|
content = GsonUtils.gson.toJson(res)
|
createTime = Date()
|
}
|
return if (fetchOneExist(stm) == null) {
|
sourceTraceMsgMapper.insert(stm)
|
} else {
|
0
|
}
|
}
|
|
fun fetchOneExist(stm: SourceTraceMsg): SourceTraceMsg? {
|
val res = sourceTraceMsgMapper.selectByExample(Example(SourceTraceMsg::class.java).apply {
|
createCriteria().andEqualTo("deviceCode", stm.deviceCode)
|
.andEqualTo("msgType", stm.msgType)
|
.andEqualTo("exceptionType", stm.exceptionType)
|
.andEqualTo("factorName", stm.factorName)
|
.andEqualTo("startTime", stm.startTime)
|
.andEqualTo("endTime", stm.endTime)
|
})
|
return if (res.isEmpty()) null else res[0]
|
}
|
|
fun fetchList(deviceCode: String, startTime: Date, endTime: Date): List<BaseExceptionResult?> {
|
return sourceTraceMsgMapper.selectByExample(Example(SourceTraceMsg::class.java).apply {
|
createCriteria().andEqualTo("deviceCode", deviceCode)
|
.andGreaterThanOrEqualTo("startTime", startTime)
|
.andLessThanOrEqualTo("endTime", endTime)
|
orderBy("id").desc()
|
}).map { stm ->
|
when (stm?.msgType) {
|
MsgType.PolClue.value,
|
MsgType.DataChange.value,
|
-> {
|
GsonUtils.gson.fromJson(stm.content, PollutedClue::class.java)
|
}
|
|
MsgType.AnaResult.value -> {
|
GsonUtils.gson.fromJson(stm.content, AnalysisResult::class.java)
|
}
|
|
else -> null
|
}
|
}
|
}
|
|
fun delete(mission: Mission): Int {
|
return sourceTraceMsgMapper.deleteByExample(Example(SourceTraceMsg::class.java).apply {
|
createCriteria().andEqualTo("deviceCode", mission.deviceCode)
|
.andGreaterThanOrEqualTo("startTime", mission.startTime)
|
.andLessThanOrEqualTo("endTime", mission.endTime)
|
})
|
}
|
}
|