@ -150,124 +150,140 @@ class BllMedicamentRecord(Repository):
# 公用方法
# 公用方法
def default_data_list ( self , sql_all , finds = None ) :
def default_data_list ( self , sql_all , finds = None ) :
"""
参数介绍
sql_all = 最终执行sql
finds = 需要聚合的字段
"""
# 获取数据
med_data = self . execute ( sql_all ) . fetchall ( )
med_data = self . execute ( sql_all ) . fetchall ( )
data_list = [ ]
data_list = [ ]
base_num = 0
base_num = 0
# 数据格式化
data = Utils . msyql_table_model ( med_data )
data = Utils . msyql_table_model ( med_data )
# 数据处理, 转换float类型, 聚合字段, 返回新列表
for d in data :
for d in data :
new_dic = { }
new_dic = { }
for k , v in d . items ( ) :
for k , v in d . items ( ) :
v = v if not isinstance ( v , Decimal ) else float ( v )
new_dic [ k ] = v if not isinstance ( v , Decimal ) else float ( v )
new_dic [ k ] = 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 , page_param ) :
sql_all = """
sql_all = """
select c . ` name ` , c . cas_number , 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 ,
sum ( CASE WHEN c . ` status ` = 2 THEN 1 ELSE 0 END ) up_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
sum ( CASE WHEN c . ` status ` = 3 THEN 1 ELSE 0 END ) no_stock
from (
from (
select a . * , b . enter_stock from (
select a . * , b . enter_stock from (
select medicament_id , ` name ` , cas_number , 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 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 , net_weight_unit
) c GROUP BY ` name ` , purity , speci
"""
"""
# 拼接搜索条件
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 } ' "
sql_all = sql_all % f " where `name` like ' { seach_word } ' or english_name like ' { seach_word } ' "
else :
else :
sql_all = sql_all % " "
sql_all = sql_all % " "
# 查询获取数据条目数
finds = [
try :
" name " , " cas_number " , " net_weight_unit " , " purity " , " enter_stock " , " in_stock " , " up_stock " , " no_stock "
count_number = len ( self . execute ( sql_all ) . fetchall ( ) )
]
except Exception :
count_sql = " select count(medicament_id) from rms_medicament group by `name`, purity, net_weight_unit "
count_number = 0
page_param . totalRecords = self . execute ( count_sql ) . fetchone ( ) [ 0 ]
page_param . totalRecords = count_number
sql_all + = ' limit ' + \
# 数据分页处理
str ( ( page_param . curPage - 1 ) * page_param . pageRows ) + \
sql_all = Utils . sql_paging_assemble ( sql_all , page_param )
' , ' + str ( page_param . pageRows )
# 获取处理后的数据
data_list , _ = self . default_data_list ( sql_all )
data_list , _ = self . default_data_list ( sql_all )
return data_list
return data_list
# 试剂用量消耗
# 试剂用量消耗
def durg_useing_info ( self , seach_word , page_param ) :
def durg_useing_info ( self , seach_word , page_param ) :
sql_all = """
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 use_num , c . sum_use use_level from (
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 ,
IFNULL ( c . c_two , 0 ) use_num ,
IFNULL ( c . sum_use , 0 ) use_level from (
select a . * , b . c_two , b . 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
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 , 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
) b on a . medicament_id = b . medicament_id
) c GROUP BY ` name ` , purity , net_weight_unit
) c GROUP BY ` name ` , purity , speci
"""
"""
# 拼接查询条件
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 } ' "
sql_all = sql_all % f " where `name` like ' { seach_word } ' or english_name like ' { seach_word } ' "
else :
else :
sql_all = sql_all % " "
sql_all = sql_all % " "
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 ]
try :
sql_all + = ' limit ' + \
count_number = len ( self . execute ( sql_all ) . fetchall ( ) )
str ( ( page_param . curPage - 1 ) * page_param . pageRows ) + \
except Exception :
' , ' + str ( page_param . pageRows )
count_number = 0
page_param . totalRecords = count_number
data , ratio_all = self . default_data_list ( sql_all , " use_level " )
page_sql_all = Utils . sql_paging_assemble ( sql_all , page_param )
# 获取组建后的数据结果
data , ratio_all = self . default_data_list ( page_sql_all , " use_level " )
# 拼接百分比数据
for i in data :
for i in data :
i [ " ratio " ] = str ( round ( float ( i [ " use_level " ] ) / ratio_all , 2 ) * 100 ) + " % "
i [ " ratio " ] = Utils . classify ( i [ " use_level " ] , ratio_all )
return data
return data
# 人员用量消耗
# 人员用量消耗
def user_use_info ( self , page_param , seach_word = None , seach_user = None ) :
def user_use_info ( self , page_param , seach_user = None ) :
# 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 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
# """
sql_all = """
sql_all = """
select c . by_user_name , c . by_user_id , c . sum_use use_number , c . c_two use_level from (
select a . * , b . role_name from (
select a . * , b . c_two , b . sum_use from (
select convert ( sum ( use_quantity ) , decimal ( 10 , 2 ) ) use_amount ,
select medicament_id , by_user_name , by_user_id from rms_medicament % s
sum ( CASE WHEN record_type = 2 THEN 1 ELSE 0 END ) use_num ,
) a LEFT JOIN (
create_user_id ,
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
create_user_name
) b on a . medicament_id = b . medicament_id
from rms_medicament_record % s GROUP BY create_user_id
) c GROUP BY by_user_name , by_user_id
) a LEFT JOIN (
SELECT role_name , user_id FROM rms_user
) b on a . create_user_id = b . user_id
"""
"""
# 查询条件组合
filter_sql = " "
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 seach_user :
if filter_sql :
filter_sql + = f " where by_user_name like ' % { seach_user } % ' "
filter_sql + = " and "
seach_user = f " % { seach_user } % "
filter_sql + = f " by_user_name like ' { seach_user } ' "
if filter_sql :
if filter_sql :
sql_all = sql_all % f " where { filter_sql } "
sql_all = sql_all % filter_sql
else :
else :
sql_all = sql_all % " "
sql_all = sql_all % " "
count_sql = " select count(medicament_id) from rms_medicament_record group by create_user_id "
# 分页总数
try :
try :
count_number = len ( self . execute ( sql_all ) . fetchall ( ) )
count_number = len ( self . execute ( sql_all ) . fetchall ( ) )
except Exception :
except Exception :
count_number = 0
count_number = 0
page_param . totalRecords = count_number
page_param . totalRecords = count_number
# sql_all += ' limit ' + \
# 数据分页
# str((page_param.curPage - 1) * page_param.pageRows) + \
# ',' + str(page_param.pageRows)
sql_all = Utils . sql_paging_assemble ( sql_all , page_param )
sql_all = Utils . sql_paging_assemble ( sql_all , page_param )
data , ratio_all = self . default_data_list ( sql_all , " use_level " )
data , ratio_all = self . default_data_list ( sql_all , " use_amount " )
# 数据处理
for i in data :
for i in data :
i [ " ratio " ] = str (
i [ " ratio " ] = Utils . classify ( i [ " use_amount " ] , ratio_all )
round ( float ( i [ " use_level " ] ) / ratio_all , 2 ) * 100 ) + " % "
print ( i )
return data
return data
# 使用频率
# 使用频率