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.
241 lines
8.9 KiB
241 lines
8.9 KiB
# -*- coding:utf-8 -*-
|
|
"""
|
|
@Created on : 2023/10/09 9:00
|
|
@Author: cxw
|
|
@Des: 大屏
|
|
"""
|
|
from fastapi import APIRouter
|
|
from starlette.requests import Request
|
|
from starlette.templating import Jinja2Templates
|
|
from tortoise.queryset import Q
|
|
from tortoise.queryset import QuerySet
|
|
from datetime import timedelta,datetime
|
|
from conf import setting
|
|
from helper.utils import timezone_now
|
|
from fastapi.encoders import jsonable_encoder
|
|
from fastapi.responses import JSONResponse
|
|
from helper.tool import parse_datetime
|
|
from helper.drug import drug_near_already_expired
|
|
from models.archive import Archive
|
|
from models.drug import Drug, DrugStateEnum
|
|
from models.cabinet import Cabinet
|
|
from models.log import EnvironmentLogs
|
|
from models import Archive,DrugUseLog,Dictionary,User,DrugUseStateEnum
|
|
|
|
|
|
router = APIRouter(prefix='/tvshow')
|
|
templates = Jinja2Templates(directory="templates")
|
|
|
|
|
|
@router.get("/index")
|
|
async def read_item(request: Request):
|
|
return templates.TemplateResponse("index2.html",{"request": request})
|
|
|
|
|
|
#获取领用未归还预警
|
|
@router.get('/warningListJson', summary="领用未归还预警信息")
|
|
async def index():
|
|
"""
|
|
逾期未归还
|
|
使用期限在终端管理中配置,按照当日固定归还时间或每试剂使用时长
|
|
状态为出库状态药剂
|
|
最后领用时间与逾期时间比较,超过的为逾期未归还药剂
|
|
:return:
|
|
"""
|
|
# 逾期未归还需要根据大类来获取值
|
|
result = list()
|
|
drugs = await Drug.filter(state=DrugStateEnum.OUT).all()
|
|
for drug_obj in drugs:
|
|
cabinet = await Cabinet.filter(id =drug_obj.cabinet_id).first()
|
|
if not cabinet:
|
|
continue
|
|
archive_obj = await Archive.get(id=cabinet.archive_id)
|
|
return_fixed_at = archive_obj.params.get("return_fixed_at") # 每日几点归还
|
|
receive_use_duration = archive_obj.params.get("receive_use_duration") # 领用几小时后归还
|
|
if return_fixed_at:
|
|
today_end_time = datetime.now().strftime(f"%Y-%m-%d {return_fixed_at}")
|
|
now_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
print(now_time,today_end_time)
|
|
if drug_obj.last_receive_at.replace(tzinfo=None) > today_end_time:
|
|
continue
|
|
|
|
elif receive_use_duration:
|
|
sub_time = datetime.now() - timedelta(hours=int(receive_use_duration))
|
|
|
|
if not drug_obj.last_receive_at or drug_obj.last_receive_at.replace(tzinfo=None) > sub_time:
|
|
continue
|
|
|
|
drug_info = await drug_obj.attribute_drug_info()
|
|
result.append({
|
|
"warning_date":datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
|
"object_name":list(map(lambda x:str(x), drug_info.values()))[0],
|
|
"warning_content": await drug_obj.attribute_last_user()+'领用('+list(map(lambda x:str(x), drug_info.values()))[0]+':'+drug_obj.rfid+')未按时归还!',
|
|
"last_receive_at":parse_datetime(str(drug_obj.last_receive_at), "%Y-%m-%d %H:%M:%S"),
|
|
"user_name": await drug_obj.attribute_last_user()
|
|
})
|
|
|
|
return JSONResponse(content={"status": 0, "msg": '成功', "data": jsonable_encoder(result)})
|
|
|
|
|
|
async def get_already_expired():
|
|
drug_objs = await Drug.filter(state__in=[DrugStateEnum.IN, DrugStateEnum.OUT]).prefetch_related(
|
|
'dictionary','template').all()
|
|
result = await drug_near_already_expired(drug_objs, "already")
|
|
return result
|
|
|
|
|
|
# 获取过期信息
|
|
@router.get('/expireListJson', summary="过期信息提醒")
|
|
async def index():
|
|
"""
|
|
过期信息提醒
|
|
:return:
|
|
"""
|
|
result=list()
|
|
drugs = await get_already_expired()
|
|
for drug_obj in drugs:
|
|
drug_info =drug_obj['drug_info']
|
|
result.append({
|
|
"warning_date":datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
|
"object_name":list(map(lambda x:str(x), drug_info.values()))[0],
|
|
"warning_content": list(map(lambda x:str(x), drug_info.values()))[0]+'('+drug_obj["rfid"]+')位置:'+drug_obj["position"]
|
|
})
|
|
|
|
return JSONResponse(content={"status": 0, "msg": '成功', "data": jsonable_encoder(result)})
|
|
|
|
|
|
# 获取常用试剂前6种
|
|
@router.get('/get_use_top', summary="获取常用试剂")
|
|
async def index():
|
|
"""
|
|
获取常用试剂
|
|
:return:
|
|
"""
|
|
result=list()
|
|
dictionaries = await Dictionary.all().values()
|
|
for dictionary in dictionaries:
|
|
drug_use_count = await DrugUseLog.filter(drug__dictionary=dictionary['id'], state=2).count()
|
|
drug_info = ",".join([value for key, value in dictionary.items() if key.startswith('k') and value is not None])
|
|
result.append({'drug_info': drug_info, 'drug_use_count': drug_use_count})
|
|
|
|
return JSONResponse(content={"status": 0, "msg": '成功', "data": jsonable_encoder(result)})
|
|
|
|
|
|
# 种类统计
|
|
@router.get('/getAllStockCount', summary="种类统计")
|
|
async def index():
|
|
"""
|
|
种类统计
|
|
:return:
|
|
"""
|
|
data = {
|
|
'allCount':0,
|
|
'drugMarterialCount': 0,
|
|
'standardsCount': 0,
|
|
'dangerousCount': 0
|
|
}
|
|
archives = await Archive.all()
|
|
query = QuerySet(Drug).filter(state__in=[DrugStateEnum.IN])
|
|
for archive in archives:
|
|
cabinets_ids =await Cabinet.filter(archive_id =archive.id).values_list("id", flat=True)
|
|
count =await query.filter(cabinet_id__in=cabinets_ids).count()
|
|
if archive.name =='普通试剂':
|
|
data["drugMarterialCount"] =count
|
|
if archive.name =='危化品':
|
|
data["dangerousCount"] =count
|
|
if archive.name =='标准品':
|
|
data["standardsCount"] =count
|
|
data["allCount"] += count
|
|
|
|
return JSONResponse(content={"status": 0, "msg": '成功', "data": jsonable_encoder(data)})
|
|
|
|
|
|
# 最近领用记录
|
|
@router.get('/getUseList', summary="最近领用记录")
|
|
async def index():
|
|
"""
|
|
获取最近领用记录
|
|
:return:
|
|
"""
|
|
result=list()
|
|
query = QuerySet(Drug).filter(state__in=[DrugStateEnum.OUT],last_receive_at__lt=timezone_now().strftime('%Y-%m-%d %H:%M:%S')).order_by('-last_receive_at')
|
|
drugs =await query.all()
|
|
for drug_obj in drugs:
|
|
receive_user_obj = await User.get_or_none(id=drug_obj.last_receive_id)
|
|
drug_info =await drug_obj.attribute_drug_info()
|
|
result.append({
|
|
"by_user_date":drug_obj.last_receive_at,
|
|
"by_user_name":receive_user_obj.name,
|
|
"name": list(map(lambda x:str(x), drug_info.values()))[0]
|
|
})
|
|
|
|
return JSONResponse(content={"status": 0, "msg": '成功', "data": jsonable_encoder(result)})
|
|
|
|
|
|
# 使用频次占比
|
|
@router.get('/roomUsagePieJson', summary="使用频次占比")
|
|
async def index():
|
|
"""
|
|
使用频次占比
|
|
:return:
|
|
"""
|
|
result=list()
|
|
archives = await Archive.all()
|
|
query = DrugUseLog.filter(state__in=[DrugUseStateEnum.TAKE, DrugStateEnum.EMPTY])
|
|
for archive in archives:
|
|
cabinets_ids =await Cabinet.filter(archive_id =archive.id).values_list("id", flat=True)
|
|
count =await query.filter(cabinet_id__in=cabinets_ids).count()
|
|
# print(archive.name)
|
|
drug_type =''
|
|
if archive.name =='普通试剂':
|
|
drug_type='1'
|
|
if archive.name =='危化品':
|
|
drug_type='3'
|
|
if archive.name =='标准品':
|
|
drug_type='2'
|
|
if archive.name =='耗材':
|
|
drug_type='耗材'
|
|
result.append({
|
|
"name":drug_type,
|
|
"value":count,
|
|
})
|
|
|
|
return JSONResponse(content={"status": 0, "msg": '成功', "data": jsonable_encoder(result)})
|
|
|
|
|
|
# 一周温度
|
|
@router.get('/week', summary="一周温度")
|
|
async def index():
|
|
"""
|
|
一周温度
|
|
:return:
|
|
"""
|
|
result = {}
|
|
query = QuerySet(EnvironmentLogs).filter(
|
|
Q(right_temperature__isnull=False))
|
|
stime = datetime.now() - timedelta(days=7)
|
|
etime = datetime.today().strftime("%Y-%m-%d")
|
|
stime =stime.strftime("%Y-%m-%d")
|
|
query = query.filter(created_at__gte=stime, created_at__lte=etime).order_by('created_at')
|
|
environmentLogs = await query.all()
|
|
left_data = list()
|
|
right_data =list()
|
|
for environment in environmentLogs:
|
|
# print(environment.created_at.strftime("%Y-%m-%d"))
|
|
left_data.append(
|
|
[f"{environment.cabinet_id}_1", environment.left_temperature, environment.created_at.strftime("%Y-%m-%d")])
|
|
right_data.append([f"{environment.cabinet_id}_2",environment.right_temperature,environment.created_at.strftime("%Y-%m-%d")])
|
|
|
|
for item in left_data:
|
|
if result.get(item[0]):
|
|
result[item[0]].append([item[2][5:].replace('-','/'), item[1]])
|
|
else:
|
|
result[item[0]] = [[item[2][5:].replace('-','/'), item[1]]]
|
|
|
|
for item in right_data:
|
|
if result.get(item[0]):
|
|
result[item[0]].append([item[2][5:].replace('-','/'), item[1]])
|
|
else:
|
|
result[item[0]] = [[item[2][5:].replace('-','/'), item[1]]]
|
|
return JSONResponse(content={"status": 0, "msg": '成功', "data": jsonable_encoder(result)})
|