[6.6.0]接管原因数据库
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
package com.mogo.eagle.core.function.takeover
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Looper
|
||||
import com.mogo.eagle.core.data.deva.report.CategoryInfo
|
||||
import com.mogo.eagle.core.function.takeover.db.ReasonDb
|
||||
import com.mogo.eagle.core.function.takeover.db.ReasonInfo
|
||||
import com.mogo.eagle.core.utilcode.kotlin.lifeCycleScope
|
||||
import com.mogo.eagle.core.utilcode.util.Utils
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
/**
|
||||
* 故障、接管原因数据库操作管理
|
||||
*/
|
||||
object ReasonManager {
|
||||
|
||||
private val scope by lazy {
|
||||
Utils.getApp().lifeCycleScope
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入故障、接管原因数据
|
||||
* @param context 上下文
|
||||
* @param categoryInfo 原因实体
|
||||
*/
|
||||
fun insertReason(context: Context, categoryInfo: CategoryInfo){
|
||||
categoryInfo.let {
|
||||
val reasonInfo = ReasonInfo(System.currentTimeMillis(),it.id,it.name,it.level,it.parentId,it.parentName,it.desc,it.terminalType)
|
||||
if (Thread.currentThread() == Looper.getMainLooper().thread){
|
||||
scope.launch{
|
||||
withContext(Dispatchers.IO){
|
||||
realInsertReason(context,reasonInfo)
|
||||
}
|
||||
}
|
||||
}else{
|
||||
realInsertReason(context,reasonInfo)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入故障、接管原因数据
|
||||
* @param context 上下文
|
||||
* @param reasonInfo 原因实体
|
||||
*/
|
||||
private fun realInsertReason(context: Context,reasonInfo: ReasonInfo){
|
||||
ReasonDb.getDb(context)
|
||||
.reasonDao()
|
||||
.insertReason(reasonInfo)
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改故障、接管原因数据
|
||||
*/
|
||||
fun updateReason(context: Context,categoryInfo: CategoryInfo){
|
||||
categoryInfo.let {
|
||||
val info = ReasonInfo(System.currentTimeMillis(),it.id,it.name,it.level,it.parentId,it.parentName,it.desc,it.terminalType)
|
||||
if (Thread.currentThread() == Looper.getMainLooper().thread){
|
||||
scope.launch{
|
||||
withContext(Dispatchers.IO){
|
||||
realUpdateReason(context,info)
|
||||
}
|
||||
}
|
||||
}else{
|
||||
realUpdateReason(context,info)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改故障、接管原因数据
|
||||
*/
|
||||
private fun realUpdateReason(context: Context,reasonInfo: ReasonInfo){
|
||||
ReasonDb.getDb(context)
|
||||
.reasonDao()
|
||||
.updateReason(reasonInfo)
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除故障、接管原因数据
|
||||
*/
|
||||
fun deleteReason(context: Context,categoryInfo: CategoryInfo){
|
||||
categoryInfo.let {
|
||||
val info = ReasonInfo(System.currentTimeMillis(),it.id,it.name,it.level,it.parentId,it.parentName,it.desc,it.terminalType)
|
||||
if (Thread.currentThread() == Looper.getMainLooper().thread){
|
||||
scope.launch {
|
||||
withContext(Dispatchers.IO) {
|
||||
realDeleteReason(context, info)
|
||||
}
|
||||
}
|
||||
}else{
|
||||
realDeleteReason(context, info)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除故障、接管原因数据
|
||||
*/
|
||||
private fun realDeleteReason(context: Context,reasonInfo: ReasonInfo){
|
||||
ReasonDb.getDb(context)
|
||||
.reasonDao().deleteReason(reasonInfo)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取故障、接管原因数据
|
||||
*/
|
||||
suspend fun getAllRecord(context: Context,parentId: Int,parentName: String): MutableList<ReasonInfo>{
|
||||
return ReasonDb.getDb(context)
|
||||
.reasonDao().getAllReason(parentId, parentName)
|
||||
}
|
||||
|
||||
fun deleteAllReason(context: Context,parentId: Int,parentName: String){
|
||||
if (Thread.currentThread() == Looper.getMainLooper().thread){
|
||||
scope.launch {
|
||||
withContext(Dispatchers.IO) {
|
||||
realDeleteAllReason(context, parentId, parentName)
|
||||
}
|
||||
}
|
||||
}else{
|
||||
realDeleteAllReason(context, parentId, parentName)
|
||||
}
|
||||
}
|
||||
|
||||
private fun realDeleteAllReason(context: Context,parentId: Int,parentName: String){
|
||||
ReasonDb.getDb(context).reasonDao().deleteAllReason(parentId, parentName)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.mogo.eagle.core.function.takeover
|
||||
import android.content.Context
|
||||
import com.alibaba.android.arouter.facade.annotation.Route
|
||||
import com.mogo.eagle.core.data.constants.MogoServicePaths
|
||||
import com.mogo.eagle.core.data.deva.report.CategoryInfo
|
||||
import com.mogo.eagle.core.data.deva.report.TakeOverRecordInfo
|
||||
import com.mogo.eagle.core.function.api.datacenter.takeover.ITakeOverProvider
|
||||
|
||||
@@ -48,4 +49,29 @@ class TakeOverRecordProvider: ITakeOverProvider {
|
||||
return recordInfoList
|
||||
}
|
||||
|
||||
override fun insertReason(context: Context, categoryInfo: CategoryInfo) {
|
||||
ReasonManager.insertReason(context, categoryInfo)
|
||||
}
|
||||
|
||||
override fun updateReason(context: Context, categoryInfo: CategoryInfo) {
|
||||
ReasonManager.updateReason(context, categoryInfo)
|
||||
}
|
||||
|
||||
override fun deleteReason(context: Context, categoryInfo: CategoryInfo) {
|
||||
ReasonManager.deleteReason(context, categoryInfo)
|
||||
}
|
||||
|
||||
override suspend fun getAllReason(context: Context,parentId: Int,parentName: String): MutableList<CategoryInfo> {
|
||||
val list = ReasonManager.getAllRecord(context,parentId, parentName)
|
||||
val categoryInfoList = mutableListOf<CategoryInfo>()
|
||||
list.forEach {
|
||||
categoryInfoList.add(CategoryInfo(it.id,it.name,it.desc,it.level,it.terminalType,it.parentId,it.parentName))
|
||||
}
|
||||
return categoryInfoList
|
||||
}
|
||||
|
||||
override fun deleteAllReason(context: Context, parentId: Int, parentName: String) {
|
||||
ReasonManager.deleteAllReason(context, parentId, parentName)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.mogo.eagle.core.function.takeover.db
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Delete
|
||||
import androidx.room.Insert
|
||||
import androidx.room.Query
|
||||
import androidx.room.Update
|
||||
|
||||
/**
|
||||
* 接管、故障实体类数据库操作
|
||||
*/
|
||||
@Dao
|
||||
interface ReasonDao {
|
||||
//插入数据
|
||||
@Insert
|
||||
fun insertReason(reason: ReasonInfo)
|
||||
//修改数据
|
||||
@Update
|
||||
fun updateReason(reason: ReasonInfo)
|
||||
//删除数据
|
||||
@Delete
|
||||
fun deleteReason(reason: ReasonInfo)
|
||||
//查询数据
|
||||
@Query("SELECT * FROM take_over_reason WHERE parentId = :parentId OR parentName = :parentName")
|
||||
suspend fun getAllReason(parentId: Int,parentName: String): MutableList<ReasonInfo>
|
||||
//清空子数据
|
||||
@Query("DELETE FROM take_over_reason WHERE parentId = :parentId OR parentName = :parentName")
|
||||
fun deleteAllReason(parentId: Int,parentName: String)
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.mogo.eagle.core.function.takeover.db
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
|
||||
/**
|
||||
* 接管、故障原因数据库
|
||||
*/
|
||||
@Database(entities = [ReasonInfo::class], version = 1, exportSchema = false)
|
||||
abstract class ReasonDb: RoomDatabase() {
|
||||
|
||||
abstract fun reasonDao(): ReasonDao
|
||||
|
||||
companion object{
|
||||
private const val RECORD_DB_NAME = "fault_reason.db"
|
||||
|
||||
private var db: ReasonDb ?= null
|
||||
|
||||
@JvmStatic
|
||||
fun getDb(context: Context): ReasonDb{
|
||||
if (db == null) {
|
||||
db = Room.databaseBuilder(context.applicationContext, ReasonDb::class.java, RECORD_DB_NAME)
|
||||
.fallbackToDestructiveMigration()
|
||||
.build()
|
||||
}
|
||||
return db!!
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.mogo.eagle.core.function.takeover.db
|
||||
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
import java.time.LocalDateTime
|
||||
|
||||
/**
|
||||
* 接管、故障原因
|
||||
*/
|
||||
@Entity(tableName = "take_over_reason")
|
||||
data class ReasonInfo(
|
||||
//自增ID主键
|
||||
@PrimaryKey(autoGenerate = false)
|
||||
val time: Long,
|
||||
//id
|
||||
@ColumnInfo
|
||||
val id: Int,
|
||||
//名称
|
||||
@ColumnInfo
|
||||
val name: String,
|
||||
//级别
|
||||
@ColumnInfo
|
||||
val level: Int,
|
||||
//父级ID
|
||||
@ColumnInfo
|
||||
val parentId: Int,
|
||||
//父级名称
|
||||
@ColumnInfo
|
||||
val parentName: String,
|
||||
//描述
|
||||
@ColumnInfo
|
||||
val desc: String,
|
||||
//适用终端 枚举: 1,2,3 枚举备注: 1 :小程序 2 :APP 3 :全部
|
||||
@ColumnInfo
|
||||
val terminalType: Int
|
||||
)
|
||||
Reference in New Issue
Block a user