From 1d715d4f639355c2f58114b1f0b8fed9e14fdf74 Mon Sep 17 00:00:00 2001 From: "Andrew K. Choi" Date: Mon, 17 Nov 2025 09:00:21 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=D1=81=D0=BE=D1=85=D1=80=D0=B0=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=83=D1=87=D0=B0=D1=81=D1=82=D0=BD?= =?UTF-8?q?=D0=B8=D0=BA=D0=BE=D0=B2=20=D0=BF=D0=BE=20=D0=9D=D0=9E=D0=9C?= =?UTF-8?q?=D0=95=D0=A0=D0=A3=20=D0=A1=D0=A7=D0=95=D0=A2=D0=90,=20=D0=B0?= =?UTF-8?q?=20=D0=BD=D0=B5=20=D0=BF=D0=BE=20user=5Fid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Критическое изменение логики: БЫЛО: - Участие сохранялось только по user_id - Один пользователь мог участвовать только 1 раз - Номер счета игнорировался СТАЛО: - Участие сохраняется по НОМЕРУ СЧЕТА (account_number) - Заполняются поля: user_id, account_id, account_number - Один пользователь может участвовать НЕСКОЛЬКИМИ счетами - Проверка дубликатов по account_number - Удаление также работает по account_number Теперь: 1. Админ отправляет: '2521 11-22-33-44-55-66-77' 2. Парсится: карта=2521, счет=11-22-33-44-55-66-77 3. Находится владелец карты 2521 4. Добавляется участие ПО СЧЕТУ 11-22-33-44-55-66-77 5. Владелец может участвовать другими счетами --- .bot.pid | 2 +- src/core/services.py | 59 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/.bot.pid b/.bot.pid index 36e4afd..c93c25b 100644 --- a/.bot.pid +++ b/.bot.pid @@ -1 +1 @@ -851623 +859161 diff --git a/src/core/services.py b/src/core/services.py index cb59336..b627055 100644 --- a/src/core/services.py +++ b/src/core/services.py @@ -583,19 +583,47 @@ class ParticipationService: results["errors"].append(f"Пользователь с счётом {formatted_account}{card_info} не найден") continue - # Пробуем добавить в розыгрыш - if await ParticipationService.add_participant(session, lottery_id, user.id): - results["added"] += 1 - detail = f"{user.first_name} ({formatted_account})" - if card_number: - detail = f"{user.first_name} (карта: {card_number}, счёт: {formatted_account})" - results["details"].append(detail) - else: + # Получаем запись Account для этого счета + account_record = await session.execute( + select(Account).where(Account.account_number == formatted_account) + ) + account_record = account_record.scalar_one_or_none() + + if not account_record: + card_info = f" (карта: {card_number})" if card_number else "" + results["errors"].append(f"Запись счета {formatted_account}{card_info} не найдена в базе") + continue + + # Проверяем, не участвует ли уже этот счет + existing = await session.execute( + select(Participation).where( + Participation.lottery_id == lottery_id, + Participation.account_number == formatted_account + ) + ) + if existing.scalar_one_or_none(): results["skipped"] += 1 detail = f"{user.first_name} ({formatted_account})" if card_number: detail = f"{user.first_name} (карта: {card_number}, счёт: {formatted_account})" results["details"].append(f"Уже участвует: {detail}") + continue + + # Добавляем участие по счету + participation = Participation( + lottery_id=lottery_id, + user_id=user.id, + account_id=account_record.id, + account_number=formatted_account + ) + session.add(participation) + await session.commit() + + results["added"] += 1 + detail = f"{user.first_name} ({formatted_account})" + if card_number: + detail = f"{user.first_name} (карта: {card_number}, счёт: {formatted_account})" + results["details"].append(detail) except Exception as e: results["errors"].append(f"Ошибка с {account_input}: {str(e)}") @@ -650,8 +678,19 @@ class ParticipationService: results["details"].append(f"Не найден: {formatted_account}{card_info}") continue - # Пробуем удалить из розыгрыша - if await ParticipationService.remove_participant(session, lottery_id, user.id): + # Ищем участие по номеру счета (не по user_id!) + participation = await session.execute( + select(Participation).where( + Participation.lottery_id == lottery_id, + Participation.account_number == formatted_account + ) + ) + participation = participation.scalar_one_or_none() + + if participation: + await session.delete(participation) + await session.commit() + results["removed"] += 1 detail = f"{user.first_name} ({formatted_account})" if card_number: