2024-06-24 13:11:19 +08:00
|
|
|
|
import Realm from 'realm'
|
|
|
|
|
|
import { BaseService } from '../baseService'
|
|
|
|
|
|
import { define } from '../../../define'
|
|
|
|
|
|
import path from 'path'
|
|
|
|
|
|
import { ComponentSize, SoftwareThemeType } from '../../../enum/softwareEnum'
|
|
|
|
|
|
import { SoftwareModel } from '../../model/SoftWare/software'
|
|
|
|
|
|
import { LoggerModel } from '../../model/SoftWare/logger'
|
|
|
|
|
|
import {
|
|
|
|
|
|
APIMjModel,
|
|
|
|
|
|
BrowserMJModel,
|
|
|
|
|
|
MjSettingModel,
|
|
|
|
|
|
RemoteMJModel
|
|
|
|
|
|
} from '../../model/SoftWare/mjSetting'
|
|
|
|
|
|
import { MJImageType, MJRobotType } from '../../../enum/mjEnum'
|
|
|
|
|
|
const { v4: uuidv4 } = require('uuid')
|
|
|
|
|
|
|
|
|
|
|
|
let dbPath = path.resolve(define.db_path, 'software.realm')
|
|
|
|
|
|
|
|
|
|
|
|
// 版本迁移
|
|
|
|
|
|
const migration = (oldRealm: Realm, newRealm: Realm) => {
|
|
|
|
|
|
const oldBooks = oldRealm.objects('Software')
|
|
|
|
|
|
const newBooks = newRealm.objects('Software')
|
|
|
|
|
|
if (oldRealm.schemaVersion < 1) {
|
|
|
|
|
|
}
|
|
|
|
|
|
// 第二个版本
|
|
|
|
|
|
if (oldRealm.schemaVersion < 2) {
|
|
|
|
|
|
newRealm.write(() => {
|
|
|
|
|
|
const newSoftwares = newRealm.objects('Software')
|
|
|
|
|
|
for (let software of newSoftwares) {
|
|
|
|
|
|
software.theme = SoftwareThemeType.LIGHT // 为新属性设置默认值
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
// 第三个版本
|
|
|
|
|
|
if (oldRealm.schemaVersion < 3) {
|
|
|
|
|
|
newRealm.write(() => {
|
|
|
|
|
|
const newSoftwares = newRealm.objects('Software')
|
|
|
|
|
|
for (let software of newSoftwares) {
|
|
|
|
|
|
software.reverse_display_show = false
|
|
|
|
|
|
software.reverse_show_book_striped = false
|
|
|
|
|
|
software.reverse_data_table_size = ComponentSize.SMALL
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
if (oldRealm.schemaVersion < 6) {
|
|
|
|
|
|
newRealm.write(() => {
|
|
|
|
|
|
const newSoftwares = newRealm.objects('MjSetting')
|
|
|
|
|
|
for (let software of newSoftwares) {
|
|
|
|
|
|
software.requestModel = MJImageType.API_MJ
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
if (oldRealm.schemaVersion < 8) {
|
|
|
|
|
|
newRealm.write(() => {
|
|
|
|
|
|
const newSoftwares = newRealm.objects('MjSetting')
|
|
|
|
|
|
for (let software of newSoftwares) {
|
|
|
|
|
|
software.imageModel = MJRobotType.MJ
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
if (oldRealm.schemaVersion < 9) {
|
|
|
|
|
|
newRealm.write(() => {
|
2024-06-27 16:24:41 +08:00
|
|
|
|
const newSoftwares = newRealm.objects('RemoteMJ')
|
2024-06-24 13:11:19 +08:00
|
|
|
|
for (let software of newSoftwares) {
|
|
|
|
|
|
software.accountId = null
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
if (oldRealm.schemaVersion < 10) {
|
|
|
|
|
|
newRealm.write(() => {
|
|
|
|
|
|
const newSoftwares = newRealm.objects('MjSetting')
|
|
|
|
|
|
for (let software of newSoftwares) {
|
|
|
|
|
|
software.accountId = null
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2024-06-27 16:24:41 +08:00
|
|
|
|
if (oldRealm.schemaVersion < 11) {
|
|
|
|
|
|
newRealm.write(() => {
|
|
|
|
|
|
const newSoftwares = newRealm.objects('MjSetting')
|
|
|
|
|
|
for (let software of newSoftwares) {
|
|
|
|
|
|
software.enable = true
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
if (oldRealm.schemaVersion < 12) {
|
|
|
|
|
|
newRealm.write(() => {
|
|
|
|
|
|
const newSoftwares = newRealm.objects('RemoteMJ')
|
|
|
|
|
|
for (let software of newSoftwares) {
|
|
|
|
|
|
software.nijiBotChannelId = null
|
|
|
|
|
|
software.mjBotChannelId = null
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
if (oldRealm.schemaVersion < 13) {
|
|
|
|
|
|
newRealm.write(() => {
|
|
|
|
|
|
const newSoftwares = newRealm.objects('RemoteMJ')
|
|
|
|
|
|
for (let software of newSoftwares) {
|
|
|
|
|
|
software.enable = true // 默认都是启用的
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2024-07-13 15:44:13 +08:00
|
|
|
|
if (oldRealm.schemaVersion < 14) {
|
|
|
|
|
|
newRealm.write(() => {
|
|
|
|
|
|
const newSoftwares = newRealm.objects('Software')
|
|
|
|
|
|
for (let software of newSoftwares) {
|
|
|
|
|
|
software.globalSetting = null // 默认都是启用的
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
if (oldRealm.schemaVersion < 15) {
|
|
|
|
|
|
newRealm.write(() => {
|
|
|
|
|
|
const newSoftwares = newRealm.objects('Software')
|
|
|
|
|
|
for (let software of newSoftwares) {
|
|
|
|
|
|
software.ttsSetting = null // 默认为空
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
if (oldRealm.schemaVersion < 16) {
|
|
|
|
|
|
newRealm.write(() => {
|
|
|
|
|
|
const newSoftwares = newRealm.objects('Software')
|
|
|
|
|
|
for (let software of newSoftwares) {
|
|
|
|
|
|
software.writeSetting = null // 文案的默认设置
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
if (oldRealm.schemaVersion < 17) {
|
|
|
|
|
|
newRealm.write(() => {
|
|
|
|
|
|
const newSoftwares = newRealm.objects('Software')
|
|
|
|
|
|
for (let software of newSoftwares) {
|
|
|
|
|
|
software.aiSetting = null // AI的默认设置
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2024-06-24 13:11:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export class BaseSoftWareService extends BaseService {
|
|
|
|
|
|
static instance: BaseSoftWareService | null = null
|
|
|
|
|
|
protected realm: Realm
|
|
|
|
|
|
dbpath: string
|
|
|
|
|
|
|
|
|
|
|
|
protected constructor() {
|
|
|
|
|
|
super()
|
|
|
|
|
|
this.dbpath = dbPath
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static async getInstance() {
|
|
|
|
|
|
if (BaseSoftWareService.instance === null) {
|
|
|
|
|
|
BaseSoftWareService.instance = new BaseSoftWareService()
|
|
|
|
|
|
await BaseSoftWareService.instance.open()
|
|
|
|
|
|
}
|
|
|
|
|
|
return BaseSoftWareService.instance
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 创建数据库连接,如果已经存在则直接返回
|
|
|
|
|
|
* @param dbPath 数据库文件地址
|
|
|
|
|
|
* @returns
|
|
|
|
|
|
*/
|
|
|
|
|
|
async open() {
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (this.realm != null) return
|
|
|
|
|
|
let config = {
|
|
|
|
|
|
schema: [
|
|
|
|
|
|
SoftwareModel,
|
|
|
|
|
|
LoggerModel,
|
|
|
|
|
|
BrowserMJModel,
|
|
|
|
|
|
RemoteMJModel,
|
|
|
|
|
|
APIMjModel,
|
|
|
|
|
|
MjSettingModel
|
|
|
|
|
|
],
|
|
|
|
|
|
path: dbPath,
|
2024-07-13 15:44:13 +08:00
|
|
|
|
schemaVersion: 17, // 当前版本号
|
2024-06-24 13:11:19 +08:00
|
|
|
|
migration: migration
|
|
|
|
|
|
}
|
|
|
|
|
|
// 判断当前全局是不是又当前这个
|
|
|
|
|
|
this.realm = await Realm.open(config)
|
|
|
|
|
|
// 判断当前是不是有数据,一条都没有的话添加一条空白数据
|
|
|
|
|
|
if (this.realm.objects('Software').length == 0) {
|
|
|
|
|
|
this.realm.write(() => {
|
|
|
|
|
|
this.realm.create('Software', {
|
|
|
|
|
|
id: uuidv4(),
|
|
|
|
|
|
theme: SoftwareThemeType.LIGHT // 默认是亮色主题
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.log(error)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|