更改总计逻辑

This commit is contained in:
2026-07-21 15:21:29 +08:00
parent 0c73c40e8a
commit cff1074474
10 changed files with 620 additions and 50 deletions

View File

@@ -670,33 +670,41 @@ def export_sum_data_to_excel(start_date=None, end_date=None, config_id=None, dat
return {'success': False, 'message': str(e)}
def get_monthly_charge_degree_total(config_id=None, month=None, split_type=None, split_value=None):
def get_monthly_charge_degree_total(config_id=None, month=None, split_type=None, split_value=None, end_date=None):
"""
获取当月充电电量累计(从求和数据表查询,对账后的准确数据)
- 如果当天已经生成了日报并采集,自动包含在内,不会重复计算
- 以配置当前拆分为准,只统计匹配的拆分值数据
- 截止到 end_date报表日期不包含 end_date 之后的数据
- 返回缺失的日期列表(从月初到 end_date 之间缺少哪些天的数据)
Args:
config_id: 配置ID可选不传则统计所有配置
month: 月份,格式 YYYY-MM可选默认当月
split_type: 拆分类型可选company_id/user_id/station_id
split_value: 拆分值,逗号分隔的字符串(可选,用于过滤)
end_date: 截止日期,格式 YYYY-MM-DD可选默认当月最后一天
Returns:
dict: {
success: bool,
data: {
total_degree: float, // 当月累计充电电量(kWh)
total_degree: float, // 累计充电电量(kWh)
total_days: int, // 有数据的天数
total_orders: int, // 累计订单数
today_degree: float, // 当天充电电量(kWh)(如果有的话)
today_degree: float, // 报表日期当天的电量(kWh)
month: str, // 统计月份
detail: list // 按天明细
detail: list, // 按天明细
end_date: str, // 截止日期
missing_dates: list, // 缺失的日期列表
expected_days: int // 应该有的天数从月初到end_date
},
message: str
}
"""
try:
from datetime import datetime, timedelta
# 默认当月
if not month:
month = datetime.now().strftime('%Y-%m')
@@ -704,6 +712,17 @@ def get_monthly_charge_degree_total(config_id=None, month=None, split_type=None,
# 拼接日期模糊匹配前缀
date_prefix = month + '-'
# 计算截止日期(默认报表日期或当月最后一天)
if end_date:
end_date_str = end_date
else:
# 取当月最后一天
if month.endswith('-12'):
next_month = str(int(month[:4]) + 1) + '-01'
else:
next_month = month[:5] + str(int(month[5:7]) + 1).zfill(2)
end_date_str = (datetime.strptime(next_month + '-01', '%Y-%m-%d') - timedelta(days=1)).strftime('%Y-%m-%d')
# 构建查询SQL
sql = """
SELECT
@@ -713,8 +732,9 @@ def get_monthly_charge_degree_total(config_id=None, month=None, split_type=None,
FROM t_daily_report_sum_data
WHERE sum_field_key = 'charge_degree'
AND report_date LIKE %s
AND report_date <= %s
"""
params = [date_prefix + '%']
params = [date_prefix + '%', end_date_str]
if config_id:
sql += ' AND config_id = %s'
@@ -733,11 +753,18 @@ def get_monthly_charge_degree_total(config_id=None, month=None, split_type=None,
rows = execute_query(sql, tuple(params))
# 计算从月初到end_date应该有多少天
start_of_month = month + '-01'
start_date_obj = datetime.strptime(start_of_month, '%Y-%m-%d')
end_date_obj = datetime.strptime(end_date_str, '%Y-%m-%d')
expected_days = (end_date_obj - start_date_obj).days + 1
# 收集已有数据的日期
existing_dates = set()
total_degree = 0.0
total_orders = 0
total_days = len(rows)
today_degree = 0.0
today_str = datetime.now().strftime('%Y-%m-%d')
detail = []
for row in rows:
@@ -745,14 +772,23 @@ def get_monthly_charge_degree_total(config_id=None, month=None, split_type=None,
orders = int(row['total_orders'] or 0)
total_degree += degree
total_orders += orders
existing_dates.add(row['report_date'])
detail.append({
'report_date': row['report_date'],
'degree': round(degree, 3),
'orders': orders
})
if row['report_date'] == today_str:
if row['report_date'] == end_date_str:
today_degree = degree
# 计算缺失的日期
missing_dates = []
for i in range(expected_days):
date_obj = start_date_obj + timedelta(days=i)
date_str = date_obj.strftime('%Y-%m-%d')
if date_str not in existing_dates:
missing_dates.append(date_str)
return {
'success': True,
'data': {
@@ -761,7 +797,10 @@ def get_monthly_charge_degree_total(config_id=None, month=None, split_type=None,
'total_orders': total_orders,
'today_degree': round(today_degree, 3),
'month': month,
'detail': detail
'detail': detail,
'end_date': end_date_str,
'missing_dates': missing_dates,
'expected_days': expected_days
},
'message': '查询成功'
}