mirror of
https://github.com/koloideal/DutyLog.git
synced 2026-06-10 18:35:29 +03:00
update
This commit is contained in:
@@ -1,60 +0,0 @@
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from dutylog.infrastructure.database.models.room_hours_transaction import RoomHoursTransaction
|
||||
|
||||
|
||||
class RoomHoursTransactionsDAO:
|
||||
def __init__(self, session: AsyncSession):
|
||||
self.session = session
|
||||
|
||||
async def get_by_id(self, transaction_id: int) -> RoomHoursTransaction | None:
|
||||
result = await self.session.execute(
|
||||
select(RoomHoursTransaction).where(RoomHoursTransaction.id == transaction_id)
|
||||
)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
async def get_by_room_id(self, room_id: int) -> list[RoomHoursTransaction]:
|
||||
result = await self.session.execute(
|
||||
select(RoomHoursTransaction)
|
||||
.where(RoomHoursTransaction.room_id == room_id)
|
||||
.order_by(RoomHoursTransaction.created_at.desc())
|
||||
)
|
||||
return list(result.scalars().all())
|
||||
|
||||
async def get_all(self) -> list[RoomHoursTransaction]:
|
||||
result = await self.session.execute(select(RoomHoursTransaction))
|
||||
return list(result.scalars().all())
|
||||
|
||||
async def create(self, transaction: RoomHoursTransaction) -> RoomHoursTransaction:
|
||||
self.session.add(transaction)
|
||||
await self.session.commit()
|
||||
await self.session.refresh(transaction)
|
||||
return transaction
|
||||
|
||||
async def get_by_period(self, start_date, end_date) -> list[RoomHoursTransaction]:
|
||||
from datetime import datetime, time
|
||||
|
||||
start_datetime = datetime.combine(start_date, time.min)
|
||||
end_datetime = datetime.combine(end_date, time.max)
|
||||
|
||||
result = await self.session.execute(
|
||||
select(RoomHoursTransaction)
|
||||
.where(RoomHoursTransaction.created_at >= start_datetime)
|
||||
.where(RoomHoursTransaction.created_at <= end_datetime)
|
||||
.order_by(RoomHoursTransaction.created_at.asc())
|
||||
)
|
||||
return list(result.scalars().all())
|
||||
async def get_by_period(self, start_date, end_date) -> list[RoomHoursTransaction]:
|
||||
from datetime import datetime, time
|
||||
|
||||
start_datetime = datetime.combine(start_date, time.min)
|
||||
end_datetime = datetime.combine(end_date, time.max)
|
||||
|
||||
result = await self.session.execute(
|
||||
select(RoomHoursTransaction)
|
||||
.where(RoomHoursTransaction.created_at >= start_datetime)
|
||||
.where(RoomHoursTransaction.created_at <= end_datetime)
|
||||
.order_by(RoomHoursTransaction.created_at.asc())
|
||||
)
|
||||
return list(result.scalars().all())
|
||||
@@ -1,10 +1,9 @@
|
||||
from dutylog.infrastructure.database.models.base import Base
|
||||
from dutylog.infrastructure.database.models.user import User
|
||||
from dutylog.infrastructure.database.models.hours_transaction import HoursTransaction
|
||||
from dutylog.infrastructure.database.models.room_hours_transaction import RoomHoursTransaction
|
||||
from dutylog.infrastructure.database.models.room import Room
|
||||
from dutylog.infrastructure.database.models.resident import Resident
|
||||
from dutylog.infrastructure.database.models.floor import Floor
|
||||
from dutylog.infrastructure.database.models.reporting_period import ReportingPeriod
|
||||
|
||||
__all__ = ["Base", "User", "HoursTransaction", "RoomHoursTransaction", "Room", "Resident", "Floor", "ReportingPeriod"]
|
||||
__all__ = ["Base", "User", "HoursTransaction", "Room", "Resident", "Floor", "ReportingPeriod"]
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
|
||||
from sqlalchemy import BigInteger, Integer, String, DateTime, ForeignKey
|
||||
from sqlalchemy import BigInteger, Boolean, Integer, String, DateTime, ForeignKey
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from dutylog.infrastructure.database.models.base import Base
|
||||
@@ -26,6 +26,9 @@ class HoursTransaction(Base):
|
||||
BigInteger, ForeignKey("users.id", ondelete="SET NULL"), nullable=True
|
||||
)
|
||||
remark: Mapped[str | None] = mapped_column(String(500), nullable=True)
|
||||
per_room: Mapped[bool] = mapped_column(
|
||||
Boolean, default=False, server_default="false"
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), default=msk_now
|
||||
)
|
||||
|
||||
@@ -12,5 +12,3 @@ class Room(Base):
|
||||
on_floor: Mapped[int] = mapped_column(
|
||||
Integer, ForeignKey("floors.id", ondelete="CASCADE"), nullable=False
|
||||
)
|
||||
active_hours: Mapped[int] = mapped_column(Integer, default=0, server_default="0")
|
||||
inactive_hours: Mapped[int] = mapped_column(Integer, default=0, server_default="0")
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import BigInteger, Integer, String, DateTime, ForeignKey
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from dutylog.infrastructure.database.models.base import Base
|
||||
from dutylog.infrastructure.utils.datetime import msk_now
|
||||
|
||||
|
||||
class RoomHoursTransaction(Base):
|
||||
__tablename__ = "room_hours_transactions"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
room_id: Mapped[int] = mapped_column(
|
||||
Integer, ForeignKey("rooms.id", ondelete="CASCADE"), nullable=False
|
||||
)
|
||||
transaction_type: Mapped[str] = mapped_column(String(50), nullable=False)
|
||||
amount: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
admin_id: Mapped[int] = mapped_column(
|
||||
BigInteger, ForeignKey("users.id", ondelete="SET NULL"), nullable=True
|
||||
)
|
||||
remark: Mapped[str | None] = mapped_column(String(500), nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), default=msk_now
|
||||
)
|
||||
@@ -25,6 +25,7 @@ class HoursTransactionsRepository:
|
||||
admin_id: int | None = None,
|
||||
is_active: bool = True,
|
||||
remark: str | None = None,
|
||||
per_room: bool = False,
|
||||
) -> tuple[HoursTransaction, Resident | None]:
|
||||
transaction = HoursTransaction(
|
||||
resident_id=resident_id,
|
||||
@@ -32,6 +33,7 @@ class HoursTransactionsRepository:
|
||||
amount=amount,
|
||||
admin_id=admin_id,
|
||||
remark=remark,
|
||||
per_room=per_room,
|
||||
)
|
||||
transaction = await self.transactions_dao.create(transaction)
|
||||
|
||||
@@ -57,6 +59,7 @@ class HoursTransactionsRepository:
|
||||
admin_id: int | None = None,
|
||||
is_active: bool = True,
|
||||
remark: str | None = None,
|
||||
per_room: bool = False,
|
||||
) -> tuple[HoursTransaction, Resident | None]:
|
||||
transaction = HoursTransaction(
|
||||
resident_id=resident_id,
|
||||
@@ -64,6 +67,7 @@ class HoursTransactionsRepository:
|
||||
amount=amount,
|
||||
admin_id=admin_id,
|
||||
remark=remark,
|
||||
per_room=per_room,
|
||||
)
|
||||
transaction = await self.transactions_dao.create(transaction)
|
||||
|
||||
@@ -88,6 +92,7 @@ class HoursTransactionsRepository:
|
||||
amount: int,
|
||||
admin_id: int | None = None,
|
||||
remark: str | None = None,
|
||||
per_room: bool = False,
|
||||
) -> tuple[HoursTransaction, Resident | None]:
|
||||
"""Перемещает часы из неотработанных в отработанные"""
|
||||
transaction = HoursTransaction(
|
||||
@@ -96,6 +101,7 @@ class HoursTransactionsRepository:
|
||||
amount=amount,
|
||||
admin_id=admin_id,
|
||||
remark=remark,
|
||||
per_room=per_room,
|
||||
)
|
||||
transaction = await self.transactions_dao.create(transaction)
|
||||
|
||||
@@ -124,3 +130,65 @@ class HoursTransactionsRepository:
|
||||
|
||||
async def get_by_period(self, start_date, end_date) -> list[HoursTransaction]:
|
||||
return await self.transactions_dao.get_by_period(start_date, end_date)
|
||||
|
||||
async def add_hours_to_room(
|
||||
self,
|
||||
room_id: int,
|
||||
amount: int,
|
||||
admin_id: int | None = None,
|
||||
is_active: bool = True,
|
||||
remark: str | None = None,
|
||||
) -> list[tuple[HoursTransaction, Resident | None]]:
|
||||
"""Начисляет часы всем резидентам комнаты с флагом per_room=True"""
|
||||
residents = await self.residents_dao.get_by_room(room_id)
|
||||
results = []
|
||||
|
||||
for resident in residents:
|
||||
result = await self.add_hours(
|
||||
resident_id=resident.id,
|
||||
amount=amount,
|
||||
admin_id=admin_id,
|
||||
is_active=is_active,
|
||||
remark=remark,
|
||||
per_room=True,
|
||||
)
|
||||
results.append(result)
|
||||
|
||||
return results
|
||||
|
||||
async def remove_hours_from_room(
|
||||
self,
|
||||
room_id: int,
|
||||
amount: int,
|
||||
admin_id: int | None = None,
|
||||
remark: str | None = None,
|
||||
) -> list[tuple[HoursTransaction, Resident | None]]:
|
||||
"""Списывает часы у всех резидентов комнаты с флагом per_room=True"""
|
||||
residents = await self.residents_dao.get_by_room(room_id)
|
||||
results = []
|
||||
|
||||
for resident in residents:
|
||||
result = await self.move_hours_to_completed(
|
||||
resident_id=resident.id,
|
||||
amount=amount,
|
||||
admin_id=admin_id,
|
||||
remark=remark,
|
||||
per_room=True,
|
||||
)
|
||||
results.append(result)
|
||||
|
||||
return results
|
||||
|
||||
async def get_room_transactions(self, room_id: int) -> list[HoursTransaction]:
|
||||
"""Получает все транзакции резидентов комнаты с флагом per_room=True"""
|
||||
residents = await self.residents_dao.get_by_room(room_id)
|
||||
all_transactions = []
|
||||
|
||||
for resident in residents:
|
||||
transactions = await self.transactions_dao.get_by_resident_id(resident.id)
|
||||
room_transactions = [t for t in transactions if t.per_room]
|
||||
all_transactions.extend(room_transactions)
|
||||
|
||||
# Сортируем по дате создания
|
||||
all_transactions.sort(key=lambda t: t.created_at, reverse=True)
|
||||
return all_transactions
|
||||
|
||||
-122
@@ -1,122 +0,0 @@
|
||||
from dutylog.infrastructure.database.dao.room_hours_transactions_dao import (
|
||||
RoomHoursTransactionsDAO,
|
||||
)
|
||||
from dutylog.infrastructure.database.dao.rooms_dao import RoomsDAO
|
||||
from dutylog.infrastructure.database.models.room_hours_transaction import (
|
||||
RoomHoursTransaction,
|
||||
)
|
||||
from dutylog.infrastructure.database.models.hours_transaction import TransactionType
|
||||
from dutylog.infrastructure.database.models.room import Room
|
||||
|
||||
|
||||
class RoomHoursTransactionsRepository:
|
||||
def __init__(
|
||||
self,
|
||||
transactions_dao: RoomHoursTransactionsDAO,
|
||||
rooms_dao: RoomsDAO,
|
||||
):
|
||||
self.transactions_dao = transactions_dao
|
||||
self.rooms_dao = rooms_dao
|
||||
|
||||
async def add_hours(
|
||||
self,
|
||||
room_id: int,
|
||||
amount: int,
|
||||
admin_id: int | None = None,
|
||||
is_active: bool = True,
|
||||
) -> tuple[RoomHoursTransaction, Room | None]:
|
||||
transaction = RoomHoursTransaction(
|
||||
room_id=room_id,
|
||||
transaction_type=TransactionType.INCREASE.value,
|
||||
amount=amount,
|
||||
admin_id=admin_id,
|
||||
)
|
||||
transaction = await self.transactions_dao.create(transaction)
|
||||
|
||||
room = await self.rooms_dao.get_by_id(room_id)
|
||||
if room:
|
||||
if is_active:
|
||||
new_hours = room.active_hours + amount
|
||||
room = await self.rooms_dao.update(
|
||||
room_id, active_hours=new_hours
|
||||
)
|
||||
else:
|
||||
new_hours = room.inactive_hours + amount
|
||||
room = await self.rooms_dao.update(
|
||||
room_id, inactive_hours=new_hours
|
||||
)
|
||||
|
||||
return transaction, room
|
||||
|
||||
async def remove_hours(
|
||||
self,
|
||||
room_id: int,
|
||||
amount: int,
|
||||
admin_id: int | None = None,
|
||||
is_active: bool = True,
|
||||
) -> tuple[RoomHoursTransaction, Room | None]:
|
||||
transaction = RoomHoursTransaction(
|
||||
room_id=room_id,
|
||||
transaction_type=TransactionType.DECREASE.value,
|
||||
amount=amount,
|
||||
admin_id=admin_id,
|
||||
)
|
||||
transaction = await self.transactions_dao.create(transaction)
|
||||
|
||||
room = await self.rooms_dao.get_by_id(room_id)
|
||||
if room:
|
||||
if is_active:
|
||||
new_hours = max(0, room.active_hours - amount)
|
||||
room = await self.rooms_dao.update(
|
||||
room_id, active_hours=new_hours
|
||||
)
|
||||
else:
|
||||
new_hours = max(0, room.inactive_hours - amount)
|
||||
room = await self.rooms_dao.update(
|
||||
room_id, inactive_hours=new_hours
|
||||
)
|
||||
|
||||
return transaction, room
|
||||
|
||||
async def move_hours_to_completed(
|
||||
self,
|
||||
room_id: int,
|
||||
amount: int,
|
||||
admin_id: int | None = None,
|
||||
) -> tuple[RoomHoursTransaction, Room | None]:
|
||||
"""Перемещает часы из неотработанных в отработанные"""
|
||||
transaction = RoomHoursTransaction(
|
||||
room_id=room_id,
|
||||
transaction_type=TransactionType.DECREASE.value,
|
||||
amount=amount,
|
||||
admin_id=admin_id,
|
||||
)
|
||||
transaction = await self.transactions_dao.create(transaction)
|
||||
|
||||
room = await self.rooms_dao.get_by_id(room_id)
|
||||
if room:
|
||||
new_active = max(0, room.active_hours - amount)
|
||||
new_inactive = room.inactive_hours + amount
|
||||
room = await self.rooms_dao.update(
|
||||
room_id,
|
||||
active_hours=new_active,
|
||||
inactive_hours=new_inactive
|
||||
)
|
||||
|
||||
return transaction, room
|
||||
|
||||
async def get_room_history(self, room_id: int) -> list[RoomHoursTransaction]:
|
||||
return await self.transactions_dao.get_by_room_id(room_id)
|
||||
|
||||
async def get_all_transactions(self) -> list[RoomHoursTransaction]:
|
||||
return await self.transactions_dao.get_all()
|
||||
|
||||
async def get_transaction_by_id(
|
||||
self, transaction_id: int
|
||||
) -> RoomHoursTransaction | None:
|
||||
return await self.transactions_dao.get_by_id(transaction_id)
|
||||
|
||||
async def get_by_period(self, start_date, end_date) -> list[RoomHoursTransaction]:
|
||||
return await self.transactions_dao.get_by_period(start_date, end_date)
|
||||
async def get_by_period(self, start_date, end_date) -> list[RoomHoursTransaction]:
|
||||
return await self.transactions_dao.get_by_period(start_date, end_date)
|
||||
@@ -8,9 +8,6 @@ from dutylog.infrastructure.database.dao.users_dao import UsersDAO
|
||||
from dutylog.infrastructure.database.dao.hours_transactions_dao import (
|
||||
HoursTransactionsDAO,
|
||||
)
|
||||
from dutylog.infrastructure.database.dao.room_hours_transactions_dao import (
|
||||
RoomHoursTransactionsDAO,
|
||||
)
|
||||
from dutylog.infrastructure.database.dao.rooms_dao import RoomsDAO
|
||||
from dutylog.infrastructure.database.dao.residents_dao import ResidentsDAO
|
||||
from dutylog.infrastructure.database.dao.floors_dao import FloorsDAO
|
||||
@@ -23,9 +20,6 @@ from dutylog.infrastructure.database.repositories.users_repository import (
|
||||
from dutylog.infrastructure.database.repositories.hours_transactions_repository import (
|
||||
HoursTransactionsRepository,
|
||||
)
|
||||
from dutylog.infrastructure.database.repositories.room_hours_transactions_repository import (
|
||||
RoomHoursTransactionsRepository,
|
||||
)
|
||||
from dutylog.infrastructure.database.repositories.rooms_repository import (
|
||||
RoomsRepository,
|
||||
)
|
||||
@@ -76,10 +70,6 @@ class DAOProvider(Provider):
|
||||
def get_hours_transactions_dao(self, session: AsyncSession) -> HoursTransactionsDAO:
|
||||
return HoursTransactionsDAO(session)
|
||||
|
||||
@provide(scope=Scope.REQUEST)
|
||||
def get_room_hours_transactions_dao(self, session: AsyncSession) -> RoomHoursTransactionsDAO:
|
||||
return RoomHoursTransactionsDAO(session)
|
||||
|
||||
@provide(scope=Scope.REQUEST)
|
||||
def get_rooms_dao(self, session: AsyncSession) -> RoomsDAO:
|
||||
return RoomsDAO(session)
|
||||
@@ -110,14 +100,6 @@ class RepositoryProvider(Provider):
|
||||
) -> HoursTransactionsRepository:
|
||||
return HoursTransactionsRepository(transactions_dao, residents_dao)
|
||||
|
||||
@provide(scope=Scope.REQUEST)
|
||||
def get_room_hours_transactions_repository(
|
||||
self,
|
||||
transactions_dao: RoomHoursTransactionsDAO,
|
||||
rooms_dao: RoomsDAO,
|
||||
) -> RoomHoursTransactionsRepository:
|
||||
return RoomHoursTransactionsRepository(transactions_dao, rooms_dao)
|
||||
|
||||
@provide(scope=Scope.REQUEST)
|
||||
def get_rooms_repository(self, rooms_dao: RoomsDAO) -> RoomsRepository:
|
||||
return RoomsRepository(rooms_dao)
|
||||
@@ -144,7 +126,6 @@ class ServiceProvider(Provider):
|
||||
def get_report_service(
|
||||
self,
|
||||
hours_transactions_repository: HoursTransactionsRepository,
|
||||
room_hours_transactions_repository: RoomHoursTransactionsRepository,
|
||||
residents_repository: ResidentsRepository,
|
||||
rooms_repository: RoomsRepository,
|
||||
floors_repository: FloorsRepository,
|
||||
@@ -152,7 +133,6 @@ class ServiceProvider(Provider):
|
||||
) -> ReportService:
|
||||
return ReportService(
|
||||
hours_transactions_repository,
|
||||
room_hours_transactions_repository,
|
||||
residents_repository,
|
||||
rooms_repository,
|
||||
floors_repository,
|
||||
|
||||
Reference in New Issue
Block a user