左晓为主开发手持机充值管理机
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.dayu.general.dao
 
import android.content.Context
import android.os.Environment
import androidx.room.Room
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import java.io.File
 
class BaseDaoSingleton {
 
    companion object {
        var baseDao: AppDataBase? = null
        var AsynchBaseDao: AppDataBase? = null
        @JvmField
        val SqlitePath: String =
            Environment.getExternalStorageDirectory().absolutePath + File.separator + ".dayu" + File.separator + "data" + File.separator
 
        // 数据库迁移策略:从版本3迁移到版本4
        private val MIGRATION_3_4 = object : Migration(3, 4) {
            override fun migrate(database: SupportSQLiteDatabase) {
                // 这里不需要实际的数据库结构修改,因为我们只是更新了Room版本
                // 如果需要修改数据库结构,可以在这里添加 ALTER TABLE 语句
            }
        }
 
        // 数据库迁移策略:从版本4迁移到版本5
        private val MIGRATION_4_5 = object : Migration(4, 5) {
            override fun migrate(database: SupportSQLiteDatabase) {
                // 创建管理卡表
                database.execSQL("""
                    CREATE TABLE IF NOT EXISTS `manager_card` (
                        `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
                        `cardAddress` TEXT NOT NULL,
                        `orderId` TEXT NOT NULL,
                        `isReported` INTEGER NOT NULL DEFAULT 0,
                        `isCardWritten` INTEGER NOT NULL DEFAULT 0,
                        `operatorId` TEXT NOT NULL DEFAULT '',
                        `createTime` INTEGER NOT NULL
                    )
                """.trimIndent())
            }
        }
 
        fun getInstance(context: Context): AppDataBase {
            if (baseDao == null) {
                baseDao = Room.databaseBuilder<AppDataBase>(
                    context,
                    AppDataBase::class.java,
                    SqlitePath + "ConfigurationData_generalV1"
                ).allowMainThreadQueries()
                .addMigrations(MIGRATION_3_4, MIGRATION_4_5) // 添加迁移策略
                .build()
            }
            return baseDao as AppDataBase
        }
        fun getAsynchInstance(context: Context): AppDataBase {
            if (AsynchBaseDao == null) {
                synchronized(this) {
                    if (AsynchBaseDao == null) {
                        AsynchBaseDao = Room.databaseBuilder(
                            context.applicationContext,
                            AppDataBase::class.java,
                            "GeneralLibrary.db"
                        )
                        .addMigrations(MIGRATION_3_4, MIGRATION_4_5) // 添加迁移策略
                        .build()
                    }
                }
            }
            return AsynchBaseDao as AppDataBase
        }
 
        // 销毁数据库实例
        fun destroyInstance() {
            AsynchBaseDao = null
        }
    }
 
 
 
 
 
//    companion object {
//        fun getAsynchInstance(baseActivity: BaseActivity): AppDataBase? {
//           return getAsynchInstance(baseActivity)
//        }
//    }
}