You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
5.4 KiB

2 weeks ago
from fastapi import APIRouter
from pydantic import BaseModel
from typing import Optional, List, Any
from starlette.requests import Request
from tortoise.queryset import QuerySet, Prefetch
import traceback
from models.warehouse import (
Vacancy,
Connection,
)
from helper import respond_to
from conf import setting
from tortoise.transactions import in_transaction
warehouse_router = APIRouter(prefix='/warehouse')
class VacancyM(BaseModel):
number: str
traynum: Optional[str] = None
memo: Optional[Any] = None
is_empty: Optional[str] = "false"
orderids: Optional[List[str]] = []
class ConnectionM(BaseModel):
name: str
vacancies: list[VacancyM]
memo: Optional[Any] = None
class SchelveM(BaseModel):
name: str
vacancies: list[VacancyM]
memo: Optional[Any] = None
class WarehouseM(BaseModel):
name: str
schelves: list[SchelveM]
memo: Optional[Any] = None
class WarehouseMClear(BaseModel):
name: str
schelvenames: Optional[list[str]] = []
class ConnectionM(BaseModel):
name: str
vacancies: list[VacancyM]
memo: Optional[Any] = None
class ConnectionMClear(BaseModel):
name: str
@warehouse_router.get("/")
def read_root():
return {"Hello": "World"}
@warehouse_router.post('/addcon', summary='仓库添加AGV库存')
async def createcon(request: Request, connectionM: ConnectionM):
# print("orderm: ", orderm)
print("connectionM: ", connectionM.name, connectionM.memo)
for vacancy in connectionM.vacancies:
print("vacancy ", vacancy.number)
async with in_transaction() as conn:
try:
connection = None
qs1 = await Connection.filter(name = connectionM.name, is_active = True, is_valid = True)
print("qs1: ", qs1, connectionM.name, connectionM.memo)
if len(qs1) > 0:
connection = qs1[0]
else:
connection = await Connection.create(
name = connectionM.name,
memo = connectionM.memo,
connection=conn
)
for vacancy in connectionM.vacancies:
vacancyObj = None
qs3 = await Vacancy.filter(number = vacancy.number, is_active = True, is_valid = True)
if len(qs3) > 0:
vacancyObj = qs3[0]
else:
vacancyObj = await Vacancy.create(
number = vacancy.number,
traynum = vacancy.traynum,
memo = vacancy.memo,
is_empty = True if "true" == vacancy.is_empty else False,
is_connect = True,
connection=conn
)
#add to connection
await connection.vacancies.add(vacancyObj)
except Exception as e:
# printing stack trace
traceback.print_exc()
msg = "Something wrong when creating connection: {}".format(e)
print(msg)
return respond_to(code=404, desc="失败", data={"state": msg})
return respond_to(data={"state": "success"})
@warehouse_router.post('/clearcon', summary='仓库清空AGV库存')
async def clearcon(request: Request, connectionMClear: ConnectionMClear):
# print("orderm: ", orderm)
print("connectionMClear: ", connectionMClear.name)
try:
connection = None
qs1 = await Connection.filter(name = connectionMClear.name, is_active = True, is_valid = True)
print("qs1: ", qs1)
if len(qs1) > 0:
connection = qs1[0]
else:
raise Exception("Cannot find connection name {}".format(connectionMClear.name))
await connection.fetch_related("vacancies")
for vacancy in connection.vacancies:
vacancy.is_valid = False
vacancy.is_active = False
await vacancy.save()
except Exception as e:
# printing stack trace
traceback.print_exc()
msg = "Something wrong when creating connection: {}".format(e)
print(msg)
return respond_to(code=404, desc="失败", data={"state": msg})
return respond_to(data={"state": "success"})
@warehouse_router.get('/listcon', summary="列出所有AGV库存")
async def index(request: Request, page_no: int = 1, page_size: int = 20):
"""
列出所有AGV库存
:param page_no: 1
:param page_size: 20
:return:
"""
offset = (page_no - 1) * page_size
query = QuerySet(Connection).filter(is_active=True).prefetch_related(
Prefetch('vacancies', queryset=Vacancy.filter(is_active = True))
)
count = await query.count()
connections = await query.limit(page_size).offset(offset)
resData = []
for connection in connections:
vacancies = await connection.vacancies.filter(is_active = True)
vacancyList = []
for vacancy in vacancies:
holes = await vacancy.holes.filter(is_active = True)
vacancyj = vacancy.as_json()
vacancyj["holes"] = holes
vacancyList.append(vacancyj)
connectionj = connection.as_json()
# connectionj["vacancies"] = vacancies
connectionj["vacancies"] = vacancyList
resData.append(connectionj)
return respond_to(code=200, data=dict(count=count, data=resData))