增加求和采集和细节修改
This commit is contained in:
@@ -21,8 +21,11 @@ init_bp = Blueprint('init', __name__, url_prefix='/api')
|
||||
# 文件下载蓝图
|
||||
download_bp = Blueprint('download', __name__, url_prefix='/api')
|
||||
|
||||
# 求和数据管理蓝图
|
||||
sum_data_bp = Blueprint('sum_data', __name__, url_prefix='/api')
|
||||
|
||||
# 导入各模块的路由
|
||||
from . import config_api, entity_api, field_api, report_api, init_api, download_api
|
||||
from . import config_api, entity_api, field_api, report_api, init_api, download_api, sum_data_api
|
||||
|
||||
|
||||
def register_blueprints(app):
|
||||
@@ -33,3 +36,4 @@ def register_blueprints(app):
|
||||
app.register_blueprint(report_bp)
|
||||
app.register_blueprint(init_bp)
|
||||
app.register_blueprint(download_bp)
|
||||
app.register_blueprint(sum_data_bp)
|
||||
|
||||
@@ -208,50 +208,60 @@ def move_config(config_id):
|
||||
"""移动配置排序"""
|
||||
try:
|
||||
direction = request.json.get('direction') # up 或 down
|
||||
print(f"[move_config] 开始移动 config_id={config_id}, direction={direction}")
|
||||
|
||||
# 获取当前配置
|
||||
current = execute_query(
|
||||
'SELECT sort_order FROM t_daily_report_config WHERE id = %s',
|
||||
(config_id,)
|
||||
# 查出所有配置,按 sort_order, id 排序
|
||||
all_configs = execute_query(
|
||||
'SELECT id, sort_order FROM t_daily_report_config ORDER BY sort_order ASC, id ASC'
|
||||
)
|
||||
if not current:
|
||||
print(f"[move_config] 所有配置数量: {len(all_configs)}")
|
||||
for idx, c in enumerate(all_configs):
|
||||
print(f" [{idx}] id={c['id']}, sort_order={c['sort_order']}")
|
||||
|
||||
# 找到当前配置的索引
|
||||
current_index = None
|
||||
for i, c in enumerate(all_configs):
|
||||
if c['id'] == config_id:
|
||||
current_index = i
|
||||
break
|
||||
|
||||
if current_index is None:
|
||||
print(f"[move_config] 未找到配置 id={config_id}")
|
||||
return jsonify({'success': False, 'message': '配置不存在'}), 404
|
||||
|
||||
current_order = current[0]['sort_order']
|
||||
print(f"[move_config] 当前配置索引: {current_index}")
|
||||
|
||||
# 计算目标索引
|
||||
if direction == 'up':
|
||||
# 找到上一个配置
|
||||
prev = execute_query(
|
||||
'SELECT id, sort_order FROM t_daily_report_config WHERE sort_order < %s ORDER BY sort_order DESC LIMIT 1',
|
||||
(current_order,)
|
||||
)
|
||||
if prev:
|
||||
# 交换排序
|
||||
execute_update(
|
||||
'UPDATE t_daily_report_config SET sort_order = %s WHERE id = %s',
|
||||
(prev[0]['sort_order'], config_id)
|
||||
)
|
||||
execute_update(
|
||||
'UPDATE t_daily_report_config SET sort_order = %s WHERE id = %s',
|
||||
(current_order, prev[0]['id'])
|
||||
)
|
||||
target_index = current_index - 1
|
||||
elif direction == 'down':
|
||||
# 找到下一个配置
|
||||
next_config = execute_query(
|
||||
'SELECT id, sort_order FROM t_daily_report_config WHERE sort_order > %s ORDER BY sort_order ASC LIMIT 1',
|
||||
(current_order,)
|
||||
)
|
||||
if next_config:
|
||||
# 交换排序
|
||||
execute_update(
|
||||
'UPDATE t_daily_report_config SET sort_order = %s WHERE id = %s',
|
||||
(next_config[0]['sort_order'], config_id)
|
||||
)
|
||||
execute_update(
|
||||
'UPDATE t_daily_report_config SET sort_order = %s WHERE id = %s',
|
||||
(current_order, next_config[0]['id'])
|
||||
)
|
||||
target_index = current_index + 1
|
||||
else:
|
||||
return jsonify({'success': False, 'message': '无效的移动方向'}), 400
|
||||
|
||||
# 边界检查
|
||||
if target_index < 0 or target_index >= len(all_configs):
|
||||
print(f"[move_config] 已到边界,无法移动 target_index={target_index}")
|
||||
return jsonify({'success': True, 'message': '已到边界'})
|
||||
|
||||
# 在内存数组中交换位置
|
||||
all_configs[current_index], all_configs[target_index] = all_configs[target_index], all_configs[current_index]
|
||||
|
||||
print(f"[move_config] 交换后顺序: {[c['id'] for c in all_configs]}")
|
||||
|
||||
# 全量重排:按新顺序给每个配置重新赋值 sort_order
|
||||
for idx, cfg in enumerate(all_configs):
|
||||
new_sort = idx + 1
|
||||
print(f" 更新 id={cfg['id']}: sort_order {cfg['sort_order']} -> {new_sort}")
|
||||
execute_update(
|
||||
'UPDATE t_daily_report_config SET sort_order = %s WHERE id = %s',
|
||||
(new_sort, cfg['id'])
|
||||
)
|
||||
|
||||
print("[move_config] 重排完成")
|
||||
return jsonify({'success': True, 'message': '移动成功'})
|
||||
except Exception as e:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
print(f"[move_config] 异常: {e}")
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
数据库初始化 API
|
||||
"""
|
||||
from flask import jsonify
|
||||
from lib.db import execute_update
|
||||
from lib.db_init import init_database_tables
|
||||
from . import init_bp
|
||||
|
||||
|
||||
@@ -10,61 +10,10 @@ from . import init_bp
|
||||
def init_tables():
|
||||
"""初始化数据库表"""
|
||||
try:
|
||||
# 创建配置表
|
||||
create_config_table = """
|
||||
CREATE TABLE IF NOT EXISTS t_daily_report_config (
|
||||
id BIGINT NOT NULL COMMENT '配置 ID',
|
||||
config_name VARCHAR(100) NOT NULL COMMENT '配置名称',
|
||||
split_type VARCHAR(20) NOT NULL COMMENT '拆分方式',
|
||||
split_value VARCHAR(100) NOT NULL COMMENT '拆分值',
|
||||
split_name VARCHAR(200) NULL COMMENT '拆分显示名称',
|
||||
selected_fields VARCHAR(3000) NOT NULL COMMENT '选择的字段列表',
|
||||
sum_fields VARCHAR(3000) NULL COMMENT '求和字段列表',
|
||||
is_active TINYINT NULL COMMENT '是否启用',
|
||||
sort_order INT NULL COMMENT '排序顺序',
|
||||
create_time DATETIME NULL COMMENT '创建时间',
|
||||
update_time DATETIME NULL COMMENT '更新时间'
|
||||
)
|
||||
UNIQUE KEY(id)
|
||||
DISTRIBUTED BY HASH(id) BUCKETS 1
|
||||
PROPERTIES("replication_num" = "1")
|
||||
"""
|
||||
execute_update(create_config_table)
|
||||
|
||||
# 创建历史表
|
||||
create_history_table = """
|
||||
CREATE TABLE IF NOT EXISTS t_daily_report_history (
|
||||
id BIGINT NOT NULL COMMENT '历史 ID',
|
||||
report_date VARCHAR(20) NULL COMMENT '报表日期',
|
||||
start_time VARCHAR(50) NULL COMMENT '开始时间',
|
||||
end_time VARCHAR(50) NULL COMMENT '结束时间',
|
||||
split_type VARCHAR(20) NULL COMMENT '拆分方式',
|
||||
split_value VARCHAR(100) NULL COMMENT '拆分值',
|
||||
split_name VARCHAR(200) NULL COMMENT '拆分显示名称',
|
||||
config_id BIGINT NULL COMMENT '配置 ID',
|
||||
config_name VARCHAR(200) NULL COMMENT '配置名称',
|
||||
total_orders INT NULL COMMENT '订单总数',
|
||||
total_amount DECIMAL(10,2) NULL COMMENT '总金额',
|
||||
sum_results VARCHAR(2000) NULL COMMENT '求和结果',
|
||||
file_path VARCHAR(500) NULL COMMENT '文件路径',
|
||||
status TINYINT NULL COMMENT '状态',
|
||||
create_time DATETIME NULL COMMENT '创建时间'
|
||||
)
|
||||
UNIQUE KEY(id)
|
||||
DISTRIBUTED BY HASH(id) BUCKETS 1
|
||||
PROPERTIES("replication_num" = "1")
|
||||
"""
|
||||
execute_update(create_history_table)
|
||||
|
||||
# 为已存在的表添加 config_name 字段(如果不存在)
|
||||
try:
|
||||
execute_update("""
|
||||
ALTER TABLE t_daily_report_history
|
||||
ADD COLUMN IF NOT EXISTS config_name VARCHAR(200) NULL COMMENT '配置名称'
|
||||
""")
|
||||
except Exception as e:
|
||||
print(f"[数据库] 添加 config_name 字段时出错(可能已存在): {e}")
|
||||
|
||||
return jsonify({'success': True, 'message': '数据库表初始化成功'})
|
||||
success = init_database_tables()
|
||||
if success:
|
||||
return jsonify({'success': True, 'message': '数据库表初始化成功'})
|
||||
else:
|
||||
return jsonify({'success': False, 'message': '数据库表初始化失败'}), 500
|
||||
except Exception as e:
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
|
||||
130
lib/api/sum_data_api.py
Normal file
130
lib/api/sum_data_api.py
Normal file
@@ -0,0 +1,130 @@
|
||||
"""
|
||||
求和数据管理 API
|
||||
"""
|
||||
from flask import request, jsonify
|
||||
from lib.sum_data_collector import (
|
||||
collect_sum_data_from_history,
|
||||
get_sum_data_list,
|
||||
add_sum_data,
|
||||
update_sum_data,
|
||||
delete_sum_data,
|
||||
batch_delete_sum_data
|
||||
)
|
||||
from . import sum_data_bp
|
||||
|
||||
|
||||
@sum_data_bp.route('/sum-data/collect', methods=['POST'])
|
||||
def collect_sum_data():
|
||||
"""从历史记录采集求和数据"""
|
||||
try:
|
||||
data = request.json or {}
|
||||
history_id = data.get('history_id')
|
||||
|
||||
result = collect_sum_data_from_history(history_id)
|
||||
if result['success']:
|
||||
return jsonify(result)
|
||||
else:
|
||||
return jsonify(result), 400
|
||||
except Exception as e:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
|
||||
|
||||
@sum_data_bp.route('/sum-data', methods=['GET'])
|
||||
def list_sum_data():
|
||||
"""查询求和数据列表"""
|
||||
try:
|
||||
page = int(request.args.get('page', 1))
|
||||
page_size = int(request.args.get('page_size', 20))
|
||||
report_date = request.args.get('report_date')
|
||||
config_id = request.args.get('config_id')
|
||||
config_name = request.args.get('config_name')
|
||||
sum_field_key = request.args.get('sum_field_key')
|
||||
data_source = request.args.get('data_source')
|
||||
|
||||
result = get_sum_data_list(
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
report_date=report_date,
|
||||
config_id=config_id,
|
||||
config_name=config_name,
|
||||
sum_field_key=sum_field_key,
|
||||
data_source=data_source
|
||||
)
|
||||
|
||||
if result['success']:
|
||||
return jsonify(result)
|
||||
else:
|
||||
return jsonify(result), 400
|
||||
except Exception as e:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
|
||||
|
||||
@sum_data_bp.route('/sum-data', methods=['POST'])
|
||||
def create_sum_data():
|
||||
"""手动添加求和数据"""
|
||||
try:
|
||||
data = request.json
|
||||
result = add_sum_data(data)
|
||||
if result['success']:
|
||||
return jsonify(result)
|
||||
else:
|
||||
return jsonify(result), 400
|
||||
except Exception as e:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
|
||||
|
||||
@sum_data_bp.route('/sum-data/<int:sum_data_id>', methods=['PUT'])
|
||||
def update_sum_data_api(sum_data_id):
|
||||
"""更新求和数据"""
|
||||
try:
|
||||
data = request.json
|
||||
result = update_sum_data(sum_data_id, data)
|
||||
if result['success']:
|
||||
return jsonify(result)
|
||||
else:
|
||||
return jsonify(result), 400
|
||||
except Exception as e:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
|
||||
|
||||
@sum_data_bp.route('/sum-data/<int:sum_data_id>', methods=['DELETE'])
|
||||
def delete_sum_data_api(sum_data_id):
|
||||
"""删除求和数据"""
|
||||
try:
|
||||
result = delete_sum_data(sum_data_id)
|
||||
if result['success']:
|
||||
return jsonify(result)
|
||||
else:
|
||||
return jsonify(result), 400
|
||||
except Exception as e:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
|
||||
|
||||
@sum_data_bp.route('/sum-data/batch-delete', methods=['POST'])
|
||||
def batch_delete_sum_data_api():
|
||||
"""批量删除求和数据"""
|
||||
try:
|
||||
data = request.json
|
||||
ids = data.get('ids', [])
|
||||
if not ids:
|
||||
return jsonify({'success': False, 'message': '请选择要删除的记录'}), 400
|
||||
|
||||
result = batch_delete_sum_data(ids)
|
||||
if result['success']:
|
||||
return jsonify(result)
|
||||
else:
|
||||
return jsonify(result), 400
|
||||
except Exception as e:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
119
lib/db_init.py
Normal file
119
lib/db_init.py
Normal file
@@ -0,0 +1,119 @@
|
||||
"""
|
||||
数据库初始化
|
||||
"""
|
||||
from lib.db import execute_update, execute_query
|
||||
|
||||
|
||||
def init_database_tables():
|
||||
"""初始化所有数据库表(应用启动时自动调用)"""
|
||||
try:
|
||||
# 创建配置表
|
||||
create_config_table = """
|
||||
CREATE TABLE IF NOT EXISTS t_daily_report_config (
|
||||
id BIGINT NOT NULL COMMENT '配置 ID',
|
||||
config_name VARCHAR(100) NOT NULL COMMENT '配置名称',
|
||||
split_type VARCHAR(20) NOT NULL COMMENT '拆分方式',
|
||||
split_value VARCHAR(100) NOT NULL COMMENT '拆分值',
|
||||
split_name VARCHAR(200) NULL COMMENT '拆分显示名称',
|
||||
selected_fields VARCHAR(3000) NOT NULL COMMENT '选择的字段列表',
|
||||
sum_fields VARCHAR(3000) NULL COMMENT '求和字段列表',
|
||||
time_periods VARCHAR(1000) NULL COMMENT '分时段电量配置',
|
||||
merge_telecom TINYINT NULL COMMENT '是否合并特来电数据',
|
||||
telecom_vehicle_no VARCHAR(500) NULL COMMENT '特来电车量自编号',
|
||||
field_custom_names VARCHAR(2000) NULL COMMENT '字段自定义报表名头',
|
||||
is_active TINYINT NULL COMMENT '是否启用',
|
||||
sort_order INT NULL COMMENT '排序顺序',
|
||||
create_time DATETIME NULL COMMENT '创建时间',
|
||||
update_time DATETIME NULL COMMENT '更新时间'
|
||||
)
|
||||
UNIQUE KEY(id)
|
||||
DISTRIBUTED BY HASH(id) BUCKETS 1
|
||||
PROPERTIES("replication_num" = "1")
|
||||
"""
|
||||
execute_update(create_config_table)
|
||||
print("[数据库] t_daily_report_config 表已就绪")
|
||||
|
||||
# 创建历史表
|
||||
create_history_table = """
|
||||
CREATE TABLE IF NOT EXISTS t_daily_report_history (
|
||||
id BIGINT NOT NULL COMMENT '历史 ID',
|
||||
report_date VARCHAR(20) NULL COMMENT '报表日期',
|
||||
start_time VARCHAR(50) NULL COMMENT '开始时间',
|
||||
end_time VARCHAR(50) NULL COMMENT '结束时间',
|
||||
split_type VARCHAR(20) NULL COMMENT '拆分方式',
|
||||
split_value VARCHAR(100) NULL COMMENT '拆分值',
|
||||
split_name VARCHAR(200) NULL COMMENT '拆分显示名称',
|
||||
config_id BIGINT NULL COMMENT '配置 ID',
|
||||
config_name VARCHAR(200) NULL COMMENT '配置名称',
|
||||
total_orders INT NULL COMMENT '订单总数',
|
||||
total_amount DECIMAL(10,2) NULL COMMENT '总金额',
|
||||
sum_results VARCHAR(2000) NULL COMMENT '求和结果',
|
||||
file_path VARCHAR(500) NULL COMMENT '文件路径',
|
||||
status TINYINT NULL COMMENT '状态',
|
||||
create_time DATETIME NULL COMMENT '创建时间'
|
||||
)
|
||||
UNIQUE KEY(id)
|
||||
DISTRIBUTED BY HASH(id) BUCKETS 1
|
||||
PROPERTIES("replication_num" = "1")
|
||||
"""
|
||||
execute_update(create_history_table)
|
||||
print("[数据库] t_daily_report_history 表已就绪")
|
||||
|
||||
# 为已存在的表添加 config_name 字段(如果不存在)
|
||||
try:
|
||||
execute_update("""
|
||||
ALTER TABLE t_daily_report_history
|
||||
ADD COLUMN IF NOT EXISTS config_name VARCHAR(200) NULL COMMENT '配置名称'
|
||||
""")
|
||||
except Exception as e:
|
||||
print(f"[数据库] 添加 config_name 字段时出错(可能已存在): {e}")
|
||||
|
||||
# 创建求和数据表
|
||||
create_sum_data_table = """
|
||||
CREATE TABLE IF NOT EXISTS t_daily_report_sum_data (
|
||||
id BIGINT NOT NULL COMMENT '主键 ID',
|
||||
report_date VARCHAR(20) NULL COMMENT '报表日期',
|
||||
config_id BIGINT NULL COMMENT '配置 ID',
|
||||
config_name VARCHAR(200) NULL COMMENT '配置名称',
|
||||
split_type VARCHAR(20) NULL COMMENT '拆分方式',
|
||||
split_value VARCHAR(100) NULL COMMENT '拆分值',
|
||||
split_name VARCHAR(200) NULL COMMENT '拆分显示名称',
|
||||
sum_field_key VARCHAR(100) NULL COMMENT '求和字段键名',
|
||||
sum_field_name VARCHAR(100) NULL COMMENT '求和字段中文名称',
|
||||
sum_value DECIMAL(18,3) NULL COMMENT '求和值',
|
||||
total_orders INT NULL COMMENT '订单数',
|
||||
data_source VARCHAR(20) NULL COMMENT '数据来源 auto=自动采集 manual=手动编辑',
|
||||
remark VARCHAR(500) NULL COMMENT '备注',
|
||||
create_time DATETIME NULL COMMENT '创建时间',
|
||||
update_time DATETIME NULL COMMENT '更新时间'
|
||||
)
|
||||
UNIQUE KEY(id)
|
||||
DISTRIBUTED BY HASH(id) BUCKETS 1
|
||||
PROPERTIES("replication_num" = "1")
|
||||
"""
|
||||
execute_update(create_sum_data_table)
|
||||
print("[数据库] t_daily_report_sum_data 表已就绪")
|
||||
|
||||
# 数据迁移:给 sort_order 为 NULL 的配置记录按 id 顺序赋值
|
||||
try:
|
||||
null_count = execute_query(
|
||||
"SELECT COUNT(*) as cnt FROM t_daily_report_config WHERE sort_order IS NULL"
|
||||
)
|
||||
if null_count and null_count[0]['cnt'] > 0:
|
||||
print(f"[数据库] 检测到 {null_count[0]['cnt']} 条配置 sort_order 为空,开始初始化排序...")
|
||||
all_configs = execute_query(
|
||||
"SELECT id FROM t_daily_report_config ORDER BY id ASC"
|
||||
)
|
||||
for idx, cfg in enumerate(all_configs):
|
||||
execute_update(
|
||||
"UPDATE t_daily_report_config SET sort_order = %s WHERE id = %s",
|
||||
(idx + 1, cfg['id'])
|
||||
)
|
||||
print("[数据库] 配置 sort_order 初始化完成")
|
||||
except Exception as e:
|
||||
print(f"[数据库] 初始化 sort_order 时出错: {e}")
|
||||
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"[数据库] 初始化失败: {e}")
|
||||
return False
|
||||
@@ -450,6 +450,12 @@ def generate_daily_report(config_id, start_time=None, end_time=None):
|
||||
total = sum(float(order.get(field, 0) or 0) for order in orders)
|
||||
sum_results[field] = round(total, 3)
|
||||
|
||||
# 强制采集充电电量(charge_degree),即使不在求和字段配置中也要采集
|
||||
if 'charge_degree' not in sum_results and orders and 'charge_degree' in orders[0]:
|
||||
total_charge_degree = sum(float(order.get('charge_degree', 0) or 0) for order in orders)
|
||||
sum_results['charge_degree'] = round(total_charge_degree, 3)
|
||||
print(f"[日报生成] 强制采集充电电量: {sum_results['charge_degree']} kWh")
|
||||
|
||||
# 构建ID到名称的映射
|
||||
id_name_maps = build_id_to_name_map(orders, selected_fields)
|
||||
|
||||
@@ -492,6 +498,18 @@ def generate_daily_report(config_id, start_time=None, end_time=None):
|
||||
)
|
||||
print(f"[日报生成] 历史记录已保存,ID={history_id}")
|
||||
|
||||
# 自动采集求和数据到求和数据表
|
||||
try:
|
||||
if sum_results:
|
||||
from lib.sum_data_collector import collect_sum_data_from_history
|
||||
collect_result = collect_sum_data_from_history(history_id)
|
||||
if collect_result['success']:
|
||||
print(f"[日报生成] 求和数据采集成功")
|
||||
else:
|
||||
print(f"[日报生成] 求和数据采集失败: {collect_result['message']}")
|
||||
except Exception as collect_error:
|
||||
print(f"[日报生成] 求和数据采集异常: {str(collect_error)}")
|
||||
|
||||
print(f"[日报生成] ✓ 生成成功")
|
||||
print(f" 订单数量: {len(orders)}")
|
||||
print(f" 总金额: {total_amount}")
|
||||
|
||||
409
lib/sum_data_collector.py
Normal file
409
lib/sum_data_collector.py
Normal file
@@ -0,0 +1,409 @@
|
||||
"""
|
||||
日报求和数据采集与管理
|
||||
"""
|
||||
import json
|
||||
from datetime import datetime
|
||||
from lib.db import execute_query, execute_update, execute_insert
|
||||
from lib.field_mapping import get_field_display_name
|
||||
|
||||
|
||||
def collect_sum_data_from_history(history_id=None):
|
||||
"""
|
||||
从历史记录中采集求和数据并写入求和数据表
|
||||
充电电量(charge_degree)是必须采集的,其他求和字段有就采集,没有就不采集
|
||||
|
||||
Args:
|
||||
history_id: 历史记录ID(可选,不填则采集所有成功的历史记录)
|
||||
|
||||
Returns:
|
||||
dict: 采集结果
|
||||
"""
|
||||
try:
|
||||
# 查询历史记录
|
||||
if history_id:
|
||||
history_list = execute_query(
|
||||
'SELECT * FROM t_daily_report_history WHERE id = %s AND status = 1',
|
||||
(history_id,)
|
||||
)
|
||||
else:
|
||||
history_list = execute_query(
|
||||
'SELECT * FROM t_daily_report_history WHERE status = 1 ORDER BY create_time DESC'
|
||||
)
|
||||
|
||||
if not history_list:
|
||||
return {'success': False, 'message': '没有找到符合条件的历史记录'}
|
||||
|
||||
collected_count = 0
|
||||
skipped_count = 0
|
||||
|
||||
for history in history_list:
|
||||
sum_results = history.get('sum_results')
|
||||
|
||||
# 解析 sum_results JSON
|
||||
if isinstance(sum_results, str):
|
||||
sum_results = json.loads(sum_results) if sum_results else {}
|
||||
elif sum_results is None:
|
||||
sum_results = {}
|
||||
|
||||
# 强制确保有充电电量(charge_degree)
|
||||
if 'charge_degree' not in sum_results or sum_results['charge_degree'] is None:
|
||||
# 从订单表重新计算充电电量
|
||||
try:
|
||||
charge_degree = _calculate_charge_degree(history)
|
||||
if charge_degree is not None:
|
||||
sum_results['charge_degree'] = charge_degree
|
||||
print(f"[采集] 历史记录 {history['id']} 补充计算充电电量: {charge_degree} kWh")
|
||||
except Exception as calc_error:
|
||||
print(f"[采集] 历史记录 {history['id']} 计算充电电量失败: {calc_error}")
|
||||
|
||||
# 如果还是没有任何求和数据,跳过
|
||||
if not sum_results:
|
||||
skipped_count += 1
|
||||
continue
|
||||
|
||||
# 先删除该历史记录对应的旧数据(避免重复)
|
||||
execute_update(
|
||||
'''DELETE FROM t_daily_report_sum_data
|
||||
WHERE report_date = %s AND config_id = %s AND split_value = %s AND data_source = 'auto'
|
||||
''',
|
||||
(history['report_date'], history['config_id'], history['split_value'])
|
||||
)
|
||||
|
||||
# 逐条插入求和字段数据
|
||||
for field_key, field_value in sum_results.items():
|
||||
sum_data_id = int(datetime.now().timestamp() * 1000000) + collected_count
|
||||
sql = '''
|
||||
INSERT INTO t_daily_report_sum_data
|
||||
(id, report_date, config_id, config_name, split_type, split_value, split_name,
|
||||
sum_field_key, sum_field_name, sum_value, total_orders, data_source, remark,
|
||||
create_time, update_time)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NOW(), NOW())
|
||||
'''
|
||||
params = (
|
||||
sum_data_id,
|
||||
history['report_date'],
|
||||
history['config_id'],
|
||||
history.get('config_name', ''),
|
||||
history.get('split_type', ''),
|
||||
history.get('split_value', ''),
|
||||
history.get('split_name', ''),
|
||||
field_key,
|
||||
get_field_display_name(field_key),
|
||||
float(field_value) if field_value is not None else 0,
|
||||
history.get('total_orders', 0),
|
||||
'auto',
|
||||
f'从历史记录自动采集,历史ID: {history["id"]}'
|
||||
)
|
||||
execute_insert(sql, params)
|
||||
collected_count += 1
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
'message': f'采集完成,共采集 {collected_count} 条求和数据,跳过 {skipped_count} 条无求和数据的记录'
|
||||
}
|
||||
except Exception as e:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return {'success': False, 'message': f'采集失败: {str(e)}'}
|
||||
|
||||
|
||||
def _calculate_charge_degree(history):
|
||||
"""
|
||||
从订单表中计算指定历史记录对应的充电电量总和
|
||||
|
||||
Args:
|
||||
history: 历史记录字典
|
||||
|
||||
Returns:
|
||||
float: 充电电量总和(保留三位小数),失败返回 None
|
||||
"""
|
||||
try:
|
||||
split_type = history.get('split_type', '')
|
||||
split_value = history.get('split_value', '')
|
||||
start_time = history.get('start_time', '')
|
||||
end_time = history.get('end_time', '')
|
||||
|
||||
if not split_type or not split_value or not start_time or not end_time:
|
||||
return None
|
||||
|
||||
# 检查是否为多选值(逗号分隔)
|
||||
split_values = [v.strip() for v in str(split_value).split(',') if v.strip()]
|
||||
|
||||
if len(split_values) > 1:
|
||||
placeholders = ', '.join(['%s'] * len(split_values))
|
||||
sql = f"""
|
||||
SELECT SUM(charge_degree) as total_degree
|
||||
FROM t_equipment_charge_order
|
||||
WHERE state = 3
|
||||
AND report_time >= %s
|
||||
AND report_time < %s
|
||||
AND {split_type} IN ({placeholders})
|
||||
"""
|
||||
params = [start_time, end_time] + split_values
|
||||
else:
|
||||
sql = f"""
|
||||
SELECT SUM(charge_degree) as total_degree
|
||||
FROM t_equipment_charge_order
|
||||
WHERE state = 3
|
||||
AND report_time >= %s
|
||||
AND report_time < %s
|
||||
AND {split_type} = %s
|
||||
"""
|
||||
params = [start_time, end_time, split_value]
|
||||
|
||||
result = execute_query(sql, tuple(params))
|
||||
if result and result[0].get('total_degree') is not None:
|
||||
return round(float(result[0]['total_degree']), 3)
|
||||
|
||||
return 0.0
|
||||
except Exception as e:
|
||||
print(f"[计算充电电量] 失败: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def get_sum_data_list(page=1, page_size=20, report_date=None, config_id=None,
|
||||
config_name=None, sum_field_key=None, data_source=None):
|
||||
"""
|
||||
查询求和数据列表
|
||||
|
||||
Args:
|
||||
page: 页码
|
||||
page_size: 每页大小
|
||||
report_date: 报表日期(可选)
|
||||
config_id: 配置ID(可选)
|
||||
config_name: 配置名称(可选,模糊搜索)
|
||||
sum_field_key: 求和字段键名(可选)
|
||||
data_source: 数据来源(可选)
|
||||
|
||||
Returns:
|
||||
dict: 查询结果
|
||||
"""
|
||||
try:
|
||||
offset = (page - 1) * page_size
|
||||
|
||||
where_clauses = ['1=1']
|
||||
params = []
|
||||
|
||||
if report_date:
|
||||
where_clauses.append('report_date = %s')
|
||||
params.append(report_date)
|
||||
|
||||
if config_id:
|
||||
where_clauses.append('config_id = %s')
|
||||
params.append(config_id)
|
||||
|
||||
if config_name:
|
||||
where_clauses.append('config_name LIKE %s')
|
||||
params.append(f'%{config_name}%')
|
||||
|
||||
if sum_field_key:
|
||||
where_clauses.append('sum_field_key = %s')
|
||||
params.append(sum_field_key)
|
||||
|
||||
if data_source:
|
||||
where_clauses.append('data_source = %s')
|
||||
params.append(data_source)
|
||||
|
||||
where_sql = ' AND '.join(where_clauses)
|
||||
|
||||
# 查询总数
|
||||
total_result = execute_query(
|
||||
f'SELECT COUNT(*) as total FROM t_daily_report_sum_data WHERE {where_sql}',
|
||||
tuple(params)
|
||||
)
|
||||
total = total_result[0]['total']
|
||||
|
||||
# 查询数据
|
||||
list_result = execute_query(
|
||||
f'''SELECT * FROM t_daily_report_sum_data
|
||||
WHERE {where_sql}
|
||||
ORDER BY report_date DESC, config_id, id DESC
|
||||
LIMIT %s OFFSET %s''',
|
||||
tuple(params) + (page_size, offset)
|
||||
)
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
'data': {
|
||||
'list': list_result,
|
||||
'total': total,
|
||||
'page': page,
|
||||
'page_size': page_size
|
||||
}
|
||||
}
|
||||
except Exception as e:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return {'success': False, 'message': str(e)}
|
||||
|
||||
|
||||
def add_sum_data(data):
|
||||
"""
|
||||
手动添加求和数据
|
||||
|
||||
Args:
|
||||
data: 求和数据字典
|
||||
|
||||
Returns:
|
||||
dict: 操作结果
|
||||
"""
|
||||
try:
|
||||
sum_data_id = int(datetime.now().timestamp() * 1000)
|
||||
sql = '''
|
||||
INSERT INTO t_daily_report_sum_data
|
||||
(id, report_date, config_id, config_name, split_type, split_value, split_name,
|
||||
sum_field_key, sum_field_name, sum_value, total_orders, data_source, remark,
|
||||
create_time, update_time)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NOW(), NOW())
|
||||
'''
|
||||
params = (
|
||||
sum_data_id,
|
||||
data.get('report_date', ''),
|
||||
data.get('config_id'),
|
||||
data.get('config_name', ''),
|
||||
data.get('split_type', ''),
|
||||
data.get('split_value', ''),
|
||||
data.get('split_name', ''),
|
||||
data.get('sum_field_key', ''),
|
||||
data.get('sum_field_name', ''),
|
||||
float(data.get('sum_value', 0)),
|
||||
data.get('total_orders', 0),
|
||||
'manual',
|
||||
data.get('remark', '')
|
||||
)
|
||||
execute_insert(sql, params)
|
||||
return {'success': True, 'message': '添加成功', 'id': sum_data_id}
|
||||
except Exception as e:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return {'success': False, 'message': str(e)}
|
||||
|
||||
|
||||
def update_sum_data(sum_data_id, data):
|
||||
"""
|
||||
更新求和数据
|
||||
|
||||
Args:
|
||||
sum_data_id: 求和数据ID
|
||||
data: 更新的数据字典
|
||||
|
||||
Returns:
|
||||
dict: 操作结果
|
||||
"""
|
||||
try:
|
||||
update_fields = []
|
||||
params = []
|
||||
|
||||
if 'report_date' in data:
|
||||
update_fields.append('report_date = %s')
|
||||
params.append(data['report_date'])
|
||||
|
||||
if 'config_id' in data:
|
||||
update_fields.append('config_id = %s')
|
||||
params.append(data['config_id'])
|
||||
|
||||
if 'config_name' in data:
|
||||
update_fields.append('config_name = %s')
|
||||
params.append(data['config_name'])
|
||||
|
||||
if 'split_type' in data:
|
||||
update_fields.append('split_type = %s')
|
||||
params.append(data['split_type'])
|
||||
|
||||
if 'split_value' in data:
|
||||
update_fields.append('split_value = %s')
|
||||
params.append(data['split_value'])
|
||||
|
||||
if 'split_name' in data:
|
||||
update_fields.append('split_name = %s')
|
||||
params.append(data['split_name'])
|
||||
|
||||
if 'sum_field_key' in data:
|
||||
update_fields.append('sum_field_key = %s')
|
||||
params.append(data['sum_field_key'])
|
||||
|
||||
if 'sum_field_name' in data:
|
||||
update_fields.append('sum_field_name = %s')
|
||||
params.append(data['sum_field_name'])
|
||||
|
||||
if 'sum_value' in data:
|
||||
update_fields.append('sum_value = %s')
|
||||
params.append(float(data['sum_value']))
|
||||
|
||||
if 'total_orders' in data:
|
||||
update_fields.append('total_orders = %s')
|
||||
params.append(data['total_orders'])
|
||||
|
||||
if 'remark' in data:
|
||||
update_fields.append('remark = %s')
|
||||
params.append(data['remark'])
|
||||
|
||||
if not update_fields:
|
||||
return {'success': False, 'message': '没有需要更新的字段'}
|
||||
|
||||
update_fields.append('update_time = NOW()')
|
||||
params.append(sum_data_id)
|
||||
|
||||
sql = f'''
|
||||
UPDATE t_daily_report_sum_data
|
||||
SET {', '.join(update_fields)}
|
||||
WHERE id = %s
|
||||
'''
|
||||
rowcount = execute_update(sql, tuple(params))
|
||||
|
||||
if rowcount > 0:
|
||||
return {'success': True, 'message': '更新成功'}
|
||||
else:
|
||||
return {'success': False, 'message': '记录不存在'}
|
||||
except Exception as e:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return {'success': False, 'message': str(e)}
|
||||
|
||||
|
||||
def delete_sum_data(sum_data_id):
|
||||
"""
|
||||
删除求和数据
|
||||
|
||||
Args:
|
||||
sum_data_id: 求和数据ID
|
||||
|
||||
Returns:
|
||||
dict: 操作结果
|
||||
"""
|
||||
try:
|
||||
rowcount = execute_update(
|
||||
'DELETE FROM t_daily_report_sum_data WHERE id = %s',
|
||||
(sum_data_id,)
|
||||
)
|
||||
if rowcount > 0:
|
||||
return {'success': True, 'message': '删除成功'}
|
||||
else:
|
||||
return {'success': False, 'message': '记录不存在'}
|
||||
except Exception as e:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return {'success': False, 'message': str(e)}
|
||||
|
||||
|
||||
def batch_delete_sum_data(ids):
|
||||
"""
|
||||
批量删除求和数据
|
||||
|
||||
Args:
|
||||
ids: ID列表
|
||||
|
||||
Returns:
|
||||
dict: 操作结果
|
||||
"""
|
||||
try:
|
||||
placeholders = ', '.join(['%s'] * len(ids))
|
||||
rowcount = execute_update(
|
||||
f'DELETE FROM t_daily_report_sum_data WHERE id IN ({placeholders})',
|
||||
tuple(ids)
|
||||
)
|
||||
return {'success': True, 'message': f'成功删除 {rowcount} 条记录'}
|
||||
except Exception as e:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return {'success': False, 'message': str(e)}
|
||||
Reference in New Issue
Block a user