|
|
# -*- coding:utf-8 -*-
|
|
|
"""
|
|
|
@Created on : 2023/7/5 10:49
|
|
|
@Author: hxl
|
|
|
@Des:
|
|
|
"""
|
|
|
from fastapi import APIRouter, Depends, Request
|
|
|
|
|
|
from endpoints.cab.drug import UpdateDictionary
|
|
|
from helper import login_required, respond_to
|
|
|
from models import Dictionary
|
|
|
from tortoise.queryset import QuerySet
|
|
|
from tortoise.expressions import Q
|
|
|
|
|
|
router = APIRouter(prefix='/dictionary', dependencies=[Depends(login_required)])
|
|
|
|
|
|
|
|
|
@router.get('/dictionaries', summary='获取试剂类别列表')
|
|
|
async def index(request: Request, keyword: str = '', page_no: int = 1, page_size: int = 10):
|
|
|
"""
|
|
|
获取药剂字典列表
|
|
|
:param keyword: 按药剂信息搜索
|
|
|
:param page_no: 分页页码,默认为1
|
|
|
:param page_size: 分页大小,默认为10
|
|
|
:return:
|
|
|
"""
|
|
|
query = QuerySet(Dictionary).filter(archive_id=request.state.archive_id)
|
|
|
if keyword:
|
|
|
keyword = keyword.strip()
|
|
|
query = query.filter(Q(k1__icontains=keyword) | Q(k2__icontains=keyword) | Q(k3__icontains=keyword)
|
|
|
| Q(k4__icontains=keyword) | Q(k5__icontains=keyword)
|
|
|
| Q(k6__icontains=keyword))
|
|
|
offset = (page_no - 1) * page_size
|
|
|
count = await query.count()
|
|
|
dictionary_obj = await query.limit(page_size).offset(offset).order_by('-created_at')
|
|
|
result_list = []
|
|
|
for obj in dictionary_obj:
|
|
|
item = dict(obj)
|
|
|
if obj.params:
|
|
|
for k, v in obj.params.items():
|
|
|
item[k] = v
|
|
|
item.pop("params")
|
|
|
result_list.append(item)
|
|
|
return respond_to(data={'data': result_list, 'count': count})
|
|
|
|
|
|
|
|
|
@router.put('/dictionary/{id}', summary='编辑试剂类别')
|
|
|
async def update(id: str, post: UpdateDictionary):
|
|
|
"""
|
|
|
编辑药剂字典
|
|
|
:param id: id
|
|
|
:param post:
|
|
|
:return:
|
|
|
"""
|
|
|
dictionary = await Dictionary.get(id=id)
|
|
|
dictionary.params = post.dict()
|
|
|
await dictionary.save()
|
|
|
return respond_to()
|
|
|
|
|
|
|
|
|
# 试剂盘存
|
|
|
@router.put('/dictionary/stock', summary='试剂盘存')
|
|
|
async def update(stock_list: list[dict]):
|
|
|
"""
|
|
|
试剂盘存
|
|
|
:param stock_list: 字典数据列表 [{'dictionary_id': '', 'count': 2}]
|
|
|
:return: 差异盘存返回前端
|
|
|
"""
|
|
|
difference_stock_list = list()
|
|
|
for stock in stock_list:
|
|
|
count = stock.get("count")
|
|
|
if not count:
|
|
|
continue
|
|
|
dictionary_obj = await Dictionary.get(id=stock.get('dictionary_id'))
|
|
|
if dictionary_obj.storage_count == count:
|
|
|
continue
|
|
|
else:
|
|
|
stock.update({
|
|
|
"difference": dictionary_obj.storage_count - count
|
|
|
})
|
|
|
difference_stock_list.append(stock)
|
|
|
return respond_to(data=difference_stock_list)
|