fix: fix db bug
This commit is contained in:
7 files changed
+49
-38
No files matched your search
@@ -11,6 +11,7 @@ import cn.rtast.fancybot.entity.db.NiuziBankAccount
|
||||
import cn.rtast.fancybot.entity.db.NiuziBankTable
|
||||
import cn.rtast.fancybot.entity.db.NiuziBankTable.balance
|
||||
import cn.rtast.fancybot.entity.db.NiuziBankTable.interestRate
|
||||
import cn.rtast.fancybot.entity.db.NiuziBankTable.nickname
|
||||
import cn.rtast.fancybot.entity.db.NiuziBankTable.timestamp
|
||||
import cn.rtast.fancybot.entity.db.NiuziBankTable.userId
|
||||
import cn.rtast.fancybot.entity.db.NiuziTable
|
||||
@@ -29,55 +30,58 @@ class NiuziBankManager {
|
||||
it[userId],
|
||||
it[balance],
|
||||
it[interestRate],
|
||||
it[timestamp]
|
||||
it[timestamp],
|
||||
it[nickname]
|
||||
)
|
||||
}.singleOrNull()
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun increaseBalance(id: Long, amount: Double) {
|
||||
private suspend fun increaseBalance(id: Long, amount: Double, username: String) {
|
||||
val current = this.getUser(id)
|
||||
suspendedTransaction {
|
||||
NiuziBankTable.update({ userId eq id }) {
|
||||
it[balance] = current?.balance!! + amount
|
||||
it[NiuziTable.timestamp] = Instant.now().epochSecond
|
||||
it[nickname] = username
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun decreaseBalance(id: Long, amount: Double) {
|
||||
this.increaseBalance(id, -amount)
|
||||
private suspend fun decreaseBalance(id: Long, amount: Double, username: String) {
|
||||
this.increaseBalance(id, -amount, username)
|
||||
}
|
||||
|
||||
suspend fun createBlankAccount(id: Long) {
|
||||
suspend fun createBlankAccount(id: Long, username: String) {
|
||||
suspendedTransaction {
|
||||
NiuziBankTable.insert {
|
||||
it[userId] = id
|
||||
it[balance] = 0.0
|
||||
it[timestamp] = Instant.now().epochSecond
|
||||
it[interestRate] = 0.54
|
||||
it[nickname] = username
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun withdraw(id: Long, amount: Double): NiuziBankAccount {
|
||||
suspend fun withdraw(id: Long, amount: Double, username: String): NiuziBankAccount {
|
||||
suspendedTransaction {
|
||||
this.decreaseBalance(id, amount)
|
||||
this.decreaseBalance(id, amount, username)
|
||||
}
|
||||
return this.getUser(id)!!
|
||||
}
|
||||
|
||||
suspend fun deposit(id: Long, amount: Double): NiuziBankAccount {
|
||||
suspend fun deposit(id: Long, amount: Double, username: String): NiuziBankAccount {
|
||||
suspendedTransaction {
|
||||
this.increaseBalance(id, amount)
|
||||
this.increaseBalance(id, amount, username)
|
||||
}
|
||||
return this.getUser(id)!!
|
||||
}
|
||||
|
||||
suspend fun transfer(from: Long, target: Long, amount: Double): NiuziBankAccount? {
|
||||
suspend fun transfer(from: Long, target: Long, amount: Double, username: String): NiuziBankAccount? {
|
||||
suspendedTransaction {
|
||||
this.decreaseBalance(from, amount)
|
||||
this.increaseBalance(target, amount)
|
||||
this.decreaseBalance(from, amount, username)
|
||||
this.increaseBalance(target, amount, username)
|
||||
}
|
||||
return this.getUser(from)
|
||||
}
|
||||
@@ -85,6 +89,6 @@ class NiuziBankManager {
|
||||
suspend fun getAllAccount(): List<NiuziBankAccount> =
|
||||
suspendedTransaction {
|
||||
NiuziBankTable.selectAll()
|
||||
.map { NiuziBankAccount(it[userId], it[balance], it[interestRate], it[timestamp]) }
|
||||
.map { NiuziBankAccount(it[userId], it[balance], it[interestRate], it[timestamp], it[nickname]) }
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ package cn.rtast.fancybot.util.file
|
||||
import cn.rtast.fancybot.entity.db.Niuzi
|
||||
import cn.rtast.fancybot.entity.db.NiuziTable
|
||||
import cn.rtast.fancybot.entity.db.NiuziTable.length
|
||||
import cn.rtast.fancybot.entity.db.NiuziTable.nickname
|
||||
import cn.rtast.fancybot.entity.db.NiuziTable.timestamp
|
||||
import cn.rtast.fancybot.entity.db.NiuziTable.userId
|
||||
import cn.rtast.fancybot.util.isSameDay
|
||||
@@ -44,8 +45,9 @@ class NiuziManager {
|
||||
|
||||
suspend fun getUser(id: Long): Niuzi? =
|
||||
suspendedTransaction {
|
||||
NiuziTable.selectAll().where { userId eq id }.map { Niuzi(it[userId], it[length], it[timestamp]) }
|
||||
.singleOrNull()
|
||||
NiuziTable.selectAll().where { userId eq id }.map {
|
||||
Niuzi(it[userId], it[length], it[timestamp], it[nickname])
|
||||
}.singleOrNull()
|
||||
}
|
||||
|
||||
suspend fun isSigned(id: Long): Boolean {
|
||||
@@ -53,7 +55,7 @@ class NiuziManager {
|
||||
return record?.timestamp?.isSameDay(Instant.now().epochSecond) ?: false
|
||||
}
|
||||
|
||||
suspend fun sign(id: Long): Pair<Double, Niuzi?> {
|
||||
suspend fun sign(id: Long, username: String): Pair<Double, Niuzi?> {
|
||||
val randomLength = Random.nextDouble(1.0, 10.0)
|
||||
if (getUser(id) == null) {
|
||||
suspendedTransaction {
|
||||
@@ -61,6 +63,7 @@ class NiuziManager {
|
||||
it[userId] = id
|
||||
it[timestamp] = Instant.now().epochSecond
|
||||
it[length] = 0.0
|
||||
it[nickname] = username
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,5 +105,7 @@ class NiuziManager {
|
||||
}
|
||||
|
||||
suspend fun getAllNiuzi(): List<Niuzi> =
|
||||
suspendedTransaction { NiuziTable.selectAll().map { Niuzi(it[userId], it[length], it[timestamp]) } }
|
||||
suspendedTransaction {
|
||||
NiuziTable.selectAll().map { Niuzi(it[userId], it[length], it[timestamp], it[nickname]) }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user