首次提交by MimoCode

This commit is contained in:
tang1219
2026-06-15 14:50:15 +08:00
parent 584b31da50
commit ead13f863c
166 changed files with 14908 additions and 1 deletions
+51
View File
@@ -0,0 +1,51 @@
from typing import Optional, List
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import selectinload
from app.models.audit_log import AuditLog
class AuditLogRepository:
def __init__(self, db: AsyncSession):
self.db = db
async def get_by_id(self, log_id: int) -> Optional[AuditLog]:
result = await self.db.execute(
select(AuditLog)
.options(selectinload(AuditLog.medicine), selectinload(AuditLog.user))
.where(AuditLog.id == log_id)
)
return result.scalar_one_or_none()
async def get_list(
self,
medicine_id: Optional[int] = None,
user_id: Optional[int] = None,
action: Optional[str] = None,
page: int = 1,
page_size: int = 20
) -> List[AuditLog]:
query = select(AuditLog).options(
selectinload(AuditLog.medicine),
selectinload(AuditLog.user)
)
if medicine_id:
query = query.where(AuditLog.medicine_id == medicine_id)
if user_id:
query = query.where(AuditLog.user_id == user_id)
if action:
query = query.where(AuditLog.action == action)
query = query.order_by(AuditLog.created_at.desc())
query = query.offset((page - 1) * page_size).limit(page_size)
result = await self.db.execute(query)
return list(result.scalars().all())
async def create(self, data: dict) -> AuditLog:
log = AuditLog(**data)
self.db.add(log)
await self.db.flush()
return log
+56
View File
@@ -0,0 +1,56 @@
from typing import Optional, List
from datetime import date
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.batch import Batch
class BatchRepository:
def __init__(self, db: AsyncSession):
self.db = db
async def get_by_id(self, batch_id: int) -> Optional[Batch]:
result = await self.db.execute(select(Batch).where(Batch.id == batch_id))
return result.scalar_one_or_none()
async def get_by_medicine_id(self, medicine_id: int) -> List[Batch]:
result = await self.db.execute(
select(Batch)
.where(Batch.medicine_id == medicine_id)
.order_by(Batch.expiry_date)
)
return list(result.scalars().all())
async def get_expiring_before(self, target_date: date) -> List[Batch]:
result = await self.db.execute(
select(Batch)
.where(
Batch.expiry_date <= target_date,
Batch.is_expired == False,
Batch.quantity > 0
)
)
return list(result.scalars().all())
async def create(self, data: dict) -> Batch:
batch = Batch(**data)
self.db.add(batch)
await self.db.flush()
return batch
async def update(self, batch_id: int, data: dict) -> Optional[Batch]:
batch = await self.get_by_id(batch_id)
if not batch:
return None
for key, value in data.items():
setattr(batch, key, value)
await self.db.flush()
return batch
async def delete(self, batch_id: int) -> bool:
batch = await self.get_by_id(batch_id)
if not batch:
return False
await self.db.delete(batch)
return True
+54
View File
@@ -0,0 +1,54 @@
from typing import Optional, List
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.category import Category
class CategoryRepository:
def __init__(self, db: AsyncSession):
self.db = db
async def get_by_id(self, category_id: int) -> Optional[Category]:
result = await self.db.execute(select(Category).where(Category.id == category_id))
return result.scalar_one_or_none()
async def get_all(self, level: Optional[int] = None, parent_id: Optional[int] = None) -> List[Category]:
query = select(Category)
if level:
query = query.where(Category.level == level)
if parent_id:
query = query.where(Category.parent_id == parent_id)
query = query.order_by(Category.sort_order)
result = await self.db.execute(query)
return list(result.scalars().all())
async def get_children(self, parent_id: int) -> List[Category]:
result = await self.db.execute(
select(Category)
.where(Category.parent_id == parent_id)
.order_by(Category.sort_order)
)
return list(result.scalars().all())
async def create(self, data: dict) -> Category:
category = Category(**data)
self.db.add(category)
await self.db.flush()
return category
async def update(self, category_id: int, data: dict) -> Optional[Category]:
category = await self.get_by_id(category_id)
if not category:
return None
for key, value in data.items():
setattr(category, key, value)
await self.db.flush()
return category
async def delete(self, category_id: int) -> bool:
category = await self.get_by_id(category_id)
if not category:
return False
await self.db.delete(category)
return True
+92
View File
@@ -0,0 +1,92 @@
from typing import Optional, List, Tuple
from sqlalchemy import select, func
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import selectinload
from app.models.medicine import Medicine
class MedicineRepository:
def __init__(self, db: AsyncSession):
self.db = db
async def get_by_id(self, medicine_id: int) -> Optional[Medicine]:
result = await self.db.execute(
select(Medicine)
.options(selectinload(Medicine.batches))
.where(Medicine.id == medicine_id)
)
return result.scalar_one_or_none()
async def get_list(
self,
category_id: Optional[int] = None,
search: Optional[str] = None,
page: int = 1,
page_size: int = 20
) -> Tuple[List[Medicine], int]:
query = select(Medicine).options(selectinload(Medicine.batches))
if category_id:
query = query.where(Medicine.category_id == category_id)
if search:
search_filter = f"%{search}%"
query = query.where(
Medicine.name.ilike(search_filter) |
Medicine.generic_name.ilike(search_filter) |
Medicine.brand_name.ilike(search_filter)
)
count_query = select(func.count()).select_from(Medicine)
if category_id:
count_query = count_query.where(Medicine.category_id == category_id)
if search:
search_filter = f"%{search}%"
count_query = count_query.where(
Medicine.name.ilike(search_filter) |
Medicine.generic_name.ilike(search_filter) |
Medicine.brand_name.ilike(search_filter)
)
total_result = await self.db.execute(count_query)
total = total_result.scalar()
query = query.offset((page - 1) * page_size).limit(page_size)
result = await self.db.execute(query)
medicines = list(result.scalars().all())
return medicines, total
async def create(self, data: dict) -> Medicine:
medicine = Medicine(**data)
self.db.add(medicine)
await self.db.flush()
return medicine
async def update(self, medicine_id: int, data: dict) -> Optional[Medicine]:
medicine = await self.get_by_id(medicine_id)
if not medicine:
return None
for key, value in data.items():
setattr(medicine, key, value)
await self.db.flush()
return medicine
async def delete(self, medicine_id: int) -> bool:
medicine = await self.get_by_id(medicine_id)
if not medicine:
return False
await self.db.delete(medicine)
return True
async def search(self, query: str) -> List[Medicine]:
search_filter = f"%{query}%"
result = await self.db.execute(
select(Medicine).options(selectinload(Medicine.batches)).where(
Medicine.name.ilike(search_filter) |
Medicine.generic_name.ilike(search_filter) |
Medicine.indications.ilike(search_filter)
)
)
return list(result.scalars().all())
+71
View File
@@ -0,0 +1,71 @@
from typing import Optional, List
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.notification import Notification
class NotificationRepository:
def __init__(self, db: AsyncSession):
self.db = db
async def get_by_id(self, notification_id: int) -> Optional[Notification]:
result = await self.db.execute(
select(Notification).where(Notification.id == notification_id)
)
return result.scalar_one_or_none()
async def get_list(
self,
user_id: Optional[int] = None,
is_read: Optional[bool] = None,
type: Optional[str] = None,
page: int = 1,
page_size: int = 20
) -> List[Notification]:
query = select(Notification)
if user_id:
query = query.where(Notification.user_id == user_id)
if is_read is not None:
query = query.where(Notification.is_read == is_read)
if type:
query = query.where(Notification.type == type)
query = query.order_by(Notification.created_at.desc())
query = query.offset((page - 1) * page_size).limit(page_size)
result = await self.db.execute(query)
return list(result.scalars().all())
async def create(self, data: dict) -> Notification:
notification = Notification(**data)
self.db.add(notification)
await self.db.flush()
return notification
async def mark_as_read(self, notification_id: int) -> bool:
notification = await self.get_by_id(notification_id)
if not notification:
return False
notification.is_read = True
return True
async def mark_all_as_read(self, user_id: int) -> int:
result = await self.db.execute(
select(Notification).where(
Notification.user_id == user_id,
Notification.is_read == False
)
)
notifications = list(result.scalars().all())
for notification in notifications:
notification.is_read = True
return len(notifications)
async def delete(self, notification_id: int) -> bool:
notification = await self.get_by_id(notification_id)
if not notification:
return False
await self.db.delete(notification)
return True
+44
View File
@@ -0,0 +1,44 @@
from typing import Optional, List
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.user import User
class UserRepository:
def __init__(self, db: AsyncSession):
self.db = db
async def get_by_id(self, user_id: int) -> Optional[User]:
result = await self.db.execute(select(User).where(User.id == user_id))
return result.scalar_one_or_none()
async def get_by_username(self, username: str) -> Optional[User]:
result = await self.db.execute(select(User).where(User.username == username))
return result.scalar_one_or_none()
async def get_all(self) -> List[User]:
result = await self.db.execute(select(User))
return list(result.scalars().all())
async def create(self, data: dict) -> User:
user = User(**data)
self.db.add(user)
await self.db.flush()
return user
async def update(self, user_id: int, data: dict) -> Optional[User]:
user = await self.get_by_id(user_id)
if not user:
return None
for key, value in data.items():
setattr(user, key, value)
await self.db.flush()
return user
async def delete(self, user_id: int) -> bool:
user = await self.get_by_id(user_id)
if not user:
return False
await self.db.delete(user)
return True