Miao-Yunzai/plugins/genshin/model/db/UserDB.js

77 lines
1.4 KiB
JavaScript
Raw Normal View History

import BaseModel from './BaseModel.js'
2023-05-09 11:03:38 +08:00
import lodash from 'lodash'
import { UserGameDB } from './index.js'
import MysUtil from '../mys/MysUtil.js'
const { Types } = BaseModel
const COLUMNS = {
// 用户IDqq为数字
id: {
type: Types.STRING,
autoIncrement: false,
primaryKey: true
},
type: {
type: Types.STRING,
defaultValue: 'qq',
notNull: true
},
// 昵称
name: Types.STRING,
// 头像
face: Types.STRING,
ltuids: Types.STRING
}
class UserDB extends BaseModel {
static async find (id, type = 'qq') {
// user_id
id = type === 'qq' ? '' + id : type + id
// DB查询
let user = await UserDB.findByPk(id, {
include: {
model: UserGameDB,
as: 'games'
}
})
if (!user) {
user = await UserDB.build({
id,
type
})
}
return user
}
2023-05-09 11:03:38 +08:00
async saveDB (user) {
let db = this
let ltuids = []
lodash.forEach(user.mysUsers, (mys) => {
if (mys.ck && mys.ltuid) {
2023-05-09 11:03:38 +08:00
ltuids.push(mys.ltuid)
}
})
db.ltuids = ltuids.join(',')
let games = []
2023-06-01 03:28:35 +08:00
MysUtil.eachGame((key) => {
let game = user.games[key]
if (game) {
games.push(game)
}
2023-05-09 11:03:38 +08:00
})
if (games.length > 0) {
await this.setGames(games)
}
2023-05-09 11:03:38 +08:00
await this.save()
}
}
BaseModel.initDB(UserDB, COLUMNS)
await UserDB.sync()
export default UserDB