feat(domain): add income calculation with commission support

This commit is contained in:
zetaloop
2026-02-23 11:04:48 +08:00
parent 77d23d0c9d
commit 2222dccbb7
3 changed files with 90 additions and 5 deletions
+11 -5
View File
@@ -1,6 +1,8 @@
import { create } from "zustand"
import { calculateOrderIncome } from "@/lib/domain/income"
import { generateId } from "@/lib/id"
import { mockTransactions, walletBalance } from "@/lib/mock"
import { useShopStore } from "@/store/shops"
import type { WalletTransaction } from "@/lib/types"
interface WalletState {
@@ -10,7 +12,7 @@ interface WalletState {
withdraw: (amount: number) => void
deductBalance: (orderId: string, amount: number) => boolean
refundPayment: (orderId: string, amount: number) => boolean
addIncome: (orderId: string, amount: number) => void
addIncome: (orderId: string, totalPrice: number, shopId?: string) => void
addTransaction: (transaction: WalletTransaction) => void
}
@@ -109,8 +111,8 @@ export const useWalletStore = create<WalletState>((set, get) => ({
return true
},
addIncome: (orderId, amount) => {
if (!Number.isFinite(amount) || amount <= 0) return
addIncome: (orderId, totalPrice, shopId) => {
if (!Number.isFinite(totalPrice) || totalPrice <= 0) return
const state = get()
const exists = state.transactions.some(
@@ -118,15 +120,19 @@ export const useWalletStore = create<WalletState>((set, get) => ({
)
if (exists) return
const shop = shopId
? useShopStore.getState().shops.find((item) => item.id === shopId)
: undefined
const { income, commissionLabel } = calculateOrderIncome(totalPrice, shop)
const now = new Date().toISOString()
const income = Number((amount * 0.85).toFixed(2))
set((prev) => ({
transactions: [
{
id: generateId("tx"),
type: "income",
amount: income,
description: `订单 ${orderId} 收入(扣除15%抽成`,
description: `订单 ${orderId} 收入(${commissionLabel}`,
createdAt: now,
},
...prev.transactions,