This commit is contained in:
2026-03-17 20:12:29 +03:00
parent e796a5bfdd
commit d05f9c3042
14 changed files with 188 additions and 525 deletions
@@ -1,3 +1,5 @@
from aiogram import Bot
from aiogram.exceptions import TelegramBadRequest, TelegramForbiddenError, TelegramRetryAfter
from aiogram.types import Message, CallbackQuery
from aiogram_dialog import Window, DialogManager
from aiogram_dialog.widgets.text import Format, Const
@@ -16,8 +18,8 @@ from dutylog.infrastructure.database.repositories.floors_repository import (
from dutylog.infrastructure.database.repositories.residents_repository import (
ResidentsRepository,
)
from dutylog.infrastructure.database.repositories.room_hours_transactions_repository import (
RoomHoursTransactionsRepository,
from dutylog.infrastructure.database.repositories.hours_transactions_repository import (
HoursTransactionsRepository,
)
from dutylog.infrastructure.database.repositories.users_repository import (
UsersRepository,
@@ -43,11 +45,7 @@ async def get_rooms_floors_data(
floors_data = [(f"🏢 Этаж {f.number}", f.id) for f in all_floors]
content = """
<blockquote>🚪 <b>Комнаты</b></blockquote>
Выберите этаж для просмотра комнат:
"""
content = "<blockquote>🚪 <b>Комнаты</b></blockquote>\n\nВыберите этаж для просмотра комнат:"
return {
"content": content,
@@ -89,13 +87,7 @@ async def get_rooms_list_data(
rooms_data = [(f"🚪 Комната {r.number}", r.id) for r in rooms]
content = f"""
<blockquote>🚪 <b>Комнаты на этаже {floor_number}</b></blockquote>
<b>Всего комнат:</b> <code>{len(rooms)}</code>
Выберите комнату для просмотра:
"""
content = f"<blockquote>🚪 <b>Комнаты на этаже {floor_number}</b></blockquote>\n\n<b>Всего комнат:</b> <code>{len(rooms)}</code>\n\nВыберите комнату для просмотра:"
return {
"content": content,
@@ -132,11 +124,7 @@ async def get_create_room_floors_data(
floors_data = [(f"🏢 Этаж {f.number}", f.id) for f in all_floors]
content = """
<blockquote> <b>Создание комнаты</b></blockquote>
Выберите этаж для новой комнаты:
"""
content = "<blockquote> <b>Создание комнаты</b></blockquote>\n\nВыберите этаж для новой комнаты:"
return {
"content": content,
@@ -238,28 +226,22 @@ async def get_room_info_data(
floor = await floors_repository.get_floor_by_id(room.on_floor)
floor_number = floor.number if floor else "???"
# Получаем резидентов комнаты
residents = await residents_repository.get_residents_by_room(room_id)
total_active_hours = sum(r.active_hours for r in residents)
total_inactive_hours = sum(r.inactive_hours for r in residents)
residents_info = ""
if residents:
residents_info = "\n<b>Проживающие:</b>\n"
for resident in residents:
status = "🟢" if resident.is_busy else "⚪️"
name = resident.real_name if resident.real_name else "Без имени"
residents_info += f"{status} {name}\n"
residents_info += f"{status} {name} (<code>{resident.active_hours}</code> ч)\n"
else:
residents_info = "\n<i>Нет проживающих</i>\n"
info_content = f"""
<blockquote>🚪 <b>Информация о комнате</b></blockquote>
<b>Номер:</b> <code>{room.number}</code>
<b>Этаж:</b> <code>{floor_number}</code>
{residents_info}
🟢 <b>Отработанные часы:</b> <code>{room.inactive_hours}</code> ч
🔴 <b>Неотработанные часы:</b> <code>{room.active_hours}</code> ч
"""
info_content = f"<blockquote>🚪 <b>Информация о комнате</b></blockquote>\n\n<b>Номер:</b> <code>{room.number}</code>\n<b>Этаж:</b> <code>{floor_number}</code>\n{residents_info}\n🟢 <b>Отработанные часы (всего):</b> <code>{total_inactive_hours}</code> ч\n🔴 <b>Неотработанные часы (всего):</b> <code>{total_active_hours}</code> ч"
return {
"info_content": info_content,
@@ -391,12 +373,11 @@ async def on_room_add_hours_confirm(
callback: CallbackQuery,
button: Button,
dialog_manager: DialogManager,
room_transactions_repository: FromDishka[RoomHoursTransactionsRepository],
transactions_repository: FromDishka[HoursTransactionsRepository],
residents_repository: FromDishka[ResidentsRepository],
users_repository: FromDishka[UsersRepository],
**kwargs,
):
from aiogram import Bot
bot: Bot = dialog_manager.middleware_data.get("bot")
room_id = dialog_manager.dialog_data.get("selected_room_id")
@@ -405,27 +386,26 @@ async def on_room_add_hours_confirm(
admin_id = callback.from_user.id
if room_id and hours:
transaction, _ = await room_transactions_repository.add_hours(
results = await transactions_repository.add_hours_to_room(
room_id=room_id,
amount=hours,
admin_id=admin_id,
is_active=True,
remark=remark,
)
# Отправляем уведомления всем проживающим
residents = await residents_repository.get_residents_by_room(room_id)
for resident in residents:
if resident.user_entity:
for transaction, resident in results:
if resident and resident.user_entity:
user = await users_repository.get_user_by_id(resident.user_entity)
if user:
try:
remark_text = f"\n💬 <i>{remark}</i>" if remark else ""
await bot.send_message(
user.id,
f"<blockquote>📢 <b>Уведомление</b></blockquote>\n\n"
f"<blockquote> <b>Уведомление</b></blockq uote>\n\n"
f"Вашей комнате начислено <b>+{hours}</b> ч{remark_text}"
)
except Exception:
except (TelegramBadRequest, TelegramForbiddenError, TelegramRetryAfter):
pass
await dialog_manager.switch_to(AdminMenuSG.room_info)
@@ -436,13 +416,12 @@ async def on_room_remove_hours_confirm(
callback: CallbackQuery,
button: Button,
dialog_manager: DialogManager,
room_transactions_repository: FromDishka[RoomHoursTransactionsRepository],
transactions_repository: FromDishka[HoursTransactionsRepository],
rooms_repository: FromDishka[RoomsRepository],
residents_repository: FromDishka[ResidentsRepository],
users_repository: FromDishka[UsersRepository],
**kwargs,
):
from aiogram import Bot
bot: Bot = dialog_manager.middleware_data.get("bot")
room_id = dialog_manager.dialog_data.get("selected_room_id")
@@ -451,25 +430,26 @@ async def on_room_remove_hours_confirm(
admin_id = callback.from_user.id
if room_id and hours:
room = await rooms_repository.get_room_by_id(room_id)
if room and room.active_hours < hours:
await callback.answer(
f"⚠️ Недостаточно часов! У комнаты {room.active_hours} неотработанных ч, а вы пытаетесь отнять {hours} ч",
show_alert=True
)
await dialog_manager.switch_to(AdminMenuSG.room_info)
return
residents = await residents_repository.get_residents_by_room(room_id)
await room_transactions_repository.move_hours_to_completed(
for resident in residents:
if resident.active_hours < hours:
await callback.answer(
f"⚠️ Недостаточно часов! У резидента {resident.real_name or 'без имени'} только {resident.active_hours} неотработанных ч, а вы пытаетесь отнять {hours} ч",
show_alert=True
)
await dialog_manager.switch_to(AdminMenuSG.room_info)
return
results = await transactions_repository.remove_hours_from_room(
room_id=room_id,
amount=hours,
admin_id=admin_id,
remark=remark,
)
# Отправляем уведомления всем проживающим
residents = await residents_repository.get_residents_by_room(room_id)
for resident in residents:
if resident.user_entity:
for transaction, resident in results:
if resident and resident.user_entity:
user = await users_repository.get_user_by_id(resident.user_entity)
if user:
try:
@@ -479,7 +459,7 @@ async def on_room_remove_hours_confirm(
f"<blockquote>📢 <b>Уведомление</b></blockquote>\n\n"
f"С вашей комнаты списано <b>-{hours}</b> ч{remark_text}"
)
except Exception:
except (TelegramBadRequest, TelegramForbiddenError, TelegramRetryAfter):
pass
await dialog_manager.switch_to(AdminMenuSG.room_info)
@@ -538,7 +518,7 @@ async def on_delete_room_cancel(
async def get_room_history_data(
dialog_manager: DialogManager,
rooms_repository: FromDishka[RoomsRepository],
room_transactions_repository: FromDishka[RoomHoursTransactionsRepository],
transactions_repository: FromDishka[HoursTransactionsRepository],
**kwargs,
):
room_id = dialog_manager.dialog_data.get("selected_room_id")
@@ -551,25 +531,13 @@ async def get_room_history_data(
if not room:
return {"history_content": "Ошибка: комната не найдена"}
transactions = await room_transactions_repository.get_room_history(room_id)
transactions_sorted = sorted(transactions, key=lambda x: x.created_at)
last_10 = transactions_sorted[-10:]
transactions = await transactions_repository.get_room_transactions(room_id)
last_10 = transactions[:10]
if not last_10:
history_text = f"""
<blockquote>📜 <b>История операций</b></blockquote>
<b>Комната:</b> {room.number}
<i>История операций пуста</i>
"""
history_text = f"<blockquote>📜 <b>История операций</b></blockquote>\n\n<b>Комната:</b> {room.number}\n\n<i>История операций пуста</i>"
else:
history_text = f"""
<blockquote>📜 <b>История операций</b></blockquote>
<b>Комната:</b> {room.number}
"""
history_text = f"<blockquote>📜 <b>История операций</b></blockquote>\n\n<b>Комната:</b> {room.number}\n\n"
for tx in last_10:
operation = "Начислено" if tx.transaction_type == "increase" else "Списано"
emoji = "+" if tx.transaction_type == "increase" else ""
@@ -577,7 +545,7 @@ async def get_room_history_data(
msk_time = tx.created_at.astimezone(msk_now().tzinfo).replace(tzinfo=None)
date_str = msk_time.strftime("%d.%m.%Y %H:%M")
remark_text = f"\n💬 <i>{tx.remark}</i>" if tx.remark else ""
remark_text = f"\n💬 <i>{tx.remark}<t/i>" if tx.remark else ""
history_text += f"<blockquote><b>{operation}</b> {emoji}<code>{tx.amount}</code> ч\n📅 {date_str}{remark_text}</blockquote>\n"