添加接口
This commit is contained in:
parent
2bce8140d5
commit
78410f550d
@ -1,9 +1,11 @@
|
||||
from typing import List, Optional
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Body
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from app.core.database import get_db
|
||||
from app.crud.event import event
|
||||
from app.schemas.event import EventList, EventDetail, EventUpdate, EventQuery
|
||||
from app.schemas.event import EventList, EventDetail, EventUpdate, EventQuery, BackStageEvent, BackStageEventDto, BackStageEventDetail
|
||||
|
||||
import datetime
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@ -34,6 +36,28 @@ async def get_events(
|
||||
events = await event.get_multi_with_query(db, query=query)
|
||||
return [EventList.model_validate(event) for event in events]
|
||||
|
||||
# 后台获取事件列表
|
||||
@router.post("/backstageEventlist", response_model=List[BackStageEvent])
|
||||
async def get_events_backstage(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
query: BackStageEventDto = Body(...)
|
||||
|
||||
):
|
||||
|
||||
events = await event.get_multi_backstage_events(db, query=query)
|
||||
return events
|
||||
|
||||
# 后台获取事件详情
|
||||
@router.get("/backstageEventDetail/{eventId}")
|
||||
async def get_event_detail(
|
||||
eventId: str,
|
||||
db: AsyncSession = Depends(get_db)
|
||||
):
|
||||
back = await event.get_event_detail(db=db, eventId=eventId)
|
||||
return back
|
||||
|
||||
|
||||
|
||||
@router.get("/events/{event_id}", response_model=EventDetail)
|
||||
async def get_event(
|
||||
event_id: str,
|
||||
|
||||
@ -4,7 +4,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import selectinload
|
||||
from app.crud.base import CRUDBase
|
||||
from app.models.models import Event, Image, Temperature
|
||||
from app.schemas.event import EventUpdate, EventQuery
|
||||
from app.schemas.event import EventUpdate, EventQuery, BackStageEvent, BackStageEventDto, BackStageEventDetail
|
||||
|
||||
class CRUDEvent(CRUDBase[Event, EventUpdate, EventUpdate]):
|
||||
async def get_by_id(self, db: AsyncSession, *, event_id: str) -> Optional[Event]:
|
||||
@ -19,6 +19,62 @@ class CRUDEvent(CRUDBase[Event, EventUpdate, EventUpdate]):
|
||||
)
|
||||
result = await db.execute(query)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
async def get_event_detail(
|
||||
self,
|
||||
db: AsyncSession,
|
||||
*,
|
||||
eventId: str
|
||||
) -> Optional[BackStageEventDetail]:
|
||||
stmt = (select(Event.eventId, Event.number, Event.name, Temperature.temperature, Temperature.status, Temperature.createTime).
|
||||
select_from(Event).
|
||||
outerjoin(Temperature, Event.eventId==Temperature.eventId).
|
||||
where(Event.eventId==eventId)
|
||||
)
|
||||
|
||||
result = await db.execute(stmt)
|
||||
result = result.mappings().first()
|
||||
|
||||
return result
|
||||
|
||||
async def get_multi_backstage_events(
|
||||
self,
|
||||
db: AsyncSession,
|
||||
*,
|
||||
query: BackStageEventDto
|
||||
) -> List[BackStageEvent]:
|
||||
"""后台根据条件获取事件列表"""
|
||||
|
||||
conditions = []
|
||||
|
||||
if query.eventId is not None:
|
||||
conditions.append(Event.eventId==query.eventId)
|
||||
if query.number is not None:
|
||||
conditions.append(Event.number == query.number)
|
||||
if query.name is not None:
|
||||
conditions.append(Event.name == query.name)
|
||||
if query.status is not None:
|
||||
conditions.append(Temperature.status == query.status)
|
||||
if query.start_time is not None:
|
||||
conditions.append(Temperature.createTime >= query.start_time)
|
||||
if query.end_time is not None:
|
||||
conditions.append(Temperature.createTime <= query.end_time)
|
||||
|
||||
stmt = (select(Event.eventId, Event.number, Event.name, Image.imageUrl, Image.localPath, Temperature.temperature, Temperature.confidence, Temperature.createTime, Temperature.status)
|
||||
.select_from(Event)
|
||||
.outerjoin(Image, Event.eventId==Image.eventId)
|
||||
.outerjoin(Temperature, Image.imageId == Temperature.imageId)
|
||||
.where(*conditions)
|
||||
.offset(query.skip)
|
||||
.limit(query.limit))
|
||||
|
||||
result = await db.execute(stmt)
|
||||
|
||||
result = result.mappings().all()
|
||||
|
||||
return [BackStageEvent(**row) for row in result]
|
||||
|
||||
|
||||
|
||||
async def get_multi_with_query(
|
||||
self,
|
||||
|
||||
@ -90,7 +90,7 @@ class Temperature(Base):
|
||||
eventId = Column(String(50), ForeignKey('event.eventId'), nullable=False, comment='关联事件ID')
|
||||
imageId = Column(BigInteger, ForeignKey('image.imageId'), nullable=False, comment='关联图片ID')
|
||||
temperature = Column(String(20), nullable=False, comment='温度值')
|
||||
# status = Column(String(2), comment='温度是否正常')
|
||||
status = Column(String(5), comment='温度是否正常')
|
||||
confidence = Column(String(40), nullable=False, comment='识别置信度')
|
||||
createTime = Column(DateTime, default=datetime.now, nullable=False, comment='创建时间')
|
||||
|
||||
|
||||
@ -99,4 +99,45 @@ class EventQuery(BaseModel):
|
||||
skip: int = 0
|
||||
limit: int = 100
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
|
||||
# 后台获取事件列表
|
||||
class BackStageEvent(BaseModel):
|
||||
eventId: str
|
||||
number: Optional[str] = None
|
||||
name : Optional[str] = None
|
||||
imageUrl : Optional[str] = None
|
||||
localPath: Optional[str] = None
|
||||
temperature: Optional[str] = None
|
||||
confidence : Optional[str] = None
|
||||
status: Optional[str] = None
|
||||
createTime: Optional[datetime] = None
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
# 后台获取事件DTO
|
||||
class BackStageEventDto(BaseModel):
|
||||
eventId: Optional[str] = None
|
||||
number: Optional[str] = None
|
||||
name: Optional[str] = None
|
||||
start_time : Optional[str] = None
|
||||
end_time : Optional[str] = None
|
||||
status: Optional[str] = None
|
||||
skip: int = 0
|
||||
limit: int = 100
|
||||
|
||||
# 后台查看事件详情
|
||||
class BackStageEventDetail(BaseModel):
|
||||
eventId:str = None
|
||||
number: Optional[str] = None
|
||||
name: Optional[str] = None
|
||||
createTime: Optional[str] = None
|
||||
temperature: Optional[str] = None
|
||||
status: Optional[str] = None
|
||||
reason: Optional[str] = None
|
||||
remark: Optional[str] = None
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,6 +1,10 @@
|
||||
import asyncio
|
||||
from app.services.event_sync_service import run_sync, run_sync_event
|
||||
|
||||
'''
|
||||
手动同步事件
|
||||
'''
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("启动事件同步服务...")
|
||||
# asyncio.run(run_sync())
|
||||
|
||||
Loading…
Reference in New Issue
Block a user