to: fix api

This commit is contained in:
车厘子 2022-11-19 21:04:26 +08:00
parent b4c10559d1
commit 545edb747b
6 changed files with 45 additions and 20 deletions

View File

@ -8,10 +8,10 @@ enum class BillType {
INCOME;
companion object {
fun toType(n: Int): BillType = when (n) {
fun of(n: Int): BillType = when (n) {
0 -> CONSUME
1 -> INCOME
else -> throw IllegalArgumentException("error type $n")
else -> throw IllegalArgumentException("error code $n")
}
}
}

View File

@ -4,7 +4,17 @@ package cn.fadinglight.models
enum class LabelType {
CLASS,
LABEL,
INCOME_CLASS,
INCOME_CLASS;
companion object {
fun of(n: Int) = when (n) {
0 -> LabelType.CLASS
1 -> LabelType.LABEL
2 -> LabelType.INCOME_CLASS
else -> throw IllegalArgumentException("error code $n")
}
}
}
data class Label(

View File

@ -4,13 +4,18 @@ import cn.fadinglight.mapers.Bills
import cn.fadinglight.mapers.Labels
import cn.fadinglight.models.Bill
import cn.fadinglight.models.BillType
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.SqlExpressionBuilder.like
import org.jetbrains.exposed.sql.transactions.experimental.suspendedTransactionAsync
import org.jetbrains.exposed.sql.transactions.transaction
class BillServiceImpl : BillService {
private val labelService = LabelServiceImpl()
private fun formatSingletonNumber(n: String) = if (n.length == 1) {
"0$n"
} else {
@ -73,7 +78,8 @@ class BillServiceImpl : BillService {
}
override suspend fun addManyBills(bills: List<Bill>): Int = transaction {
Bills
val newBills = Bills
.batchInsert(bills) {
this[Bills.type] = it.type.name
this[Bills.date] = it.date
@ -82,7 +88,23 @@ class BillServiceImpl : BillService {
this[Bills.money] = it.money
this[Bills.options] = it.options
}
.count()
.map(::resultRowToBill)
newBills.forEach {
val classId = Labels
.select(Labels.name eq it.cls)
.map { it2 -> it2[Labels.id].value }
.single()
val labelId = Labels
.select(Labels.name eq it.label)
.map { it2 -> it2[Labels.id].value }
.single()
Labels.update({ (Labels.id eq labelId) or (Labels.id eq classId) }) {
with(SqlExpressionBuilder) {
it[count] = count + 1
}
}
}
newBills.count()
}
override suspend fun updateManyBills(bills: List<Bill>): Int {

View File

@ -5,7 +5,6 @@ import cn.fadinglight.mapers.Labels
import cn.fadinglight.models.Label
import cn.fadinglight.models.LabelType
import cn.fadinglight.vo.LabelGroup
import cn.fadinglight.vo.LabelPost
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
@ -34,15 +33,9 @@ class LabelServiceImpl : LabelService {
?: emptyList()
}
} ?: emptyList()
val incomeLabels = labelGroups[LabelType.CLASS]
?.map {
it.vo().apply {
this.labels = labelGroups[LabelType.INCOME_CLASS]
?.filter { it2 -> it2.relativeId == it.id }
?.map(Label::vo)
?: emptyList()
}
} ?: emptyList()
val incomeLabels = labelGroups[LabelType.INCOME_CLASS]
?.map(Label::vo) ?: emptyList()
return LabelGroup(income = incomeLabels, consume = consumeLabels)
}

View File

@ -8,7 +8,7 @@ import java.util.*
@Serializable
data class BillVO(
val id: Int? = null,
val type: BillType,
val type: Int,
val date: String,
val money: Float,
val cls: String,
@ -18,7 +18,7 @@ data class BillVO(
fun BillVO.bill() = Bill(
id = id,
type = type,
type = BillType.of(type),
date = date,
money = money,
cls = cls,
@ -28,7 +28,7 @@ fun BillVO.bill() = Bill(
fun Bill.vo() = BillVO(
id = id,
type = type,
type = type.ordinal,
date = date,
money = money,
cls = cls,

View File

@ -7,14 +7,14 @@ import kotlinx.serialization.Serializable
@Serializable
data class LabelPost(
val type: LabelType,
val type: Int,
val name: String,
val relativeId: Int?,
)
fun LabelPost.label() = Label(
id = null,
type = type,
type = LabelType.of(type),
name = name,
count = 0,
relativeId = relativeId