66from nonebot_plugin_orm import AsyncSession
77from sqlalchemy import insert , select , update
88
9+ from .exception import (
10+ AccountFrozen ,
11+ AccountNotFound ,
12+ CurrencyNotFound ,
13+ TransactionException ,
14+ TransactionNotFound ,
15+ )
916from .models .balance import Transaction , UserAccount
1017from .models .currency import CurrencyMeta
1118from .pyd_models .currency_pyd import CurrencyData
@@ -96,7 +103,7 @@ async def remove_currency(self, currency_id: str):
96103 )
97104 ).scalar_one_or_none ()
98105 if not currency :
99- raise ValueError ( "Currency not found" )
106+ raise CurrencyNotFound ( f "Currency { currency_id } not found" )
100107 await session .delete (currency )
101108 users = await session .execute (
102109 select (UserAccount )
@@ -124,7 +131,7 @@ async def get_or_create_account(
124131 result = await session .execute (stmt )
125132 currency = result .scalar_one_or_none ()
126133 if currency is None :
127- raise ValueError (f"Currency { currency_id } not found" )
134+ raise CurrencyNotFound (f"Currency { currency_id } not found" )
128135
129136 # 检查账户是否存在
130137 stmt = (
@@ -158,6 +165,39 @@ async def get_or_create_account(
158165 session .add (account )
159166 return account
160167
168+ async def set_account_frozen (
169+ self ,
170+ account_id : str ,
171+ currency_id : str ,
172+ frozen : bool ,
173+ ) -> None :
174+ """设置账户冻结状态"""
175+ async with self .session as session :
176+ account = await self .get_or_create_account (account_id , currency_id )
177+ session .add (account )
178+ account .frozen = frozen
179+ await session .commit ()
180+
181+ async def set_frozen_all (self , account_id : str , frozen : bool ):
182+ async with self .session as session :
183+ result = await session .execute (
184+ select (UserAccount ).where (UserAccount .id == account_id )
185+ )
186+ accounts = result .scalars ().all ()
187+ session .add_all (accounts )
188+ for account in accounts :
189+ account .frozen = frozen
190+ await session .commit ()
191+
192+ async def is_account_frozen (
193+ self ,
194+ account_id : str ,
195+ currency_id : str ,
196+ ) -> bool :
197+ """判断账户是否冻结"""
198+ async with self .session :
199+ return (await self .get_or_create_account (account_id , currency_id )).frozen
200+
161201 async def get_balance (self , account_id : str , currency_id : str ) -> float | None :
162202 """获取账户余额"""
163203 uni_id = get_uni_id (account_id , currency_id )
@@ -180,16 +220,21 @@ async def update_balance(
180220 ).scalar_one_or_none ()
181221
182222 if account is None :
183- raise ValueError ("Account not found" )
223+ raise AccountNotFound ("Account not found" )
184224 session .add (account )
185225
226+ if account .frozen :
227+ raise AccountFrozen (
228+ f"Account { account_id } on currency { currency_id } is frozen"
229+ )
230+
186231 # 获取货币规则
187232 currency = await session .get (CurrencyMeta , account .currency_id )
188233 session .add (currency )
189234
190235 # 负余额检查
191236 if amount < 0 and not getattr (currency , "allow_negative" , False ):
192- raise ValueError ("Insufficient funds" )
237+ raise TransactionException ("Insufficient funds" )
193238
194239 # 记录原始余额
195240 old_balance = account .balance
@@ -235,7 +280,7 @@ async def remove_account(self, account_id: str, currency_id: str | None = None):
235280 )
236281 accounts = (await session .execute (stmt )).scalars ().all ()
237282 if not accounts :
238- raise ValueError ("Account not found" )
283+ raise AccountNotFound ("Account not found" )
239284 for account in accounts :
240285 await session .delete (account )
241286 await session .commit ()
@@ -337,6 +382,6 @@ async def remove_transaction(self, transaction_id: str) -> None:
337382 )
338383 ).scalar_one_or_none ()
339384 if not transaction :
340- raise ValueError ("Transaction not found" )
385+ raise TransactionNotFound ("Transaction not found" )
341386 await session .delete (transaction )
342387 await session .commit ()
0 commit comments