You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
5.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env python
# encoding: utf-8
"""
@author: tx
@file: screen.py
@time: 2023/3/21 15:09
@desc: 大屏数据接口
"""
import datetime
from collections import defaultdict
from db_logic.medicament import BllMedicament
from db_logic.client import BllClient
from Common.Utils import PageParam, Utils
def get_chemicals_total_num_weight():
"""
获取化学品总数总重量
:return: 化学品总数,化学品总重量
"""
total_num = BllMedicament().get_chemicals_total_num()
total_weight = BllMedicament().get_chemicals_total_weight()
return total_num, total_weight
def get_other_drug_total_num():
standard_num, reagent_num, consumables_num = BllMedicament().get_chemicals_other_total_num()
return standard_num, reagent_num, consumables_num
def count_by(lst, fn):
"""
列表分组求和
:param lst: list
:param fn: func
:return: defaultdict(<class 'int'>, {'氯化钠': 2, '盐酸': 1, '氨水': 3})
"""
d = defaultdict(int)
for el in lst:
d[fn(el)] += 1
return d
def get_storage_summary():
"""
获取今日出入库信息
:return: chemicals: 物品名
day_out_num 今日出库数
day_in_num: 今日入库数
opt_count: 操作次数
"""
start_time = datetime.datetime.now().strftime("%Y-%m-%d 00:00:00")
end_time = datetime.datetime.now().strftime("%Y-%m-%d 23:59:59")
kwargs = {
"put_in_user_name": "",
"func_type": '',
"statue_type": '',
"name": '',
"start_time": start_time,
"end_time": end_time,
"page_param": {},
"client_place": '',
"client_id": ''
}
def _get_in_out_storage_dict(record_type):
kwargs.update({"record_type": record_type})
data_list = BllMedicament().drug_show_type_info(**kwargs)
in_list = Utils.msyql_table_model(data_list)
_count_dict = count_by(in_list, lambda x: x["name"])
return _count_dict
# 入库计数
in_dict = _get_in_out_storage_dict(1)
# 领用计数
out_dict = _get_in_out_storage_dict(2)
chemical_names = set(in_dict.keys()) | set(out_dict.keys())
result = [{
"chemicals": item,
"day_out_num": out_dict[item],
"day_in_num": in_dict[item],
"opt_count": sum([out_dict[item], in_dict[item]])
} for item in chemical_names]
return result
def get_drug_operate():
"""
获取药剂操作情况列表
:return:
"""
page_param = PageParam(1, 20)
kwargs = {
"record_type": "",
"put_in_user_name": "",
"func_type": '',
"statue_type": '',
"name": '',
"start_time": "",
"end_time": "",
"page_param": page_param,
"client_place": '',
"client_id": ''
}
data_list = BllMedicament().drug_show_type_info(**kwargs)
record_list = Utils.msyql_table_model(data_list)
return record_list
def get_all_inventory():
"""
获取库存总览
:return:
"""
# 易燃易爆品
page_param = PageParam(1, 1000)
def _get_stock_all_info(functype):
data_list = BllMedicament().get_stock_all_info(
name="",
func_type=functype,
page_param=page_param,
client_place="",
client_id=""
)
data_list = Utils.msyql_table_model(data_list)
return data_list
data_list_1 = _get_stock_all_info(1)
data_list_2 = _get_stock_all_info(2)
data_list_3 = _get_stock_all_info(3)
all_list = data_list_1 + data_list_2 + data_list_3
result = defaultdict()
for item in all_list:
name = item["name"]
if name not in result:
result[name] = {"total_num": item["count_number"] if item["count_number"] else 0,
"total_weight": item["sum_remain"] if item["sum_remain"] else 0}
else:
result[name]["total_num"] += item["count_number"]
result[name]["total_weight"] += item["sum_remain"]
parse_result = [{
"name": k,
"total_num": v["total_num"],
"total_weight": v["total_weight"],
} for k, v in dict(result).items()]
return parse_result
def get_all_client_cell_ratio(func_type_dict):
"""
获取货位占比情况 每个分类func_type 所有药剂数量 / 所有位置占比
:return:
"""
def get_drug_list_total_count(func_type):
# 获取药剂总数量
page_param = PageParam(1, 10)
data_list = BllMedicament().get_drug_list(
seach_word="",
b_code="",
client_id="", func_type=func_type, page_param=page_param
)
return page_param.totalRecords
def _get_client_list_item(func_type):
# 货位总数
sql_all = f"""
select b.total_quantity as total_quantity, a.client_id, a.func_type
from (select * from rms_client where func_type = {func_type}) as a
left join
(select sum(storage_quantity) as total_quantity, client_id from rms_client_cell group by client_Id ) as b
on a.client_id = b.client_id
"""
total_quantity_list = BllClient().execute(sql_all).fetchall()
return sum([i.total_quantity for i in total_quantity_list if i.total_quantity])
func_type_list = func_type_dict.keys()
resp = dict()
for i in func_type_list:
total_records = get_drug_list_total_count(i)
total_quantity = _get_client_list_item(i)
ratio = total_records / total_quantity * 100 if total_quantity else 0
resp.update({i: f"{Utils.reserve_decimal(ratio, 2)}%"})
return resp