Browse Source

@task: dict data to db

bai 3 years ago
parent
commit
0aab3d0386

+ 1 - 1
demo/src/main/java/com/framework/app/api/Api.kt

@@ -13,7 +13,7 @@ interface Api {
      * 获取自字典数据
      */
     @POST("adm/dict")
-    suspend fun getDefine(): Model<Define>
+    suspend fun getDefine(@Body request: FrameRequest): Model<Define>
 
     /**
      * 获取框架数据

+ 25 - 0
demo/src/main/java/com/framework/mvvm/model/data/ToDb.kt

@@ -0,0 +1,25 @@
+package com.framework.mvvm.model.data
+
+import android.util.Log
+
+/**
+ * 网络数据直接从 from 流入 to
+ */
+suspend fun <Result> toDb(
+    from: suspend () -> Model<Result>,
+    to: suspend (Result) -> Unit
+): Boolean {
+    return when (val response: Response<Result> = request { from() }) {
+        is EmptyResponse -> {
+            false
+        }
+        is SuccessResponse -> {
+            to(response.data)
+            true
+        }
+        is FailureResponse -> {
+            Log.d("ntToDb", response.msg)
+            false
+        }
+    }
+}

+ 3 - 0
demo/src/main/java/com/framework/mvvm/model/db/dao/ConfigDao.kt

@@ -17,6 +17,9 @@ interface ConfigDao {
     @Insert(onConflict = OnConflictStrategy.REPLACE)
     suspend fun insConfig(vararg config: ConfigEntity)
 
+    @Insert(onConflict = OnConflictStrategy.REPLACE)
+    suspend fun insConfigs(configs: List<ConfigEntity>)
+
     @Delete
     suspend fun delConfig(config: ConfigEntity)
 }

+ 4 - 1
demo/src/main/java/com/framework/mvvm/model/db/dao/EquipmentDao.kt

@@ -15,7 +15,10 @@ interface EquipmentDao {
     suspend fun getEquipmentById(id: String): EquipmentEntity
 
     @Insert(onConflict = OnConflictStrategy.REPLACE)
-    suspend fun insEquipment(equipment: EquipmentEntity)
+    suspend fun insEquipment(vararg equipment: EquipmentEntity)
+
+    @Insert(onConflict = OnConflictStrategy.REPLACE)
+    suspend fun insEquipments(equipments: List<EquipmentEntity>)
 
     @Delete
     suspend fun delEquipment(equipment: EquipmentEntity)

+ 4 - 1
demo/src/main/java/com/framework/mvvm/model/db/dao/InfosConfigDao.kt

@@ -15,7 +15,10 @@ interface InfosConfigDao {
     suspend fun getConfigById(id: String): InfosConfigEntity
 
     @Insert(onConflict = OnConflictStrategy.REPLACE)
-    suspend fun insConfig(config: InfosConfigEntity)
+    suspend fun insConfig(vararg config: InfosConfigEntity)
+
+    @Insert(onConflict = OnConflictStrategy.REPLACE)
+    suspend fun insConfigs(configs: List<InfosConfigEntity>)
 
     @Delete
     suspend fun delConfig(config: InfosConfigEntity)

+ 4 - 1
demo/src/main/java/com/framework/mvvm/model/db/dao/M2dEquipDao.kt

@@ -15,7 +15,10 @@ interface M2dEquipDao {
     suspend fun getM2dById(id: String): M2dEquipEntity
 
     @Insert(onConflict = OnConflictStrategy.REPLACE)
-    suspend fun insM2d(m2d: M2dEquipEntity)
+    suspend fun insM2d(vararg m2d: M2dEquipEntity)
+
+    @Insert(onConflict = OnConflictStrategy.REPLACE)
+    suspend fun insM2ds(m2ds: List<M2dEquipEntity>)
 
     @Delete
     suspend fun delM2d(m2d: M2dEquipEntity)

+ 4 - 1
demo/src/main/java/com/framework/mvvm/model/db/dao/MajorDao.kt

@@ -15,7 +15,10 @@ interface MajorDao {
     suspend fun getMajorById(id: String): MajorEntity
 
     @Insert(onConflict = OnConflictStrategy.REPLACE)
-    suspend fun insMajor(major: MajorEntity)
+    suspend fun insMajor(vararg major: MajorEntity)
+
+    @Insert(onConflict = OnConflictStrategy.REPLACE)
+    suspend fun insMajors(majors: List<MajorEntity>)
 
     @Delete
     suspend fun delMajor(major: MajorEntity)

+ 4 - 1
demo/src/main/java/com/framework/mvvm/model/db/dao/PipeConfigDao.kt

@@ -15,7 +15,10 @@ interface PipeConfigDao {
     suspend fun getConfigById(id: String): PipeConfigEntity
 
     @Insert(onConflict = OnConflictStrategy.REPLACE)
-    suspend fun insConfig(config: PipeConfigEntity)
+    suspend fun insConfig(vararg config: PipeConfigEntity)
+
+    @Insert(onConflict = OnConflictStrategy.REPLACE)
+    suspend fun insConfigs(configs: List<PipeConfigEntity>)
 
     @Delete
     suspend fun delConfig(config: PipeConfigEntity)

+ 4 - 1
demo/src/main/java/com/framework/mvvm/model/db/dao/RelConfigDao.kt

@@ -15,7 +15,10 @@ interface RelConfigDao {
     suspend fun getConfigById(id: String): RelConfigEntity
 
     @Insert(onConflict = OnConflictStrategy.REPLACE)
-    suspend fun insConfig(config: RelConfigEntity)
+    suspend fun insConfig(vararg config: RelConfigEntity)
+
+    @Insert(onConflict = OnConflictStrategy.REPLACE)
+    suspend fun insConfigs(configs: List<RelConfigEntity>)
 
     @Delete
     suspend fun delConfig(config: RelConfigEntity)

+ 4 - 1
demo/src/main/java/com/framework/mvvm/model/db/dao/RelationDictDao.kt

@@ -15,7 +15,10 @@ interface RelationDictDao {
     suspend fun getRelationDictById(id: String): RelationDictEntity
 
     @Insert(onConflict = OnConflictStrategy.REPLACE)
-    suspend fun insRelationDict(relationDict: RelationDictEntity)
+    suspend fun insRelationDict(vararg relationDict: RelationDictEntity)
+
+    @Insert(onConflict = OnConflictStrategy.REPLACE)
+    suspend fun insRelationDicts(relationDicts: List<RelationDictEntity>)
 
     @Delete
     suspend fun delRelationDict(relationDict: RelationDictEntity)

+ 8 - 0
demo/src/main/java/com/framework/mvvm/model/db/dao/SystemDao.kt

@@ -1,6 +1,8 @@
 package com.framework.mvvm.model.db.dao
 
 import androidx.room.Dao
+import androidx.room.Insert
+import androidx.room.OnConflictStrategy
 import androidx.room.Query
 import com.framework.mvvm.model.db.entity.SystemEntity
 
@@ -14,4 +16,10 @@ interface SystemDao {
 
     @Query("select * from system where id = :id")
     suspend fun getEquById(id: String): SystemEntity
+
+    @Insert(onConflict = OnConflictStrategy.REPLACE)
+    suspend fun insSystem(vararg system: SystemEntity)
+
+    @Insert(onConflict = OnConflictStrategy.REPLACE)
+    suspend fun insSystems(systems: List<SystemEntity>)
 }

+ 8 - 8
demo/src/main/java/com/framework/mvvm/model/db/entity/EquipmentEntity.kt

@@ -11,14 +11,14 @@ import androidx.room.PrimaryKey
 data class EquipmentEntity(
     @PrimaryKey val id: String,
     @ColumnInfo(name = "valid") val valid: Int,                  // 1 正常 0 删除
-    @ColumnInfo(name = "project_id") val projectId: String,      // 项目 id
-    @ColumnInfo(name = "code") val code: String,                 // 系统编码
-    @ColumnInfo(name = "name") val name: String,                 // 系统名称
-    @ColumnInfo(name = "obj_type") val objType: String,          // 对象类型 "system"
-    @ColumnInfo(name = "group_code") val groupCode: String,      // 集团编码
-    @ColumnInfo(name = "major_code") val majorCode: String,      // 专业编码
-    @ColumnInfo(name = "system_code") val systemCode: String,       // 系统编码
-    @ColumnInfo(name = "parent_code") val parentCode: String,    // 父编码
+    @ColumnInfo(name = "project_id") val projectId: String?,      // 项目 id
+    @ColumnInfo(name = "code") val code: String?,                 // 系统编码
+    @ColumnInfo(name = "name") val name: String?,                 // 系统名称
+    @ColumnInfo(name = "obj_type") val objType: String?,          // 对象类型 "system"
+    @ColumnInfo(name = "group_code") val groupCode: String?,      // 集团编码
+    @ColumnInfo(name = "major_code") val majorCode: String?,      // 专业编码
+    @ColumnInfo(name = "system_code") val systemCode: String?,       // 系统编码
+    @ColumnInfo(name = "parent_code") val parentCode: String?,    // 父编码
     @ColumnInfo(name = "equipment_code") val equipmentCode: String?, // 设备编码
     @ColumnInfo(name = "creator") val creator: String?,           // 创建人
     @ColumnInfo(name = "creation_time") val creationTime: Long?,  // 创建时间

+ 8 - 3
demo/src/main/java/com/framework/mvvm/model/db/entity/InfosConfigEntity.kt

@@ -3,7 +3,9 @@ package com.framework.mvvm.model.db.entity
 import androidx.room.ColumnInfo
 import androidx.room.Entity
 import androidx.room.PrimaryKey
+import kotlinx.serialization.Contextual
 import kotlinx.serialization.Serializable
+import org.json.JSONObject
 
 /**
  * 信息配置 表
@@ -12,11 +14,14 @@ import kotlinx.serialization.Serializable
 @Serializable
 data class InfosConfigEntity(
     @PrimaryKey(autoGenerate = false) val id: String,
-    @ColumnInfo(name = "name") val name: String,               // 对象类型名称
+    @ColumnInfo(name = "name") val name: String?,                // 对象类型名称
     @ColumnInfo(name = "obj_type") val objType: String?,        // 对象类型(space|equipment)
     @ColumnInfo(name = "class_code") val classCode: String?,    // 对象class_code
     @ColumnInfo(name = "major_code") val majorCode: String?,    // 专业代码(类型为设备时)
     @ColumnInfo(name = "system_code") val systemCode: String?,  // 系统代码(类型为设备时)
-    @ColumnInfo(name = "project_id") val projectId: String?,             // 对象类型的信息详情点配置
-    @ColumnInfo(name = "infos") val infos: String?,             // 对象类型的信息详情点配置
+    @ColumnInfo(name = "project_id") val projectId: String?,    // 对象类型的信息详情点配置
+    @ColumnInfo(name = "str_infos") val strInfos: String?,      // 对象类型的信息详情点配置
+    @ColumnInfo(name = "infos")
+    @Contextual val infos: JSONObject?                         // 对象类型的信息详情点配置
+
 )

+ 1 - 1
demo/src/main/java/com/framework/mvvm/model/db/entity/ObjectEntity.kt

@@ -10,7 +10,7 @@ import org.json.JSONObject
 
 /**
  * 对象表
- * 建筑,楼层,空间, 设备
+ * 建筑,楼层,空间,设备
  */
 
 @Entity(tableName = "object")

+ 6 - 6
demo/src/main/java/com/framework/mvvm/model/db/entity/PipeConfigEntity.kt

@@ -12,10 +12,10 @@ import kotlinx.serialization.Serializable
 @Serializable
 data class PipeConfigEntity(
     @PrimaryKey(autoGenerate = false) val id: String,
-    @ColumnInfo(name = "class_code_from") val classCodeFrom: String,    // from 端设备对象class_code
-    @ColumnInfo(name = "class_code_to") val classCodeTo: String,        //  to  端设备对象class_code
-    @ColumnInfo(name = "pipe_type") val pipeType: String,               // 管道类型(数据字典定义)
-    @ColumnInfo(name = "pipe_sub_type") val pipeSubType: String,        // 管道用途(业务定义补充描述类型)
-    @ColumnInfo(name = "pipe_direction") val pipeDirection: Int,        // 管道方向(1:正向from->to;-1:反向to->from;0:无方向)
-    @ColumnInfo(name = "pipe_description") val pipeDescription: String, // 描述信息
+    @ColumnInfo(name = "class_code_from") val classCodeFrom: String?,    // from 端设备对象class_code
+    @ColumnInfo(name = "class_code_to") val classCodeTo: String?,        //  to  端设备对象class_code
+    @ColumnInfo(name = "pipe_type") val pipeType: String?,               // 管道类型(数据字典定义)
+    @ColumnInfo(name = "pipe_sub_type") val pipeSubType: String?,        // 管道用途(业务定义补充描述类型)
+    @ColumnInfo(name = "pipe_direction") val pipeDirection: Int?,        // 管道方向(1:正向from->to;-1:反向to->from;0:无方向)
+    @ColumnInfo(name = "pipe_description") val pipeDescription: String?, // 描述信息
 )

+ 33 - 1
demo/src/main/java/com/framework/mvvm/model/repository/FrameRepository.kt

@@ -7,6 +7,7 @@ import com.framework.app.base.IRepository
 import com.framework.mvvm.model.data.Data
 import com.framework.mvvm.model.data.dataSource
 import com.framework.mvvm.model.data.request
+import com.framework.mvvm.model.data.toDb
 import com.framework.mvvm.model.db.AdmDatabase
 import com.framework.mvvm.model.db.entity.ProjectEntity
 import com.framework.mvvm.model.vo.FrameRequest
@@ -20,7 +21,7 @@ class FrameRepository(
         return dataSource(
             fromDb = { db.projectDao().getProjects() },
             fromNt = { request { api.getFrame(request) } },
-            isFromNt = {true},
+            isFromNt = { true },
             onSuccess = { frame ->
                 val projects = frame.projects
                 val bafs = frame.buildingsAndFloors
@@ -45,5 +46,36 @@ class FrameRepository(
         return db.projectDao().insProject(projects)
     }
 
+    suspend fun requestDict(request: FrameRequest) {
+        toDb(
+            from = { api.getDefine(request) },
+            to = { define ->
+
+                val pipeConfig = define.pipeConfig
+                val infosConfig = define.infosConfig
+                val config = define.config
+                val m2dTypes = define.m2dTypes
+                val relConfig = define.relConfig
+                val system = define.dict.system
+                val equipment = define.dict.equipment
+                val relation = define.dict.relation
+                val major = define.dict.major
+
+                try {
+                    db.pipeConfigDao().insConfigs(pipeConfig)
+                    db.infosConfigDao().insConfigs(infosConfig) // wu
+                    db.configDao().insConfigs(config)
+                    db.m2dEquipDao().insM2ds(m2dTypes)
+                    db.relConfigDao().insConfigs(relConfig)
+                    db.systemDao().insSystems(system)
+                    db.equipmentDao().insEquipments(equipment)
+                    db.relationDictDao().insRelationDicts(relation)
+                    db.majorDao().insMajors(major)
+                } catch (e: Exception) {
+                    Log.d("requestDict", e.message!!)
+                }
+            }
+        )
+    }
 
 }

+ 4 - 3
demo/src/main/java/com/framework/mvvm/model/vo/Define.kt

@@ -9,9 +9,10 @@ data class Define(
     val config: List<ConfigEntity>,
     val dict: Dict,
     val m2dTypes: List<M2dEquipEntity>,
-    //val pipeConfig: List<Any>,
-    //val infosConfig: List<Any>,
-    //val relConfig: List<Any>
+    val pipeConfig: List<PipeConfigEntity>,
+    val infosConfig: List<InfosConfigEntity>,
+    val relConfig: List<RelConfigEntity>,
+    val component: List<ComponentEntity>
 )
 
 /**

+ 6 - 0
demo/src/main/java/com/framework/mvvm/mv/FrameViewModel.kt

@@ -38,4 +38,10 @@ class FrameViewModel(private val repository: FrameRepository) : ViewModel() {
             repository.insProject(projects)
         }
     }
+
+    fun requestDict(request: FrameRequest) {
+        viewModelScope.launch {
+            repository.requestDict(request)
+        }
+    }
 }

+ 9 - 2
demo/src/main/java/com/sybotan/android/demo/activities/PocActivity.kt

@@ -40,7 +40,7 @@ class PocActivity : AppCompatActivity(), DIAware {
             startActivity<SceneActivity>()
         }
 
-        mVm.projects.observe(this) { data ->
+        /*mVm.projects.observe(this) { data ->
             // TODO use data todo sth
         }
 
@@ -50,8 +50,15 @@ class PocActivity : AppCompatActivity(), DIAware {
                 projectId = "Pj0002220002",
                 groupCode = "BR"
             )
-        )
+        )*/
 
+        mVm.requestDict(
+            FrameRequest(
+                userId = "bdtp",
+                projectId = "Pj0002220002",
+                groupCode = "BR"
+            )
+        )
     }
 
     private fun checkStoragePermission() {