[6.9.0]
[fix] [部分db迁移]
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package com.mogo.och.data.db.bean
|
||||
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
/**
|
||||
* 启动自驾使用的轨迹信息
|
||||
*/
|
||||
@Entity(tableName = ContrailDataBean.tableName)
|
||||
data class ContrailDataBean(
|
||||
|
||||
@PrimaryKey(autoGenerate = true) var id: Int = 0,
|
||||
|
||||
/**
|
||||
* 线路id
|
||||
*/
|
||||
@ColumnInfo(name = "line_id", typeAffinity = ColumnInfo.INTEGER, index = true)
|
||||
var lineId: Long? = null,
|
||||
|
||||
/**
|
||||
* csv格式的轨迹文件
|
||||
*/
|
||||
@ColumnInfo(name = "csv_file_url", typeAffinity = ColumnInfo.TEXT)
|
||||
var csvFileUrl: String? = null,
|
||||
|
||||
/**
|
||||
* csv_file_url 文件的md5值
|
||||
*/
|
||||
@ColumnInfo(name = "csv_file_md5", typeAffinity = ColumnInfo.TEXT)
|
||||
var csvFileMd5: String? = null,
|
||||
|
||||
/**
|
||||
* txt格式的轨迹文件
|
||||
*/
|
||||
@ColumnInfo(name = "txt_file_url", typeAffinity = ColumnInfo.TEXT)
|
||||
var txtFileUrl: String? = null,
|
||||
|
||||
/**
|
||||
* txt文件的md5
|
||||
*/
|
||||
@ColumnInfo(name = "txt_file_md5", typeAffinity = ColumnInfo.TEXT)
|
||||
var txtFileMd5: String? = null,
|
||||
|
||||
/**
|
||||
* 文件的保存时间
|
||||
*/
|
||||
@ColumnInfo(name = "contrail_save_time", typeAffinity = ColumnInfo.INTEGER)
|
||||
var contrailSaveTime: Long? = null,
|
||||
|
||||
/**
|
||||
* 前几个字符拼接后做md5的值
|
||||
*/
|
||||
@ColumnInfo(name = "md5", typeAffinity = ColumnInfo.TEXT, index = true)
|
||||
var md5: String? = null,
|
||||
|
||||
) {
|
||||
companion object {
|
||||
const val tableName: String = "contrail_data_table"
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return "ContrailDataBean(lineId=$lineId, csvFileUrl=$csvFileUrl, csvFileMd5=$csvFileMd5, txtFileUrl=$txtFileUrl, txtFileMd5=$txtFileMd5, contrailSaveTime=$contrailSaveTime)"
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.mogo.och.data.db.bean
|
||||
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
@Entity(tableName = LineDataBean.tableName)
|
||||
data class LineDataBean(
|
||||
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
var id: Long = 0,
|
||||
|
||||
/**
|
||||
* 线路id
|
||||
*/
|
||||
@ColumnInfo(name = "line_id", typeAffinity = ColumnInfo.INTEGER, index = true)
|
||||
var lineId: Long? = null,
|
||||
|
||||
/**
|
||||
* 线路名称
|
||||
*/
|
||||
@ColumnInfo(name = "line_name", typeAffinity = ColumnInfo.TEXT)
|
||||
@SerializedName("name")
|
||||
var lineName: String? = null,
|
||||
|
||||
/**
|
||||
* 终点站名称
|
||||
*/
|
||||
@ColumnInfo(name = "end_station_name", typeAffinity = ColumnInfo.TEXT)
|
||||
@SerializedName("endSiteName")
|
||||
var endStationName: String? = null,
|
||||
|
||||
/**
|
||||
* 存储此条数据时时间戳
|
||||
*/
|
||||
@ColumnInfo(name = "line_get_time", typeAffinity = ColumnInfo.INTEGER)
|
||||
val linegetTime: Long = System.currentTimeMillis(),
|
||||
) {
|
||||
companion object {
|
||||
const val tableName: String = "line_data_table"
|
||||
}
|
||||
|
||||
|
||||
|
||||
fun getLineIdAndName(function: (lineId:Long,lineName:String) -> Unit) {
|
||||
lineId?.let {id->
|
||||
lineName?.let {name->
|
||||
function(id,name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (javaClass != other?.javaClass) return false
|
||||
|
||||
other as LineDataBean
|
||||
|
||||
if (lineId != other.lineId) return false
|
||||
if (lineName != other.lineName) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = lineId?.hashCode() ?: 0
|
||||
result = 31 * result + (lineName?.hashCode() ?: 0)
|
||||
return result
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package com.mogo.och.data.db.bean
|
||||
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.Ignore
|
||||
import androidx.room.PrimaryKey
|
||||
import com.mogo.och.data.bean.SiteIntroduce
|
||||
|
||||
@Entity(tableName = SiteDataBean.tableName)
|
||||
data class SiteDataBean(
|
||||
|
||||
@PrimaryKey(autoGenerate = true) var id: Int = 0,
|
||||
/**
|
||||
* 站点id
|
||||
*/
|
||||
@ColumnInfo(name = "site_id", typeAffinity = ColumnInfo.INTEGER, index = true)
|
||||
var siteId: Long? = null,
|
||||
|
||||
/**
|
||||
* 站点所属线路Id
|
||||
*/
|
||||
@ColumnInfo(name = "line_id", typeAffinity = ColumnInfo.INTEGER, index = true)
|
||||
var lineId: Long? = null,
|
||||
|
||||
/**
|
||||
* 站点名称
|
||||
*/
|
||||
@ColumnInfo(name = "name", typeAffinity = ColumnInfo.TEXT)
|
||||
var name: String? = null,
|
||||
|
||||
/**
|
||||
* 站点韩文名称
|
||||
*/
|
||||
@ColumnInfo(name = "name_kr", typeAffinity = ColumnInfo.TEXT)
|
||||
var nameKr: String? = null,
|
||||
|
||||
/**
|
||||
* 站点排序
|
||||
*/
|
||||
@ColumnInfo(name = "seq", typeAffinity = ColumnInfo.INTEGER)
|
||||
var seq: Int? = null,
|
||||
|
||||
/**
|
||||
* 高德坐标
|
||||
*/
|
||||
@ColumnInfo(name = "gcj_lon", typeAffinity = ColumnInfo.REAL)
|
||||
var gcjLon: Double? = null,
|
||||
|
||||
/**
|
||||
* 高德坐标
|
||||
*/
|
||||
@ColumnInfo(name = "gcj_lat", typeAffinity = ColumnInfo.REAL)
|
||||
var gcjLat: Double? = null,
|
||||
|
||||
/**
|
||||
* 高精坐标
|
||||
*/
|
||||
@ColumnInfo(name = "lon", typeAffinity = ColumnInfo.REAL)
|
||||
var lon: Double? = null,
|
||||
/**
|
||||
* 高精坐标
|
||||
*/
|
||||
@ColumnInfo(name = "lat", typeAffinity = ColumnInfo.REAL)
|
||||
var lat: Double? = null,
|
||||
|
||||
/**
|
||||
* 站点介绍
|
||||
*/
|
||||
@ColumnInfo(name = "introduction", typeAffinity = ColumnInfo.TEXT)
|
||||
var introduction: String? = null,
|
||||
|
||||
/**
|
||||
* 是否播放站点介绍
|
||||
*/
|
||||
@ColumnInfo(name = "is_play_tts", typeAffinity = ColumnInfo.INTEGER)
|
||||
var isPlayTts: Boolean? = false,
|
||||
|
||||
/**
|
||||
* 除id 外其他值做的md5
|
||||
*/
|
||||
@ColumnInfo(name = "md5", typeAffinity = ColumnInfo.TEXT)
|
||||
var md5: String? = null,
|
||||
|
||||
/**
|
||||
* 站点视频
|
||||
*/
|
||||
@ColumnInfo(name = "videoList", typeAffinity = ColumnInfo.TEXT)
|
||||
var videoListDB: String? = null,
|
||||
|
||||
@Ignore
|
||||
var videoList:MutableList<SiteIntroduce>?=null
|
||||
) {
|
||||
companion object {
|
||||
const val tableName = "site_data_table"
|
||||
const val mediaTypeVideo = 1
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return "SiteDataBean(siteId=$siteId, lineId=$lineId, name=$name, nameKr=$nameKr, seq=$seq, gcjLon=$gcjLon, gcjLat=$gcjLat, lon=$lon, lat=$lat, introduction=$introduction, isPlayTts=$isPlayTts, videoList=$videoListDB)"
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (javaClass != other?.javaClass) return false
|
||||
|
||||
other as SiteDataBean
|
||||
|
||||
if (siteId != other.siteId) return false
|
||||
if (lineId != other.lineId) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = siteId?.hashCode() ?: 0
|
||||
result = 31 * result + (lineId?.hashCode() ?: 0)
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.mogo.och.data.db.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import com.mogo.och.data.db.bean.ContrailDataBean
|
||||
|
||||
@Dao
|
||||
interface ContrailDataDao {
|
||||
|
||||
//插入轨迹信息
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insert(vararg contrailDataBean: ContrailDataBean)
|
||||
|
||||
@Query("UPDATE ${ContrailDataBean.tableName} SET csv_file_url = :csv_file_url ,csv_file_md5 = :csv_file_md5 ,txt_file_url = :txt_file_url " +
|
||||
",txt_file_md5 = :txt_file_md5 ,contrail_save_time = :contrail_save_time,md5 = :md5 WHERE id = :id")
|
||||
fun update(csv_file_url: String,csv_file_md5: String,txt_file_url: String,txt_file_md5: String,contrail_save_time: Long,md5: String,id:Int)
|
||||
|
||||
// 线路删除时同步删除所属轨迹信息
|
||||
@Query("DELETE FROM ${ContrailDataBean.tableName} WHERE line_id = :lineId")
|
||||
fun deleteByLineId(lineId: Long)
|
||||
|
||||
//查询线路对应的轨迹信息
|
||||
@Query("SELECT * FROM ${ContrailDataBean.tableName} WHERE line_id = :lineId")
|
||||
fun queryContrailByLineId(lineId:Long): List<ContrailDataBean>?
|
||||
|
||||
//查询线路对应的轨迹信息
|
||||
@Query("SELECT * FROM ${ContrailDataBean.tableName} WHERE md5 = :md5")
|
||||
fun queryContrailByMd5(md5:String): List<ContrailDataBean>?
|
||||
|
||||
//查询线路对应的轨迹信息 只要一个结果
|
||||
fun queryContrailByLineIdOne(lineId: Long): ContrailDataBean?{
|
||||
val queryContrailByLineId = queryContrailByLineId(lineId)
|
||||
return if(queryContrailByLineId.isNullOrEmpty()){
|
||||
null
|
||||
}else{
|
||||
queryContrailByLineId.first()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.mogo.och.data.db.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Delete
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import com.mogo.och.common.module.utils.DateTimeUtil
|
||||
import com.mogo.och.data.db.bean.LineDataBean
|
||||
import io.reactivex.Observable
|
||||
|
||||
@Dao
|
||||
interface LineDataDao {
|
||||
//插入数据
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insert(vararg lineDataBean: LineDataBean)
|
||||
|
||||
//删除数据
|
||||
@Delete
|
||||
fun delete(vararg lineDataBean: LineDataBean)
|
||||
|
||||
@Query("UPDATE ${LineDataBean.tableName} SET line_name = :lineName ,end_station_name = :endStationName WHERE id = :id")
|
||||
fun updateInfo(lineName: String?, endStationName: String?, id: Long)
|
||||
|
||||
// 删除过时数据
|
||||
@Query("DELETE FROM ${LineDataBean.tableName} WHERE line_get_time < :zeroTime")
|
||||
fun deleteObsoleteData(zeroTime: Long = DateTimeUtil.getCurrentDateZero()):Int
|
||||
|
||||
|
||||
//删除非昨天添加的数据
|
||||
@Query("DELETE FROM ${LineDataBean.tableName} WHERE line_get_time < :zeroTime")
|
||||
fun deleteWeltData(zeroTime: Long = DateTimeUtil.getCurrentDateZero()): Int
|
||||
|
||||
//查询当天插入的所有数据
|
||||
@Query("SELECT * FROM ${LineDataBean.tableName} WHERE line_get_time > :zeroTime")
|
||||
fun loadDataRx(zeroTime: Long = DateTimeUtil.getCurrentDateZero()): Observable<List<LineDataBean>?>
|
||||
|
||||
//查询当天插入的所有数据
|
||||
@Query("SELECT * FROM ${LineDataBean.tableName} WHERE line_get_time > :zeroTime")
|
||||
fun loadData(zeroTime: Long = DateTimeUtil.getCurrentDateZero()): List<LineDataBean>?
|
||||
|
||||
@Query("SELECT * FROM ${LineDataBean.tableName} WHERE line_id = :lineId")
|
||||
fun queryByLineId(lineId: Int) : List<LineDataBean>?
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.mogo.och.data.db.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Delete
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import com.mogo.och.data.db.bean.SiteDataBean
|
||||
|
||||
@Dao
|
||||
interface SiteDataDao {
|
||||
//插入数据
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insert(vararg lineDataBean: SiteDataBean)
|
||||
|
||||
@Delete()
|
||||
fun deleteById(vararg siteDataBean: SiteDataBean)
|
||||
|
||||
@Query("UPDATE ${SiteDataBean.tableName} SET site_id = :site_id ,line_id = :lineId,name = :name,name_kr = :nameKr,seq = :seq," +
|
||||
"gcj_lon = :gcjLon,gcj_lat = :gcjLat,lon = :lon,lat = :lat,introduction = :introduction," +
|
||||
"is_play_tts = :isPlayTts,md5 = :md5,videoList = :videoListDB WHERE id = :id")
|
||||
fun updateInfo(site_id: Long?, lineId: Long?, name: String?,nameKr:String?,seq:Int?,
|
||||
gcjLon:Double?, gcjLat:Double?,lon:Double?,lat:Double?,introduction:String?,
|
||||
isPlayTts:Boolean?,md5:String?,videoListDB:String?,id:Int
|
||||
)
|
||||
|
||||
|
||||
// 线路删除时同步删除所属站点
|
||||
@Query("DELETE FROM ${SiteDataBean.tableName} WHERE line_id = :lineId")
|
||||
fun deleteByLineId(lineId: Long)
|
||||
|
||||
//查询线路对应的轨迹信息
|
||||
@Query("SELECT * FROM ${SiteDataBean.tableName} WHERE md5 = :md5")
|
||||
fun querySitesByMd5(md5:String): List<SiteDataBean>?
|
||||
|
||||
//查询线路对应的轨迹信息
|
||||
@Query("SELECT * FROM ${SiteDataBean.tableName}")
|
||||
fun queryAllSites(): List<SiteDataBean>?
|
||||
|
||||
|
||||
@Query("SELECT * FROM ${SiteDataBean.tableName} WHERE line_id = :lineId and site_id = :siteId")
|
||||
fun querySitesByLineIdAndSiteId(lineId: Long,siteId:Long):List<SiteDataBean>?
|
||||
|
||||
@Query("SELECT * FROM ${SiteDataBean.tableName} WHERE line_id = :lineId")
|
||||
fun querySitesByLineId(lineId: Long):List<SiteDataBean>?
|
||||
|
||||
@Query("SELECT * FROM ${SiteDataBean.tableName} GROUP by md5 HAVING count(1) > 1")
|
||||
fun checkData():List<SiteDataBean>?
|
||||
|
||||
fun querySiteByLineIdAndSiteId(lineId: Long,siteId:Long): SiteDataBean?{
|
||||
val queryContrailByLineId = querySitesByLineIdAndSiteId(lineId,siteId)
|
||||
return if(queryContrailByLineId.isNullOrEmpty()){
|
||||
null
|
||||
}else{
|
||||
queryContrailByLineId.first()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.mogo.och.data.db.exception
|
||||
|
||||
class DbException: RuntimeException {
|
||||
constructor() : super()
|
||||
constructor(message: String?) : super(message)
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.mogo.och.data.db.repository
|
||||
|
||||
import com.mogo.eagle.core.network.utils.digest.DigestUtils
|
||||
import com.mogo.och.common.module.manager.loop.BizLoopManager
|
||||
import com.mogo.och.data.db.bean.ContrailDataBean
|
||||
import com.mogo.och.data.db.dao.ContrailDataDao
|
||||
|
||||
abstract class BaseContraiDb {
|
||||
|
||||
open var contrailDao: ContrailDataDao?=null
|
||||
|
||||
fun addOrUpdate(contrailDatalist:List<ContrailDataBean>){
|
||||
val runnable = Runnable {
|
||||
// 校验数据个数
|
||||
contrailDao?.let { contrailDao->
|
||||
contrailDatalist.forEach {
|
||||
val md5Source = it.toString()
|
||||
val md5Hex = DigestUtils.md5Hex(md5Source)
|
||||
val queryContrailByMd5 = contrailDao.queryContrailByMd5(md5Hex)
|
||||
if(queryContrailByMd5.isNullOrEmpty()){// 没有值或者值有变化
|
||||
val needUpdate = contrailDao.queryContrailByLineIdOne(it.lineId!!)
|
||||
it.md5 = md5Hex
|
||||
if(needUpdate!=null){// 更新
|
||||
it.id = needUpdate.id
|
||||
contrailDao.update(it.csvFileUrl?:"",it.csvFileMd5?:"",it.txtFileUrl?:"",it.txtFileMd5?:"",System.currentTimeMillis(),md5Hex,needUpdate.id)
|
||||
}else{// 插入新的
|
||||
contrailDao.insert(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
BizLoopManager.runInIoThread(runnable)
|
||||
}
|
||||
|
||||
fun deleteByLineId(lineId: Long) {
|
||||
contrailDao?.deleteByLineId(lineId)
|
||||
}
|
||||
|
||||
fun queryAutopilotInfoByLineid(lineId: Long): ContrailDataBean? {
|
||||
return contrailDao?.queryContrailByLineIdOne(lineId)
|
||||
}
|
||||
|
||||
fun saveRunningInfo(
|
||||
lineId: Int,
|
||||
csvFileMd5: String?,
|
||||
csvFileUrl: String?,
|
||||
txtFileUrl: String?,
|
||||
txtFileMd5: String?,
|
||||
contrailSaveTime: Long
|
||||
) {
|
||||
val runningLine = ContrailDataBean(
|
||||
lineId = lineId.toLong(),
|
||||
csvFileUrl = csvFileUrl,
|
||||
csvFileMd5 = csvFileMd5,
|
||||
txtFileUrl = txtFileUrl,
|
||||
txtFileMd5 = txtFileMd5,
|
||||
contrailSaveTime = contrailSaveTime
|
||||
)
|
||||
addOrUpdate(mutableListOf(runningLine))
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package com.mogo.och.data.db.repository
|
||||
|
||||
import androidx.room.Transaction
|
||||
import com.mogo.och.common.module.manager.logchainanalytic.OchChainLogManager
|
||||
import com.mogo.och.common.module.manager.loop.BizLoopManager
|
||||
import com.mogo.och.data.db.bean.LineDataBean
|
||||
import com.mogo.och.data.db.dao.LineDataDao
|
||||
import io.reactivex.Observable
|
||||
|
||||
abstract class BaseLineDb {
|
||||
|
||||
open var lineDao: LineDataDao?=null
|
||||
|
||||
|
||||
fun cleanWeltData(){
|
||||
lineDao?.deleteWeltData()
|
||||
}
|
||||
|
||||
fun queryCanUserLine(): List<LineDataBean>? {
|
||||
return lineDao?.loadData()
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取可用线路
|
||||
*/
|
||||
fun queryCanUseLineRx(): Observable<List<LineDataBean>?>? {
|
||||
return lineDao?.loadDataRx()
|
||||
}
|
||||
|
||||
/**
|
||||
* 后台line 删除 需要连带着把轨迹信息、站点信息和任务信息删除
|
||||
*/
|
||||
abstract fun deleteSitesTaskAndContraiDb(lineId:Long)
|
||||
|
||||
fun checkAndUpdate(serverDatalist:List<LineDataBean>){
|
||||
val runable = object :Runnable{
|
||||
@Transaction
|
||||
override fun run() {
|
||||
// 校验数据个数
|
||||
lineDao?.let { lineDao->
|
||||
val loadData = lineDao.loadData()
|
||||
if(loadData.isNullOrEmpty()){
|
||||
// 插入新的值
|
||||
lineDao.insert(*serverDatalist.toTypedArray())
|
||||
// TODO: Ui展示需要动态刷新UI去
|
||||
return
|
||||
}
|
||||
// 后台新增数据
|
||||
val needAddDatas = serverDatalist-loadData
|
||||
// 后台没有本地数据库有的未分配线路
|
||||
val needMinusDatas = loadData-serverDatalist
|
||||
|
||||
// TODO: Ui展示需要动态刷新UI去
|
||||
|
||||
if (needAddDatas.isNotEmpty()) {
|
||||
// 新增线路
|
||||
lineDao.insert(*needAddDatas.toTypedArray())
|
||||
}
|
||||
if (needMinusDatas.isNotEmpty()) {
|
||||
// 删除线路
|
||||
lineDao.delete(*needMinusDatas.toTypedArray())
|
||||
needMinusDatas.forEach { minusLine->
|
||||
minusLine.lineId?.let { lineId->
|
||||
// 删除线路对应的站点
|
||||
|
||||
deleteSitesTaskAndContraiDb(lineId)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 交集 服务器端
|
||||
val needUpdateByMd5 = serverDatalist-needAddDatas
|
||||
// 交集 本地数据库
|
||||
val oldDataList = loadData-needMinusDatas
|
||||
|
||||
val needDelete = mutableListOf<LineDataBean>()
|
||||
val needUpDate = mutableListOf<LineDataBean>()
|
||||
|
||||
needUpdateByMd5.forEach {
|
||||
val oldList = oldDataList.filter { local -> local.lineId == it.lineId }
|
||||
if(oldList.isNotEmpty()){
|
||||
oldList.forEachIndexed { index, lineBean ->
|
||||
if(index==0){
|
||||
if (lineBean.lineName == it.lineName && lineBean.endStationName == it.endStationName) {
|
||||
// 数据没有变化
|
||||
}else{
|
||||
it.id = lineBean.id
|
||||
needUpDate.add(it)
|
||||
}
|
||||
}else{
|
||||
// 站点多了 需要删除
|
||||
needDelete.add(lineBean)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
lineDao.delete(*needDelete.toTypedArray())
|
||||
|
||||
needUpDate.forEach { updateItem->
|
||||
// 跟新数据
|
||||
lineDao.updateInfo(updateItem.lineName,updateItem.endStationName,updateItem.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BizLoopManager.runInIoThread(runable)
|
||||
}
|
||||
|
||||
fun deleteObsoleteData(){
|
||||
lineDao?.deleteObsoleteData()?.let {
|
||||
OchChainLogManager.writeChainLogDb("删除临时数据","Line删除数量:${it}")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 没有线路就插入线路
|
||||
*/
|
||||
fun saveRunningInfo(lineId: Int, lineName: String?, endStationName: String?) {
|
||||
val queryByLineId = lineDao?.queryByLineId(lineId)
|
||||
if(queryByLineId.isNullOrEmpty()){
|
||||
lineDao?.insert(LineDataBean(lineId = lineId.toLong(), lineName = lineName, endStationName = endStationName))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
package com.mogo.och.data.db.repository
|
||||
|
||||
import androidx.room.Transaction
|
||||
import com.mogo.eagle.core.network.utils.digest.DigestUtils
|
||||
import com.mogo.eagle.core.utilcode.mogo.logger.CallerLogger
|
||||
import com.mogo.eagle.core.utilcode.util.TAG
|
||||
import com.mogo.och.common.module.manager.logchainanalytic.OchChainLogManager
|
||||
import com.mogo.och.common.module.manager.loop.BizLoopManager
|
||||
import com.mogo.och.data.db.bean.SiteDataBean
|
||||
import com.mogo.och.data.db.dao.SiteDataDao
|
||||
|
||||
open class BaseSiteDb {
|
||||
|
||||
open var siteDataDao: SiteDataDao? = null
|
||||
|
||||
fun addOrUpdate(serverDataList: List<SiteDataBean>) {
|
||||
val runnable = object :Runnable {
|
||||
@Transaction
|
||||
override fun run() {
|
||||
siteDataDao?.let { siteDataDao ->
|
||||
val localAllSites = siteDataDao.queryAllSites()
|
||||
if (localAllSites.isNullOrEmpty()) {
|
||||
// 插入新数据
|
||||
siteDataDao.insert(*serverDataList.toTypedArray())
|
||||
return
|
||||
}
|
||||
// 后台新增数据
|
||||
val needAddDatas = serverDataList-localAllSites
|
||||
// 后台没有本地数据库有的未分配线路
|
||||
val needMinusDatas = localAllSites - serverDataList
|
||||
|
||||
if (needAddDatas.isNotEmpty()) {
|
||||
// 新增线路
|
||||
needAddDatas.forEach {
|
||||
val md5Source = it.toString()
|
||||
val md5Hex = DigestUtils.md5Hex(md5Source)
|
||||
it.md5 = md5Hex
|
||||
}
|
||||
// 新增插入新数据
|
||||
siteDataDao.insert(*needAddDatas.toTypedArray())
|
||||
}
|
||||
|
||||
if (needMinusDatas.isNotEmpty()) {
|
||||
// 删除线路
|
||||
siteDataDao.deleteById(*needMinusDatas.toTypedArray())
|
||||
}
|
||||
|
||||
// 交集 服务器端
|
||||
val needUpdateByMd5 = serverDataList-needAddDatas
|
||||
// 交集 本地数据库
|
||||
val oldDataList = localAllSites-needMinusDatas
|
||||
|
||||
val needDelete = mutableListOf<SiteDataBean>()
|
||||
val needUpDate = mutableListOf<SiteDataBean>()
|
||||
|
||||
|
||||
needUpdateByMd5.forEach {
|
||||
val md5Source = it.toString()
|
||||
val md5Hex = DigestUtils.md5Hex(md5Source)
|
||||
val oldList =
|
||||
oldDataList.filter { local -> local.siteId == it.siteId && local.lineId == it.lineId }
|
||||
if(oldList.isNotEmpty()){
|
||||
oldList.forEachIndexed { index, siteDataBean ->
|
||||
if(index==0){
|
||||
if (siteDataBean.md5 == md5Hex) {
|
||||
// 数据没有变化
|
||||
}else{
|
||||
it.id = siteDataBean.id
|
||||
it.md5 = md5Hex
|
||||
needUpDate.add(it)
|
||||
|
||||
}
|
||||
}else{
|
||||
needDelete.add(siteDataBean)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
needUpDate.forEach {updateDateItem->
|
||||
siteDataDao.updateInfo(updateDateItem.siteId,updateDateItem.lineId,updateDateItem.name,updateDateItem.nameKr,
|
||||
updateDateItem.seq,updateDateItem.gcjLon,updateDateItem.gcjLat,updateDateItem.lon,updateDateItem.lat,
|
||||
updateDateItem.introduction,updateDateItem.isPlayTts,updateDateItem.md5,updateDateItem.videoListDB,updateDateItem.id)
|
||||
}
|
||||
siteDataDao.deleteById(*needDelete.toTypedArray())
|
||||
checkData()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
BizLoopManager.runInIoThread(runnable)
|
||||
}
|
||||
|
||||
fun checkData() {
|
||||
CallerLogger.d(TAG,"检测站点数据")
|
||||
siteDataDao?.checkData()?.let { list ->
|
||||
list.forEach {
|
||||
siteDataDao?.deleteById(it)
|
||||
}
|
||||
if (list.isNotEmpty()) {
|
||||
OchChainLogManager.writeChainLogDb("数据检测", "有重复数据${list}")
|
||||
checkData()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteByLineId(lineId: Long) {
|
||||
siteDataDao?.deleteByLineId(lineId)
|
||||
}
|
||||
|
||||
fun querySiteByLineId(lineId:Long):List<SiteDataBean>?{
|
||||
siteDataDao?.let { siteDataDao->
|
||||
return siteDataDao.querySitesByLineId(lineId)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user