desc:修正无结束时间的告警数据

main
xiaowusky 2 years ago
parent 5dbe3fef20
commit af0b856491

@ -2,11 +2,38 @@ package com.yinuo.safetywatcher.watcher
import com.common.commonlib.CommonApplication import com.common.commonlib.CommonApplication
import com.lztek.toolkit.Lztek import com.lztek.toolkit.Lztek
import com.yinuo.safetywatcher.watcher.db.DBUtils
import com.yinuo.safetywatcher.watcher.db.dao.WarningDao
import com.yinuo.safetywatcher.watcher.db.entity.Warning
import com.yinuo.safetywatcher.watcher.utils.LztekUtil import com.yinuo.safetywatcher.watcher.utils.LztekUtil
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
@OptIn(DelicateCoroutinesApi::class)
class App : CommonApplication() { class App : CommonApplication() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
LztekUtil.setObject(Lztek.create(this)) LztekUtil.setObject(Lztek.create(this))
tryFixDbData()
}
private fun tryFixDbData() {
GlobalScope.launch {
val warningDao = DBUtils.warningDao()
val warningList = warningDao.queryWarningsNoEndTime()
warningList?.apply {
forEach { warning ->
warning.endTime = tryAssignment(warningDao, warning)
}
warningDao.updateAll(warningList)
}
}
}
private suspend fun tryAssignment(warningDao: WarningDao, warning: Warning): Long {
val latest = warningDao.findLatestByName(warning.gasName)
return latest?.endTime ?: System.currentTimeMillis()
} }
} }

@ -16,7 +16,7 @@ abstract class AppDatabase : RoomDatabase() {
} }
object DBUtils { object DBUtils {
val db by lazy { private val db by lazy {
Room.databaseBuilder( Room.databaseBuilder(
CommonApplication.getContext()!!, CommonApplication.getContext()!!,
AppDatabase::class.java, "watcher_db" AppDatabase::class.java, "watcher_db"

@ -4,15 +4,22 @@ import androidx.room.Dao
import androidx.room.Delete import androidx.room.Delete
import androidx.room.Insert import androidx.room.Insert
import androidx.room.Query import androidx.room.Query
import androidx.room.Update
import com.yinuo.safetywatcher.watcher.db.entity.Warning import com.yinuo.safetywatcher.watcher.db.entity.Warning
@Dao @Dao
interface WarningDao { interface WarningDao {
@Query("SELECT * FROM warning") @Query("SELECT * FROM warning")
suspend fun getAll(): List<Warning> suspend fun getAll(): List<Warning>?
@Query("SELECT * FROM warning WHERE gas_name IS :name AND start_time BETWEEN :startTime AND :endTime") @Query("SELECT * FROM warning WHERE gas_name IS :name AND start_time BETWEEN :startTime AND :endTime")
suspend fun findByName(name: String, startTime: Long, endTime: Long): List<Warning> suspend fun findAllByName(name: String, startTime: Long, endTime: Long): List<Warning>?
@Query("SELECT * FROM warning WHERE gas_name IS :name ORDER BY id DESC LIMIT 1")
suspend fun findLatestByName(name: String): Warning?
@Query("SELECT * FROM warning WHERE end_time = -1")
suspend fun queryWarningsNoEndTime(): List<Warning>?
@Insert @Insert
suspend fun insertAll(vararg warnings: Warning) suspend fun insertAll(vararg warnings: Warning)
@ -22,4 +29,10 @@ interface WarningDao {
@Delete @Delete
suspend fun delete(warning: Warning) suspend fun delete(warning: Warning)
@Update
suspend fun updateAll(warningList: List<Warning>)
@Update
suspend fun update(warning: Warning)
} }

@ -13,5 +13,5 @@ data class Warning(
@ColumnInfo(name = "threshold_low") val thresholdLow: Double, @ColumnInfo(name = "threshold_low") val thresholdLow: Double,
@ColumnInfo(name = "threshold_high") val thresholdHigh: Double, @ColumnInfo(name = "threshold_high") val thresholdHigh: Double,
@ColumnInfo(name = "start_time") val startTime: Long, @ColumnInfo(name = "start_time") val startTime: Long,
@ColumnInfo(name = "end_time") val endTime: Long, @ColumnInfo(name = "end_time") var endTime: Long = -1L,
) )

Loading…
Cancel
Save