fix: 解决导入数据时将int类型转换为Double的问题
This commit is contained in:
parent
8e9bd3bc5d
commit
0a1e6c6f2b
@ -31,7 +31,7 @@ android {
|
|||||||
targetCompatibility JavaVersion.VERSION_11
|
targetCompatibility JavaVersion.VERSION_11
|
||||||
}
|
}
|
||||||
kotlinOptions {
|
kotlinOptions {
|
||||||
jvmTarget = '1.8'
|
jvmTarget = '11'
|
||||||
}
|
}
|
||||||
buildFeatures {
|
buildFeatures {
|
||||||
viewBinding true
|
viewBinding true
|
||||||
@ -77,7 +77,6 @@ dependencies {
|
|||||||
|
|
||||||
// 读取spatialite文件
|
// 读取spatialite文件
|
||||||
implementation 'com.github.sevar83:android-spatialite:2.0.1'
|
implementation 'com.github.sevar83:android-spatialite:2.0.1'
|
||||||
|
|
||||||
}
|
}
|
||||||
//允许引用生成的代码
|
//允许引用生成的代码
|
||||||
kapt {
|
kapt {
|
||||||
|
@ -111,47 +111,53 @@ class ImportOMDBHelper @AssistedInject constructor(@Assisted("context") val cont
|
|||||||
unZipFolder.mkdirs()
|
unZipFolder.mkdirs()
|
||||||
// 开始解压zip文件
|
// 开始解压zip文件
|
||||||
val unZipFiles = ZipUtils.unzipFile(omdbZipFile, unZipFolder)
|
val unZipFiles = ZipUtils.unzipFile(omdbZipFile, unZipFolder)
|
||||||
|
// 将listResult数据插入到Realm数据库中
|
||||||
|
Realm.getDefaultInstance().beginTransaction()
|
||||||
// 遍历解压后的文件,读取该数据返回
|
// 遍历解压后的文件,读取该数据返回
|
||||||
for ((index, currentConfig) in importConfig.tables.withIndex()) {
|
for ((index, currentConfig) in importConfig.tables.withIndex()) {
|
||||||
val txtFile = unZipFiles.find {
|
val txtFile = unZipFiles.find {
|
||||||
it.name == currentConfig.table
|
it.name == currentConfig.table
|
||||||
}
|
}
|
||||||
|
|
||||||
val listResult: MutableList<Map<String, Any>> = mutableListOf()
|
val listResult = mutableListOf<Map<String, Any?>>()
|
||||||
currentConfig?.let {
|
currentConfig?.let {
|
||||||
val list = FileIOUtils.readFile2List(txtFile, "UTF-8")
|
val list = FileIOUtils.readFile2List(txtFile, "UTF-8")
|
||||||
if (list!=null) {
|
if (list!=null) {
|
||||||
// 将list数据转换为map
|
// 将list数据转换为map
|
||||||
for (line in list) {
|
for (line in list) {
|
||||||
val map = gson.fromJson<Map<String, Any>>(line, object : TypeToken<MutableMap<String, Any>>() {}.type)
|
val map = gson.fromJson<Map<String, Any?>>(line, object:TypeToken<Map<String, Any?>>(){}.getType())
|
||||||
.toMutableMap()
|
.toMutableMap()
|
||||||
map["QItable"] = currentConfig.table
|
map["qi_table"] = currentConfig.table
|
||||||
map["QIname"] = currentConfig.name
|
map["qi_name"] = currentConfig.name
|
||||||
map["QIcode"] = currentConfig.code
|
map["qi_code"] = currentConfig.code
|
||||||
listResult.add(map)
|
listResult.add(map)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 将listResult数据插入到Realm数据库中
|
|
||||||
Realm.getDefaultInstance().beginTransaction()
|
|
||||||
for (map in listResult) { // 每一个map就是Realm的一条数据
|
for (map in listResult) { // 每一个map就是Realm的一条数据
|
||||||
// 先查询这个mesh下有没有数据,如果有则跳过即可
|
// 先查询这个mesh下有没有数据,如果有则跳过即可
|
||||||
// val meshEntity = Realm.getDefaultInstance().where(RenderEntity::class.java).equalTo("properties['mesh']", map["mesh"].toString()).findFirst()
|
// val meshEntity = Realm.getDefaultInstance().where(RenderEntity::class.java).equalTo("properties['mesh']", map["mesh"].toString()).findFirst()
|
||||||
val renderEntity = RenderEntity()
|
val renderEntity = RenderEntity()
|
||||||
renderEntity.code = map["QIcode"].toString().toInt()
|
renderEntity.code = map["qi_code"].toString().toInt()
|
||||||
renderEntity.name = map["QIname"].toString()
|
renderEntity.name = map["qi_name"].toString()
|
||||||
renderEntity.table = map["QItable"].toString()
|
renderEntity.table = map["qi_table"].toString()
|
||||||
// 其他数据插入到Properties中
|
// 其他数据插入到Properties中
|
||||||
renderEntity.geometry = map["geometry"].toString()
|
renderEntity.geometry = map["geometry"].toString()
|
||||||
for (entry in map) {
|
for ((key, value) in map) {
|
||||||
renderEntity.properties[entry.key] = entry.value.toString()
|
when (value) {
|
||||||
|
is String -> renderEntity.properties[key.toString()] = value
|
||||||
|
is Int -> renderEntity.properties[key.toString()] = value.toInt().toString()
|
||||||
|
is Double -> renderEntity.properties[key.toString()] = value.toDouble().toString()
|
||||||
|
else -> renderEntity.properties[key.toString()] = value.toString()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Realm.getDefaultInstance().insert(renderEntity)
|
Realm.getDefaultInstance().insert(renderEntity)
|
||||||
}
|
}
|
||||||
Realm.getDefaultInstance().commitTransaction()
|
|
||||||
// 1个文件发送一次flow流
|
// 1个文件发送一次flow流
|
||||||
emit("${index+1}/${importConfig.tables.size}")
|
emit("${index+1}/${importConfig.tables.size}")
|
||||||
}
|
}
|
||||||
|
Realm.getDefaultInstance().commitTransaction()
|
||||||
|
emit("finish")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,14 +1,16 @@
|
|||||||
package com.navinfo.omqs.hilt
|
package com.navinfo.omqs.hilt
|
||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.content.Context
|
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.room.Room
|
import androidx.room.Room
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
|
import com.google.gson.GsonBuilder
|
||||||
|
import com.google.gson.reflect.TypeToken
|
||||||
import com.navinfo.omqs.Constant
|
import com.navinfo.omqs.Constant
|
||||||
import com.navinfo.omqs.OMQSApplication
|
import com.navinfo.omqs.OMQSApplication
|
||||||
import com.navinfo.omqs.db.RoomAppDatabase
|
import com.navinfo.omqs.db.RoomAppDatabase
|
||||||
import com.navinfo.omqs.http.RetrofitNetworkServiceAPI
|
import com.navinfo.omqs.http.RetrofitNetworkServiceAPI
|
||||||
|
import com.navinfo.omqs.tools.IntTypeAdapter
|
||||||
import com.tencent.wcdb.database.SQLiteCipherSpec
|
import com.tencent.wcdb.database.SQLiteCipherSpec
|
||||||
import com.tencent.wcdb.room.db.WCDBOpenHelperFactory
|
import com.tencent.wcdb.room.db.WCDBOpenHelperFactory
|
||||||
import dagger.Lazy
|
import dagger.Lazy
|
||||||
@ -86,7 +88,12 @@ class GlobalModule {
|
|||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun provideGson(): Gson = Gson()
|
fun provideGson(): Gson = GsonBuilder()
|
||||||
|
// 解决解析Json时将int类型自动转换为Double的问题
|
||||||
|
.registerTypeAdapter(object : TypeToken<Map<String, Any?>>() {}.getType(), IntTypeAdapter())
|
||||||
|
.registerTypeAdapter(object : TypeToken<Map<String, Any>>() {}.getType(), IntTypeAdapter())
|
||||||
|
.registerTypeAdapter(object : TypeToken<Map<Any, Any>>() {}.getType(), IntTypeAdapter())
|
||||||
|
.create()
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
|
69
app/src/main/java/com/navinfo/omqs/tools/IntTypeAdapter.kt
Normal file
69
app/src/main/java/com/navinfo/omqs/tools/IntTypeAdapter.kt
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
package com.navinfo.omqs.tools
|
||||||
|
|
||||||
|
import com.google.gson.Gson
|
||||||
|
import com.google.gson.TypeAdapter
|
||||||
|
import com.google.gson.internal.LinkedTreeMap
|
||||||
|
import com.google.gson.stream.JsonReader
|
||||||
|
import com.google.gson.stream.JsonToken
|
||||||
|
import com.google.gson.stream.JsonWriter
|
||||||
|
import java.io.IOException
|
||||||
|
|
||||||
|
class IntTypeAdapter : TypeAdapter<Any>() {
|
||||||
|
private val delegate: TypeAdapter<Any> = Gson().getAdapter(Any::class.java)
|
||||||
|
|
||||||
|
@Throws(IOException::class)
|
||||||
|
override fun read(`in`: JsonReader): Any? {
|
||||||
|
val token = `in`.peek()
|
||||||
|
when (token) {
|
||||||
|
JsonToken.BEGIN_ARRAY -> {
|
||||||
|
val list: MutableList<Any?> = ArrayList()
|
||||||
|
`in`.beginArray()
|
||||||
|
while (`in`.hasNext()) {
|
||||||
|
list.add(read(`in`))
|
||||||
|
}
|
||||||
|
`in`.endArray()
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
JsonToken.BEGIN_OBJECT -> {
|
||||||
|
val map: MutableMap<String, Any?> = LinkedTreeMap()
|
||||||
|
`in`.beginObject()
|
||||||
|
while (`in`.hasNext()) {
|
||||||
|
map[`in`.nextName()] = read(`in`)
|
||||||
|
}
|
||||||
|
`in`.endObject()
|
||||||
|
return map
|
||||||
|
}
|
||||||
|
JsonToken.STRING -> return `in`.nextString()
|
||||||
|
JsonToken.NUMBER -> {
|
||||||
|
// 改写数字的处理逻辑,将数字值分为整型与浮点型。
|
||||||
|
val dbNum = `in`.nextDouble()
|
||||||
|
// 数字超过long的最大值,返回浮点类型
|
||||||
|
if (dbNum > Long.MAX_VALUE) {
|
||||||
|
return dbNum
|
||||||
|
}
|
||||||
|
// 判断数字是否为整数值
|
||||||
|
val lngNum = dbNum.toLong()
|
||||||
|
return if (dbNum == lngNum.toDouble()) {
|
||||||
|
try {
|
||||||
|
lngNum.toInt()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
lngNum
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
dbNum
|
||||||
|
}
|
||||||
|
}
|
||||||
|
JsonToken.BOOLEAN -> return `in`.nextBoolean()
|
||||||
|
JsonToken.NULL -> {
|
||||||
|
`in`.nextNull()
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
else -> throw IllegalStateException()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(IOException::class)
|
||||||
|
override fun write(out: JsonWriter, value: Any?) {
|
||||||
|
delegate.write(out, value)
|
||||||
|
}
|
||||||
|
}
|
@ -98,19 +98,11 @@ class PersonalCenterFragment : BaseFragment(), FSAFActivityCallbacks {
|
|||||||
|
|
||||||
override fun onResult(uri: Uri) {
|
override fun onResult(uri: Uri) {
|
||||||
val file = UriUtils.uri2File(uri)
|
val file = UriUtils.uri2File(uri)
|
||||||
val importOMDBHelper: ImportOMDBHelper =
|
val importOMDBHelper: ImportOMDBHelper = importOMDBHiltFactory.obtainImportOMDBHelper(
|
||||||
importOMDBHiltFactory.obtainImportOMDBHelper(
|
|
||||||
requireContext(),
|
requireContext(),
|
||||||
file
|
file
|
||||||
)
|
)
|
||||||
viewModel.importOMDBData(importOMDBHelper)
|
viewModel.importOMDBData(importOMDBHelper)
|
||||||
// // 开始导入数据
|
|
||||||
// CoroutineUtils.launchWithLoading(
|
|
||||||
// requireContext(),
|
|
||||||
// loadingMessage = "导入数据..."
|
|
||||||
// ) {
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -122,14 +114,13 @@ class PersonalCenterFragment : BaseFragment(), FSAFActivityCallbacks {
|
|||||||
|
|
||||||
override fun onResult(uri: Uri) {
|
override fun onResult(uri: Uri) {
|
||||||
viewModel.importScProblemData(uri)
|
viewModel.importScProblemData(uri)
|
||||||
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
R.id.personal_center_menu_test -> {
|
R.id.personal_center_menu_test -> {
|
||||||
viewModel.readRealmData()
|
viewModel.readRealmData()
|
||||||
// 定位到指定位置
|
// 定位到指定位置
|
||||||
niMapController.mMapView.vtmMap.animator().animateTo(GeoPoint(28.608398, 115.67901))
|
niMapController.mMapView.vtmMap.animator().animateTo(GeoPoint(30.21137798479949, 113.84832672274896))
|
||||||
}
|
}
|
||||||
R.id.personal_center_menu_task_list -> {
|
R.id.personal_center_menu_task_list -> {
|
||||||
findNavController().navigate(R.id.TaskListFragment)
|
findNavController().navigate(R.id.TaskListFragment)
|
||||||
|
@ -44,8 +44,8 @@ android {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
compileOptions {
|
compileOptions {
|
||||||
sourceCompatibility JavaVersion.VERSION_1_8
|
sourceCompatibility JavaVersion.VERSION_11
|
||||||
targetCompatibility JavaVersion.VERSION_1_8
|
targetCompatibility JavaVersion.VERSION_11
|
||||||
}
|
}
|
||||||
|
|
||||||
sourceSets {
|
sourceSets {
|
||||||
|
@ -1549,4 +1549,9 @@
|
|||||||
<symbol src="assets:symbols/dot_blue.svg" />
|
<symbol src="assets:symbols/dot_blue.svg" />
|
||||||
</m>
|
</m>
|
||||||
</m>
|
</m>
|
||||||
|
|
||||||
|
<m k="QIcode">
|
||||||
|
<!-- 道路线 -->
|
||||||
|
<m v="2001"></m>
|
||||||
|
</m>
|
||||||
</rendertheme>
|
</rendertheme>
|
@ -42,7 +42,7 @@ class GeometryToolsKt {
|
|||||||
val tileY1 = MercatorProjection.latitudeToTileY(minMaxY[1], Constant.OVER_ZOOM.toByte())
|
val tileY1 = MercatorProjection.latitudeToTileY(minMaxY[1], Constant.OVER_ZOOM.toByte())
|
||||||
val minTileY = if (tileY0 <= tileY1) tileY0 else tileY1
|
val minTileY = if (tileY0 <= tileY1) tileY0 else tileY1
|
||||||
val maxTileY = if (tileY0 <= tileY1) tileY1 else tileY0
|
val maxTileY = if (tileY0 <= tileY1) tileY1 else tileY0
|
||||||
println("getTileYByGeometry$envelope===$minTileY===$maxTileY")
|
// println("getTileYByGeometry$envelope===$minTileY===$maxTileY")
|
||||||
|
|
||||||
for (i in minTileY..maxTileY) {
|
for (i in minTileY..maxTileY) {
|
||||||
tileYSet.add(i)
|
tileYSet.add(i)
|
||||||
@ -86,7 +86,7 @@ class GeometryToolsKt {
|
|||||||
val tileX1 = MercatorProjection.longitudeToTileX(minMaxX[1], Constant.OVER_ZOOM.toByte())
|
val tileX1 = MercatorProjection.longitudeToTileX(minMaxX[1], Constant.OVER_ZOOM.toByte())
|
||||||
val minTileX = if (tileX0 <= tileX1) tileX0 else tileX1
|
val minTileX = if (tileX0 <= tileX1) tileX0 else tileX1
|
||||||
val maxTileX = if (tileX0 <= tileX1) tileX1 else tileX0
|
val maxTileX = if (tileX0 <= tileX1) tileX1 else tileX0
|
||||||
println("getTileXByGeometry$envelope$minTileX===$maxTileX")
|
// println("getTileXByGeometry$envelope$minTileX===$maxTileX")
|
||||||
for (i in minTileX..maxTileX) {
|
for (i in minTileX..maxTileX) {
|
||||||
tileXSet.add(i)
|
tileXSet.add(i)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user