|
|
|
@ -79,53 +79,167 @@ class BllMedicamentRecord(Repository):
|
|
|
|
|
).select_from(self.entityType).group_by(EntityMedicamentRecord.record_type).all()
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
def default_data_list(self, **kw):
|
|
|
|
|
sql_all = kw.get("sql_all")
|
|
|
|
|
finds = kw.get("finds")
|
|
|
|
|
base_bool = kw.get("base_bool", False)
|
|
|
|
|
med_data = self.execute(sql_all).fetchall()
|
|
|
|
|
data_list = []
|
|
|
|
|
base_num = 0
|
|
|
|
|
for med in med_data:
|
|
|
|
|
new_med = []
|
|
|
|
|
# for n in med:
|
|
|
|
|
# new_med.append(float(n) if isinstance(n, Decimal) else n)
|
|
|
|
|
new_med = [n if not isinstance(n, Decimal) else float(n) for n in med]
|
|
|
|
|
data_list.append(dict(zip(finds, new_med)))
|
|
|
|
|
if base_bool:
|
|
|
|
|
base_num += float(new_med[-1])
|
|
|
|
|
return data_list, base_num
|
|
|
|
|
|
|
|
|
|
# 获取库存消耗数据
|
|
|
|
|
def durg_stock_loss(self, seach_word):
|
|
|
|
|
def durg_stock_loss(self, seach_word, page_param):
|
|
|
|
|
sql_all = """
|
|
|
|
|
select c.`name`, c.cas_number, 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`=2 THEN 1 ELSE 0 END) up_stock,
|
|
|
|
|
sum(CASE WHEN c.`status`=3 THEN 1 ELSE 0 END) no_stock
|
|
|
|
|
from (
|
|
|
|
|
select a.*, b.enter_stock from (
|
|
|
|
|
|
|
|
|
|
select medicament_id, `name`, cas_number, purity, net_weight_unit, `status` from rms_medicament
|
|
|
|
|
%s
|
|
|
|
|
) 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
|
|
|
|
|
) b on b.medicament_id = a.medicament_id) c GROUP BY `name`, purity, net_weight_unit
|
|
|
|
|
select a.*, b.enter_stock from (
|
|
|
|
|
select medicament_id, `name`, cas_number, purity, net_weight_unit, `status` from rms_medicament
|
|
|
|
|
%s
|
|
|
|
|
) 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
|
|
|
|
|
) b on b.medicament_id = a.medicament_id
|
|
|
|
|
) c GROUP BY `name`, purity, net_weight_unit
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
if seach_word:
|
|
|
|
|
sql_all = sql_all % f"where name link {seach_word} or english_name link {seach_word}"
|
|
|
|
|
seach_word = f"%{seach_word}%"
|
|
|
|
|
sql_all = sql_all % f"where `name` like '{seach_word}' or english_name like '{seach_word}'"
|
|
|
|
|
else:
|
|
|
|
|
sql_all = sql_all % ''
|
|
|
|
|
sql_all = sql_all % ""
|
|
|
|
|
|
|
|
|
|
finds = [
|
|
|
|
|
"name", "cas_number", "net_weight_unit", "purity", "enter_stock", "in_stock", "up_stock", "no_stock"
|
|
|
|
|
]
|
|
|
|
|
count_sql = "select count(medicament_id) from rms_medicament group by `name`, purity, net_weight_unit"
|
|
|
|
|
page_param.totalRecords = self.execute(count_sql).fetchone()[0]
|
|
|
|
|
sql_all += ' limit ' + \
|
|
|
|
|
str((page_param.curPage - 1) * page_param.pageRows) + \
|
|
|
|
|
',' + str(page_param.pageRows)
|
|
|
|
|
|
|
|
|
|
med_data = self.execute(sql_all).fetchall()
|
|
|
|
|
data_list = []
|
|
|
|
|
for med in med_data:
|
|
|
|
|
new_med = []
|
|
|
|
|
for n in med:
|
|
|
|
|
if isinstance(n, Decimal):
|
|
|
|
|
n = float(n)
|
|
|
|
|
new_med.append(n)
|
|
|
|
|
data_list.append(dict(zip(finds, new_med)))
|
|
|
|
|
data_list, _ = self.default_data_list(**{"sql_all":sql_all, "finds":finds})
|
|
|
|
|
return data_list
|
|
|
|
|
|
|
|
|
|
# 试剂用量消耗
|
|
|
|
|
def durg_useing_info(self, seach_word, page_param):
|
|
|
|
|
sql_all = """
|
|
|
|
|
select c.`name`, c.purity, c.cas_number, c.net_weight_unit, sum(CASE WHEN `status`=1 THEN 1 ELSE 0 END) on_in, c.c_two, c.sum_use from (
|
|
|
|
|
select a.*, b.c_two, b.sum_use from(
|
|
|
|
|
select medicament_id, `name`, cas_number, purity, net_weight_unit, `status` from rms_medicament
|
|
|
|
|
%s
|
|
|
|
|
) 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
|
|
|
|
|
) b on a.medicament_id=b.medicament_id
|
|
|
|
|
) c GROUP BY `name`, purity, net_weight_unit
|
|
|
|
|
"""
|
|
|
|
|
if seach_word:
|
|
|
|
|
seach_word = f"%{seach_word}%"
|
|
|
|
|
sql_all = sql_all % f"where `name` like '{seach_word}' or english_name like '{seach_word}'"
|
|
|
|
|
else:
|
|
|
|
|
sql_all = sql_all % ""
|
|
|
|
|
finds = [
|
|
|
|
|
"name", "purity", "cas_number", "net_weight_unit", "on_in", "use_num", "use_level", "ratio"
|
|
|
|
|
]
|
|
|
|
|
count_sql = "select count(medicament_id) from rms_medicament group by `name`, purity, net_weight_unit"
|
|
|
|
|
page_param.totalRecords = self.execute(count_sql).fetchone()[0]
|
|
|
|
|
sql_all += ' limit ' + \
|
|
|
|
|
str((page_param.curPage - 1) * page_param.pageRows) + \
|
|
|
|
|
',' + str(page_param.pageRows)
|
|
|
|
|
|
|
|
|
|
def update_db(self):
|
|
|
|
|
import random
|
|
|
|
|
typ_dic = BllMedicamentRecord().findList().all()
|
|
|
|
|
for a in typ_dic:
|
|
|
|
|
a.use_quantity = round(random.uniform(1, 10), 2)
|
|
|
|
|
self.session.merge(a)
|
|
|
|
|
self.session.commit()
|
|
|
|
|
data, ratio_all = self.default_data_list(**{"sql_all": sql_all, "finds": finds, "base_bool": True})
|
|
|
|
|
for i in data:
|
|
|
|
|
i["ratio"] = str(round(float(i["use_level"]) / ratio_all, 2) * 100) + "%"
|
|
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
# 人员用量消耗
|
|
|
|
|
def user_use_info(self, page_param, seach_word=None, seach_user=None):
|
|
|
|
|
sql_all = """
|
|
|
|
|
select c.by_user_name, c.by_user_id, c.sum_use, c.c_two from (
|
|
|
|
|
select a.*, b.c_two, b.sum_use from(
|
|
|
|
|
select medicament_id, by_user_name, by_user_id from rms_medicament %s
|
|
|
|
|
) 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
|
|
|
|
|
) b on a.medicament_id=b.medicament_id
|
|
|
|
|
) c GROUP BY by_user_name, by_user_id
|
|
|
|
|
"""
|
|
|
|
|
filter_sql = ""
|
|
|
|
|
if seach_word:
|
|
|
|
|
seach_word = f"%{seach_word}%"
|
|
|
|
|
filter_sql = f"`name` like '{seach_word}' or english_name like '{seach_word}'"
|
|
|
|
|
if seach_user:
|
|
|
|
|
if filter_sql:
|
|
|
|
|
filter_sql += " and "
|
|
|
|
|
seach_user = f"%{seach_user}%"
|
|
|
|
|
filter_sql += f"by_user_name like '{seach_user}'"
|
|
|
|
|
if filter_sql:
|
|
|
|
|
sql_all = sql_all % f"where {filter_sql}"
|
|
|
|
|
else:
|
|
|
|
|
sql_all = sql_all % ""
|
|
|
|
|
finds = [
|
|
|
|
|
"by_user_name", "by_user_id", "use_number", "use_level", "ratio"
|
|
|
|
|
]
|
|
|
|
|
count_sql = "select count(medicament_id) from rms_medicament_record group by create_user_id"
|
|
|
|
|
page_param.totalRecords = self.execute(count_sql).fetchone()[0]
|
|
|
|
|
sql_all += ' limit ' + \
|
|
|
|
|
str((page_param.curPage - 1) * page_param.pageRows) + \
|
|
|
|
|
',' + str(page_param.pageRows)
|
|
|
|
|
data, ratio_all = self.default_data_list(**{"sql_all": sql_all, "finds": finds, "base_bool": True})
|
|
|
|
|
for i in data:
|
|
|
|
|
i["ratio"] = str(
|
|
|
|
|
round(float(i["use_level"]) / ratio_all, 2) * 100) + "%"
|
|
|
|
|
print(i)
|
|
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
# 使用频率
|
|
|
|
|
def use_frequency(self, page_param, client_id=None, seach_word=None):
|
|
|
|
|
sql_all = """
|
|
|
|
|
select c.`name`, c.purity, c.cas_number, c.net_weight_unit,
|
|
|
|
|
sum( CASE WHEN c.`status` = 1 THEN 1 ELSE 0 END ) new_on,
|
|
|
|
|
c.client_id, sum(c.tp_2) sum_tp2, sum(c.tp_3) sum_tp3 from (
|
|
|
|
|
select a.*, IFNULL(b.two_type,0) tp_2, IFNULL(b.three_type,0) tp_3 from (
|
|
|
|
|
select medicament_id, `name`, purity, cas_number, net_weight_unit, `status`, client_id from rms_medicament
|
|
|
|
|
%s
|
|
|
|
|
) a LEFT JOIN(
|
|
|
|
|
select medicament_id,
|
|
|
|
|
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
|
|
|
|
|
from rms_medicament_record GROUP BY medicament_id
|
|
|
|
|
)b on a.medicament_id=b.medicament_id
|
|
|
|
|
) c GROUP BY `name`, purity, net_weight_unit
|
|
|
|
|
"""
|
|
|
|
|
filter_sql = ""
|
|
|
|
|
if client_id:
|
|
|
|
|
filter_sql += f" client_id='{client_id}' "
|
|
|
|
|
if seach_word:
|
|
|
|
|
if filter_sql:
|
|
|
|
|
filter_sql += "and "
|
|
|
|
|
seach_word = f"%{seach_word}%"
|
|
|
|
|
filter_sql += f" `name` like '{seach_word}' or english_name like '{seach_word}'"
|
|
|
|
|
if filter_sql:
|
|
|
|
|
sql_all = sql_all % f"where {filter_sql}"
|
|
|
|
|
else:
|
|
|
|
|
sql_all = sql_all % ""
|
|
|
|
|
finds = [
|
|
|
|
|
"name", "purity", "cas_number", "net_weight_unit", "use_num", "return_num"
|
|
|
|
|
]
|
|
|
|
|
count_sql = "select count(medicament_id) from rms_medicament group by `name`, purity, net_weight_unit"
|
|
|
|
|
page_param.totalRecords = self.execute(count_sql).fetchone()[0]
|
|
|
|
|
sql_all += ' limit ' + str((page_param.curPage - 1) * page_param.pageRows) + ',' + str(page_param.pageRows)
|
|
|
|
|
data, _ = self.default_data_list(**{"sql_all": sql_all, "finds":finds})
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
# #获取药剂最后一次使用余量
|
|
|
|
|
# def getLastRecordRemain(self, drugId):
|
|
|
|
@ -150,7 +264,7 @@ class BllMedicamentRecord(Repository):
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
import random
|
|
|
|
|
from db_logic.medicament_record import BllMedicamentRecord
|
|
|
|
|
customerId, clientId = None, '8db7e540-070f-11ed-a286-f47b094925e1'
|
|
|
|
|
sql = f"update rms_medicament_record set use_quantity={round(random.uniform(1,10), 1)}"
|
|
|
|
|
typ_dic = BllMedicamentRecord().update_db()
|
|
|
|
|
# customerId, clientId = None, '8db7e540-070f-11ed-a286-f47b094925e1'
|
|
|
|
|
# sql = f"update rms_medicament_record set use_quantity={round(random.uniform(1,10), 1)}"
|
|
|
|
|
typ_dic = BllMedicamentRecord().drug_use_f('', '化')
|
|
|
|
|
print(typ_dic)
|
|
|
|
|