feat: add lesson 7 order flow and orders screen
Build Android app / Build (push) Has been cancelled
Build iOS app / Build (push) Has been cancelled
Template Cleanup / Template Cleanup (push) Has been cancelled

This commit is contained in:
OpenClaw committed 2026-03-09 02:25:41 +08:00
1 parent fc8ea48ea1
commit e42597b8b7
6 files changed
+647 -25

No files matched your search

@@ -1,7 +1,7 @@
package com.example.minishop.checkout
import com.example.minishop.cart.CartRepository
import kotlin.random.Random
import com.example.minishop.order.OrderRepository
data class CheckoutResult(
val success: Boolean,
@@ -9,7 +9,8 @@ data class CheckoutResult(
)
class CheckoutService(
private val cartRepository: CartRepository
private val cartRepository: CartRepository,
private val orderRepository: OrderRepository
) {
fun totalPrice(): Double {
return cartRepository.items().sumOf { item ->
@@ -18,15 +19,13 @@ class CheckoutService(
}
fun checkout(): CheckoutResult {
val total = totalPrice()
if (total <= 0.0) return CheckoutResult(false, "购物车为空")
val cartItems = cartRepository.items()
if (cartItems.isEmpty()) return CheckoutResult(false, "购物车为空")
val success = Random.nextBoolean()
return if (success) {
cartRepository.clear()
CheckoutResult(true, "结算成功(模拟),金额:¥${"%.2f".format(total)}")
} else {
CheckoutResult(false, "结算失败(模拟),请重试")
}
val order = orderRepository.createOrder(cartItems)
?: return CheckoutResult(false, "创建订单失败")
cartRepository.clear()
return CheckoutResult(true, "下单成功,订单号:${order.orderId}")
}
}
@@ -0,0 +1,17 @@
package com.example.minishop.order
data class OrderItem(
val productId: String,
val title: String,
val price: Double,
val imageUrl: String,
val quantity: Int
)
data class ShopOrder(
val orderId: String,
val totalPrice: Double,
val status: String,
val createdAt: Long,
val items: List<OrderItem>
)
@@ -0,0 +1,78 @@
package com.example.minishop.order
import com.example.minishop.cart.CartItem
import com.example.minishop.db.ShopDatabase
import com.example.minishop.platform.currentTimeMillis
import kotlin.random.Random
class OrderRepository(
private val database: ShopDatabase
) {
private val queries = database.ordersQueries
fun createOrder(cartItems: List<CartItem>): ShopOrder? {
if (cartItems.isEmpty()) return null
val orderId = buildOrderId()
val createdAt = currentTimeMillis()
val totalPrice = cartItems.sumOf { it.price * it.quantity }
val items = cartItems.map {
OrderItem(
productId = it.productId,
title = it.title,
price = it.price,
imageUrl = it.imageUrl,
quantity = it.quantity
)
}
queries.insertOrder(
order_id = orderId,
total_price = totalPrice,
status = "CREATED",
created_at = createdAt
)
items.forEach { item ->
queries.insertOrderItem(
order_id = orderId,
product_id = item.productId,
title = item.title,
price = item.price,
image_url = item.imageUrl,
quantity = item.quantity.toLong()
)
}
return ShopOrder(
orderId = orderId,
totalPrice = totalPrice,
status = "CREATED",
createdAt = createdAt,
items = items
)
}
fun getOrders(): List<ShopOrder> {
return queries.selectOrders().executeAsList().map { order ->
val items = queries.selectItemsByOrderId(order.order_id).executeAsList().map { item ->
OrderItem(
productId = item.product_id,
title = item.title,
price = item.price,
imageUrl = item.image_url,
quantity = item.quantity.toInt()
)
}
ShopOrder(
orderId = order.order_id,
totalPrice = order.total_price,
status = order.status,
createdAt = order.created_at,
items = items
)
}
}
private fun buildOrderId(): String = "ORD-${currentTimeMillis()}-${Random.nextInt(100, 999)}"
}