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.

138 lines
4.9 KiB

# -*- coding: utf-8 -*-
# @Time : 2023/11/20 13:29
# @Author : tx
# @File : basic.py
# @Description :
import datetime
from datetime import timedelta, timezone
from tortoise.queryset import Q
from conf.setting import setting
from models.drug import Drug, DrugStateEnum
from models.drug_use_log import DrugUseLog, DrugUseStateEnum
from models.user import User
from helper.log import record_log
class BasicStandard:
"""
标准流转基类
"""
async def write_log(self, operate_type, drug_use_dict):
"""药剂流转平台日志记录"""
drug_info = await self.drug_obj.attribute_drug_info()
print("记录日志 drug_info", drug_info)
drug_info_comment = "-".join(list(map(lambda x: str(x), drug_info.values())))
if operate_type == "in":
# 入库
record = {
"user_id": drug_use_dict.get("user_id"),
"users": drug_use_dict.get("users"),
"kind": "入库",
"comment": f"药剂入库,药剂信息:{drug_info_comment}"
}
elif operate_type == "return":
# 归还
record = {
"user_id": drug_use_dict.get("user_id"),
"users": drug_use_dict.get("users"),
"kind": "归还",
"comment": f"药剂归还,药剂信息:{drug_info_comment}"
}
else:
# 领用
record = {
"user_id": drug_use_dict.get("user_id"),
"users": drug_use_dict.get("users"),
"kind": "领用",
"comment": f"药剂领用,药剂信息:{drug_info_comment}"
}
await record_log(**record)
async def empty(self, rfid: str, weight: int = 0, terminal_id: str = setting.TERMINAL_ID):
'''空瓶处置'''
self.drug_obj = await Drug.get_or_none(Q(barcode=rfid) | Q(rfid=rfid))
drug_use_dict = {
"drug": self.drug_obj,
"state": DrugUseStateEnum.EMPTY,
"user_id": self.users[0],
"peer_ids": ",".join(self.users[1:]),
"users": await User.parse_id_user(self.users),
"alerted": False,
"weight": 0,
"terminal_id": terminal_id,
"use_weight": weight, # 用量为多少(药剂的所有余量?)
}
drug_use_log_obj = await DrugUseLog().create(**drug_use_dict)
# 更新药剂信息
drug_update_dict = {
"state": DrugStateEnum.EMPTY,
"remain_gross_weight": 0,
"position": None,
}
await self.drug_obj.update_from_dict(drug_update_dict)
await self.drug_obj.save()
receive_obj = await DrugUseLog.filter(drug=self.drug_obj.id, state=2).order_by('-created_at').first()
if receive_obj:
# 更新return_log_id字段
receive_obj.return_log_id = drug_use_log_obj.id
await receive_obj.save()
drug_use_dict.update({
"drug_use_log_id": drug_use_log_obj.id,
})
return drug_use_dict
async def open_update_expired_at(self, open_date):
"""
首次开瓶更新过期日期
- 新保质期 = 当前时间 + 开封保质期
- 与原保质期比较,谁更小,谁就是新保质期
:return:
"""
if not open_date:
return ""
print("首次开瓶更新过期日期")
# 根据最后领用人判断是否为首次开瓶
if self.drug_obj.last_receive_id or self.drug_obj.last_receive_at:
return ""
shelf_life_k = [i.get("key") for i in self.template_obj.xlsx if i.get("text") in "开封保质期(天)"]
shelf_life_key = shelf_life_k[0] if shelf_life_k else ""
if not shelf_life_key:
return ""
shelf_life_day = self.drug_obj.fill_json_content.get(shelf_life_key) # 开封保质期500天
if not shelf_life_day:
return ""
# 保质期 = 当前时间 + 开封保质期
# 与原保质期比较,谁更小,谁就是新保质期
open_date_aware = open_date.replace(tzinfo=timezone.utc)
new_expired_at = open_date_aware + timedelta(days=int(shelf_life_day))
if not self.drug_obj.expired_at or new_expired_at < self.drug_obj.expired_at:
return new_expired_at
return ""
async def basic_put_in(self, app, *args, **kwargs):
"""
归还/入库
:param app:
:param args:
:param kwargs:
:return:
"""
data = await app.put_in(*args, **kwargs)
return data
async def basic_take_out(self, app, *args, **kwargs):
data = await app.take_out(*args, **kwargs)
return data
async def basic_empty(self, app, *args, **kwargs):
data = await app.empty(*args, **kwargs)
return data
@staticmethod
def _now_strftime():
return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")