Commit 44ca1ca4 authored by xianyang's avatar xianyang

优化业务汇总用户背包

parent 7a67eec5
......@@ -10,7 +10,7 @@ from core.config.env import env
from libs.business import TYPE_NAME
from libs.db_link import LinkMysql
from libs.functions import wrapper_out, get_now_timestamp, uuid, get_before_timestamp, time_str_to_timestamp, \
get_yesterday_timestamp, search
get_yesterday_timestamp, search, get_last_month
from libs.orm import QueryAllData
from models import account as models
from models.account import AccountFinance, AccountFinanceDetails, AccountType
......@@ -214,43 +214,64 @@ def get_finance_info(unique_tag, page, size, start_time, end_time, is_list=None)
return res
def get_finance_details(page, size, uuid, start_time, end_time, type, gift_type, is_list=None):
def user_query(date, condition):
"""用户查询"""
if condition:
# user_sql = f"select FROM_UNIXTIME(create_time, '%y%m%d') as create_time,type,SUM(cast(amount as decimal(20,6)))/1000 as money,reference_type from {date} WHERE {' and '.join(condition)} GROUP BY FROM_UNIXTIME(create_time, '%y%m%d'),type ORDER BY FROM_UNIXTIME(create_time, '%y%m%d') DESC"
user_sql = f"select id,uuid,order_number,type,reference_type,amount/1000 as amount,create_time from {date} WHERE {' and '.join(condition)} ORDER BY create_time DESC"
else:
user_sql = f"select id,uuid,order_number,type,reference_type,amount/1000 as amount,create_time from {date} ORDER BY create_time DESC"
result = LinkMysql(env.DB_HISTORY).query_mysql(user_sql)
return result
def get_finance_details(page, size, uuid, user_id, start_time, end_time, type, gift_type, is_list=None):
"""账户财务明细"""
details_condition = [f" uuid='{uuid}' "]
details_condition = []
total_list = []
if user_id:
user_sql = f"select uuid from v2_user where user_id={user_id} limit 0,1"
result_user = LinkMysql(env.DB_3YV2).query_mysql(user_sql)
if result_user:
uuid = result_user[0]['uuid']
if uuid != "52cbb068-1676-7b36-4bc5-bbc33fdc172f":
details_condition.append(f" uuid='{uuid}' ")
if uuid == '52cbb068-1676-7b36-4bc5-bbc33fdc172f' and not gift_type: # 区分用户账户
reference_type_list = ['userWithdrawal', 'userRecharge', 'pay_discount', 'studioGift']
details_condition.append(f" reference_type in{tuple(reference_type_list)}")
if gift_type:
details_condition.append(f" reference_type = {gift_type}")
if type or type == 0:
details_condition.append(f" type={type}")
if gift_type:
details_condition.append(f" reference_type like '%{gift_type}%'")
if start_time:
details_condition.append(f" create_time >= {time_str_to_timestamp(start_time + ' 00:00:00')} ")
if end_time:
details_condition.append(f" create_time <= {time_str_to_timestamp(end_time + ' 23:59:59')} ")
year_month = datetime.now().strftime('%Y%m')
if details_condition:
count_sql = f"select count(id) as num from assets_log_{year_month} where {' and '.join(details_condition)}"
data_sql = f"select id,order_number,type,reference_type,amount/1000 as amount,create_time from assets_log_{year_month} where {' and '.join(details_condition)} order by id DESC limit {(int(page) - 1) * size},{size}"
amount_sql = f"select sum(cast(amount as decimal(20,6)))/1000 as total_amount from assets_log_{year_month} where {' and '.join(details_condition)}"
else:
count_sql = f"select count(id) as num from assets_log_{year_month}"
data_sql = f"select id,order_number,type,reference_type,amount/1000 as amount,create_time from assets_log_{year_month} order by id DESC limit {(int(page) - 1) * size},{size}"
amount_sql = f"select sum(cast(amount as decimal(20,6)))/1000 as total_amount from assets_log_{year_month}"
month, last_month, before_last_month = get_last_month()
with ThreadPoolExecutor(max_workers=3) as pool:
future1 = pool.submit(LinkMysql(env.DB_HISTORY).query_mysql, count_sql)
future2 = pool.submit(LinkMysql(env.DB_HISTORY).query_mysql, data_sql)
future3 = pool.submit(LinkMysql(env.DB_HISTORY).query_mysql, amount_sql)
total = future1.result()
res = future2.result()
amount_res = future3.result()
future1 = pool.submit(user_query, 'assets_log_' + month, details_condition)
future2 = pool.submit(user_query, 'assets_log_' + last_month, details_condition)
future3 = pool.submit(user_query, 'assets_log_' + before_last_month, details_condition)
month_data = future1.result()
last_data = future2.result()
before_last_data = future3.result()
total_list = total_list + month_data if month_data else total_list
total_list = total_list + last_data if last_data else total_list
total_list = total_list + before_last_data if before_last_data else total_list
total = len(total_list)
res = total_list[int(page - 1) * size: page * size]
import pandas as pd
# 判断是列表还是导出接口
if is_list:
if not res:
return [], 0, 0
for i in res:
i['reference_name'] = TYPE_NAME[i['reference_type']] if TYPE_NAME[i['reference_type']] else i['reference_type']
return res, total[0]['num'], amount_res[0]['total_amount']
i['reference_name'] = TYPE_NAME[i['reference_type']] if TYPE_NAME.get(i['reference_type']) else i['reference_type']
data_pd = pd.DataFrame(total_list)
amount_total = data_pd['amount'].sum()
return res, total, float(amount_total)
else:
return res
......
......@@ -53,7 +53,7 @@ def finance_information(unique_tag: str,
end_time: Optional[str] = "",
token=Depends(login_required)):
# unique_tag:Optional[str] = Query(None, min_length=3, max_length=50, regex="^xiao\d+$") 参数验证
"""账户财务详情"""
"""账户财务详情列表"""
res, total = crud.get_finance_info(unique_tag, page, size, start_time, end_time, 1)
return HttpResultResponse(total=total, data=res)
......@@ -72,13 +72,16 @@ def finance_info_excel(data: schemas.FinanceInfo, request: Request,
def finance_details(page: int,
size: int,
uuid: str,
user_id: Optional[str] = "",
start_time: Optional[str] = "",
end_time: Optional[str] = "",
type: Optional[int] = None,
gift_type: Optional[str] = "",
token=Depends(login_required)):
"""账户财务明细"""
res, total, count = crud.get_finance_details(page, size, uuid, start_time, end_time, type, gift_type, is_list=1)
"""账户财务明细列表"""
if not start_time and not end_time:
return HttpResultResponse(code=500, msg="请输入你要查询的时间段")
res, total, count = crud.get_finance_details(page, size, uuid, user_id, start_time, end_time, type, gift_type, is_list=1)
return HttpResultResponse(total=total, data=res, count=count)
......
......@@ -243,7 +243,7 @@ def account_money(uuid, amount_type):
print(f"清算系统异常:{e}")
platform_money = '清算系统异常'
return platform_money
return 0
return '清算系统异常'
def query_token(db, h_list):
......
......@@ -121,7 +121,7 @@ def query_account_money(uuid: str, amount_type: Optional[str] = "consumable", to
if not uuid:
return HttpResultResponse(code=500, msg='请输入uuid')
consumable = account_money(uuid, amount_type)
if not consumable:
if consumable == '清算系统异常':
return HttpResultResponse(code=500, msg='清算系统异常,暂未查询到余额,请稍后再试!')
return HttpResultResponse(data=consumable)
......
......@@ -59,6 +59,13 @@ def timestamp_to_time_str(time_stamp):
return time.strftime("%Y-%m-%d %H:%M:%S", time_array)
def get_last_month():
"""获取三个月月份"""
last_month = datetime.now().date() - relativedelta(months=1)
before_last_month = datetime.now().date() - relativedelta(months=2)
return datetime.now().strftime("%Y%m"), last_month.strftime("%Y%m"), before_last_month.strftime("%Y%m")
def md5(s):
"""md5加密"""
sign_str = hashlib.md5()
......
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