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.
146 lines
5.5 KiB
146 lines
5.5 KiB
#!/usr/bin/env python
|
|
# encoding: utf-8
|
|
"""
|
|
@author: tx
|
|
@file: warehouse.py
|
|
@time: 2023/5/8 17:53
|
|
@desc:
|
|
"""
|
|
from datetime import datetime, timedelta
|
|
|
|
from fastapi import APIRouter, Depends, Request
|
|
from pydantic import BaseModel
|
|
|
|
from helper import login_required, respond_to
|
|
from helper.drug import gram_to_milligram
|
|
from helper.logger import logger
|
|
from helper.utils import timezone_now
|
|
from models import Template
|
|
from models.archive import Archive
|
|
from models.dictionary import Dictionary
|
|
from models.drug import Drug
|
|
from models.storage_form_item import StorageFormItem
|
|
from models.drawers import Drawer
|
|
from models.terminal import Terminal
|
|
from conf.setting import setting
|
|
from transfer.code_standard import CodeStandard
|
|
|
|
router = APIRouter(dependencies=[Depends(login_required)])
|
|
|
|
|
|
class StorageFormItemEdit(BaseModel):
|
|
fill_json_content: dict | None
|
|
|
|
|
|
@router.put('/{id}', summary='用户药剂模板条目更改')
|
|
async def update(id: str, model: StorageFormItemEdit):
|
|
"""
|
|
药剂模板条目更改
|
|
:param id: 用户药剂模板条目id
|
|
:param model: fill_json_content
|
|
:return:
|
|
"""
|
|
storage_form_item_obj = await StorageFormItem.get(id=id)
|
|
if not model.fill_json_content:
|
|
return respond_to(500, desc="传入参数有误")
|
|
storage_form_item_obj.fill_json_content = model.fill_json_content
|
|
await storage_form_item_obj.save(update_fields=['fill_json_content'])
|
|
return respond_to()
|
|
|
|
|
|
@router.post('/warehousing/binding/{archive_id}', summary="入库绑定上传")
|
|
async def binding_warehousing_drug(request: Request, archive_id: str, drug_list: list[dict]):
|
|
"""
|
|
绑定入库上传
|
|
用户选择货架cabinet_id,层drawer_id传递上来
|
|
:param request: Request
|
|
:param archive_id: str
|
|
:param drug_list: [{"fill_json_content": json, "bar_code": str, "cabinet_id", str, "drawer_id": str}]
|
|
:return: {"abnormal_drugs": List[Dict], "normal_drugs": List[Dict]}
|
|
"""
|
|
if not drug_list:
|
|
return respond_to(code=400, desc="绑定数据为空")
|
|
users = request.state.users
|
|
current_user = request.state.current_user
|
|
_storage_item_dict = dict()
|
|
normal_drugs, abnormal_drugs = list(), list()
|
|
drug_data = list()
|
|
template_obj = await Template.get(archive_id=archive_id).prefetch_related('archive')
|
|
is_in_weight = template_obj.archive.params.get("storage_require_weigh")
|
|
print(drug_list)
|
|
for drug in drug_list:
|
|
if not drug.get("bar_code") or not drug.get("fill_json_content"):
|
|
abnormal_drugs.append(drug)
|
|
continue
|
|
|
|
fill_json_content = drug["fill_json_content"]
|
|
print(fill_json_content)
|
|
print(is_in_weight)
|
|
k_args = {f'k{i}': fill_json_content.get(f'k{i}') for i in range(1, 7) if fill_json_content.get(f'k{i}')}
|
|
dictionary_obj = await Dictionary.get_or_none(**k_args)
|
|
if not dictionary_obj:
|
|
k_args["archive"] = template_obj.archive
|
|
k_args["params"] = {"lack_stock_count": 0, "expiration_alert": 0}
|
|
dictionary_obj = await Dictionary.create(**k_args)
|
|
|
|
if is_in_weight and not fill_json_content.get("remain_gross_weight"):
|
|
abnormal_drugs.append(drug)
|
|
logger.warning("[入库需称重]入库药剂条目未称重:{0}".format(drug))
|
|
continue
|
|
barcode = drug.get("bar_code").upper()
|
|
|
|
# 过期日期如果有填写(生产日期保质期),则添加过期日期字段
|
|
if fill_json_content.get("expire_date"):
|
|
expired_at = fill_json_content.get("expire_date")
|
|
|
|
elif not fill_json_content.get("produce_date") or not fill_json_content.get("shelf_life"):
|
|
expired_at = ""
|
|
else:
|
|
produce_date = fill_json_content.get("produce_date")
|
|
shelf_life = fill_json_content.get("shelf_life")
|
|
production_date = datetime.strptime(produce_date, '%Y-%m-%d')
|
|
shelf_life_date = timedelta(days=int(shelf_life))
|
|
expired_at = production_date + shelf_life_date
|
|
expired_at = expired_at.strftime('%Y-%m-%d')
|
|
|
|
mill_gross_weight = fill_json_content.get("remain_gross_weight")
|
|
|
|
|
|
# 位置信息
|
|
drawer_obj = await Drawer.get(id=drug.get("drawer_id")).prefetch_related("cabinet")
|
|
cabinet = drawer_obj.cabinet
|
|
position_str = f"{cabinet.label}-{drawer_obj.label}"
|
|
|
|
drug_dict = {
|
|
"dictionary_id": dictionary_obj.id,
|
|
"barcode": barcode,
|
|
"rfid": barcode, # rfid也保存为其条码编号
|
|
"fill_json_content": fill_json_content,
|
|
"remain_gross_weight": mill_gross_weight,
|
|
"bind_id": current_user.id,
|
|
"bind_at": timezone_now(),
|
|
"template": template_obj,
|
|
"expired_at": expired_at,
|
|
"cabinet_id": drug.get("cabinet_id"),
|
|
"drawer_id": drug.get("drawer_id"),
|
|
"position": position_str
|
|
}
|
|
drug_data.append(drug_dict)
|
|
|
|
# 药剂批量保存
|
|
for data in drug_data:
|
|
try:
|
|
print(data)
|
|
drug_obj = await Drug.create(**data)
|
|
|
|
# 壁挂终端入库操作调用
|
|
standard = CodeStandard(users, drug_obj)
|
|
await standard.put_in(barcode=drug_obj.barcode)
|
|
|
|
normal_drugs.append(data)
|
|
except Exception as e:
|
|
logger.error("药剂导入绑定异常:{0}".format(e))
|
|
abnormal_drugs.append(data)
|
|
desc = "药剂部分保存成功" if abnormal_drugs else "药剂保存成功"
|
|
return respond_to(code=200, desc=desc, data={"abnormal_drugs": abnormal_drugs, "normal_drugs": drug_data})
|