@ -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_num ber " ] , 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 , s each_user= None ) :
def user_use_info ( self , page_param , s tart_time, end_time , s each_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'