| | |
| | | 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.* |
| | | |
| | |
| | | * @author feiyu02 |
| | | */ |
| | | @Repository |
| | | class SourceTraceRep(private val sourceTraceMsgMapper: SourceTraceMsgMapper) { |
| | | 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 |
| | |
| | | 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) |
| | | val c = sourceTraceMsgMapper.insert(stm) |
| | | insertBlob(stm, GsonUtils.gson.toJson(obj)) |
| | | c |
| | | } else { |
| | | 0 |
| | | } |
| | |
| | | /** |
| | | * 插入线索信息 |
| | | */ |
| | | @Transactional |
| | | 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) |
| | | val c = sourceTraceMsgMapper.insert(stm) |
| | | insertBlob(stm, GsonUtils.gson.toJson(res)) |
| | | c |
| | | } else { |
| | | 0 |
| | | } |
| | |
| | | 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 -> |
| | | 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.content, PollutedClue::class.java) |
| | | GsonUtils.gson.fromJson(stm.blobContent, PollutedClue::class.java) |
| | | } |
| | | |
| | | MsgType.AnaResult.value -> { |
| | | GsonUtils.gson.fromJson(stm.content, AnalysisResult::class.java) |
| | | GsonUtils.gson.fromJson(stm.blobContent, AnalysisResult::class.java) |
| | | } |
| | | |
| | | else -> null |
| | |
| | | } |
| | | } |
| | | |
| | | @Transactional |
| | | fun delete(mission: Mission): Int { |
| | | return sourceTraceMsgMapper.deleteByExample(Example(SourceTraceMsg::class.java).apply { |
| | | 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) |
| | | }) |
| | | } |
| | | } |