支持已绑定设备追加 NFC 卡
This commit is contained in:
@@ -60,32 +60,6 @@ class CardService(DatabaseServiceBase):
|
||||
session_logger.error("card", "service", f"load card failed: {exc}")
|
||||
return None
|
||||
|
||||
async def _clear_existing_device_card(
|
||||
self,
|
||||
device_id: str,
|
||||
card_uuid: str,
|
||||
async_session: AsyncSession,
|
||||
) -> None:
|
||||
query = select(DBCard).where(
|
||||
DBCard.device_id == device_id,
|
||||
DBCard.card_uuid != card_uuid,
|
||||
)
|
||||
result = await async_session.execute(query)
|
||||
existing_cards = result.scalars().all()
|
||||
|
||||
for db_card in existing_cards:
|
||||
db_card.device_id = None
|
||||
db_card.status = 0
|
||||
|
||||
async with self.lock:
|
||||
cached = self.cards.get(db_card.card_uuid)
|
||||
if cached is not None:
|
||||
cached.device_id = None
|
||||
cached.status = 0
|
||||
|
||||
if existing_cards:
|
||||
await async_session.flush()
|
||||
|
||||
async def _save_card_to_db(self, card: Card, async_session: AsyncSession, commit: bool = True) -> None:
|
||||
try:
|
||||
query = select(DBCard).where(DBCard.card_uuid == card.card_uuid)
|
||||
@@ -182,9 +156,10 @@ class CardService(DatabaseServiceBase):
|
||||
|
||||
try:
|
||||
existing_card = await self.get_card_by_uuid(card_uuid, db_session=db_session)
|
||||
await self._clear_existing_device_card(device_id=device_id, card_uuid=card_uuid, async_session=db_session)
|
||||
|
||||
if existing_card:
|
||||
if existing_card.device_id and existing_card.device_id != device_id:
|
||||
raise ValueError("card is already bound to another device")
|
||||
existing_card.device_id = device_id
|
||||
existing_card.card_name = card_name
|
||||
existing_card.status = 1
|
||||
@@ -230,5 +205,9 @@ class CardService(DatabaseServiceBase):
|
||||
card = await self.get_card_by_uuid(card_uuid)
|
||||
return bool(card and card.device_id == device_id)
|
||||
|
||||
async def is_active_for_device(self, card_uuid: str, device_id: str) -> bool:
|
||||
card = await self.get_card_by_uuid(card_uuid)
|
||||
return bool(card and card.device_id == device_id and int(card.status) == 1)
|
||||
|
||||
|
||||
card_service = CardService()
|
||||
|
||||
Reference in New Issue
Block a user