Commit 77a301e1 authored by xianyang's avatar xianyang

业务类型汇总缓存

parent 925d8546
...@@ -3,7 +3,7 @@ import threading ...@@ -3,7 +3,7 @@ import threading
from concurrent.futures.thread import ThreadPoolExecutor from concurrent.futures.thread import ThreadPoolExecutor
from sqlalchemy.orm import Session 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.business import TYPE_NAME, query_fi_account_type
from libs.db_link import LinkMysql from libs.db_link import LinkMysql
from libs.functions import get_now_datetime from libs.functions import get_now_datetime
...@@ -100,9 +100,9 @@ class CalculationMonthlyBill(object): ...@@ -100,9 +100,9 @@ class CalculationMonthlyBill(object):
# reference_type第二次进来 self.structure_dict有拼接好的数据 # reference_type第二次进来 self.structure_dict有拼接好的数据
if reference_type in self.structure_key: if reference_type in self.structure_key:
if type == 0: if type == 0:
self.structure_dict[reference_type]['expenditure'] = v / 1000 self.structure_dict[reference_type]['expenditure'] = float('%.3f' % (v / 1000))
else: else:
self.structure_dict[reference_type]['income'] = v / 1000 self.structure_dict[reference_type]['income'] = float('%.3f' % (v / 1000))
else: else:
# reference_type类型第一次进来 类型加入self.structure_key列表,拼接数据加入self.structure_dict下次计算异常数据 # reference_type类型第一次进来 类型加入self.structure_key列表,拼接数据加入self.structure_dict下次计算异常数据
if reference_type in TYPE_NAME: if reference_type in TYPE_NAME:
...@@ -118,20 +118,42 @@ class CalculationMonthlyBill(object): ...@@ -118,20 +118,42 @@ class CalculationMonthlyBill(object):
"error_money": 0 "error_money": 0
} }
if type == 0: if type == 0:
constructed['expenditure'] = v / 1000 constructed['expenditure'] = float('%.3f' % (v / 1000))
else: else:
constructed['income'] = v / 1000 constructed['income'] = float('%.3f' % (v / 1000))
self.structure_key.append(reference_type) self.structure_key.append(reference_type)
self.structure_dict[reference_type] = constructed self.structure_dict[reference_type] = constructed
def month_statistics_task(self): def kv_search(self):
assert_list = [] """查询筛选的key, value"""
if self.name:
k_list = [] k_list = []
type_name = query_fi_account_type() type_name = query_fi_account_type()
for k, v in type_name.items(): for k, v in type_name.items():
if v == self.name or self.name in v: if v == self.name or self.name in v:
k_list.append(k) 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 = self.kv_search()
if len(k_list) > 1: if len(k_list) > 1:
assert_list.append(f" reference_type in{tuple(k_list)}") assert_list.append(f" reference_type in{tuple(k_list)}")
if len(k_list) == 1: if len(k_list) == 1:
...@@ -141,6 +163,7 @@ class CalculationMonthlyBill(object): ...@@ -141,6 +163,7 @@ class CalculationMonthlyBill(object):
assert_list.append(f" reference_type='{py.replace('-', '')}'") assert_list.append(f" reference_type='{py.replace('-', '')}'")
if self.key_type: if self.key_type:
assert_list.append(f" reference_type like '%{self.key_type}%'") assert_list.append(f" reference_type like '%{self.key_type}%'")
if not business_type_sum_data:
count_sql = f"select count(id) as num from {self.date}" count_sql = f"select count(id) as num from {self.date}"
count_data = LinkMysql(env.DB_HISTORY).query_mysql(count_sql) count_data = LinkMysql(env.DB_HISTORY).query_mysql(count_sql)
num = math.ceil(count_data[0]['num'] / 10) num = math.ceil(count_data[0]['num'] / 10)
...@@ -162,6 +185,13 @@ class CalculationMonthlyBill(object): ...@@ -162,6 +185,13 @@ class CalculationMonthlyBill(object):
v['is_error'] = 0 if v['expenditure'] == v['income'] else 1 v['is_error'] = 0 if v['expenditure'] == v['income'] else 1
v['error_money'] = float('%.3f' % (v['expenditure'] - v['income'])) v['error_money'] = float('%.3f' % (v['expenditure'] - v['income']))
res_all_data.append(v) 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) 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