Commit 018ca4ce authored by xianyang's avatar xianyang

优化业务汇总

parent 8dfc3210
...@@ -214,68 +214,6 @@ def get_finance_info(unique_tag, page, size, start_time, end_time, is_list=None) ...@@ -214,68 +214,6 @@ def get_finance_info(unique_tag, page, size, start_time, end_time, is_list=None)
return res return res
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 = []
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 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')} ")
month, last_month, before_last_month = get_last_month()
with ThreadPoolExecutor(max_workers=3) as pool:
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.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
def get_account_type(db: Session, **data): def get_account_type(db: Session, **data):
"""礼物类型配置列表""" """礼物类型配置列表"""
finance_filters = [] finance_filters = []
...@@ -320,3 +258,100 @@ def create_type(db: Session, data): ...@@ -320,3 +258,100 @@ def create_type(db: Session, data):
print(e) print(e)
return {} return {}
return db_type return db_type
class AccountStatistics(object):
"""账户列表,查询"""
def __init__(self, page, size, uuid, user_id, start_time, end_time, type, gift_type, unique):
self.page = page
self.size = size
self.uuid = uuid
self.user_id = user_id
self.start_time = start_time
self.end_time = end_time
self.type = type
self.gift_type = gift_type
self.unique = unique
self.guild_uuid = []
def business_query(self, date, condition):
"""业务数据查询"""
if self.unique in ["user_account", "guild_account", "knapsack_account"]:
u_sql = f"select uuid,type,SUM(cast(amount as decimal(20,6)))/1000 as amount,FROM_UNIXTIME(create_time, '%y%m%d') as create_time,amount_type from {date} WHERE {' and '.join(condition)} GROUP BY uuid,type ORDER BY FROM_UNIXTIME(create_time, '%y%m%d') DESC"
else:
if condition:
u_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:
u_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(u_sql)
return result
def user_guild_query(self, data):
guild_sql = f"select uuid from guild"
system_sql = f"select uuid from fi_account"
with ThreadPoolExecutor(max_workers=2) as pool:
guild = pool.submit(LinkMysql(env.DB_3YV2).query_mysql, guild_sql)
system = pool.submit(LinkMysql(env.DB_3YV2).query_mysql, system_sql)
guild_uuid_list = guild.result()
system_uuid_list = system.result()
res_list = []
if self.unique == 'guild_account':
for info in data:
if info.get('uuid') in guild_uuid_list:
res_list.append(info)
if self.unique == 'knapsack_account':
for info in data:
if info.get('amount_type') == 'backpack':
res_list.append(info)
if self.unique == 'user_account':
for info in data:
if info.get('uuid') not in guild_uuid_list and info.get('uuid') not in system_uuid_list:
res_list.append(info)
return res_list
def public_query(self):
"""公共查询条件判断"""
public_list = []
if self.gift_type:
public_list.append(f" reference_type = {self.gift_type}")
if self.type or self.type == 0:
public_list.append(f" type={self.type}")
if self.start_time:
public_list.append(f" create_time >= {time_str_to_timestamp(self.start_time + ' 00:00:00')} ")
if self.end_time:
public_list.append(f" create_time <= {time_str_to_timestamp(self.end_time + ' 23:59:59')} ")
return public_list
def get_finance_details(self, is_list=None):
"""账户财务明细"""
total_list = []
yw_condition = self.public_query()
month, last_month, before_last_month = get_last_month()
with ThreadPoolExecutor(max_workers=3) as pool:
future1 = pool.submit(self.business_query, 'assets_log_' + month, yw_condition)
future2 = pool.submit(self.business_query, 'assets_log_' + last_month, yw_condition)
future3 = pool.submit(self.business_query, 'assets_log_' + before_last_month, yw_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
ultimately_data = total_list + before_last_data if before_last_data else total_list
if self.unique in ["user_account", "guild_account", "knapsack_account"]:
ultimately_data = self.user_guild_query(total_list)
total = len(ultimately_data)
res = ultimately_data[int(self.page - 1) * self.size: self.page * self.size]
# 判断是列表还是导出接口
if is_list:
if not res:
return [], 0, 0
for i in res:
i['reference_name'] = TYPE_NAME[i['reference_type']] if TYPE_NAME.get(i['reference_type']) else i[
'reference_type']
data_pd = pd.DataFrame(ultimately_data)
amount_total = data_pd['amount'].sum()
return res, total, float(amount_total)
else:
return res
...@@ -42,6 +42,8 @@ class FinanceDetails(PublicModel): ...@@ -42,6 +42,8 @@ class FinanceDetails(PublicModel):
type: Optional[int] = None type: Optional[int] = None
gift_type: Optional[str] = "" gift_type: Optional[str] = ""
uuid: str uuid: str
user_id: Optional[int] = None
unique_tag: Optional[str] = ""
class FixTable(BaseModel): class FixTable(BaseModel):
......
...@@ -3,6 +3,7 @@ from fastapi import Depends, APIRouter, Request, Query ...@@ -3,6 +3,7 @@ from fastapi import Depends, APIRouter, Request, Query
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from app import get_db from app import get_db
from app.api.account import schemas, crud from app.api.account import schemas, crud
from app.api.account.crud import AccountStatistics
from app.api.statement import crud as statement_crud from app.api.statement import crud as statement_crud
from libs.result_format import HttpResultResponse, HttpMessage from libs.result_format import HttpResultResponse, HttpMessage
from libs.token_verify import login_required from libs.token_verify import login_required
...@@ -77,11 +78,13 @@ def finance_details(page: int, ...@@ -77,11 +78,13 @@ def finance_details(page: int,
end_time: Optional[str] = "", end_time: Optional[str] = "",
type: Optional[int] = None, type: Optional[int] = None,
gift_type: Optional[str] = "", gift_type: Optional[str] = "",
unique_tag: Optional[str] = "",
token=Depends(login_required)): token=Depends(login_required)):
"""账户财务明细列表""" """账户财务明细列表"""
if not start_time and not end_time: if not start_time and not end_time:
return HttpResultResponse(code=500, msg="请输入你要查询的时间段") 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) res, total, count = AccountStatistics(page, size, uuid, user_id, start_time, end_time, type, gift_type,
unique_tag).get_finance_details(is_list=1)
return HttpResultResponse(total=total, data=res, count=count) return HttpResultResponse(total=total, data=res, count=count)
...@@ -90,11 +93,27 @@ def finance_info_excel(data: schemas.FinanceDetails, request: Request, ...@@ -90,11 +93,27 @@ def finance_info_excel(data: schemas.FinanceDetails, request: Request,
token=Depends(login_required), db: Session = Depends(get_db)): token=Depends(login_required), db: Session = Depends(get_db)):
"""账户财务明细导出""" """账户财务明细导出"""
headers = request.get("headers") headers = request.get("headers")
statement_list = crud.get_finance_details(data.page, 99999, data.uuid, data.start_time, data.end_time, data.type, data.gift_type) statement_list = AccountStatistics(data.page, 99999, data.uuid, data.user_id, data.start_time, data.end_time, data.type,
data.gift_type, data.unique_tag).get_finance_details()
field_head = ['订单号', '出入账方式', '礼物类型', '金额', '时间'] field_head = ['订单号', '出入账方式', '礼物类型', '金额', '时间']
return statement_crud.data_to_file(db, statement_list, "财务明细", headers, field_head) return statement_crud.data_to_file(db, statement_list, "财务明细", headers, field_head)
@router.get("/finance/fourth/details")
def finance_fourth_info(page: int,
size: int,
uuid: str,
start_time: Optional[str] = "",
end_time: Optional[str] = "",
type: Optional[int] = None,
amount_type: Optional[str] = "",
token=Depends(login_required)):
"""账户财务明细第四层"""
# res, total, count = AccountStatistics.get_finance_details(page, size, uuid, start_time, end_time, type,
# amount_type, l)
# return HttpResultResponse(total=total, data=res, count=count)
@router.get("/type") @router.get("/type")
def finance_fix(page: int, def finance_fix(page: int,
size: int, size: int,
......
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