修改接口bug;

添加报表时间维度搜索
duizhaopin
apan 2 years ago
parent f24ee7a750
commit bbb2c6f9cd

@ -11,6 +11,7 @@ import platform
import hashlib import hashlib
import uuid import uuid
import psutil import psutil
import math
from config.SystemConfig import SystemConfig from config.SystemConfig import SystemConfig
# 判断当前系统是linux还是windows # 判断当前系统是linux还是windows
@ -39,6 +40,7 @@ class Utils(object):
def classify(num, all_num, fl=2): def classify(num, all_num, fl=2):
return str(round(float(round(float(num) / all_num, fl)) * 100)) + "%" return str(round(float(round(float(num) / all_num, fl)) * 100)) + "%"
# return str(round(float(round(math.ceil(num) / all_num, fl)) * 100)) + "%"
#获取唯一识别码 #获取唯一识别码
def UUID(): def UUID():

@ -15,30 +15,62 @@ from flask import Blueprint
report_router = Blueprint("report", __name__) report_router = Blueprint("report", __name__)
# 获取报表统计页面展示 # 获取报表统计页面展示
@report_router.route('/report_home', methods=["GET", "POST"]) # @report_router.route('/report_home', methods=["GET", "POST"])
def report_home_info(): # def report_home_info():
# # 试剂使用统计
# data_list = BllMedicamentRecord().get_drug_record_count()
# return jsonify(Utils.true_return(data=data_list))
# # 消耗统计
# @report_router.route("/drug_stock_use_classify", methods=["GET", "POST"])
# def get_drug_stock_use_classify():
# data_list, total_number = BllMedicament().get_drug_stock_use_classify()
# return jsonify(Utils.true_return(data={"total_count":total_number, "data":data_list}))
# # 试剂用量消耗
# @report_router.route("/drug_use_classify", methods=["GET", "POST"])
# def get_drug_use_classify():
# data_list = BllMedicamentRecord().report_home_drug_useing_classify()
# return jsonify(Utils.true_return(data=data_list))
# # 用户试剂消耗
# @report_router.route("/drug_user_use_info", methods=["GET", "POST"])
# def get_drug_user_use_number():
# data_list = BllMedicamentRecord().report_home_user_use_info()
# return jsonify(Utils.true_return(data=data_list))
# 报表统计主页
@report_router.route("/report_home", methods=["GET", "POST"])
def report_home_show():
resp_data = {
"drug_use_type_data": "",
"drug_stock_expend": {"count_number":0, "data":""},
"drug_use_expend":"",
"user_use_expend": ""
}
try:
obj_data = BllMedicamentRecord()
# 试剂使用统计 # 试剂使用统计
data_list = BllMedicamentRecord().get_drug_record_count() drug_use_type_data = obj_data.get_drug_record_count()
return jsonify(Utils.true_return(data=data_list)) # 消耗统计部分
drug_stock_expend, num_all = BllMedicament().get_drug_stock_use_classify()
# 消耗统计
@report_router.route("/drug_stock_use_classify", methods=["GET", "POST"])
def get_drug_stock_use_classify():
data_list, total_number = BllMedicament().get_drug_stock_use_classify()
return jsonify(Utils.true_return(data={"total_count":total_number, "data":data_list}))
# 试剂用量消耗 # 试剂用量消耗
@report_router.route("/drug_use_classify", methods=["GET", "POST"]) drug_use_expend = obj_data.report_home_drug_useing_classify()
def get_drug_use_classify():
data_list = BllMedicamentRecord().report_home_drug_useing_classify()
return jsonify(Utils.true_return(data=data_list))
# 用户试剂消耗 # 用户试剂消耗
@report_router.route("/drug_user_use_info", methods=["GET", "POST"]) user_use_expend = obj_data.report_home_user_use_info()
def get_drug_user_use_number(): resp_data["drug_use_type_data"] = drug_use_type_data
data_list = BllMedicamentRecord().report_home_user_use_info() resp_data["drug_stock_expend"] = {
return jsonify(Utils.true_return(data=data_list)) "count_number": num_all,
"data": drug_stock_expend
}
resp_data["drug_use_expend"] = drug_use_expend
resp_data["user_use_expend"] = user_use_expend
return jsonify(Utils.true_return(data=resp_data))
except Exception as error:
return jsonify(Utils.except_return(msg=f"error:{error}", data=resp_data))
@ -51,7 +83,8 @@ def get_stock_data_info():
page_size = int(request.values.get('page_size', 15)) page_size = int(request.values.get('page_size', 15))
page_param = PageParam(page, page_size) page_param = PageParam(page, page_size)
data_list = BllMedicament().get_stock_all_info( data_list = BllMedicament().get_stock_all_info(
name=name, page_param=page_param) name=name,
page_param=page_param)
data_list = Utils.msyql_table_model(data_list) data_list = Utils.msyql_table_model(data_list)
if data_list: if data_list:
return jsonify(Utils.true_return(data={"total_count": page_param.totalRecords, "data": data_list})) return jsonify(Utils.true_return(data={"total_count": page_param.totalRecords, "data": data_list}))
@ -64,10 +97,11 @@ def get_stock_data_info():
def drun_input_info(): def drun_input_info():
# 获取参数 # 获取参数
seach_word = request.values.get('seach_word') seach_word = request.values.get('seach_word')
# manufacturer = request.values.get("manufacturer") manufacturer = request.values.get("manufacturer")
# customer_id = request.values.get("customer_id") start_time = request.values.get("start_time")
# 获取配置文件内的客户id end_time = request.values.get("end_time")
customer_id = SystemConfig.getConfig("customer_id") # 客户id
customer_id = None
client_id = request.values.get("client_id") client_id = request.values.get("client_id")
# 分页处理 # 分页处理
page = int(request.values.get("page", 1)) page = int(request.values.get("page", 1))
@ -76,6 +110,9 @@ def drun_input_info():
# 获取数据 # 获取数据
data = BllMedicament().getAllDrugList( data = BllMedicament().getAllDrugList(
search_word=seach_word, search_word=seach_word,
manufacturer=manufacturer,
start_time=start_time,
end_time=end_time,
page_param=page_param, page_param=page_param,
customer_id=customer_id, customer_id=customer_id,
client_id=client_id client_id=client_id
@ -92,11 +129,17 @@ def drun_input_info():
def stock_loss_info(): def stock_loss_info():
# 获取参数 # 获取参数
seach_word = request.values.get('seach_word') seach_word = request.values.get('seach_word')
start_time = request.values.get('start_time')
end_time = request.values.get('end_time')
page = int(request.values.get("page", 1)) page = int(request.values.get("page", 1))
page_size = int(request.values.get("page_size", 10)) page_size = int(request.values.get("page_size", 10))
page_param = PageParam(page, page_size) page_param = PageParam(page, page_size)
# 获取数据结果 # 获取数据结果
data = BllMedicamentRecord().durg_stock_loss(seach_word, page_param=page_param) data = BllMedicamentRecord().durg_stock_loss(
seach_word=seach_word,
start_time=start_time,
end_time=end_time,
page_param=page_param)
return jsonify(Utils.true_return(data={"total_count": page_param.totalRecords, "data": data})) return jsonify(Utils.true_return(data={"total_count": page_param.totalRecords, "data": data}))
@ -106,11 +149,17 @@ def stock_loss_info():
def get_drug_use_expend(): def get_drug_use_expend():
# 参数获取 # 参数获取
seach_word = request.values.get('seach_word') seach_word = request.values.get('seach_word')
start_time = request.values.get('start_time')
end_time = request.values.get('end_time')
page = int(request.values.get("page", 1)) page = int(request.values.get("page", 1))
page_size = int(request.values.get("page_size", 10)) page_size = int(request.values.get("page_size", 10))
page_param = PageParam(page, page_size) page_param = PageParam(page, page_size)
# 获取数据 # 获取数据
data = BllMedicamentRecord().durg_useing_info(seach_word, page_param=page_param) data = BllMedicamentRecord().durg_useing_info(
seach_word=seach_word,
start_time=start_time,
end_time=end_time,
page_param=page_param)
return jsonify(Utils.true_return(data={"total_count": page_param.totalRecords, "data": data})) return jsonify(Utils.true_return(data={"total_count": page_param.totalRecords, "data": data}))
@ -119,12 +168,17 @@ def get_drug_use_expend():
def get_drug_user_use_expend(): def get_drug_user_use_expend():
# 参数获取 # 参数获取
seach_user = request.values.get('seach_user') seach_user = request.values.get('seach_user')
start_time = request.values.get('start_time')
end_time = request.values.get('end_time')
page = int(request.values.get("page", 1)) page = int(request.values.get("page", 1))
page_size = int(request.values.get("page_size", 10)) page_size = int(request.values.get("page_size", 10))
page_param = PageParam(page, page_size) page_param = PageParam(page, page_size)
# 获取数据 # 获取数据
data = BllMedicamentRecord().user_use_info( data = BllMedicamentRecord().user_use_info(
seach_user=seach_user, page_param=page_param) seach_user=seach_user,
start_time=start_time,
end_time=end_time,
page_param=page_param)
return jsonify(Utils.true_return(data={"total_count": page_param.totalRecords, "data": data})) return jsonify(Utils.true_return(data={"total_count": page_param.totalRecords, "data": data}))
@ -133,10 +187,17 @@ def get_drug_user_use_expend():
def drug_use_frequency(): def drug_use_frequency():
seach_word = request.values.get('seach_word') seach_word = request.values.get('seach_word')
client_id = request.values.get("client_id") client_id = request.values.get("client_id")
start_time = request.values.get('start_time')
end_time = request.values.get('end_time')
page = int(request.values.get("page", 1)) page = int(request.values.get("page", 1))
page_size = int(request.values.get("page_size", 10)) page_size = int(request.values.get("page_size", 10))
page_param = PageParam(page, page_size) page_param = PageParam(page, page_size)
data = BllMedicamentRecord().use_frequency(seach_word=seach_word, client_id=client_id, page_param=page_param) data = BllMedicamentRecord().use_frequency(
seach_word=seach_word,
start_time=start_time,
end_time=end_time,
client_id=client_id,
page_param=page_param)
return jsonify(Utils.true_return(data={"total_count": page_param.totalRecords, "data": data})) return jsonify(Utils.true_return(data={"total_count": page_param.totalRecords, "data": data}))
@ -149,14 +210,14 @@ def drug_log_type_info():
record_type = request.values.get("record_type") record_type = request.values.get("record_type")
put_in_user_name = request.values.get("user_name") put_in_user_name = request.values.get("user_name")
name = request.values.get("name") name = request.values.get("name")
# start_time = request.values.get("start_time") start_time = request.values.get("start_time")
# end_time = request.values.get("end_time") end_time = request.values.get("end_time")
page_param = PageParam(page, page_size) page_param = PageParam(page, page_size)
data_list = BllMedicament().drug_show_type_info( data_list = BllMedicament().drug_show_type_info(
record_type=record_type, put_in_user_name=put_in_user_name, statue_type=statue_type, record_type=record_type, put_in_user_name=put_in_user_name, statue_type=statue_type,
name=name, name=name,
# start_time=start_time, end_time=end_time, start_time=start_time, end_time=end_time,
page_param=page_param page_param=page_param
) )
data_list = Utils.msyql_table_model(data_list) data_list = Utils.msyql_table_model(data_list)

@ -67,13 +67,17 @@ class BllMedicament(Repository):
sql_all = """ sql_all = """
select count(medicament_id) count_number, `status` from rms_medicament GROUP BY `status` select count(medicament_id) count_number, `status` from rms_medicament GROUP BY `status`
""" """
# 查看分组数据
data = self.execute(sql_all).fetchall() data = self.execute(sql_all).fetchall()
# 计算总数
data_number = sum([i[0] for i in data]) data_number = sum([i[0] for i in data])
data_li = Utils.msyql_table_model(data) data_li = Utils.msyql_table_model(data)
data_list = [] data_list = []
# 计算百分比,组装数据
for i in data_li: for i in data_li:
new_dict = { new_dict = {
"ratio": str(round(i["count_number"] / data_number,2) * 100) + "%" # "ratio": str(round(i["count_number"] / data_number,2) * 100) + "%"
"ratio": Utils.classify(i['count_number'], data_number)
} }
new_dict.update(i) new_dict.update(i)
data_list.append(new_dict) data_list.append(new_dict)
@ -245,7 +249,6 @@ class BllMedicament(Repository):
filter_base = "" filter_base = ""
if name: if name:
filter_base = f" where `name` LIKE %'{name}'%" filter_base = f" where `name` LIKE %'{name}'%"
filter_base += " GROUP BY `name`, speci, purity" filter_base += " GROUP BY `name`, speci, purity"
sql_all = sql_all % filter_base sql_all = sql_all % filter_base
@ -262,13 +265,17 @@ class BllMedicament(Repository):
return sql_data return sql_data
#获取所有药剂列表 #获取所有药剂列表
def getAllDrugList(self, search_word, page_param, customer_id=None, client_id=None):
def getAllDrugList(self, search_word, manufacturer, start_time, end_time, page_param, customer_id=None, client_id=None):
# (Name like :searchWord or BarCode like :searchWord or EnglishName like :searchWord) # (Name like :searchWord or BarCode like :searchWord or EnglishName like :searchWord)
sql_all = """ sql_all = """
select `name`,speci, manufacturer, distributor, net_weight_unit, remain, production_date, expiration_date, select `name`, bar_code, status, speci, manufacturer, distributor, net_weight_unit, remain, production_date, expiration_date,
shelf_life, put_in_user_name, put_in_date, english_name, purity, is_supervise, by_user_name shelf_life, put_in_user_name, put_in_date, english_name, purity, is_supervise, by_user_name
from rms_medicament %s from rms_medicament %s
""" """
# manufacturer = manufacturer,
# start_time = start_time,
# end_time = end_time,
# 动态添加查询条件 # 动态添加查询条件
filter_base = "" filter_base = ""
if customer_id: if customer_id:
@ -277,12 +284,23 @@ class BllMedicament(Repository):
if filter_base: if filter_base:
filter_base += " and " filter_base += " and "
filter_base += f"client_id='{client_id}'" filter_base += f"client_id='{client_id}'"
# 模糊搜索 # 名称搜索
if search_word: if search_word:
seach_w = f"%{search_word}%" seach_w = f"%{search_word}%"
if filter_base: if filter_base:
filter_base += " and " filter_base += " and "
filter_base += f" `name` like '{seach_w}' or english_name like '{seach_w}' or manufacturer like '{seach_w}'" filter_base += f" `name` like '{seach_w}' or english_name like '{seach_w}' or manufacturer like '{seach_w}'"
# 厂商搜索
if manufacturer:
manufacturer = f"%{manufacturer}%"
if filter_base:
filter_base += " and "
filter_base += f"manufacturer like '{manufacturer}'"
# 时间范围搜索
if start_time and end_time:
if filter_base:
filter_base += " and "
filter_base += f"put_in_date >= '{start_time}' and put_in_date<='{end_time}'"
if filter_base: if filter_base:
filter_base = "where " + filter_base filter_base = "where " + filter_base
sql_all = sql_all % filter_base sql_all = sql_all % filter_base
@ -297,7 +315,7 @@ class BllMedicament(Repository):
return self.execute(sql_all).fetchall() return self.execute(sql_all).fetchall()
# 入库、领用、归还记录 # 入库、领用、归还记录
def drug_show_type_info(self, record_type, put_in_user_name, name, statue_type, page_param): def drug_show_type_info(self, record_type, start_time, end_time, put_in_user_name, name, statue_type, page_param):
sql_all = """ sql_all = """
SELECT SELECT
b.`name`, b.english_name, b.bar_code, b.purity, b.cas_number, a.use_quantity, b.`status`, a.create_date, a.create_user_name, b.client_id b.`name`, b.english_name, b.bar_code, b.purity, b.cas_number, a.use_quantity, b.`status`, a.create_date, a.create_user_name, b.client_id
@ -311,13 +329,14 @@ class BllMedicament(Repository):
filter_sql = f"WHERE record_type={record_type}" filter_sql = f"WHERE record_type={record_type}"
if put_in_user_name: if put_in_user_name:
filter_sql += f" and create_user_name like {put_in_user_name}" filter_sql += f" and create_user_name like '{put_in_user_name}'"
# if start_time and end_time: if start_time and end_time:
# filter_sql += f"and create_date >= {start_time} and create_date <= {end_time}" filter_sql += f" and create_date >= '{start_time}' and create_date <= '{end_time}'"
filter_sql_1 = '' filter_sql_1 = ''
if statue_type: if statue_type:
filter_sql_1 = f" where `status`={statue_type}" filter_sql_1 = f" where `status`={statue_type}"
if name: if name:
name = f"%{name}%"
if filter_sql_1: if filter_sql_1:
filter_sql_1 += " and" filter_sql_1 += " and"
else: else:
@ -325,8 +344,8 @@ class BllMedicament(Repository):
filter_sql_1 += f" `name` like '{name}' or english_name like '{name}'" filter_sql_1 += f" `name` like '{name}' or english_name like '{name}'"
sql_all = sql_all % (filter_sql, filter_sql_1) sql_all = sql_all % (filter_sql, filter_sql_1)
finds = ["name", "english_name", "bar_code", # finds = ["name", "english_name", "bar_code",
"purity", "cas_number", "use_quantity", "status", "create_date", "create_user_name", "client_id"] # "purity", "cas_number", "use_quantity", "status", "create_date", "create_user_name", "client_id"]
try: try:
count_number = len(self.execute(sql_all).fetchall()) count_number = len(self.execute(sql_all).fetchall())
except Exception: except Exception:

@ -80,31 +80,43 @@ class BllMedicamentRecord(Repository):
return dict(recordListNew) return dict(recordListNew)
def get_drug_log_time_name(self, record_type): def get_drug_log_time_name(self, record_type_list):
sql_all = f""" sql_all = """
select a.`name` `name`,a.medicament_id, b.create_date create_date from ( select a.`name` `name`,a.medicament_id, b.create_date create_date, b.record_type from (
select `name`, medicament_id from rms_medicament select `name`, medicament_id from rms_medicament
) a RIGHT JOIN( ) a RIGHT JOIN(
select medicament_id, create_date, record_type from rms_medicament_record WHERE record_type=%s ORDER BY create_date desc LIMIT 1
select medicament_id, create_date from rms_medicament_record WHERE record_type={record_type} ORDER BY create_date desc LIMIT 1
) b on a.medicament_id=b.medicament_id ) b on a.medicament_id=b.medicament_id
""" """
return self.execute(sql_all).fetchall()[0] sql_list = []
for record_type in record_type_list:
sql_list.append(
sql_all % record_type
)
sql_all = " UNION ".join(sql_list)
# print(sql_all)
return self.execute(sql_all).fetchall()
# 获取报表统计主页入库记录信息 # 获取报表统计主页入库记录信息
def get_drug_record_count(self): def get_drug_record_count(self):
sql_all = """ sql_all = """
select COUNT(record_id) count_number, record_type from rms_medicament_record GROUP BY record_type select COUNT(record_id) count_number, record_type from rms_medicament_record GROUP BY record_type
""" """
# 获取分类数量
data = self.execute(sql_all).fetchall() data = self.execute(sql_all).fetchall()
data_li = Utils.msyql_table_model(data) data_li = Utils.msyql_table_model(data)
data_list = [] data_list = []
# 获取时间维度最后一条名称及时间
name_info_list = self.get_drug_log_time_name([1,2,3])
name_info_dict = {}
for name in name_info_list:
name_info_dict[name[-1]] = name
# 组装数据
for i in data_li: for i in data_li:
name_date = self.get_drug_log_time_name(i["record_type"]) name_info = name_info_dict.get(i['record_type'])
new_dic = { new_dic = {
"name": name_date[0], "name": name_info[0],
"date_time": name_date[2] "date_time": name_info[2]
} }
new_dic.update(**i) new_dic.update(**i)
data_list.append(new_dic) data_list.append(new_dic)
@ -112,22 +124,26 @@ class BllMedicamentRecord(Repository):
# 获取报表统计主页试剂用量消耗 # 获取报表统计主页试剂用量消耗
def report_home_drug_useing_classify(self): def report_home_drug_useing_classify(self):
# select a.count_id count_number, b.`name` from (
# select count(record_id) count_id, medicament_id from rms_medicament_record GROUP BY medicament_id
# ) a LEFT JOIN(
# select `name`, medicament_id from rms_medicament
# ) b on a.medicament_id=b.medicament_id
sql_all = """ sql_all = """
select a.count_id count_number, b.`name` from ( select a.`name`, sum(b.count_id) count_num from (
select `name`, medicament_id, speci, purity from rms_medicament) a LEFT JOIN(
select count(record_id) count_id, medicament_id from rms_medicament_record GROUP BY medicament_id select count(record_id) count_id, medicament_id from rms_medicament_record GROUP BY medicament_id
) a LEFT JOIN( )b on a.medicament_id=b.medicament_id GROUP BY `name`, speci, purity
select `name`, medicament_id from rms_medicament
) b on a.medicament_id=b.medicament_id
""" """
data = self.execute(sql_all).fetchall() data = self.execute(sql_all).fetchall()
data_number = sum([i[0] for i in data]) data_number = sum([float(i[1]) for i in data])
data_li = Utils.msyql_table_model(data) data_li = Utils.msyql_table_model(data)
data_list = [] data_list = []
for i in data_li: for i in data_li:
new_dic = { new_dic = {
"ratio": Utils.classify(i["count_number"], data_number) "ratio": Utils.classify(i["count_num"], data_number)
} }
i["count_num"] = int(i["count_num"])
new_dic.update(**i) new_dic.update(**i)
data_list.append(new_dic) data_list.append(new_dic)
return data_list return data_list
@ -168,12 +184,11 @@ class BllMedicamentRecord(Repository):
new_dic[k] = v if not isinstance(v, Decimal) else float(v) new_dic[k] = v if not isinstance(v, Decimal) else float(v)
data_list.append(new_dic) data_list.append(new_dic)
if finds: if finds:
print(base_num)
base_num += float(new_dic.get(finds)) base_num += float(new_dic.get(finds))
return data_list, base_num return data_list, base_num
# 获取库存消耗数据 # 获取库存消耗数据
def durg_stock_loss(self, seach_word, page_param): def durg_stock_loss(self, seach_word, start_time, end_time, page_param):
sql_all = """ sql_all = """
select c.`name`, c.cas_number,c.speci, c.net_weight_unit, c.purity, c.enter_stock, select c.`name`, c.cas_number,c.speci, c.net_weight_unit, c.purity, c.enter_stock,
sum(CASE WHEN c.`status`=1 THEN 1 ELSE 0 END) in_stock, sum(CASE WHEN c.`status`=1 THEN 1 ELSE 0 END) in_stock,
@ -184,17 +199,21 @@ class BllMedicamentRecord(Repository):
select medicament_id, `name`, cas_number, speci, purity, net_weight_unit, `status` from rms_medicament select medicament_id, `name`, cas_number, speci, purity, net_weight_unit, `status` from rms_medicament
%s %s
) a LEFT JOIN( ) a LEFT JOIN(
SELECT medicament_id, sum(CASE WHEN record_type=1 THEN 1 ELSE 0 END) enter_stock from rms_medicament_record GROUP BY medicament_id SELECT medicament_id, sum(CASE WHEN record_type=1 THEN 1 ELSE 0 END) enter_stock from rms_medicament_record %s GROUP BY medicament_id
) b on b.medicament_id = a.medicament_id ) b on b.medicament_id = a.medicament_id
) c GROUP BY `name`, purity, speci ) c GROUP BY `name`, purity, speci
""" """
# 拼接搜索条件 # 拼接搜索条件
filter_base1 = ""
if seach_word: if seach_word:
seach_word = f"%{seach_word}%" seach_word = f"%{seach_word}%"
sql_all = sql_all % f"where `name` like '{seach_word}' or english_name like '{seach_word}'" filter_base1 = f" where `name` like '{seach_word}' or english_name like '{seach_word}'"
else: filter_base2 = ""
sql_all = sql_all % "" if start_time and end_time:
filter_base2 += f" where create_date >= '{start_time}' and create_date <= '{end_time}'"
sql_all = sql_all % (filter_base1, filter_base2)
# 查询获取数据条目数 # 查询获取数据条目数
try: try:
count_number = len(self.execute(sql_all).fetchall()) count_number = len(self.execute(sql_all).fetchall())
@ -208,7 +227,9 @@ class BllMedicamentRecord(Repository):
return data_list return data_list
# 试剂用量消耗 # 试剂用量消耗
def durg_useing_info(self, seach_word, page_param): def durg_useing_info(self, seach_word, start_time, end_time, page_param):
# select medicament_id, FORMAT(sum(use_quantity),2) sum_use, sum(CASE WHEN record_type=2 THEN 1 ELSE 0 END) c_two from rms_medicament_record %s GROUP BY medicament_id
sql_all = """ sql_all = """
select c.`name`, c.purity, c.speci, c.cas_number, c.net_weight_unit, select c.`name`, c.purity, c.speci, c.cas_number, c.net_weight_unit,
sum(CASE WHEN `status`=1 THEN 1 ELSE 0 END) on_in, sum(CASE WHEN `status`=1 THEN 1 ELSE 0 END) on_in,
@ -218,16 +239,20 @@ class BllMedicamentRecord(Repository):
select medicament_id, `name`, speci, cas_number, purity, net_weight_unit, `status` from rms_medicament select medicament_id, `name`, speci, cas_number, purity, net_weight_unit, `status` from rms_medicament
%s %s
) a LEFT JOIN( ) a LEFT JOIN(
select medicament_id, FORMAT(sum(use_quantity),2) sum_use, sum(CASE WHEN record_type=2 THEN 1 ELSE 0 END) c_two from rms_medicament_record GROUP BY medicament_id select medicament_id, FORMAT(sum(use_quantity),2) sum_use, count(record_id) c_two from rms_medicament_record where record_type=2 %s GROUP BY medicament_id
) b on a.medicament_id=b.medicament_id ) b on a.medicament_id=b.medicament_id
) c GROUP BY `name`, purity, speci ) c GROUP BY `name`, purity, speci
""" """
# 拼接查询条件 # 拼接查询条件
filter_base1 = ""
if seach_word: if seach_word:
seach_word = f"%{seach_word}%" filter_base1 = f"where `name` like '{seach_word}' or english_name like '%{seach_word}%'"
sql_all = sql_all % f"where `name` like '{seach_word}' or english_name like '{seach_word}'" filter_base2 = ""
else: if start_time and end_time:
sql_all = sql_all % "" filter_base2 = f" and create_date >= '{start_time}' and create_date <= '{end_time}'"
sql_all = sql_all %(filter_base1, filter_base2)
# 获取数量 # 获取数量
try: try:
count_number = len(self.execute(sql_all).fetchall()) count_number = len(self.execute(sql_all).fetchall())
@ -239,11 +264,14 @@ class BllMedicamentRecord(Repository):
data, ratio_all = self.default_data_list(page_sql_all, "use_level") data, ratio_all = self.default_data_list(page_sql_all, "use_level")
# 拼接百分比数据 # 拼接百分比数据
for i in data: for i in data:
try:
i["ratio"] = Utils.classify(i["use_level"], ratio_all) i["ratio"] = Utils.classify(i["use_level"], ratio_all)
except Exception:
i["ratio"] = "0%"
return data return data
# 人员用量消耗 # 人员用量消耗
def user_use_info(self, page_param, seach_user=None): def user_use_info(self, page_param, start_time, end_time, seach_user=None):
# sql_all = """ # sql_all = """
# select c.by_user_name, c.by_user_id, c.sum_use use_number, IFNULL(c.c_two, 0) use_level from ( # select c.by_user_name, c.by_user_id, c.sum_use use_number, IFNULL(c.c_two, 0) use_level from (
# select a.*, b.c_two, b.sum_use from( # select a.*, b.c_two, b.sum_use from(
@ -265,13 +293,16 @@ class BllMedicamentRecord(Repository):
) b on a.create_user_id = b.user_id ) b on a.create_user_id = b.user_id
""" """
# 查询条件组合 # 查询条件组合
filter_sql = "" filter_base = ""
if seach_user: if seach_user:
filter_sql += f"where by_user_name like '%{seach_user}%'" filter_base += f"by_user_name like '%{seach_user}%'"
if filter_sql: if start_time and end_time:
sql_all = sql_all % filter_sql if filter_base:
else: filter_base += " and "
sql_all = sql_all % "" filter_base += f" create_date >= '{start_time}' and create_date <= '{end_time}'"
if filter_base:
filter_base = " where " + filter_base
sql_all = sql_all % filter_base
# 分页总数 # 分页总数
try: try:
count_number = len(self.execute(sql_all).fetchall()) count_number = len(self.execute(sql_all).fetchall())
@ -283,11 +314,14 @@ class BllMedicamentRecord(Repository):
data, ratio_all = self.default_data_list(sql_all, "use_amount") data, ratio_all = self.default_data_list(sql_all, "use_amount")
# 数据处理 # 数据处理
for i in data: for i in data:
try:
i["ratio"] = Utils.classify(i["use_amount"], ratio_all) i["ratio"] = Utils.classify(i["use_amount"], ratio_all)
except Exception:
i["ratio"] = "0%"
return data return data
# 使用频率 # 使用频率
def use_frequency(self, page_param, client_id=None, seach_word=None): def use_frequency(self, page_param, start_time, end_time,client_id=None, seach_word=None):
sql_all = """ sql_all = """
select c.`name`, c.purity, c.speci, c.cas_number, c.net_weight_unit, select c.`name`, c.purity, c.speci, c.cas_number, c.net_weight_unit,
sum( CASE WHEN c.`status` = 1 THEN 1 ELSE 0 END ) new_on, sum( CASE WHEN c.`status` = 1 THEN 1 ELSE 0 END ) new_on,
@ -299,23 +333,26 @@ class BllMedicamentRecord(Repository):
select medicament_id, select medicament_id,
sum( CASE WHEN record_type = 2 THEN 1 ELSE 0 END ) two_type, sum( CASE WHEN record_type = 2 THEN 1 ELSE 0 END ) two_type,
sum( CASE WHEN record_type = 3 THEN 1 ELSE 0 END ) three_type sum( CASE WHEN record_type = 3 THEN 1 ELSE 0 END ) three_type
from rms_medicament_record GROUP BY medicament_id from rms_medicament_record %s GROUP BY medicament_id
)b on a.medicament_id=b.medicament_id )b on a.medicament_id=b.medicament_id
) c GROUP BY `name`, purity, speci ) c GROUP BY `name`, purity, speci
""" """
# 查询条件sql组合 # 查询条件sql组合
filter_sql = "" filter_base1 = ""
if client_id: if client_id:
filter_sql += f" client_id='{client_id}' " filter_base1 += f" client_id='{client_id}' "
if seach_word: if seach_word:
if filter_sql: if filter_base1:
filter_sql += "and " filter_base1 += "and "
seach_word = f"%{seach_word}%" seach_word = f"%{seach_word}%"
filter_sql += f" `name` like '{seach_word}' or english_name like '{seach_word}'" filter_base1 += f" `name` like '{seach_word}' or english_name like '{seach_word}'"
if filter_sql: if filter_base1:
sql_all = sql_all % f"where {filter_sql}" filter_base1 = " where " + filter_base1
else: filter_base2 = ""
sql_all = sql_all % "" if start_time and end_time:
filter_base2 += f" where create_date >= '{start_time}' and create_date <= '{end_time}'"
sql_all = sql_all % (filter_base1, filter_base2)
# 查询数据列表长度 # 查询数据列表长度
try: try:
count_number = len(self.execute(sql_all).fetchall()) count_number = len(self.execute(sql_all).fetchall())
@ -369,7 +406,9 @@ class BllMedicamentRecord(Repository):
# result = BllMedicamentRecord().executeNoParam(SQL) # result = BllMedicamentRecord().executeNoParam(SQL)
# return result # return result
# if __name__ == '__main__': if __name__ == '__main__':
aaa = BllMedicamentRecord().report_home_drug_useing_classify()
print(aaa)
# import random # import random
# from db_logic.medicament_record import BllMedicamentRecord # from db_logic.medicament_record import BllMedicamentRecord
# # customerId, clientId = None, '8db7e540-070f-11ed-a286-f47b094925e1' # # customerId, clientId = None, '8db7e540-070f-11ed-a286-f47b094925e1'

Loading…
Cancel
Save