修复财务接口:统一date字段查询,板子全平台合计
This commit is contained in:
143
backend/view.py
143
backend/view.py
@@ -38,7 +38,7 @@ from backend.utils import (
|
||||
update_dashou_daily_by_action,
|
||||
update_shangjia_daily
|
||||
)
|
||||
from jituan.services.club_context import filter_queryset_by_club, filter_user_related_by_club, filter_user_qs_by_club, orders_for_request
|
||||
from jituan.services.club_context import filter_club_char_field, filter_queryset_by_club, filter_user_related_by_club, filter_user_qs_by_club, orders_for_request
|
||||
from jituan.services.club_penalty import (
|
||||
filter_penalty_qs, resolve_penalty_club_id, filter_gsfenhong_qs,
|
||||
forbid_penalty_out_of_scope, resolve_penalty_record_club_id,
|
||||
@@ -6939,7 +6939,7 @@ class CaiwuView(APIView):
|
||||
today_start = datetime.combine(today, datetime.min.time())
|
||||
today_end = today_start + timedelta(days=1)
|
||||
|
||||
# 今日收入 / 支出(全平台各俱乐部合计,只读 daily_income_stat / daily_payout_stat)
|
||||
# 四个板子 + 累计:全平台 daily_income_stat / daily_payout_stat 按 date 汇总(不按俱乐部缩范围)
|
||||
today_inc = DailyIncomeStat.query.filter(date=today).aggregate(
|
||||
total_amount=Sum('total_amount'),
|
||||
total_count=Sum('total_count'),
|
||||
@@ -7016,12 +7016,12 @@ class CaiwuView(APIView):
|
||||
all_zuzhang_yue = float(UserZuzhang.query.aggregate(total=Sum('ketixian_jine'))['total'] or 0.00)
|
||||
all_shangjia_yue = float(UserShangjia.query.aggregate(total=Sum('yue'))['total'] or 0.00)
|
||||
|
||||
# 平台总收支及利润(只读日统计表)
|
||||
# 累计收支 / 利润:日统计表全平台各行相加
|
||||
total_income = float(DailyIncomeStat.query.aggregate(total=Sum('total_amount'))['total'] or 0.00)
|
||||
total_payout = float(DailyPayoutStat.query.aggregate(total=Sum('total_amount'))['total'] or 0.00)
|
||||
platform_profit = round(total_income - total_payout, 2)
|
||||
|
||||
# 近30天每日收支明细(按日期合并各俱乐部)
|
||||
# 近30天每日收支明细(全平台)
|
||||
daily_stats = []
|
||||
since = today - timedelta(days=30)
|
||||
income_by_date = {
|
||||
@@ -7340,19 +7340,14 @@ class SzxxView(APIView):
|
||||
"""
|
||||
每日收支详细统计数据
|
||||
POST /houtai/szxx
|
||||
Body: {
|
||||
"phone": "管理员手机号",
|
||||
"granularity": "day" | "month" | "year",
|
||||
"year": 2026, // day/month 时必填
|
||||
"month": 5, // day 时必填
|
||||
"summary_date": "2026-05-16" // 可选,格式随颗粒度
|
||||
}
|
||||
只读 daily_income_stat / daily_payout_stat,按 date 字段汇总。
|
||||
"""
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def post(self, request):
|
||||
try:
|
||||
# 避免模块名冲突,局部导入
|
||||
from jituan.services.szxx_stats import build_szxx_payload
|
||||
|
||||
username_from_frontend = request.data.get('phone', None)
|
||||
kefu_obj, permissions = verify_kefu_permission(request, username_from_frontend)
|
||||
if kefu_obj is None:
|
||||
@@ -7366,127 +7361,11 @@ class SzxxView(APIView):
|
||||
month = request.data.get('month')
|
||||
summary_date = request.data.get('summary_date')
|
||||
|
||||
now = date.today()
|
||||
data, err = build_szxx_payload(request, granularity, year, month, summary_date)
|
||||
if err:
|
||||
return Response({'code': 1, 'msg': err}, status=400)
|
||||
|
||||
# ---------- 时间范围与分组字段 ----------
|
||||
if granularity == 'day':
|
||||
if not year or not month:
|
||||
return Response({'code': 1, 'msg': '按日统计需要年份和月份'}, status=400)
|
||||
# 分组字段
|
||||
group_field = 'day'
|
||||
# 过滤条件
|
||||
income_filter = {'year': year, 'month': month}
|
||||
payout_filter = {'year': year, 'month': month}
|
||||
# 默认汇总日期
|
||||
if not summary_date:
|
||||
summary_date = now.strftime('%Y-%m-%d')
|
||||
summary_parts = summary_date.split('-')
|
||||
summary_filter = {'year': int(summary_parts[0]), 'month': int(summary_parts[1]), 'day': int(summary_parts[2])}
|
||||
# 用于构造返回的日期字符串
|
||||
date_format = '%Y-%m-%d'
|
||||
|
||||
elif granularity == 'month':
|
||||
if not year:
|
||||
return Response({'code': 1, 'msg': '按月统计需要年份'}, status=400)
|
||||
group_field = 'month'
|
||||
income_filter = {'year': year}
|
||||
payout_filter = {'year': year}
|
||||
if not summary_date:
|
||||
summary_date = now.strftime('%Y-%m')
|
||||
summary_parts = summary_date.split('-')
|
||||
summary_filter = {'year': int(summary_parts[0]), 'month': int(summary_parts[1])}
|
||||
date_format = '%Y-%m'
|
||||
|
||||
elif granularity == 'year':
|
||||
group_field = 'year'
|
||||
income_filter = {}
|
||||
payout_filter = {}
|
||||
if not summary_date:
|
||||
summary_date = str(now.year)
|
||||
summary_filter = {'year': int(summary_date)}
|
||||
date_format = '%Y'
|
||||
else:
|
||||
return Response({'code': 1, 'msg': '无效的颗粒度'}, status=400)
|
||||
|
||||
# ---------- 收入分组查询(daily_income_stat)----------
|
||||
income_qs = filter_queryset_by_club(
|
||||
DailyIncomeStat.query.filter(**income_filter), request,
|
||||
).values(group_field) \
|
||||
.annotate(total_income=Sum('total_amount'), total_count=Sum('total_count')) \
|
||||
.order_by(group_field)
|
||||
|
||||
# ---------- 支出分组查询(daily_payout_stat)----------
|
||||
payout_qs = filter_queryset_by_club(
|
||||
DailyPayoutStat.query.filter(**payout_filter), request,
|
||||
).values(group_field) \
|
||||
.annotate(total_payout=Sum('total_amount'), total_count=Sum('total_count')) \
|
||||
.order_by(group_field)
|
||||
|
||||
# ---------- 合并数据 ----------
|
||||
data_map = {}
|
||||
for item in income_qs:
|
||||
key = item[group_field]
|
||||
data_map[key] = {
|
||||
'income': float(item['total_income'] or 0),
|
||||
'income_count': item['total_count'] or 0,
|
||||
'payout': 0.0,
|
||||
'payout_count': 0
|
||||
}
|
||||
|
||||
for item in payout_qs:
|
||||
key = item[group_field]
|
||||
if key not in data_map:
|
||||
data_map[key] = {'income': 0.0, 'income_count': 0, 'payout': 0.0, 'payout_count': 0}
|
||||
data_map[key]['payout'] = float(item['total_payout'] or 0)
|
||||
data_map[key]['payout_count'] = item['total_count'] or 0
|
||||
|
||||
time_series = []
|
||||
for key, vals in data_map.items():
|
||||
# 构造日期字符串
|
||||
if granularity == 'day':
|
||||
date_str = f"{year}-{month:02d}-{key:02d}"
|
||||
elif granularity == 'month':
|
||||
date_str = f"{year}-{key:02d}"
|
||||
else:
|
||||
date_str = str(key)
|
||||
profit = round(vals['income'] - vals['payout'], 2)
|
||||
time_series.append({
|
||||
'date': date_str,
|
||||
'income': vals['income'],
|
||||
'payout': vals['payout'],
|
||||
'profit': profit
|
||||
})
|
||||
time_series.sort(key=lambda x: x['date'])
|
||||
|
||||
# ---------- 汇总数据 ----------
|
||||
income_summary = filter_queryset_by_club(
|
||||
DailyIncomeStat.query.filter(**summary_filter), request,
|
||||
).aggregate(
|
||||
total_income=Sum('total_amount'), total_count=Sum('total_count')
|
||||
)
|
||||
payout_summary = filter_queryset_by_club(
|
||||
DailyPayoutStat.query.filter(**summary_filter), request,
|
||||
).aggregate(
|
||||
total_payout=Sum('total_amount'), total_count=Sum('total_count')
|
||||
)
|
||||
total_income = float(income_summary['total_income'] or 0)
|
||||
total_payout = float(payout_summary['total_payout'] or 0)
|
||||
total_profit = round(total_income - total_payout, 2)
|
||||
|
||||
return Response({
|
||||
'code': 0,
|
||||
'msg': 'ok',
|
||||
'data': {
|
||||
'time_series': time_series,
|
||||
'summary': {
|
||||
'total_income': total_income,
|
||||
'total_income_count': income_summary['total_count'] or 0,
|
||||
'total_payout': total_payout,
|
||||
'total_payout_count': payout_summary['total_count'] or 0,
|
||||
'total_profit': total_profit
|
||||
}
|
||||
}
|
||||
})
|
||||
return Response({'code': 0, 'msg': 'ok', 'data': data})
|
||||
|
||||
except Exception as e:
|
||||
logger.exception("SzxxView 接口错误")
|
||||
|
||||
Reference in New Issue
Block a user