美化表格
This commit is contained in:
@@ -407,3 +407,96 @@ def batch_delete_sum_data(ids):
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return {'success': False, 'message': str(e)}
|
||||
|
||||
|
||||
def get_monthly_charge_degree_total(config_id=None, month=None):
|
||||
"""
|
||||
获取当月充电电量累计(从求和数据表查询,对账后的准确数据)
|
||||
|
||||
注意:
|
||||
- 数据来源于 t_daily_report_sum_data 表(线下对账后的数据,可能被手动修改过)
|
||||
- 只统计 sum_field_key = 'charge_degree' 的记录
|
||||
- 如果当天已经生成了日报并采集,自动包含在内,不会重复计算
|
||||
|
||||
Args:
|
||||
config_id: 配置ID(可选,不传则统计所有配置)
|
||||
month: 月份,格式 YYYY-MM(可选,默认当月)
|
||||
|
||||
Returns:
|
||||
dict: {
|
||||
success: bool,
|
||||
data: {
|
||||
total_degree: float, // 当月累计充电电量(kWh)
|
||||
total_days: int, // 有数据的天数
|
||||
total_orders: int, // 累计订单数
|
||||
today_degree: float, // 当天充电电量(kWh)(如果有的话)
|
||||
month: str, // 统计月份
|
||||
detail: list // 按天明细
|
||||
},
|
||||
message: str
|
||||
}
|
||||
"""
|
||||
try:
|
||||
# 默认当月
|
||||
if not month:
|
||||
month = datetime.now().strftime('%Y-%m')
|
||||
|
||||
# 拼接日期模糊匹配前缀
|
||||
date_prefix = month + '-'
|
||||
|
||||
# 构建查询SQL
|
||||
sql = """
|
||||
SELECT
|
||||
report_date,
|
||||
SUM(sum_value) as total_degree,
|
||||
SUM(total_orders) as total_orders
|
||||
FROM t_daily_report_sum_data
|
||||
WHERE sum_field_key = 'charge_degree'
|
||||
AND report_date LIKE %s
|
||||
"""
|
||||
params = [date_prefix + '%']
|
||||
|
||||
if config_id:
|
||||
sql += ' AND config_id = %s'
|
||||
params.append(config_id)
|
||||
|
||||
sql += ' GROUP BY report_date ORDER BY report_date ASC'
|
||||
|
||||
rows = execute_query(sql, tuple(params))
|
||||
|
||||
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:
|
||||
degree = float(row['total_degree'] or 0)
|
||||
orders = int(row['total_orders'] or 0)
|
||||
total_degree += degree
|
||||
total_orders += orders
|
||||
detail.append({
|
||||
'report_date': row['report_date'],
|
||||
'degree': round(degree, 3),
|
||||
'orders': orders
|
||||
})
|
||||
if row['report_date'] == today_str:
|
||||
today_degree = degree
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
'data': {
|
||||
'total_degree': round(total_degree, 3),
|
||||
'total_days': total_days,
|
||||
'total_orders': total_orders,
|
||||
'today_degree': round(today_degree, 3),
|
||||
'month': month,
|
||||
'detail': detail
|
||||
},
|
||||
'message': '查询成功'
|
||||
}
|
||||
except Exception as e:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return {'success': False, 'message': str(e)}
|
||||
|
||||
Reference in New Issue
Block a user