feiyu02
2024-07-18 5b0d58c3f7f35f61c0a0437bac3ff708db57fe61
src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/ExceptionAnalysisController.kt
@@ -10,15 +10,20 @@
import com.flightfeather.uav.common.location.LocationRoadNearby
import com.flightfeather.uav.domain.entity.Mission
import com.flightfeather.uav.domain.repository.RealTimeDataRep
import com.flightfeather.uav.domain.repository.SegmentInfoRep
import org.springframework.stereotype.Component
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
/**
 * 数据异常分析控制器
 */
@Component
class ExceptionAnalysisController(
class  ExceptionAnalysisController(
    private val realTimeDataRep: RealTimeDataRep,
    private val locationRoadNearby: LocationRoadNearby,
    private val segmentInfoRep: SegmentInfoRep,
) {
    var running = false
@@ -49,22 +54,11 @@
        taskList.forEach { it.onDone() }
        taskList.forEach {
            // 查询异常周边可能污染源
            it.resultList.forEach { r->
                if (r.longitude != null && r.latitude != null) {
                    val sceneList = locationRoadNearby.searchByRadius(
                        r.longitude!!.toDouble() to r.latitude!!.toDouble(), config.radius)
                    if (sceneList.isNotEmpty()) {
                        val idList = mutableListOf<String>()
                        val nameList = mutableListOf<String>()
                        sceneList.forEach { s->
                            idList.add(s?.guid?:"")
                            nameList.add(s?.name ?: "")
                        }
                        r.relatedSceneId = idList
                        r.relatedSceneName = nameList
                    }
                }
                // 查询异常周边可能污染源
                nearBy(r, config)
                // 查询时段所在路段
                road(r)
            }
            // 存储分析结果
            result.addAll(it.resultList)
@@ -72,4 +66,47 @@
        running = false
        return result
    }
    private fun nearBy(r: ExceptionResult, config: DataAnalysisConfig) {
        if (r.longitude != null && r.latitude != null) {
            val sceneList = locationRoadNearby.searchByRadius(
                r.longitude!!.toDouble() to r.latitude!!.toDouble(), config.radius)
            if (sceneList.isNotEmpty()) {
                val idList = mutableListOf<String>()
                val nameList = mutableListOf<String>()
                sceneList.forEach { s->
                    idList.add(s?.guid?:"")
                    nameList.add(s?.name ?: "")
                }
                r.relatedSceneId = idList
                r.relatedSceneName = nameList
            }
        }
    }
    private fun road(r: ExceptionResult) {
        val sT = LocalDateTime.ofInstant(r.startDate?.toInstant(), ZoneId.systemDefault())
        val eT = LocalDateTime.ofInstant(r.endDate?.toInstant(), ZoneId.systemDefault())
        val segments = segmentInfoRep.findPeriod(sT, eT)
        var txt = ""
        val size = segments.size
        segments.forEachIndexed { i, s ->
            txt += if (i == 0) {
                if (size == 1) {
                    "在${s?.street}"
                } else {
                    "从${s?.street}"
                }
            } else if (i == size - 1 && i >= 2) {
                ",至${s?.street}"
            } else {
                if (i == 1) {
                    ",经${s?.street}"
                } else {
                    "、${s?.street}"
                }
            }
        }
        r.road = txt
    }
}