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.

616 lines
24 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@Date:2022/07/26 11:19:30
'''
import datetime
import os
import json
from flask import jsonify, request, g, send_from_directory
from flask import Blueprint
from db_logic.client import BllClient
from db_logic.dangerous import BllDangerous
from sqlalchemy import and_
from db_logic.medicament import BllMedicament
from db_logic.medicament_record import BllMedicamentRecord
from db_logic.msds import BllMsDs
from db_logic.user_medicament import BllUserMedicament
from db_logic.medicament_relation_image import BllMedicamentRelationImage
from models.medicament_models import EntityMedicament, EntityMedicamentRelationImage
from models.client_models import EntityClient
from Common.Utils import DrugStatus, PageParam, Utils
from Common.auth import token_auth
from db_logic.user_temporary_auth import BllUserTemporaryAuth
from Common.GaoPaiYi import GaoPaiYi
from Common.Utils import DooropenUser
from db_logic.meidcament_variety import BllMedicamentVariety
from models.medicament_models import EntityMedicamentVariety
from Common.create_barcode import CreateBarcode
drug_router = Blueprint("drug", __name__)
# 试剂扫码领用
@drug_router.route("/use", methods=["GET", "POST"])
@token_auth.login_required
def drug_collect():
# 获取参数
bar_code = request.values.get("bar_code")
func_type = request.values.get("func_type")
notes = request.values.get("notes","")
# user_id=request.values.get("user_id")
# 默认用户id后续从token 中获取
# user_id = '4cea74ee-0d8b-11ed-943e-f47b094925e1'
# 强制使用默认0不强制1强制
force_use = request.values.get("force_use")
force_use = force_use if force_use != '' else '0'
# 获取条码试剂
drug_entity = BllMedicament().findEntity(EntityMedicament.bar_code==bar_code)
# 条码实际逻辑判段
if not drug_entity:
data = Utils.false_return(msg="药剂条码无效")
elif drug_entity.status not in [DrugStatus.Normal, DrugStatus.Empty]:
data = Utils.false_return(msg="药剂未在库")
elif drug_entity.status == DrugStatus.Empty:
data = Utils.false_return(msg="药剂已为空瓶")
elif str(drug_entity.func_type) != str(func_type):
data = Utils.false_return(msg="试剂不属于这里当前管理")
elif str(func_type) != "3" and (drug_entity.remain == '' or drug_entity.remain == None or not drug_entity):
data = Utils.false_return(msg="试剂未称重,请先称重!")
else:
customer_id = drug_entity.customer_id
client_id = drug_entity.client_id
# 获取临期的试剂
drug_obj = BllMedicament().getDrugNearExpired(drug_entity.variety_id, customer_id)
# date_str = lambda x: datetime.datetime.strptime('', "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%d")
# drug_obj_date_str = date_str(drug_obj.expiration_date)
drug_obj_date_str = None
if drug_obj.expiration_date:
drug_obj_date_str = datetime.datetime.strptime(drug_obj.expiration_date, "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%d")
# drug_entity_date_str = date_str(drug_entity.expiration_date)
drug_entity_date_str = None
if drug_entity.expiration_date:
drug_entity_date_str = datetime.datetime.strptime(drug_entity.expiration_date, "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%d")
if drug_obj.medicament_id != drug_entity.medicament_id and force_use == "False" and drug_obj_date_str != drug_entity_date_str:
data = Utils.false_return(status=2, msg=f'"{drug_entity.name}({drug_entity.bar_code})"此类药有更接近保质期的实例"{drug_obj.bar_code}", 存在于"{drug_obj.client_code}"号终端')
else:
# 领取试剂
# user_info = BllUser().findEntity(EntityUser.user_id==user_id)
user_info = g.current_user
drug_entity.by_user_date = Utils.get_str_datetime()
drug_entity.by_user_id=user_info.user_id
drug_entity.by_user_name=user_info.real_name
drug_entity.status=DrugStatus.Out
# print(Utils.to_dict(drug_entity))
# for k,v in drug_entity:
# print(k,v)
# 试剂领用--事务逻辑处理
BllMedicament().drugUse(
drug_entity,
BllClient().findEntity(EntityClient.client_id==client_id),
user_info,
notes =notes
)
# 试剂领用后有效期更改
BllMedicament().update_expiration_date(drug_entity)
data = Utils.true_return(msg='领用成功', data=Utils.to_dict(drug_entity))
return jsonify(data)
# 试剂归还
@drug_router.route("/drug_return", methods=["GET", "POST"])
@token_auth.login_required
def drug_return():
# 获取参数
bar_code = request.values.get("bar_code")
func_type = request.values.get("func_type")
# place = request.values.get("place")
# user_id = request.values.get("user_id")
remain = request.values.get("remain")
# 默认用户id后续从token 中获取
# user_id = '4cea74ee-0d8b-11ed-943e-f47b094925e1'
# 根据条码查询试剂信息
drug_info = BllMedicament().findEntity(EntityMedicament.bar_code==bar_code)
if not drug_info:
data = Utils.false_return(msg="药剂条码无效")
elif drug_info.status != DrugStatus.Out:
data = Utils.false_return(msg="此药剂未被领用")
elif drug_info.func_type != func_type:
data = Utils.false_return(msg="试剂不属于这里当前管理")
else:
# 药剂归还,进入归还事务处理
if drug_info.client_id not in ['72e70542-b70d-11e8-aea5-448a5bc6c411','72e70542-b70d-11e8-aea5-448a5bc6c412','72e70542-b70d-11e8-aea5-448a5bc6c413','72e70542-b70d-11e8-aea5-448a5bc6c414','72e70542-b70d-11e8-aea5-448a5bc6c415','72e70542-b70d-11e8-aea5-448a5bc6c416','72e70542-b70d-11e8-aea5-448a5bc6c417']:
drug_info.status = DrugStatus.Normal
drug_info.remain = remain
# if place:
# drug_info.client_id = place
# user_info = BllUser().findEntity(EntityUser.user_id == user_id)
user_info = g.current_user
user_id = user_info.user_id
user_id_pt = user_id
BllUserTemporaryAuth().create_temporary_auth(user_ids=[user_id, user_id_pt], auth_type=3)
result = Utils.to_dict(drug_info)
remain_result = BllMedicament().drugReturn(drug_info, BllClient().findEntity(drug_info.client_id), user_info)
result.update(remain_result)
print("L", result)
data = Utils.true_return(msg="药剂归还成功", data=result)
return jsonify(data)
# 试剂领用/归还,列表展示
@drug_router.route("/use_or_return_list", methods=["GET", "POST"])
@token_auth.login_required
def drug_use_retur_list():
status = request.values.get("status")
func_type = request.values.get("func_type")
page = int(request.values.get("page", 1))
page_size = int(request.values.get("page_size", 10))
page_param = PageParam(page, page_size)
# 判断用户是否为管理员(管理员要能看到所有领用/归还列表数据)
# 普通危化品所有人要能看到所有待归还列表
is_admin = True if g.current_user.role_name == "管理员" else False
if str(func_type) == "2" and str(status) == "2":
is_admin = True
data = BllMedicament().drug_use_return(status=status, func_type=func_type, user=g.current_user, page_param=page_param,
is_admin=is_admin)
return jsonify(Utils.true_return(data={"total_count": page_param.totalRecords, "data": Utils.msyql_table_model(data)}))
@drug_router.route("/variety/get_list", methods=["POST"])
@token_auth.login_required
def get_seach_variety():
seach_word = request.values.get("seach_word")
page = request.values.get("page", 1)
func_type = request.values.get("func_type")
page_size = request.values.get("page_size", 10)
page_param = PageParam(int(page), int(page_size))
data_list = BllMedicamentVariety().get_seach_info(seach_word=seach_word, page_param=page_param, func_type=func_type)
return jsonify(
Utils.true_return(
data={
"data_list":Utils.msyql_table_model(data_list),
"total_count": page_param.totalRecords
}
)
)
@drug_router.route("/variety/update", methods=["POST"])
@token_auth.login_required
def update_variety():
variety_id = request.values.get("variety_id")
db_obj = BllMedicamentVariety().findEntity(EntityMedicamentVariety.variety_id == variety_id)
if not db_obj:
return jsonify(Utils.false_return(msg="品类id有误"))
finds_list = dir(EntityMedicamentVariety)
for i in finds_list:
va = request.values.get(i)
if not va:
continue
setattr(db_obj, i, va)
try:
BllMedicamentVariety().update(db_obj)
return jsonify(Utils.true_return(msg="修改成功"))
except Exception:
return jsonify(Utils.false_return(msg="修改有误"))
# 试剂称重展示试剂详细信息
@drug_router.route("/get_drug_info", methods=["GET", "POST"])
@token_auth.login_required
def get_drug_info():
bar_code = request.values.get("bar_code")
func_type = request.values.get("func_type")
bar_code, new_code = Utils.get_bar_code_reverse(bar_code)
data = BllMedicament().execute(
f"select * from rms_medicament where func_type={func_type} and (bar_code like '%{bar_code}%' or bar_code like '%{new_code}%') "
).fetchall()
data = Utils.msyql_table_model(data)
if data:
return jsonify(Utils.true_return(data=data[0]))
else:
return jsonify(Utils.false_return(msg="条码有误"))
# 试剂管理
@drug_router.route("/drug_list", methods=["GET", "POST"])
@token_auth.login_required
def get_drug_manage():
seach_word = request.values.get("seach_word")
b_code = request.values.get("b_code")
client_id = request.values.get("client_id")
func_type = request.values.get("func_type")
page = int(request.values.get("page", 1))
page_size = int(request.values.get("page_size", 10))
page_param = PageParam(page, page_size)
data_list = BllMedicament().get_drug_list(
seach_word=seach_word,
b_code=b_code,
client_id=client_id, func_type=func_type, page_param=page_param
)
return jsonify(Utils.true_return(data={"data": Utils.msyql_table_model(data_list), "total_count":page_param.totalRecords}))
# 编辑数据
@drug_router.route("/update", methods=["GET", "POST"])
@token_auth.login_required
def drug_update():
finds_list = dir(EntityMedicament)
medicament_id = request.values.get("medicament_id")
bar_code = request.values.get("bar_code")
medicament_obj = BllMedicament().findEntity(
EntityMedicament.medicament_id == medicament_id)
if medicament_id:
obj = BllMedicament().findEntity(EntityMedicament.bar_code == bar_code)
if obj.medicament_id == medicament_obj.medicament_id:
for i in finds_list:
va = request.values.get(i)
if va:
setattr(medicament_obj, i, request.values.get(i))
BllMedicament().update(medicament_obj)
return jsonify(Utils.true_return(msg="修改成功"))
else:
return jsonify(Utils.true_return(msg="条码不能重复"))
return jsonify(Utils.true_return(msg="试剂id不能为空"))
# 删除数据
@drug_router.route("/del", methods=["GET", "POST"])
@token_auth.login_required
def drug_del():
drug_id = request.values.get("medicament_id")
try:
BllMedicament().delete(EntityMedicament.medicament_id==drug_id)
return jsonify(Utils.true_return())
except Exception:
return jsonify(Utils.except_return(msg="删除失败"))
# 流转记录
@drug_router.route("/get_durg_record", methods=["GET", "POST"])
@token_auth.login_required
def get_durg_record():
medicament_id = request.values.get("medicament_id")
func_type = request.values.get("func_type")
page = int(request.values.get("page", 1))
page_size = int(request.values.get("page_size", 10))
page_param = PageParam(page, page_size)
data_list = BllMedicamentRecord().get_drug_flow_log(
medicament_id=medicament_id,
func_type=func_type,
page_param=page_param)
return jsonify(Utils.true_return(data={"data_list": Utils.msyql_table_model(data_list), "total_count":page_param.totalRecords}))
# 设置空瓶
@drug_router.route("/set_drug_empty_bottle", methods=["GET", "POST"])
@token_auth.login_required
def drug_set_empty_bottle():
drug_id = request.values.get("medicament_id")
empty_str = request.values.get("empty_str")
obj = BllMedicament()
drug_obj = obj.findEntity(EntityMedicament.medicament_id == drug_id)
remain_result = obj.calculate_use_drug(drug_obj, weight_remain=0)
if drug_obj:
if drug_obj.status == 3:
return jsonify(Utils.false_return(msg="该试剂为空瓶,无需设置"))
else:
drug_obj.status = 3
drug_obj.remain = 0
if empty_str:
drug_obj.remark11=empty_str
obj.update(drug_obj)
return jsonify(Utils.true_return(data=remain_result))
else:
return jsonify(Utils.false_return(msg="试剂id有误"))
# 修改试剂余量
@drug_router.route("/update_drug_remain", methods=["GET", "POST"])
@token_auth.login_required
def update_drug_remain():
drug_id = request.values.get("medicament_id")
remain = request.values.get("remain")
obj = BllMedicament().findEntity(EntityMedicament.medicament_id==drug_id)
if obj:
if not remain:
setattr(obj, "status", "3")
else:
setattr(obj, "remain", remain)
BllMedicament().update(obj)
return jsonify(Utils.true_return())
else:
return jsonify(Utils.false_return())
#################################
##验证通过后需要添加至试剂领用处。##
#################################
# 分配禁用用户
@drug_router.route("/user_ban_list", methods=["GET", "POST"])
@token_auth.login_required
def drug_ban_user_list():
seach_user = request.values.get("seach_user")
drug_id = request.values.get("drug_id")
page = int(request.values.get("page", 1))
page_size = int(request.values.get("page_size", 10))
page_param = PageParam(page, page_size)
data_list = BllUserMedicament().get_user_jin_medicament(
seach_user=seach_user, page_param=page_param,
drug_id=drug_id
)
return jsonify(Utils.true_return(data={"data": Utils.msyql_table_model(data_list), "total_count": page_param.totalRecords}))
# 确认禁用
@drug_router.route("/user_ban_confirm", methods=["GET", "POST"])
@token_auth.login_required
def drug_ban_user_confirm():
user_id = request.values.get("user_id")
drug_id = request.values.get("drug_id")
data = BllUserMedicament().inster_user_drug(user_id, drug_id)
if data:
return jsonify(Utils.true_return())
else:
return jsonify(Utils.false_return())
# # 获取用户禁用试剂列表
# @drug_router.route("/user_ban_drug_list", methods=["GET", "POST"])
# @token_auth.login_required
# def get_user_ban_drug_list():
# user_id = request.values.get("user_id")
# data_list = BllUserMedicament().get_user_drug_name(user_id)
# return jsonify(Utils.true_return(data=Utils.msyql_table_model(data_list)))
# 取消禁用试剂
@drug_router.route("/user_ban_relieve", methods=["GET", "POST"])
@token_auth.login_required
def user_ban_relieve():
user_id = request.values.get("user_id")
drug_id = request.values.get("drug_id")
data = BllUserMedicament().del_user_ban_drug(user_id=user_id, drug_id=drug_id)
if data:
return jsonify(Utils.true_return(msg="删除成功"))
else:
return jsonify(Utils.false_return(msg="删除失败"))
# msds数据展示
@drug_router.route("/msds_db_list", methods=["GET", "POST"])
@token_auth.login_required
def get_msds_list():
seach_word = request.values.get("seach_word")
page = int(request.values.get("page", 1))
page_size = int(request.values.get("page_size", 10))
page_param = PageParam(page, page_size)
data_list = BllMsDs().get_seach_list(seach_word=seach_word, page_param=page_param)
return jsonify(Utils.true_return(data={"data_list": Utils.msyql_table_model(data_list), "total_cout": page_param.totalRecords}))
# msds数据展示
@drug_router.route("/get_msds_drug", methods=["GET", "POST"])
@token_auth.login_required
def get_msds_drug():
seach_word = request.values.get("seach_word")
cas_number = request.values.get("cas_number")
data_list = BllMsDs().get_seach(seach_word=seach_word,cas=cas_number)
if data_list:
return jsonify(Utils.true_return(data={"data_list": Utils.msyql_table_model(data_list)}))
return jsonify(Utils.true_return(data={"data_list": []}))
# 危化品数据展示
@drug_router.route("/dangerous_db_list", methods=["GET", "POST"])
@token_auth.login_required
def get_dangerous_list():
seach_word = request.values.get("seach_word")
page = int(request.values.get("page", 1))
page_size = int(request.values.get("page_size", 10))
page_param = PageParam(page, page_size)
data_list = BllDangerous().get_seach_list(seach_word=seach_word, page_param=page_param)
return jsonify(Utils.true_return(data={"data_list": Utils.msyql_table_model(data_list), "total_cout": page_param.totalRecords}))
# 图片上传接口
@drug_router.route("/save_drug_img", methods=["POST"])
# @token_auth.login_required
def save_drug_img_info():
# 药剂类别
# variety_id = request.values.get("variety_id")
# medicament_id = request.values.get("medicament_id")
if request.values.get("selected"):
medicament_list = json.loads(request.values.get("selected"))
else:
medicament_list = [{
"variety_id": request.values.get("variety_id"),
"medicament_id": request.values.get("medicament_id"),
}]
file_base64 = request.files.get("file")
# try:
gaopaiyi_obj = GaoPaiYi()
img_path = f"{Utils.UUID()}.jpg"
file_base64.save(os.path.join(gaopaiyi_obj.path, img_path))
for medicament in medicament_list:
variety_id = medicament.get("variety_id")
medicament_id = medicament.get("medicament_id")
# 根据条码查询试剂信息
drug_info = BllMedicament().findEntity(
EntityMedicament.medicament_id == medicament_id)
if not drug_info:
return jsonify(Utils.false_return(msg="试剂信息有误"))
obj = EntityMedicamentRelationImage(
name=drug_info.name,
variety_id=variety_id,
medicament_id=medicament_id,
pic_url=img_path,
create_date=Utils.get_str_datetime(),
)
BllMedicamentRelationImage().insert(obj)
return jsonify(Utils.true_return(msg="上传成功"))
# 读取文件返回
@drug_router.route("/get_img_info/<img_path>", methods=["GET"])
def get_drug_img(img_path):
return send_from_directory(GaoPaiYi().path, img_path)
# 高拍仪接口
@drug_router.route("/use_gaopaiyi", methods=["GET", "POST"])
@token_auth.login_required
def use_gaopaiyi():
# 药剂类别
# variety_id = request.values.get("variety_id")
# medicament_id = request.values.get("medicament_id")
if request.values.get("selected"):
medicament_list = json.loads(request.values.get("selected"))
else:
medicament_list = [{
"variety_id": request.values.get("variety_id"),
"medicament_id": request.values.get("medicament_id"),
}]
pic_data = GaoPaiYi().getPic()
if pic_data['code'] == 0:
for medicament in medicament_list:
variety_id = medicament.get("variety_id")
medicament_id = medicament.get("medicament_id")
# 根据条码查询试剂信息
drug_info = BllMedicament().findEntity(
EntityMedicament.medicament_id == medicament_id)
obj = EntityMedicamentRelationImage(
name=drug_info.name,
variety_id=variety_id,
medicament_id=medicament_id,
pic_url=pic_data['path'],
create_date=Utils.get_str_datetime(),
)
BllMedicamentRelationImage().insert(obj)
data = Utils.true_return(msg="高拍仪拍照成功")
else:
data = Utils.false_return(msg=pic_data['msg'])
return jsonify(data)
# 获取药剂图片列表
@drug_router.route("/get_medicament_image", methods=["GET", "POST"])
# @token_auth.login_required
def get_medicament_image():
# 药剂类别
variety_id = request.values.get("variety_id")
medicament_id = request.values.get("medicament_id")
data_list = BllMedicamentRelationImage().findList(
EntityMedicamentRelationImage.medicament_id == medicament_id).all()
# data_list = Utils.resultAlchemyData(data_list)
data = Utils.true_return(msg='获取成功', data=data_list)
return jsonify(data)
# 删除药剂图片
@drug_router.route("/del_medicament_image", methods=["GET", "POST"])
@token_auth.login_required
def del_medicament_image():
# 药剂类别
image_id = request.values.get("image_id")
data_obj = BllMedicamentRelationImage().findEntity(
EntityMedicamentRelationImage.image_id == image_id)
if data_obj:
BllMedicamentRelationImage().delete(
EntityMedicamentRelationImage.image_id == image_id)
data = Utils.true_return(msg='删除成功')
else:
data = Utils.false_return(msg='未找到该图片')
return jsonify(data)
# 待入库试剂信息and 待归还试剂信息
@drug_router.route("/stay_putin_return_list", methods=["POST",'GET'])
def get_stay_putin_list():
client_id = request.values.get("client_id")
page = request.values.get("page",'1')
page_size = request.values.get("page_size",'10000')
status = request.values.get("status")
page_param = PageParam(int(page), int(page_size))
print(DooropenUser.user_id,status,'*************************************************')
data = BllMedicament().get_drug_type_list(status=int(status), client_id=client_id, page_param=page_param, user_id=DooropenUser.user_id)
data_list = Utils.msyql_table_model(data)
return jsonify(Utils.true_return(data={"data_list":data_list, "total_count": page_param.totalRecords}))
# 更改试剂状态
@drug_router.route("/update_drug_status", methods=["POST"])
@token_auth.login_required
def update_drug_status():
bar_code = request.values.get("bar_code")
status = request.values.get("status")
bar_code, new_code = Utils.get_bar_code_reverse(bar_code=bar_code)
obj = BllMedicament().findEntity(
EntityMedicament.bar_code == new_code
)
if not obj:
return jsonify(Utils.false_return(msg="试剂信息有误"))
obj.status = status
BllMedicament().update(obj)
return jsonify(Utils.true_return(msg="修改成功"))
@drug_router.route("/print_select_drug_code", methods=["POST"])
@token_auth.login_required
def print_select_drug_code():
"""
药剂管理打印已选药剂标签
:return:
"""
drug_list = request.values.get("selected")
for drug in json.loads(drug_list):
medicament_id = drug.get("medicament_id")
drug_obj = BllMedicament().findEntity(EntityMedicament.medicament_id == medicament_id)
if not drug_obj:
return jsonify(Utils.false_return(msg=f"{drug_obj.get('name')}试剂信息有误"))
client_obj = BllClient().findEntity(EntityClient.client_id == drug_obj.client_id)
new_dict = {
"name": drug_obj.name,
"purity": drug_obj.purity,
"standard_code": drug_obj.standard_code,
"remark12": drug_obj.remark12,
"client_name": client_obj.client_name if client_obj else '',
"flow_position_code": drug_obj.flow_position_code,
}
obj = CreateBarcode()
obj.create_drug_lobel_code(**new_dict)
return jsonify(Utils.true_return(msg="打印完成"))
@drug_router.route("/calculate_volume_weight", methods=["POST"])
@token_auth.login_required
def calculate_volume_weight():
"""
计算体积转化为质量 M = P * V
:param variety_id: 类别ID
:param input_volume: 输入体积
:return:
"""
variety_id = request.values.get("variety_id")
input_volume = request.values.get("input_volume")
medicament_variety_obj = BllMedicamentVariety().findEntity(
EntityMedicamentVariety.variety_id == variety_id)
weight = float(input_volume) * float(medicament_variety_obj.density)
return jsonify(Utils.true_return(msg="完成", data={"weight": Utils.reserve_decimal(weight)}))