"use client" import { Badge } from "@/components/ui/badge" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table" import { requestWithAuth } from "@/lib/api" import { listWalletTransactions } from "@/lib/api/transactions" import { isActiveOrder, isCompletedOrder } from "@/lib/domain/order-filters" import { resolveOwnerShop } from "@/lib/domain/resolve-current-shop" import { toApiError } from "@/lib/errors" import type { WalletTransaction } from "@/lib/types" import { useAuthStore } from "@/store/auth" import { useOrderStore } from "@/store/orders" import { useShopStore } from "@/store/shops" import { ArrowDownLeft, ArrowUpRight, CreditCard, DollarSign } from "lucide-react" import { useEffect, useState } from "react" export default function ShopIncomePage() { const userId = useAuthStore((state) => state.user?.id) const shops = useShopStore((state) => state.shops) const orders = useOrderStore((state) => state.orders) const shop = resolveOwnerShop(userId, shops) const [transactions, setTransactions] = useState([]) const [isLoading, setIsLoading] = useState(false) const [loadError, setLoadError] = useState(null) useEffect(() => { if (!shop) return let cancelled = false async function load() { setIsLoading(true) setLoadError(null) try { const res = await requestWithAuth(() => listWalletTransactions({ offset: 0, limit: 1000 })) if (cancelled) return if (!res) { setLoadError("请先登录") setTransactions([]) return } setTransactions(res) } catch (error) { if (cancelled) return setLoadError(toApiError(error).msg) } finally { if (!cancelled) setIsLoading(false) } } void load() return () => { cancelled = true } }, [shop]) if (!shop) { return
当前账号没有可管理的店铺
} const shopOrders = orders.filter((order) => order.shopId === shop?.id) const completedOrders = shopOrders.filter((o) => isCompletedOrder(o.status)) const totalIncome = completedOrders.reduce((acc, order) => acc + order.totalPrice, 0) const currentMonth = new Date().getMonth() const thisMonthIncome = completedOrders .filter((o) => new Date(o.completedAt || "").getMonth() === currentMonth) .reduce((acc, order) => acc + order.totalPrice, 0) const pendingSettlement = shopOrders .filter( (o) => isActiveOrder(o.status) && o.status !== "pending_payment" && o.status !== "pending_accept", ) .reduce((acc, order) => acc + order.totalPrice, 0) const shopOrderIds = new Set(shopOrders.map((order) => order.id)) const relatedTransactions = transactions.filter((transaction) => { if (transaction.type === "withdrawal") return true if (transaction.type !== "income") return false const match = transaction.description.match(/ord[-\d]+/) if (!match) return false return shopOrderIds.has(match[0]) }) return (

收入统计

总收入
¥{totalIncome.toFixed(2)}
本月收入
¥{thisMonthIncome.toFixed(2)}
待结算
¥{pendingSettlement.toFixed(2)}
交易明细 类型 描述 金额 时间 {isLoading ? ( 加载中... ) : loadError ? ( {loadError} ) : relatedTransactions.length > 0 ? ( relatedTransactions.map((transaction) => (
{Number(transaction.amount) > 0 ? ( ) : ( )} 0 ? "default" : "secondary"}> {transaction.type === "topup" ? "充值" : transaction.type === "payment" ? "支付" : transaction.type === "income" ? "收入" : transaction.type === "withdrawal" ? "提现" : "退款"}
{transaction.description} 0 ? "text-green-600" : "text-red-600"} > {Number(transaction.amount) > 0 ? "+" : ""} {transaction.amount} {new Date(transaction.createdAt).toLocaleString()}
)) ) : ( 暂无交易记录 )}
) }