diff --git a/src/main/kotlin/cn/fadinglight/models/Bill.kt b/src/main/kotlin/cn/fadinglight/models/Bill.kt index c72bdac..899af1b 100644 --- a/src/main/kotlin/cn/fadinglight/models/Bill.kt +++ b/src/main/kotlin/cn/fadinglight/models/Bill.kt @@ -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") } } } diff --git a/src/main/kotlin/cn/fadinglight/models/Label.kt b/src/main/kotlin/cn/fadinglight/models/Label.kt index 91ee643..8521e00 100644 --- a/src/main/kotlin/cn/fadinglight/models/Label.kt +++ b/src/main/kotlin/cn/fadinglight/models/Label.kt @@ -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( diff --git a/src/main/kotlin/cn/fadinglight/services/BillServiceImpl.kt b/src/main/kotlin/cn/fadinglight/services/BillServiceImpl.kt index fa312c0..3157357 100644 --- a/src/main/kotlin/cn/fadinglight/services/BillServiceImpl.kt +++ b/src/main/kotlin/cn/fadinglight/services/BillServiceImpl.kt @@ -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): 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): Int { diff --git a/src/main/kotlin/cn/fadinglight/services/LabelServiceImpl.kt b/src/main/kotlin/cn/fadinglight/services/LabelServiceImpl.kt index 0e61b7c..1c9da11 100644 --- a/src/main/kotlin/cn/fadinglight/services/LabelServiceImpl.kt +++ b/src/main/kotlin/cn/fadinglight/services/LabelServiceImpl.kt @@ -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) } diff --git a/src/main/kotlin/cn/fadinglight/vo/BillVO.kt b/src/main/kotlin/cn/fadinglight/vo/BillVO.kt index e298243..f52dc2d 100644 --- a/src/main/kotlin/cn/fadinglight/vo/BillVO.kt +++ b/src/main/kotlin/cn/fadinglight/vo/BillVO.kt @@ -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, diff --git a/src/main/kotlin/cn/fadinglight/vo/LabelVO.kt b/src/main/kotlin/cn/fadinglight/vo/LabelVO.kt index 71f38ea..31bf96c 100644 --- a/src/main/kotlin/cn/fadinglight/vo/LabelVO.kt +++ b/src/main/kotlin/cn/fadinglight/vo/LabelVO.kt @@ -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