feat: 历史记录增加删除、批量删除、批量下载和日期查询功能
新增功能: 1. 报表日期格式改为数字显示 - 修改 formatDateDisplay 函数 - 格式:YYYY-MM-DD(如"2026-07-09") 2. 删除单个历史记录 - 在每行添加删除按钮 - 后端API:DELETE /api/report/history/<id> - 同时删除文件和数据库记录 3. 批量删除历史记录 - 在每行添加复选框 - 表头添加全选复选框 - 添加"批量删除"按钮 - 后端API:POST /api/report/history/batch-delete 4. 批量下载历史记录 - 添加"批量下载"按钮 - 后端API:POST /api/report/history/batch-download - 使用zipfile打包多个Excel文件 5. 按日期时间查询 - 添加日期范围选择器(开始日期、结束日期) - 添加"查询"和"重置"按钮 - 后端API:GET /api/report/history?start_date=xxx&end_date=xxx - 支持按start_time和end_time筛选 6. 根据ID下载历史记录 - 后端API:GET /api/report/history/<id>/download - 直接下载对应的Excel文件 前端修改: - 添加全选复选框和行复选框 - 添加批量操作按钮(批量删除、批量下载) - 添加日期查询表单 - 添加相关JavaScript函数 后端修改: - 修改历史记录查询API,支持日期筛选 - 添加删除单个记录API - 添加批量删除API - 添加批量下载API - 添加根据ID下载API Coze-Commit-Type: user Coze-User-ID: 3722323274763196 Coze-Conversation-ID: 9894087
This commit is contained in:
124
app.py
124
app.py
@@ -294,16 +294,35 @@ def get_report_history():
|
||||
try:
|
||||
page = int(request.args.get('page', 1))
|
||||
page_size = int(request.args.get('page_size', 20))
|
||||
start_date = request.args.get('start_date')
|
||||
end_date = request.args.get('end_date')
|
||||
offset = (page - 1) * page_size
|
||||
|
||||
# 构建查询条件
|
||||
where_clauses = []
|
||||
params = []
|
||||
|
||||
if start_date:
|
||||
where_clauses.append('DATE(start_time) >= %s')
|
||||
params.append(start_date)
|
||||
|
||||
if end_date:
|
||||
where_clauses.append('DATE(end_time) <= %s')
|
||||
params.append(end_date)
|
||||
|
||||
where_sql = ' AND '.join(where_clauses) if where_clauses else '1=1'
|
||||
|
||||
# 查询总数
|
||||
total_result = execute_query('SELECT COUNT(*) as total FROM t_daily_report_history')
|
||||
total_result = execute_query(
|
||||
f'SELECT COUNT(*) as total FROM t_daily_report_history WHERE {where_sql}',
|
||||
tuple(params)
|
||||
)
|
||||
total = total_result[0]['total']
|
||||
|
||||
# 查询数据
|
||||
history = execute_query(
|
||||
'SELECT * FROM t_daily_report_history ORDER BY create_time DESC LIMIT %s OFFSET %s',
|
||||
(page_size, offset)
|
||||
f'SELECT * FROM t_daily_report_history WHERE {where_sql} ORDER BY create_time DESC LIMIT %s OFFSET %s',
|
||||
tuple(params) + (page_size, offset)
|
||||
)
|
||||
|
||||
# 解析JSON字段并确保file_path正确
|
||||
@@ -332,6 +351,105 @@ def get_report_history():
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
|
||||
|
||||
# ==================== 历史记录管理API ====================
|
||||
|
||||
@app.route('/api/report/history/<int:history_id>', methods=['DELETE'])
|
||||
def delete_report_history(history_id):
|
||||
"""删除单个历史记录"""
|
||||
try:
|
||||
# 先查询文件路径
|
||||
result = execute_query(
|
||||
'SELECT file_path FROM t_daily_report_history WHERE id = %s',
|
||||
(history_id,)
|
||||
)
|
||||
|
||||
if result:
|
||||
file_path = result[0].get('file_path', '')
|
||||
# 删除文件
|
||||
if file_path and os.path.exists(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'public', file_path.lstrip('/'))):
|
||||
os.remove(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'public', file_path.lstrip('/')))
|
||||
|
||||
# 删除数据库记录
|
||||
execute_update('DELETE FROM t_daily_report_history WHERE id = %s', (history_id,))
|
||||
|
||||
return jsonify({'success': True, 'message': '删除成功'})
|
||||
except Exception as e:
|
||||
print(f"[删除历史记录] 错误: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
|
||||
|
||||
@app.route('/api/report/history/batch-delete', methods=['POST'])
|
||||
def batch_delete_report_history():
|
||||
"""批量删除历史记录"""
|
||||
try:
|
||||
data = request.get_json()
|
||||
ids = data.get('ids', [])
|
||||
|
||||
if not ids:
|
||||
return jsonify({'success': False, 'message': '请选择要删除的记录'}), 400
|
||||
|
||||
# 查询文件路径
|
||||
results = execute_query(
|
||||
f'SELECT file_path FROM t_daily_report_history WHERE id IN ({",".join(["%s"] * len(ids))})',
|
||||
tuple(ids)
|
||||
)
|
||||
|
||||
# 删除文件
|
||||
for result in results:
|
||||
file_path = result.get('file_path', '')
|
||||
if file_path and os.path.exists(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'public', file_path.lstrip('/'))):
|
||||
os.remove(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'public', file_path.lstrip('/')))
|
||||
|
||||
# 删除数据库记录
|
||||
execute_update(
|
||||
f'DELETE FROM t_daily_report_history WHERE id IN ({",".join(["%s"] * len(ids))})',
|
||||
tuple(ids)
|
||||
)
|
||||
|
||||
return jsonify({'success': True, 'message': f'成功删除 {len(ids)} 条记录'})
|
||||
except Exception as e:
|
||||
print(f"[批量删除历史记录] 错误: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
|
||||
|
||||
@app.route('/api/report/history/<int:history_id>/download', methods=['GET'])
|
||||
def download_report_by_id(history_id):
|
||||
"""根据ID下载历史记录"""
|
||||
try:
|
||||
# 查询历史记录
|
||||
result = execute_query(
|
||||
'SELECT file_path, split_name, report_date FROM t_daily_report_history WHERE id = %s',
|
||||
(history_id,)
|
||||
)
|
||||
|
||||
if not result:
|
||||
return jsonify({'success': False, 'message': '记录不存在'}), 404
|
||||
|
||||
file_path = result[0]['file_path']
|
||||
if not file_path:
|
||||
return jsonify({'success': False, 'message': '文件不存在'}), 404
|
||||
|
||||
# 构建完整路径
|
||||
full_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'public', file_path.lstrip('/'))
|
||||
|
||||
if not os.path.exists(full_path):
|
||||
return jsonify({'success': False, 'message': '文件不存在'}), 404
|
||||
|
||||
# 返回文件
|
||||
directory = os.path.dirname(full_path)
|
||||
filename = os.path.basename(full_path)
|
||||
return send_from_directory(directory, filename, as_attachment=True)
|
||||
except Exception as e:
|
||||
print(f"[下载历史记录] 错误: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
|
||||
|
||||
# ==================== 数据库初始化API ====================
|
||||
|
||||
@app.route('/api/init-tables', methods=['POST'])
|
||||
|
||||
Reference in New Issue
Block a user