feiyu02
2025-09-30 94fee0b511279679b43e210878d3d36e5a14384b
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
135
136
137
138
139
140
141
142
143
144
145
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.entity.SourceTraceMsgBlob
import com.flightfeather.uav.domain.mapper.SourceTraceMsgBlobMapper
import com.flightfeather.uav.domain.mapper.SourceTraceMsgMapper
import com.flightfeather.uav.socket.sender.MsgType
import org.springframework.stereotype.Repository
import org.springframework.transaction.annotation.Transactional
import tk.mybatis.mapper.entity.Example
import java.util.*
 
/**
 * 动态污染溯源数据库操作
 * @date 2025/7/4
 * @author feiyu02
 */
@Repository
class SourceTraceRep(
    private val sourceTraceMsgMapper: SourceTraceMsgMapper,
    private val sourceTraceMsgBlobMapper: SourceTraceMsgBlobMapper,
) {
 
    private fun insertBlob(stm: SourceTraceMsg, json: String) {
        sourceTraceMsgBlobMapper.insert(SourceTraceMsgBlob().apply {
            msgId = stm.id
            content = json
        })
    }
 
    /**
     * 插入溯源信息和提醒信息
     */
    @Transactional
    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
            createTime = Date()
        }
        return if (fetchOneExist(stm) == null) {
            val c = sourceTraceMsgMapper.insert(stm)
            insertBlob(stm, GsonUtils.gson.toJson(obj))
            c
        } else {
            0
        }
    }
 
    fun insertList(msgType: MsgType, objList: List<PollutedClue>): Int {
        var res = 0
        objList.forEach {
            res += insert(msgType, it)
        }
        return res
    }
 
    /**
     * 插入线索信息
     */
    @Transactional
    fun insert(res: AnalysisResult): Int {
        val stm = SourceTraceMsg().apply {
            deviceCode = res.deviceCode
            startTime = res.time
            endTime = res.time
            this.msgType = MsgType.AnaResult.value
            createTime = Date()
        }
        return if (fetchOneExist(stm) == null) {
            val c = sourceTraceMsgMapper.insert(stm)
            insertBlob(stm, GsonUtils.gson.toJson(res))
            c
        } 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,
        msgType: MsgType? = null,
    ): List<BaseExceptionResult?> {
        var res = sourceTraceMsgBlobMapper.selectWithBlob(deviceCode, startTime, endTime)
        if (msgType !== null) {
            res = res.filter { it?.msgType == msgType.value }
        }
 
        return res.map { stm ->
            when (stm?.msgType) {
                MsgType.PolClue.value,
                MsgType.DataChange.value,
                    -> {
                    GsonUtils.gson.fromJson(stm.blobContent, PollutedClue::class.java)
                }
 
                MsgType.AnaResult.value -> {
                    GsonUtils.gson.fromJson(stm.blobContent, AnalysisResult::class.java)
                }
 
                else -> null
            }
        }
    }
 
    @Transactional
    fun delete(mission: Mission): Int {
        val idList = sourceTraceMsgMapper.selectByExample(Example(SourceTraceMsg::class.java).apply {
            createCriteria().andEqualTo("deviceCode", mission.deviceCode)
                .andGreaterThanOrEqualTo("startTime", mission.startTime)
                .andLessThanOrEqualTo("endTime", mission.endTime)
        }).map { it?.id }
        if (idList.isEmpty()) return 0
        sourceTraceMsgMapper.deleteByExample(Example(SourceTraceMsg::class.java).apply {
            createCriteria().andIn("id", idList)
        })
        return sourceTraceMsgBlobMapper.deleteByExample(Example(SourceTraceMsgBlob::class.java).apply {
            createCriteria().andIn("msgId", idList)
        })
    }
}