|
|
# -*- coding:utf-8 -*-
|
|
|
"""
|
|
|
@Created on : 2023/6/30 16:17
|
|
|
@Author: hxl
|
|
|
@Des:
|
|
|
"""
|
|
|
import httpx
|
|
|
|
|
|
from fastapi import APIRouter, Request, Depends
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
from helper import respond_to, login_required
|
|
|
from helper.log import logger_wrapper, logger
|
|
|
from models import Cabinet, Drawer, User, Archive
|
|
|
|
|
|
router = APIRouter(prefix='/cabinets', dependencies=[Depends(login_required)])
|
|
|
|
|
|
|
|
|
class UpdateCabinet(BaseModel):
|
|
|
label: str = None
|
|
|
type: int = 1
|
|
|
location: str = None
|
|
|
director: str = None
|
|
|
phone: str = None
|
|
|
model: str = None
|
|
|
archive_id: str
|
|
|
door: bool
|
|
|
|
|
|
class DeleteCabinetRequest(BaseModel):
|
|
|
cabinet_ids: list
|
|
|
|
|
|
|
|
|
class CreateCabinet(BaseModel):
|
|
|
label: str
|
|
|
type: int = 2 #1=智能柜 2=普通柜
|
|
|
location: str = None
|
|
|
director: str = None
|
|
|
phone: str = None
|
|
|
model: str = None
|
|
|
archive_id: str
|
|
|
|
|
|
|
|
|
@router.post('', summary='新增普通柜体')
|
|
|
async def create_cabinet(cabinet: CreateCabinet):
|
|
|
"""
|
|
|
新增普通柜体
|
|
|
:return:
|
|
|
"""
|
|
|
|
|
|
if not cabinet.label:
|
|
|
return respond_to(code=400, desc='柜体名称必填')
|
|
|
|
|
|
# 验证 archive 外键是否存在
|
|
|
archive_exists = await Archive.filter(id=cabinet.archive_id).exists()
|
|
|
if not archive_exists:
|
|
|
return respond_to(code=404, desc='试剂大类不存在')
|
|
|
|
|
|
cabinet_obj = await Cabinet.create(
|
|
|
label=cabinet.label,
|
|
|
location=cabinet.location,
|
|
|
director=cabinet.director,
|
|
|
phone=cabinet.phone,
|
|
|
model=cabinet.model,
|
|
|
type=cabinet.type,
|
|
|
archive_id=cabinet.archive_id if cabinet.archive_id else None, # 如果archive为空,则设置为None
|
|
|
)
|
|
|
|
|
|
for i in range(1, 10):
|
|
|
await Drawer.create(label=f'抽屉-{i}', line_no=0, cabinet_id = cabinet_obj.id)
|
|
|
|
|
|
return respond_to(desc=f'新增{cabinet.label}成功')
|
|
|
|
|
|
|
|
|
@router.get('/index', summary='柜体下拉列表')
|
|
|
async def index(request: Request):
|
|
|
"""
|
|
|
获取柜体下拉列表
|
|
|
:return:
|
|
|
"""
|
|
|
query = Cabinet.filter()
|
|
|
if request.state and request.state.archive_id:
|
|
|
query = query.filter(archive_id=request.state.archive_id)
|
|
|
cabinets = await query.all().values('id', 'label')
|
|
|
data = [{"label": cabinet['label'], "id": cabinet['id']} for cabinet in cabinets]
|
|
|
return respond_to(data=data)
|
|
|
@router.get('/all', summary='全部柜体下拉列表')
|
|
|
async def index(request: Request):
|
|
|
"""
|
|
|
获取柜体下拉列表
|
|
|
:return:
|
|
|
"""
|
|
|
cabinets = await Cabinet.filter().all().values('id', 'label')
|
|
|
data = [{"label": cabinet['label'], "id": cabinet['id']} for cabinet in cabinets]
|
|
|
return respond_to(data=data)
|
|
|
|
|
|
@router.get('', summary='柜体管理列表')
|
|
|
async def index(page_no: int = None, page_size: int = None, label: str = None, archive_id: str = None, type: str = None):
|
|
|
"""
|
|
|
柜体管理列表
|
|
|
:param page_no: int
|
|
|
:param page_size: int
|
|
|
:param label: str
|
|
|
:param archive_id: int
|
|
|
:param type: int 1=智能柜 2=普通柜
|
|
|
:return:
|
|
|
"""
|
|
|
query = Cabinet.all().prefetch_related('archive')
|
|
|
if label:
|
|
|
query = query.filter(label__contains=label)
|
|
|
if archive_id:
|
|
|
query = query.filter(archive=archive_id)
|
|
|
if type:
|
|
|
query = query.filter(type=type)
|
|
|
total_count = await query.count()
|
|
|
if page_no is not None and page_size is not None:
|
|
|
offset = (page_no - 1) * page_size
|
|
|
cabinets = await query.offset(offset).limit(page_size).order_by("-created_at")
|
|
|
else:
|
|
|
cabinets = await query.limit(total_count).order_by("-created_at")
|
|
|
cabinet_list = [
|
|
|
{
|
|
|
'id': cabinet.id,
|
|
|
'label': cabinet.label,
|
|
|
'model': cabinet.model,
|
|
|
'type': '智能柜' if cabinet.type == 1 else '普通柜',
|
|
|
'archive_id': cabinet.archive.id if cabinet.archive else None,
|
|
|
'archive_name': cabinet.archive.name if cabinet.archive else None,
|
|
|
'location': cabinet.location,
|
|
|
'director': cabinet.director,
|
|
|
'phone': cabinet.phone,
|
|
|
}
|
|
|
for cabinet in cabinets
|
|
|
]
|
|
|
return respond_to(data={"total_count": total_count, "cabinets": cabinet_list})
|
|
|
|
|
|
|
|
|
@router.put('/{cabinet_id}', summary='柜体编辑信息')
|
|
|
@logger_wrapper
|
|
|
async def update(cabinet_id: str, cabinet_data: UpdateCabinet):
|
|
|
"""
|
|
|
柜体信息编辑
|
|
|
:param cabinet_id: str
|
|
|
:param cabinet_data: ProfileEdit
|
|
|
:return:
|
|
|
"""
|
|
|
cabinet_obj = await Cabinet.get_or_none(id=cabinet_id)
|
|
|
if not cabinet_obj:
|
|
|
return respond_to(code=404, desc="柜体不存在")
|
|
|
archive_exists = await Archive.filter(id=cabinet_data.archive_id).exists()
|
|
|
if not archive_exists:
|
|
|
return respond_to(code=404, desc='试剂大类不存在')
|
|
|
# 更新柜体信息
|
|
|
# update_fields = {k: v for k, v in cabinet_data.dict().items() if v}
|
|
|
# await cabinet_obj.update_from_dict(update_fields).save()
|
|
|
|
|
|
return respond_to(200) # , desc=f"{cabinet_data.label}修改成功"
|
|
|
|
|
|
|
|
|
@router.delete('/delete', summary='删除普通柜体')
|
|
|
async def delete_cabinet(body: DeleteCabinetRequest):
|
|
|
"""
|
|
|
删除普通柜体
|
|
|
:param cabinet_id: str
|
|
|
:return:
|
|
|
"""
|
|
|
|
|
|
for cabinet_id in body.cabinet_ids:
|
|
|
cabinet = await Cabinet.get_or_none(id=cabinet_id)
|
|
|
if not cabinet:
|
|
|
return respond_to(code=400, desc='柜体不存在')
|
|
|
if cabinet.type == 1:
|
|
|
return respond_to(code=400, desc='智能柜无法删除')
|
|
|
|
|
|
await Cabinet.filter(id__in=body.cabinet_ids).delete()
|
|
|
return respond_to(desc='删除成功')
|
|
|
|
|
|
|
|
|
@router.get('/{cabinet_id}/{user_id}', summary='抽屉列表')
|
|
|
async def index(request: Request, cabinet_id: str, user_id: str):
|
|
|
"""
|
|
|
柜体管理列表
|
|
|
:param request: Request
|
|
|
:param cabinet_id: str
|
|
|
:return:
|
|
|
"""
|
|
|
cabinet = await Cabinet.get(id=cabinet_id)
|
|
|
# if not cabinet.matrix:
|
|
|
# return respond_to(400, '请先设置布局信息')
|
|
|
user = await User.get(id=user_id)
|
|
|
query = Drawer.filter(cabinet_id=cabinet.id)
|
|
|
drawers = await query.order_by("-rank", "created_at").values("id", "cabinet_id", "line_no", "label", "tags", "rank")
|
|
|
result = []
|
|
|
for drawer in drawers:
|
|
|
drawer['is_permission'] = True if str(drawer['id']) in user.drawers else False
|
|
|
result.append(drawer)
|
|
|
return respond_to(data=dict(drawers=result))
|
|
|
|
|
|
|
|
|
@router.patch('/{cabinet_id}', summary='柜体参数编辑')
|
|
|
async def update(cabinet_id: str, params: dict):
|
|
|
"""
|
|
|
柜体参数编辑
|
|
|
:param cabinet_id: str
|
|
|
:param params: dict
|
|
|
:return:
|
|
|
"""
|
|
|
cabinet = await Cabinet.get(id=cabinet_id)
|
|
|
url = f'http://{cabinet.ip}/api/cabinet/v1/setting'
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
try:
|
|
|
response = await client.post(url, json=params)
|
|
|
except Exception as e:
|
|
|
logger.error(f"下位机通讯失败:{str(e)}")
|
|
|
return respond_to(code=400, desc='下位机通讯失败')
|
|
|
|
|
|
if response.json()['code'] != 200:
|
|
|
return respond_to(code=405, desc='参数有误')
|
|
|
# cabinet.params = params
|
|
|
# await cabinet.save()
|
|
|
return respond_to()
|