Commit 77a301e1 authored by xianyang's avatar xianyang

业务类型汇总缓存

parent 925d8546
......@@ -3,7 +3,7 @@ import threading
from concurrent.futures.thread import ThreadPoolExecutor
from sqlalchemy.orm import Session
from core.config.env import env
from core.config.env import env, red
from libs.business import TYPE_NAME, query_fi_account_type
from libs.db_link import LinkMysql
from libs.functions import get_now_datetime
......@@ -100,9 +100,9 @@ class CalculationMonthlyBill(object):
# reference_type第二次进来 self.structure_dict有拼接好的数据
if reference_type in self.structure_key:
if type == 0:
self.structure_dict[reference_type]['expenditure'] = v / 1000
self.structure_dict[reference_type]['expenditure'] = float('%.3f' % (v / 1000))
else:
self.structure_dict[reference_type]['income'] = v / 1000
self.structure_dict[reference_type]['income'] = float('%.3f' % (v / 1000))
else:
# reference_type类型第一次进来 类型加入self.structure_key列表,拼接数据加入self.structure_dict下次计算异常数据
if reference_type in TYPE_NAME:
......@@ -118,20 +118,42 @@ class CalculationMonthlyBill(object):
"error_money": 0
}
if type == 0:
constructed['expenditure'] = v / 1000
constructed['expenditure'] = float('%.3f' % (v / 1000))
else:
constructed['income'] = v / 1000
constructed['income'] = float('%.3f' % (v / 1000))
self.structure_key.append(reference_type)
self.structure_dict[reference_type] = constructed
def kv_search(self):
"""查询筛选的key, value"""
k_list = []
type_name = query_fi_account_type()
for k, v in type_name.items():
if v == self.name or self.name in v:
k_list.append(k)
return k_list
def search_red_data(self, red_str):
"""redis缓存数据筛选"""
res_list = []
red_data_list = list(eval(red_str))
for reds in red_data_list:
if self.name and not self.key_type:
if self.name in reds.get('name'):
res_list.append(reds)
elif self.key_type and not self.name:
if self.key_type in reds.get('type'):
res_list.append(reds)
else:
if self.name in reds.get('name') and self.key_type in reds.get('type'):
res_list.append(reds)
return res_list
def month_statistics_task(self):
business_type_sum_data = red.get('business_type_sum-' + str(self.date))
assert_list = []
if self.name:
k_list = []
type_name = query_fi_account_type()
for k, v in type_name.items():
if v == self.name or self.name in v:
k_list.append(k)
k_list = self.kv_search()
if len(k_list) > 1:
assert_list.append(f" reference_type in{tuple(k_list)}")
if len(k_list) == 1:
......@@ -141,27 +163,35 @@ class CalculationMonthlyBill(object):
assert_list.append(f" reference_type='{py.replace('-', '')}'")
if self.key_type:
assert_list.append(f" reference_type like '%{self.key_type}%'")
count_sql = f"select count(id) as num from {self.date}"
count_data = LinkMysql(env.DB_HISTORY).query_mysql(count_sql)
num = math.ceil(count_data[0]['num'] / 10)
# 多线程
ths = []
# 创建线程
for i in range(10):
ths.append(threading.Thread(target=self.thead_task,
args=[assert_list, num*i, num]))
# 启动线程
for i in range(10):
ths[i].start()
# 等待子线程
for i in range(10):
ths[i].join()
self.data_deal_with()
res_all_data = []
for k, v in self.structure_dict.items():
v['is_error'] = 0 if v['expenditure'] == v['income'] else 1
v['error_money'] = float('%.3f' % (v['expenditure'] - v['income']))
res_all_data.append(v)
if not business_type_sum_data:
count_sql = f"select count(id) as num from {self.date}"
count_data = LinkMysql(env.DB_HISTORY).query_mysql(count_sql)
num = math.ceil(count_data[0]['num'] / 10)
# 多线程
ths = []
# 创建线程
for i in range(10):
ths.append(threading.Thread(target=self.thead_task,
args=[assert_list, num*i, num]))
# 启动线程
for i in range(10):
ths[i].start()
# 等待子线程
for i in range(10):
ths[i].join()
self.data_deal_with()
res_all_data = []
for k, v in self.structure_dict.items():
v['is_error'] = 0 if v['expenditure'] == v['income'] else 1
v['error_money'] = float('%.3f' % (v['expenditure'] - v['income']))
res_all_data.append(v)
# 存入redis
red.set('business_type_sum-' + str(self.date), str(res_all_data), 3600)
else:
if assert_list:
res_all_data = self.search_red_data(business_type_sum_data)
else:
res_all_data = list(eval(business_type_sum_data)) # 返回redis缓存
return res_all_data[(self.page - 1) * self.size:self.page * self.size], len(res_all_data)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment