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.
yy_rms_39zhiyao_duizhao/Common/SupervisionWarning.py

229 lines
12 KiB

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@Date:2022/07/14 08:36:50
'''
import threading
import time
from Business.Warning import BllWarning
from Business.MedicamentVariety import BllMedicamentVariety, Utils, datetime
from DataEntity.WarningModels import EntityWarning
class SupervisionWarning:
def __init__(self):
# 删除视频事件
self.deleteVideoDatetime = None
def supervision_start(self):
p = threading.Thread(target=self.supervision_start_thread)
p.start()
def insert_warning_default(self, customer_id, object_type, object_id, object_name, warning_content):
try:
warning_obj = EntityWarning(
warning_id=str(Utils.UUID()),
customer_id=customer_id,
object_type=object_type,
object_id=object_id,
object_name=object_name,
warning_content=warning_content,
warning_date=datetime.datetime.now(),
warning_user_name='系统',
is_solve=0,
is_add=1
)
bool_ = BllWarning().insert(warning_obj)
if bool_:
print('插入成功!')
except Exception as error:
print(error)
return
def warning_content_type(self, tp):
# 根据object_type俩进行消息区分
content_dict = {
"2": "药剂过期预警:过期药剂[%s](%s), 当前时间: %s; 超出设定过期时间%s; 位置:%s; 号柜%s",
"3": "药剂种类余量预警:药剂种类%s, 当前种类药剂余量%s瓶,小于设定余量%s瓶;",
"1": "药剂保质期预警:药剂 %s(%s) 超过保质期预警线, 当前时间:%s; 设定警戒线:%s天; 位置:%s号柜 %s",
"5": "%s 药柜滤芯保质期预警:药柜滤芯提前预警时间%s天,生产日期%s,当前时间:%s,保质期:%s天; 已达到提前预警时间;",
"6": "出库超期预警:用户[%s]已领用: %s(%s) %s天,超过预警线:%s"
}
return content_dict.get(tp, "")
def supervision_start_thread(self):
while True:
try:
BllWarning().executeNoParam("delete from rms_warning where object_type not in('7') ")
# 查询预警数量和某一类型当前在库数量
SQL = """
SELECT b.variety_id as variety_id, b.customer_id, b.Name, sum(case when a.status = 1 then 1 else 0 end) as normal_count, b.inventory_warning_value
from rms_medicament_variety as b LEFT JOIN rms_medicament as a on b.variety_id = a.variety_id GROUP by b.variety_id
"""
data_obj_list = BllMedicamentVariety().execute(SQL).fetchall()
for data_obj in data_obj_list:
if data_obj.inventory_warning_value:
if data_obj.inventory_warning_value > data_obj.normal_count:
# 封装conent统一化管理
# 封装创建warning方法统一化管理
# 药剂种类余量预警
object_type = 3
content_info = self.warning_content_type(
object_type)
self.insert_warning_default(
customer_id=data_obj.customer_id,
object_type=object_type,
object_id=data_obj.variety_id,
object_name=data_obj.name,
warning_content=content_info % (
data_obj.name,
str(data_obj.normal_count),
str(data_obj.inventory_warning_value)
)
)
time.sleep(1)
# SQL查询过期预警数量
SQL = """
select * from rms_medicament a where a.expiration_date < now();
"""
expire_obj_list = BllWarning().execute(SQL).fetchall()
if expire_obj_list:
for expire_obj in expire_obj_list:
if expire_obj.status != 3:
# 封装conent统一化管理
# 封装创建warning方法统一化管理
# 药剂过期预警
object_type = 2
conent = self.warning_content_type(object_type)
self.insert_warning_default(
customer_id=expire_obj.customer_id,
object_type=object_type,
object_id=expire_obj.medicament_id,
object_name=expire_obj.name,
warning_content=conent % (
expire_obj.name,
expire_obj.bar_code,
str(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S')),
str(expire_obj.expiration_date or ''),
str(expire_obj.client_code or ''),
str(expire_obj.place or '')
),
)
time.sleep(1)
# SQL查询保质期预警数量
SQL = """
select * from rms_medicament a where a.expiration_date < DATE_ADD(now(), INTERVAL a.shelf_life_warning_value DAY);
"""
expireIdList = [x.medicament_id for x in expire_obj_list]
shelflife_obj_list = BllWarning().execute(SQL).fetchall()
if shelflife_obj_list:
for expire_obj in shelflife_obj_list:
if (expire_obj.status != 3 and expire_obj.medicament_id not in expireIdList):
# 封装conent统一化管理
# 封装创建warning方法统一化管理
# 药剂保质期预警
object_type = 1
conent = self.warning_content_type(object_type)
self.insert_warning_default(
customer_id=expire_obj.customer_id,
object_type=object_type,
object_id=expire_obj.medicament_id,
object_name=expire_obj.name,
warning_content=conent % (
expire_obj.name,
expire_obj.bar_code,
str(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S')),
str(expire_obj.shelf_life_warning_value),
expire_obj.client_code,
(expire_obj.place if expire_obj.place else '')
)
)
time.sleep(1)
# SQL查询领用超期预警数量
todayDate = datetime.datetime.now().strftime("%Y-%m-%d")
SQL = """
SELECT DATEDIFF('"""+todayDate+"""',STR_TO_DATE(by_user_date,'%Y-%m-%d')) as user_days,
b.use_days_warning_value,a.by_user_name,a.`name`,a.bar_code,a.medicament_id,a.customer_id
FROM rms_medicament a LEFT JOIN rms_medicamentvariety b ON a.variety_id=b.variety_id
WHERE DATEDIFF('"""+todayDate+"""',STR_TO_DATE(by_user_date,'%Y-%m-%d'))>b.use_days_warning_value AND a.`status`=2;
"""
shelflife_obj_list = BllWarning().execute(SQL).fetchall()
if shelflife_obj_list:
for expire_obj in shelflife_obj_list:
# 封装conent统一化管理
# 封装创建warning方法统一化管理
# 出库超期预警
object_type = 6
conent = self.warning_content_type(object_type)
self.insert_warning_default(
customer_id=expire_obj.customer_id,
object_type=object_type,
object_id=expire_obj.medicament_id,
object_name=expire_obj.name,
warning_content=conent % (
expire_obj.by_user_name,
expire_obj.name,
expire_obj.bar_code,
str(expire_obj.user_days),
str(expire_obj.use_days_warning_value)
)
)
time.sleep(1)
# SQL查询药柜滤芯保质期预警
SQL = """
SELECT * FROM `rms_client`;
"""
filter_obj_list = BllWarning().execute(SQL).fetchall()
if filter_obj_list is not None:
for filter_obj in filter_obj_list:
if filter_obj.filter_production_date:
# 滤芯过期提前预警时间 生产日期 + 保质期 - 提前预警天数
filter_expire_date = filter_obj.filter_production_date + datetime.timedelta(days=(
filter_obj.filter_shelf_life - filter_obj.filter_shelf_life_warning_value))
if filter_expire_date <= datetime.datetime.now():
BllWarning_obj = BllWarning().findList(
EntityWarning.object_id == filter_obj.client_id).all()
if not BllWarning_obj:
# 封装conent统一化管理
# 封装创建warning方法统一化管理
# 药柜滤芯保质期预警
object_type = 5
conent = self.warning_content_type(
object_type)
self.insert_warning_default(
customer_id=filter_obj.customer_id,
object_type=object_type,
object_id=filter_obj.client_id,
object_name=filter_obj.client_name,
warning_content=conent % (
filter_obj.client_name,
str(filter_obj.filter_shelf_life_warning_value),
filter_obj.filter_production_date.strftime(
'%Y-%m-%d %H:%M:%S'),
str(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S')),
str(filter_obj.filter_shelf_life),)
)
time.sleep(1)
except Exception as e:
print(str(e))
# logUtil().debug('预警监测:'+traceback.format_exc())
time.sleep(3600)