@ -30,21 +30,40 @@ class BllMedicamentRecord(Repository):
# 获取流转记录
def get_drug_flow_log ( self , medicament_id , page_param ) :
# filter_base = ""
# if medicament_id:
# filter_base += f" medicament_id='{medicament_id}' "
# if filter_base:
# filter_base = f" where {filter_base}"
# sql_all = f"""
# select a.`name`, a.speci, a.bar_code, a.cas_number, a.purity, a.client_id, a.`status`, b.create_date, b.create_user_name from(
# select medicament_id, `name`, speci, bar_code, cas_number, purity, client_id, `status` from rms_medicament
# ) a RIGHT JOIN(
# select medicament_id, create_date, create_user_id, create_user_name from rms_medicament_record {filter_base} order by create_date desc
# ) b on b.medicament_id=a.medicament_id
# """
# # count_sql = "select count(*) from rms_medicament_record"
# try:
# count_number = len(self.execute(sql_all).fetchall())
# except Exception:
# count_number = 0
# page_param.totalRecords = count_number
# sql_all = Utils.sql_paging_assemble(sql_all, page_param)
# return self.execute(sql_all).fetchall()
filter_base = " "
if medicament_id :
filter_base + = f " medicament_id= ' { medicament_id } ' "
if filter_base :
filter_base = f " where { filter_base } "
sql_all = f """
select a . ` name ` , a . speci , a . bar_code , a . cas_number , a . purity , a . client_id , a . ` status ` , b . create_date , b . create_user_name from (
select medicament_id , ` name ` , speci , bar_code , cas_number , purity , client_id , ` status ` from rms_medicament
) a RIGHT JOIN (
select medicament_id , create_date , create_user_id , create_user_name from rms_medicament_record { filter_base } order by create_date desc
) b on b . medicament_id = a . medicament_id
select a . * , b . create_date , b . create_user_name , b . record_type from (
select medicament_id , create_date , create_user_id , create_user_name , record_type from rms_medicament_record { filter_base }
) b LEFT JOIN (
select * from rms_medicament { filter_base }
) a on b . medicament_id = a . medicament_id order by create_date desc
"""
# count_sql = "select count(*) from rms_medicament_record"
try :
count_number = len ( self . execute ( sql_all ) . fetchall ( ) )
except Exception :
@ -71,31 +90,64 @@ class BllMedicamentRecord(Repository):
#获取今日药剂流转信息数量
def getTodayDrugRecordCount ( self ) :
sql_all = """
SELECT
record_type , count ( record_id ) type_number
FROM rms_medicament_record
where TO_DAYS ( create_date ) = TO_DAYS ( NOW ( ) ) % s
GROUP BY record_type
# sql_all = """
# SELECT
# record_type, count(record_id) type_number
# FROM rms_medicament_record
# where TO_DAYS(create_date)=TO_DAYS(NOW()) %s
# GROUP BY record_type
# """
# filter_base =""
# # if customer_id:
# # filter_base += f" and customer_id='{customer_id}'"
# # if client_id:
# # if filter_base:
# # filter_base += " and "
# # filter_base += f"client_id='{client_id}'"
# sql_all = sql_all % filter_base
# sql_data = self.execute(sql_all).fetchall()
# n_a, n_b = 0,0
# data_list = []
# default_list = [(1,0),(2,0),(3,0)]
# sql_data = sql_data if sql_data else default_list
# for i in sql_data:
# if i[0] == 2:
# n_a = i[1]
# if i[0] == 3:
# n_b = i[1]
# data_list.append({
# "record_type":i[0],
# "type_number":i[1]
# })
# data_list.append(
# {
# "record_type":100,
# "type_number": n_a - n_b
# }
# )
# return data_list
sql_all = f """
SELECT
record_type , count ( record_id ) type_number
FROM rms_medicament_record as a LEFT JOIN rms_medicament as b on a . medicament_id = b . medicament_id
where TO_DAYS ( create_date ) = TO_DAYS ( ' { Utils.get_str_datetime()} ' )
GROUP BY record_type
"""
filter_base = " "
# filter_base =" "
# if customer_id:
# filter_base += f" and customer_id='{customer_id}'"
# if client_id:
# if filter_base:
# filter_base += " and "
# filter_base += f"client_id='{client_id}'"
sql_all = sql_all % filter_base
# sql_all = sql_all % filter_base
sql_data = self . execute ( sql_all ) . fetchall ( )
n_a , n_b = 0 , 0
n_ c = self . execute ( f " select count(*) num from rms_medicament where `status`= 2 " ) . fetchone ( ) . num
data_list = [ ]
default_list = [ ( 1 , 0 ) , ( 2 , 0 ) , ( 3 , 0 ) ]
sql_data = sql_data if sql_data else default_list
for i in sql_data :
if i [ 0 ] == 2 :
n_a = i [ 1 ]
if i [ 0 ] == 3 :
n_b = i [ 1 ]
data_list . append ( {
" record_type " : i [ 0 ] ,
" type_number " : i [ 1 ]
@ -103,7 +155,7 @@ class BllMedicamentRecord(Repository):
data_list . append (
{
" record_type " : 100 ,
" type_number " : n_ a - n_b
" type_number " : n_ c
}
)
return data_list
@ -127,7 +179,25 @@ class BllMedicamentRecord(Repository):
# 获取报表统计主页,使用频率
def get_use_avg_number ( self ) :
sql_all = """
# sql_all = """
# select a.`name`, IFNULL(b.count_number ,0) count_number from (
# select medicament_id, purity, `name`, speci from rms_medicament
# ) a LEFT JOIN(
# select medicament_id, count(record_id) count_number from rms_medicament_record where record_type=2 GROUP BY medicament_id
# ) b
# on a.medicament_id=b.medicament_id GROUP BY a.purity, a.`name`, a.speci ORDER BY count_number desc
# """
# sql_data = self.execute(sql_all).fetchall()
# use_number = sum([float(i[1]) for i in sql_data])
# drug_num = len(sql_data)
# resp_data = {
# "avg_use_number": round(use_number / drug_num),
# "max_name": sql_data[0][0],
# "max_number": sql_data[0][1]
# }
# return resp_data
sql_all = f """
select a . ` name ` , IFNULL ( b . count_number , 0 ) count_number from (
select medicament_id , purity , ` name ` , speci from rms_medicament
) a LEFT JOIN (
@ -136,20 +206,53 @@ class BllMedicamentRecord(Repository):
on a . medicament_id = b . medicament_id GROUP BY a . purity , a . ` name ` , a . speci ORDER BY count_number desc
"""
sql_data = self . execute ( sql_all ) . fetchall ( )
use_number = sum ( [ float ( i [ 1 ] ) for i in sql_data ] )
drug_num = len ( sql_data )
if not sql_data :
resp_data = {
" avg_use_number " : 0 ,
" max_name " : " " ,
" max_number " : 0
}
else :
use_number = sum ( [ float ( i [ 1 ] ) for i in sql_data ] )
drug_num = len ( sql_data )
resp_data = {
" avg_use_number " : round ( use_number / drug_num ) ,
" max_name " : sql_data [ 0 ] [ 0 ] ,
" max_number " : sql_data [ 0 ] [ 1 ]
}
resp_data = {
" avg_use_number " : round ( use_number / drug_num ) ,
" max_name " : sql_data [ 0 ] [ 0 ] ,
" max_number " : sql_data [ 0 ] [ 1 ]
}
return resp_data
# 获取报表统计主页入库记录信息
def get_drug_record_count ( self ) :
sql_all = """
select COUNT ( record_id ) count_number , record_type from rms_medicament_record GROUP BY record_type
# sql_all = """
# select COUNT(record_id) count_number, record_type from rms_medicament_record GROUP BY record_type
# """
# # 获取分类数量
# data = self.execute(sql_all).fetchall()
# data_li = Utils.msyql_table_model(data)
# 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:
# name_info = name_info_dict.get(i['record_type'])
# new_dic = {
# "name": name_info[0],
# "date_time": name_info[2]
# }
# new_dic.update(**i)
# data_list.append(new_dic)
# return data_list, self.get_use_avg_number()
sql_all = f """
select count ( record_id ) count_number , record_type from (
select record_id , medicament_id , record_type from rms_medicament_record
) as a LEFT JOIN
rms_medicament as b on a . medicament_id = b . medicament_id
GROUP BY record_type
"""
# 获取分类数量
data = self . execute ( sql_all ) . fetchall ( )
@ -160,7 +263,8 @@ class BllMedicamentRecord(Repository):
name_info_dict = { }
for name in name_info_list :
name_info_dict [ name [ - 1 ] ] = name
# 组装数据
# 组装数据
type_num = [ ]
for i in data_li :
name_info = name_info_dict . get ( i [ ' record_type ' ] )
new_dic = {
@ -168,7 +272,17 @@ class BllMedicamentRecord(Repository):
" date_time " : name_info [ 2 ]
}
new_dic . update ( * * i )
type_num . append ( str ( i [ " record_type " ] ) )
data_list . append ( new_dic )
set_num_li = list ( set ( [ " 1 " , " 2 " , " 3 " ] ) . difference ( set ( type_num ) ) )
for i in set_num_li :
data_list . append (
{
" record_type " : int ( i ) ,
" name " : " " ,
" date_time " : " "
}
)
return data_list , self . get_use_avg_number ( )
# 获取报表统计主页试剂用量消耗
@ -208,14 +322,31 @@ class BllMedicamentRecord(Repository):
# 获取报表统计主页人员用量消耗
def report_home_user_use_info ( self ) :
sql_all = """
select a . count_id count_number , a . create_user_id , a . create_user_name , b . avatar_url from (
# sql_all = """
# select a.count_id count_number, a.create_user_id, a.create_user_name, b.avatar_url from(
select count ( record_id ) count_id , create_user_id , create_user_name from rms_medicament_record GROUP BY create_user_id
) a LEFT JOIN (
# select count(record_id) count_id, create_user_id,create_user_name from rms_medicament_record GROUP BY create_user_id
# ) a LEFT JOIN (
select avatar_url , user_id from rms_user
# select avatar_url, user_id from rms_user
# ) b on a.create_user_id= b.user_id
# """
# data = self.execute(sql_all).fetchall()
# data_list = Utils.msyql_table_model(data)
# return data_list
sql_all = f """
select a . count_id count_number , a . create_user_id , a . create_user_name , b . avatar_url from (
select count ( record_id ) count_id , create_user_id , create_user_name from
rms_medicament_record as c
LEFT JOIN
rms_medicament as d
on c . medicament_id = d . medicament_id
GROUP BY create_user_id
) a LEFT JOIN (
select avatar_url , user_id from rms_user
) b on a . create_user_id = b . user_id
order by count_number desc
"""
data = self . execute ( sql_all ) . fetchall ( )
@ -247,39 +378,72 @@ class BllMedicamentRecord(Repository):
# 获取库存消耗数据
def durg_stock_loss ( self , seach_word , start_time , end_time , page_param ) :
sql_all = """
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 ` = 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 , speci , 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 % s GROUP BY medicament_id
) b on b . medicament_id = a . medicament_id
) c GROUP BY ` name ` , purity , speci
# sql_all = """
# 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`=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, speci, 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 %s GROUP BY medicament_id
# ) b on b.medicament_id = a. medicament_id
# ) c GROUP BY `name`, purity, speci
"""
# 拼接搜索条件
filter_base1 = " "
# """
# # 拼接搜索条件
# filter_base1 = ""
# if seach_word:
# seach_word = f"%{seach_word}%"
# filter_base1 = f" where `name` like '{seach_word}' or english_name like '{seach_word}'"
# filter_base2 = ""
# 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:
# count_number = len(self.execute(sql_all).fetchall())
# except Exception:
# count_number = 0
# page_param.totalRecords = count_number
# # 数据分页处理
# sql_all = Utils.sql_paging_assemble(sql_all, page_param)
# # 获取处理后的数据
# data_list, _ = self.default_data_list(sql_all)
# return data_list
filter_base = " "
if seach_word :
seach_word = f " % { seach_word } % "
filter_base1 = f " where `name` like ' { seach_word } ' or english_name like ' { seach_word } ' "
filter_base2 = " "
if start_time and end_time :
filter_base2 + = f " where create_date >= ' { start_time } ' and create_date <= ' { end_time } ' "
filter_base + = f " (`name` like ' { seach_word } ' or english_name like ' { seach_word } ' ) "
if filter_base :
filter_base = f " where { filter_base } "
sql_all = f """
select ` name ` , cas_number , speci , net_weight_unit , purity , enter_stock ,
sum ( CASE WHEN ` status ` = 1 THEN 1 ELSE 0 END ) in_stock ,
sum ( CASE WHEN ` status ` = 2 THEN 1 ELSE 0 END ) up_stock ,
sum ( CASE WHEN ` status ` = 3 THEN 1 ELSE 0 END ) no_stock
from (
select medicament_id , ` name ` , english_name , cas_number , speci , purity , net_weight_unit , ` status ` , func_type from rms_medicament
) a LEFT JOIN (
SELECT medicament_id , count ( medicament_id ) enter_stock , create_date from rms_medicament_record where record_type = 1 GROUP BY medicament_id
) b on b . medicament_id = a . medicament_id
{ filter_base }
GROUP BY ` name ` , purity , speci
sql_all = sql_all % ( filter_base1 , filter_base2 )
"""
# 查询获取数据条目数
try :
count_number = len ( self . execute ( sql_all ) . fetchall ( ) )
except Exception :
count_number = 0
page_param . totalRecords = count_number
# 数据分页处理
sql_all = Utils . sql_paging_assemble ( sql_all , page_param )
if page_param :
page_param . totalRecords = count_number
# 数据分页处理
sql_all = Utils . sql_paging_assemble ( sql_all , page_param )
# 获取处理后的数据
data_list , _ = self . default_data_list ( sql_all )
return data_list
@ -288,39 +452,72 @@ class BllMedicamentRecord(Repository):
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 = """
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 medicament_id , ` name ` , speci , cas_number , purity , net_weight_unit , ` status ` from rms_medicament
% s
) a LEFT JOIN (
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
) c GROUP BY ` name ` , purity , speci
"""
# 拼接查询条件
filter_base1 = " "
# sql_all = """
# 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 medicament_id, `name`, speci, cas_number, purity, net_weight_unit, `status` from rms_medicament
# %s
# ) a LEFT JOIN(
# 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
# ) c GROUP BY `name`, purity, speci
# """
# # 拼接查询条件
# filter_base1 = ""
# if seach_word:
# seach_word = f"%{seach_word}%"
# filter_base1 = f"where `name` like '{seach_word}' or english_name like '{seach_word}'"
# filter_base2 = ""
# if start_time and end_time:
# filter_base2 = f" and create_date >= '{start_time}' and create_date <= '{end_time}'"
# sql_all = sql_all %(filter_base1, filter_base2)
# # 获取数量
# try:
# count_number = len(self.execute(sql_all).fetchall())
# except Exception:
# count_number = 0
# page_param.totalRecords = count_number
# 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:
# try:
# i["ratio"] = Utils.classify(i["use_level"], ratio_all)
# except Exception:
# i["ratio"] = "0%"
# return data
filter_base = " "
if seach_word :
seach_word = f " % { seach_word } % "
filter_base1 = f " where `name` like ' { seach_word } ' or english_name like ' { seach_word } ' "
filter_base2 = " "
if start_time and end_time :
filter_base2 = f " and create_date >= ' { start_time } ' and create_date <= ' { end_time } ' "
sql_all = sql_all % ( filter_base1 , filter_base2 )
filter_base + = f " (`name` like ' { seach_word } ' or english_name like ' { seach_word } ' ) "
if filter_base :
filter_base = f " where { filter_base } "
sql_all = f """
select ` name ` , purity , speci , cas_number , net_weight_unit ,
sum ( CASE WHEN ` status ` = 1 THEN 1 ELSE 0 END ) on_in ,
IFNULL ( c_two , 0 ) use_num , IFNULL ( sum_use , 0 ) use_level from (
select medicament_id , ` name ` , english_name , speci , cas_number , purity , net_weight_unit , put_in_date , ` status ` , func_type from rms_medicament
) as a LEFT JOIN (
select medicament_id , FORMAT ( sum ( use_quantity ) , 2 ) sum_use , count ( medicament_id ) c_two from rms_medicament_record where record_type = 2 GROUP BY medicament_id
) b on a . medicament_id = b . medicament_id { filter_base } GROUP BY ` name ` , purity , speci
"""
# 获取数量
try :
count_number = len ( self . execute ( sql_all ) . fetchall ( ) )
except Exception :
count_number = 0
page_param . totalRecords = count_number
page_sql_all = Utils . sql_paging_assemble ( sql_all , page_param )
if page_param :
page_param . totalRecords = count_number
sql_all = Utils . sql_paging_assemble ( sql_all , page_param )
# 获取组建后的数据结果
data , ratio_all = self . default_data_list ( page_sql_all , " use_level " )
data , ratio_all = self . default_data_list ( sql_all, " use_level " )
# 拼接百分比数据
for i in data :
try :
@ -328,48 +525,89 @@ class BllMedicamentRecord(Repository):
except Exception :
i [ " ratio " ] = " 0 % "
return data
# 人员用量消耗
def user_use_info ( self , page_param , start_time , end_time , 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 = """
# 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
# select a.*, b.role_name from (
# select convert(sum(use_quantity), decimal(10,2)) use_amount,
# sum( CASE WHEN record_type = 2 THEN 1 ELSE 0 END ) use_num,
# create_user_id,
# create_user_name
# from rms_medicament_record %s GROUP BY create_user_id
# ) a LEFT JOIN (
# SELECT role_name, user_id FROM rms_user
# ) b on a.create_user_id = b.user_id
# """
sql_all = """
select a . * , b . role_name from (
select convert ( sum ( use_quantity ) , decimal ( 10 , 2 ) ) use_amount ,
sum ( CASE WHEN record_type = 2 THEN 1 ELSE 0 END ) use_num ,
create_user_id ,
create_user_name
from rms_medicament_record % s GROUP BY create_user_id
) a LEFT JOIN (
SELECT role_name , user_id FROM rms_user
) b on a . create_user_id = b . user_id
"""
# 查询条件组合
# # 查询条件组合
# filter_base = ""
# if seach_user:
# filter_base += f"by_user_name like '%{seach_user}%'"
# if start_time and end_time:
# if filter_base:
# filter_base += " and "
# 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:
# count_number = len(self.execute(sql_all).fetchall())
# except Exception:
# count_number = 0
# page_param.totalRecords = count_number
# # 数据分页
# sql_all=Utils.sql_paging_assemble(sql_all, page_param)
# data, ratio_all = self.default_data_list(sql_all, "use_amount")
# # 数据处理
# for i in data:
# try:
# i["ratio"] = Utils.classify(i["use_amount"], ratio_all)
# except Exception:
# i["ratio"] = "0%"
# return data
filter_base = " "
if seach_user :
filter_base + = f " by_user_name like ' % { seach_user } % ' "
filter_base + = f " create _user_name like ' % { seach_user } % ' "
if start_time and end_time :
if filter_base :
filter_base + = " and "
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
filter_base = f " where { filter_base } "
sql_all = f """
select
convert ( sum ( use_quantity ) , decimal ( 10 , 2 ) ) use_amount ,
sum ( CASE WHEN record_type = 2 THEN 1 ELSE 0 END ) use_num ,
create_user_id , create_user_name , role_name
from rms_medicament_record as a
LEFT JOIN (
SELECT role_name , user_id FROM rms_user
) b on a . create_user_id = b . user_id
LEFT JOIN ( select medicament_id , func_type from rms_medicament
) as c on c . medicament_id = a . medicament_id
{ filter_base }
"""
# 查询条件组合
# 分页总数
try :
count_number = len ( self . execute ( sql_all ) . fetchall ( ) )
except Exception :
count_number = 0
page_param . totalRecords = count_number
# 数据分页
sql_all = Utils . sql_paging_assemble ( sql_all , page_param )
if page_param :
page_param . totalRecords = count_number
# 数据分页
sql_all = Utils . sql_paging_assemble ( sql_all , page_param )
data , ratio_all = self . default_data_list ( sql_all , " use_amount " )
# 数据处理
for i in data :
@ -378,48 +616,88 @@ class BllMedicamentRecord(Repository):
except Exception :
i [ " ratio " ] = " 0 % "
return data
# 使用频率
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,
# 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`, speci, 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 %s GROUP BY medicament_id
# )b on a.medicament_id=b.medicament_id
# ) c GROUP BY `name`, purity, speci
# """
# # 查询条件sql组合
# filter_base1 = ""
# if client_id:
# filter_base1 += f" client_id='{client_id}' "
# if seach_word:
# if filter_base1:
# filter_base1 += "and "
# seach_word = f"%{seach_word}%"
# filter_base1 += f" `name` like '{seach_word}' or english_name like '{seach_word}'"
# if filter_base1:
# filter_base1 = " where " + filter_base1
# filter_base2 = ""
# 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:
# count_number = len(self.execute(sql_all).fetchall())
# except Exception:
# count_number = 0
# page_param.totalRecords = count_number
# # 处理数据
# sql_all = Utils.sql_paging_assemble(sql_all, page_param)
# data, _ = self.default_data_list(sql_all)
# return data
filter_base1 = " "
if client_id :
filter_base1 + = f " client_id= ' { client_id } ' "
if seach_word :
if filter_base1 :
filter_base1 + = " and "
seach_word = f " % { seach_word } % "
filter_base1 + = f " (`name` like ' { seach_word } ' or english_name like ' { seach_word } ' ) "
if filter_base1 :
filter_base1 = " where " + filter_base1
filter_base2 = " "
if start_time and end_time :
filter_base2 + = f " where create_date >= ' { start_time } ' and create_date <= ' { end_time } ' "
sql_all = f """
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 ,
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 ` , speci , purity , cas_number , net_weight_unit , ` status ` , client_id from rms_medicament
% s
{ filter_base1 }
) 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 % s GROUP BY medicament_id
from rms_medicament_record { filter_base2 } GROUP BY medicament_id
) b on a . medicament_id = b . medicament_id
) c GROUP BY ` name ` , purity , speci
"""
# 查询条件sql组合
filter_base1 = " "
if client_id :
filter_base1 + = f " client_id= ' { client_id } ' "
if seach_word :
if filter_base1 :
filter_base1 + = " and "
seach_word = f " % { seach_word } % "
filter_base1 + = f " `name` like ' { seach_word } ' or english_name like ' { seach_word } ' "
if filter_base1 :
filter_base1 = " where " + filter_base1
filter_base2 = " "
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 :
count_number = len ( self . execute ( sql_all ) . fetchall ( ) )
except Exception :
count_number = 0
page_param . totalRecords = count_number
# 处理数据
sql_all = Utils . sql_paging_assemble ( sql_all , page_param )
if page_param :
page_param . totalRecords = count_number
# 处理数据
sql_all = Utils . sql_paging_assemble ( sql_all , page_param )
data , _ = self . default_data_list ( sql_all )
return data