|
@@ -1,7 +1,9 @@
|
|
|
import sqlite3
|
|
|
-from flask import current_app
|
|
|
+from flask import current_app, send_file
|
|
|
from werkzeug.security import generate_password_hash, check_password_hash
|
|
|
from flask import Blueprint, request, jsonify, current_app as app
|
|
|
+from werkzeug.utils import secure_filename
|
|
|
+from io import BytesIO
|
|
|
from .model import predict, train_and_save_model, calculate_model_score
|
|
|
import pandas as pd
|
|
|
from . import db # 从 app 包导入 db 实例
|
|
@@ -440,11 +442,11 @@ def get_model_parameters(model_id):
|
|
|
def add_item():
|
|
|
"""
|
|
|
接收 JSON 格式的请求体,包含表名和要插入的数据。
|
|
|
- 尝试将数据插入到指定的表中。
|
|
|
+ 尝试将数据插入到指定的表中,并进行字段查重。
|
|
|
:return:
|
|
|
"""
|
|
|
try:
|
|
|
- # 确保请求体是JSON格式
|
|
|
+ # 确保请求体是 JSON 格式
|
|
|
data = request.get_json()
|
|
|
if not data:
|
|
|
raise ValueError("No JSON data provided")
|
|
@@ -454,16 +456,43 @@ def add_item():
|
|
|
|
|
|
if not table_name or not item_data:
|
|
|
return jsonify({'error': 'Missing table name or item data'}), 400
|
|
|
- cur = db.cursor()
|
|
|
|
|
|
- # 动态构建 SQL 语句
|
|
|
+ # 定义各个表的字段查重规则
|
|
|
+ duplicate_check_rules = {
|
|
|
+ 'users': ['email', 'username'],
|
|
|
+ 'products': ['product_code'],
|
|
|
+ 'current_reduce': [ 'Q_over_b', 'pH', 'OM', 'CL', 'H', 'Al'],
|
|
|
+ 'current_reflux': ['OM', 'CL', 'CEC', 'H_plus', 'N', 'Al3_plus', 'Delta_pH'],
|
|
|
+ # 其他表和规则
|
|
|
+ }
|
|
|
+
|
|
|
+ # 获取该表的查重字段
|
|
|
+ duplicate_columns = duplicate_check_rules.get(table_name)
|
|
|
+
|
|
|
+ if not duplicate_columns:
|
|
|
+ return jsonify({'error': 'No duplicate check rule for this table'}), 400
|
|
|
+
|
|
|
+ # 动态构建查询条件,逐一检查是否有重复数据
|
|
|
+ condition = ' AND '.join([f"{column} = :{column}" for column in duplicate_columns])
|
|
|
+ duplicate_query = f"SELECT 1 FROM {table_name} WHERE {condition} LIMIT 1"
|
|
|
+
|
|
|
+ result = db.session.execute(text(duplicate_query), item_data).fetchone()
|
|
|
+
|
|
|
+ if result:
|
|
|
+ return jsonify({'error': '重复数据,已有相同的数据项存在。'}), 409
|
|
|
+
|
|
|
+ # 动态构建 SQL 语句,进行插入操作
|
|
|
columns = ', '.join(item_data.keys())
|
|
|
- placeholders = ', '.join(['?'] * len(item_data))
|
|
|
+ placeholders = ', '.join([f":{key}" for key in item_data.keys()])
|
|
|
sql = f"INSERT INTO {table_name} ({columns}) VALUES ({placeholders})"
|
|
|
- cur.execute(sql, tuple(item_data.values()))
|
|
|
- db.commit()
|
|
|
|
|
|
- # 返回更详细的成功响应
|
|
|
+ # 直接执行插入操作,无需显式的事务管理
|
|
|
+ db.session.execute(text(sql), item_data)
|
|
|
+
|
|
|
+ # 提交事务
|
|
|
+ db.session.commit()
|
|
|
+
|
|
|
+ # 返回成功响应
|
|
|
return jsonify({'success': True, 'message': 'Item added successfully'}), 201
|
|
|
|
|
|
except ValueError as e:
|
|
@@ -471,18 +500,16 @@ def add_item():
|
|
|
except KeyError as e:
|
|
|
return jsonify({'error': f'Missing data field: {e}'}), 400
|
|
|
except sqlite3.IntegrityError as e:
|
|
|
- # 处理例如唯一性约束违反等数据库完整性错误
|
|
|
- return jsonify({'error': 'Database integrity error', 'details': str(e)}), 409
|
|
|
+ return jsonify({'error': '数据库完整性错误', 'details': str(e)}), 409
|
|
|
except sqlite3.Error as e:
|
|
|
- # 处理其他数据库错误
|
|
|
- return jsonify({'error': 'Database error', 'details': str(e)}), 500
|
|
|
- finally:
|
|
|
- db.close()
|
|
|
+ return jsonify({'error': '数据库错误', 'details': str(e)}), 500
|
|
|
|
|
|
|
|
|
-# 定义删除数据库记录的 API 接口
|
|
|
@bp.route('/delete_item', methods=['POST'])
|
|
|
def delete_item():
|
|
|
+ """
|
|
|
+ 删除数据库记录的 API 接口
|
|
|
+ """
|
|
|
data = request.get_json()
|
|
|
table_name = data.get('table')
|
|
|
condition = data.get('condition')
|
|
@@ -494,39 +521,50 @@ def delete_item():
|
|
|
"message": "缺少表名或条件参数"
|
|
|
}), 400
|
|
|
|
|
|
- # 尝试从条件字符串中分离键和值
|
|
|
+ # 尝试从条件字符串中解析键和值
|
|
|
try:
|
|
|
key, value = condition.split('=')
|
|
|
+ key = key.strip() # 去除多余的空格
|
|
|
+ value = value.strip().strip("'\"") # 去除多余的空格和引号
|
|
|
except ValueError:
|
|
|
return jsonify({
|
|
|
"success": False,
|
|
|
"message": "条件格式错误,应为 'key=value'"
|
|
|
}), 400
|
|
|
|
|
|
- cur = db.cursor()
|
|
|
+ # 准备 SQL 删除语句
|
|
|
+ sql = f"DELETE FROM {table_name} WHERE {key} = :value"
|
|
|
|
|
|
try:
|
|
|
- # 执行删除操作
|
|
|
- cur.execute(f"DELETE FROM {table_name} WHERE {key} = ?", (value,))
|
|
|
- db.commit()
|
|
|
- # 如果没有错误发生,返回成功响应
|
|
|
+ # 使用 SQLAlchemy 执行删除
|
|
|
+ with db.session.begin():
|
|
|
+ result = db.session.execute(text(sql), {"value": value})
|
|
|
+
|
|
|
+ # 检查是否有记录被删除
|
|
|
+ if result.rowcount == 0:
|
|
|
+ return jsonify({
|
|
|
+ "success": False,
|
|
|
+ "message": "未找到符合条件的记录"
|
|
|
+ }), 404
|
|
|
+
|
|
|
return jsonify({
|
|
|
"success": True,
|
|
|
"message": "记录删除成功"
|
|
|
}), 200
|
|
|
- except sqlite3.Error as e:
|
|
|
- # 发生错误,回滚事务
|
|
|
- db.rollback()
|
|
|
- # 返回失败响应,并包含错误信息
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
return jsonify({
|
|
|
"success": False,
|
|
|
"message": f"删除失败: {e}"
|
|
|
- }), 400
|
|
|
-
|
|
|
+ }), 500
|
|
|
|
|
|
# 定义修改数据库记录的 API 接口
|
|
|
@bp.route('/update_item', methods=['PUT'])
|
|
|
def update_record():
|
|
|
+ """
|
|
|
+ 接收 JSON 格式的请求体,包含表名和更新的数据。
|
|
|
+ 尝试更新指定的记录。
|
|
|
+ """
|
|
|
data = request.get_json()
|
|
|
|
|
|
# 检查必要的数据是否提供
|
|
@@ -539,52 +577,55 @@ def update_record():
|
|
|
table_name = data['table']
|
|
|
item = data['item']
|
|
|
|
|
|
- # 假设 item 的第一个元素是 ID
|
|
|
- if not item or next(iter(item.keys())) is None:
|
|
|
+ # 假设 item 的第一个键是 ID
|
|
|
+ id_key = next(iter(item.keys())) # 获取第一个键
|
|
|
+ record_id = item.get(id_key)
|
|
|
+
|
|
|
+ if not record_id:
|
|
|
return jsonify({
|
|
|
"success": False,
|
|
|
- "message": "记录数据为空"
|
|
|
+ "message": "缺少记录 ID"
|
|
|
}), 400
|
|
|
|
|
|
- # 获取 ID 和其他字段值
|
|
|
- id_key = next(iter(item.keys()))
|
|
|
- record_id = item[id_key]
|
|
|
- updates = {key: value for key, value in item.items() if key != id_key} # 排除 ID
|
|
|
+ # 获取更新的字段和值
|
|
|
+ updates = {key: value for key, value in item.items() if key != id_key}
|
|
|
|
|
|
- cur = db.cursor()
|
|
|
-
|
|
|
- try:
|
|
|
- record_id = int(record_id) # 确保 ID 是整数
|
|
|
- except ValueError:
|
|
|
+ if not updates:
|
|
|
return jsonify({
|
|
|
"success": False,
|
|
|
- "message": "ID 必须是整数"
|
|
|
+ "message": "没有提供需要更新的字段"
|
|
|
}), 400
|
|
|
|
|
|
- # 准备参数列表,包括更新的值和 ID
|
|
|
- parameters = list(updates.values()) + [record_id]
|
|
|
+ # 动态构建 SQL
|
|
|
+ set_clause = ', '.join([f"{key} = :{key}" for key in updates.keys()])
|
|
|
+ sql = f"UPDATE {table_name} SET {set_clause} WHERE {id_key} = :id_value"
|
|
|
+
|
|
|
+ # 添加 ID 到参数
|
|
|
+ updates['id_value'] = record_id
|
|
|
|
|
|
- # 执行更新操作
|
|
|
- set_clause = ','.join([f"{k} = ?" for k in updates.keys()])
|
|
|
- sql = f"UPDATE {table_name} SET {set_clause} WHERE {id_key} = ?"
|
|
|
try:
|
|
|
- cur.execute(sql, parameters)
|
|
|
- db.commit()
|
|
|
- if cur.rowcount == 0:
|
|
|
+ # 使用 SQLAlchemy 执行更新
|
|
|
+ with db.session.begin():
|
|
|
+ result = db.session.execute(text(sql), updates)
|
|
|
+
|
|
|
+ # 检查是否有更新的记录
|
|
|
+ if result.rowcount == 0:
|
|
|
return jsonify({
|
|
|
"success": False,
|
|
|
"message": "未找到要更新的记录"
|
|
|
}), 404
|
|
|
+
|
|
|
return jsonify({
|
|
|
"success": True,
|
|
|
"message": "数据更新成功"
|
|
|
}), 200
|
|
|
- except sqlite3.Error as e:
|
|
|
- db.rollback()
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ # 捕获所有异常并返回
|
|
|
return jsonify({
|
|
|
"success": False,
|
|
|
- "message": f"更新失败: {e}"
|
|
|
- }), 400
|
|
|
+ "message": f"更新失败: {str(e)}"
|
|
|
+ }), 500
|
|
|
|
|
|
|
|
|
# 定义查询数据库记录的 API 接口
|
|
@@ -923,6 +964,7 @@ def login_user():
|
|
|
|
|
|
# 更新用户信息接口
|
|
|
|
|
|
+
|
|
|
@bp.route('/update_user', methods=['POST'])
|
|
|
def update_user():
|
|
|
# 获取前端传来的数据
|
|
@@ -988,6 +1030,7 @@ def update_user():
|
|
|
logger.error(f"Error updating user: {e}", exc_info=True)
|
|
|
return jsonify({"success": False, "message": "更新失败"}), 500
|
|
|
|
|
|
+
|
|
|
# 注册用户
|
|
|
@bp.route('/register', methods=['POST'])
|
|
|
def register_user():
|
|
@@ -1054,4 +1097,171 @@ def get_column_names(table_name):
|
|
|
return [row[1] for row in result] # 第二列是列名
|
|
|
except Exception as e:
|
|
|
logger.error(f"Error getting column names for table {table_name}: {e}", exc_info=True)
|
|
|
- return []
|
|
|
+ return []
|
|
|
+
|
|
|
+
|
|
|
+# 导出数据
|
|
|
+@bp.route('/export_data', methods=['GET'])
|
|
|
+def export_data():
|
|
|
+ table_name = request.args.get('table')
|
|
|
+ file_format = request.args.get('format', 'excel').lower()
|
|
|
+
|
|
|
+ if not table_name:
|
|
|
+ return jsonify({'error': '缺少表名参数'}), 400
|
|
|
+ if not table_name.isidentifier():
|
|
|
+ return jsonify({'error': '无效的表名'}), 400
|
|
|
+
|
|
|
+ try:
|
|
|
+ conn = get_db()
|
|
|
+ query = "SELECT name FROM sqlite_master WHERE type='table' AND name=?;"
|
|
|
+ table_exists = conn.execute(query, (table_name,)).fetchone()
|
|
|
+ if not table_exists:
|
|
|
+ return jsonify({'error': f"表 {table_name} 不存在"}), 404
|
|
|
+
|
|
|
+ query = f"SELECT * FROM {table_name};"
|
|
|
+ df = pd.read_sql(query, conn)
|
|
|
+
|
|
|
+ output = BytesIO()
|
|
|
+ if file_format == 'csv':
|
|
|
+ df.to_csv(output, index=False, encoding='utf-8')
|
|
|
+ output.seek(0)
|
|
|
+ return send_file(output, as_attachment=True, download_name=f'{table_name}_data.csv', mimetype='text/csv')
|
|
|
+ elif file_format == 'excel':
|
|
|
+ df.to_excel(output, index=False, engine='openpyxl')
|
|
|
+ output.seek(0)
|
|
|
+ return send_file(output, as_attachment=True, download_name=f'{table_name}_data.xlsx',
|
|
|
+ mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
|
|
+ else:
|
|
|
+ return jsonify({'error': '不支持的文件格式,仅支持 CSV 和 Excel'}), 400
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ logger.error(f"Error in export_data: {e}", exc_info=True)
|
|
|
+ return jsonify({'error': str(e)}), 500
|
|
|
+
|
|
|
+
|
|
|
+# 导入数据接口
|
|
|
+@bp.route('/import_data', methods=['POST'])
|
|
|
+def import_data():
|
|
|
+ logger.debug("Import data endpoint accessed.")
|
|
|
+ if 'file' not in request.files:
|
|
|
+ logger.error("No file in request.")
|
|
|
+ return jsonify({'success': False, 'message': '文件缺失'}), 400
|
|
|
+
|
|
|
+ file = request.files['file']
|
|
|
+ table_name = request.form.get('table')
|
|
|
+
|
|
|
+ if not table_name:
|
|
|
+ logger.error("Missing table name parameter.")
|
|
|
+ return jsonify({'success': False, 'message': '缺少表名参数'}), 400
|
|
|
+
|
|
|
+ if file.filename == '':
|
|
|
+ logger.error("No file selected.")
|
|
|
+ return jsonify({'success': False, 'message': '未选择文件'}), 400
|
|
|
+
|
|
|
+ try:
|
|
|
+ # 保存文件到临时路径
|
|
|
+ temp_path = os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(file.filename))
|
|
|
+ file.save(temp_path)
|
|
|
+ logger.debug(f"File saved to temporary path: {temp_path}")
|
|
|
+
|
|
|
+ # 根据文件类型读取文件
|
|
|
+ if file.filename.endswith('.xlsx'):
|
|
|
+ df = pd.read_excel(temp_path)
|
|
|
+ elif file.filename.endswith('.csv'):
|
|
|
+ df = pd.read_csv(temp_path)
|
|
|
+ else:
|
|
|
+ logger.error("Unsupported file format.")
|
|
|
+ return jsonify({'success': False, 'message': '仅支持 Excel 和 CSV 文件'}), 400
|
|
|
+
|
|
|
+ # 获取数据库列名
|
|
|
+ db_columns = get_column_names(table_name)
|
|
|
+ if 'id' in db_columns:
|
|
|
+ db_columns.remove('id') # 假设 id 列是自增的,不需要处理
|
|
|
+
|
|
|
+ if not set(db_columns).issubset(set(df.columns)):
|
|
|
+ logger.error(f"File columns do not match database columns. File columns: {df.columns.tolist()}, Expected: {db_columns}")
|
|
|
+ return jsonify({'success': False, 'message': '文件列名与数据库表不匹配'}), 400
|
|
|
+
|
|
|
+ # 清洗数据并删除空值行
|
|
|
+ df_cleaned = df[db_columns].dropna()
|
|
|
+
|
|
|
+ # 统一数据类型,避免 int 和 float 合并问题
|
|
|
+ df_cleaned[db_columns] = df_cleaned[db_columns].apply(pd.to_numeric, errors='coerce')
|
|
|
+
|
|
|
+ # 获取现有的数据
|
|
|
+ conn = get_db()
|
|
|
+ with conn:
|
|
|
+ existing_data = pd.read_sql(f"SELECT * FROM {table_name}", conn)
|
|
|
+
|
|
|
+ # 查找重复数据
|
|
|
+ duplicates = df_cleaned.merge(existing_data, on=db_columns, how='inner')
|
|
|
+
|
|
|
+ # 如果有重复数据,删除它们
|
|
|
+ df_cleaned = df_cleaned[~df_cleaned.index.isin(duplicates.index)]
|
|
|
+ logger.warning(f"Duplicate data detected and removed: {duplicates}")
|
|
|
+
|
|
|
+ # 获取导入前后的数据量
|
|
|
+ total_data = len(df_cleaned) + len(duplicates)
|
|
|
+ new_data = len(df_cleaned)
|
|
|
+ duplicate_data = len(duplicates)
|
|
|
+
|
|
|
+ # 导入不重复的数据
|
|
|
+ df_cleaned.to_sql(table_name, conn, if_exists='append', index=False)
|
|
|
+ logger.debug(f"Imported {new_data} new records into the database.")
|
|
|
+
|
|
|
+ # 删除临时文件
|
|
|
+ os.remove(temp_path)
|
|
|
+ logger.debug(f"Temporary file removed: {temp_path}")
|
|
|
+
|
|
|
+ # 返回结果
|
|
|
+ return jsonify({
|
|
|
+ 'success': True,
|
|
|
+ 'message': '数据导入成功',
|
|
|
+ 'total_data': total_data,
|
|
|
+ 'new_data': new_data,
|
|
|
+ 'duplicate_data': duplicate_data
|
|
|
+ }), 200
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ logger.error(f"Import failed: {e}", exc_info=True)
|
|
|
+ return jsonify({'success': False, 'message': f'导入失败: {str(e)}'}), 500
|
|
|
+
|
|
|
+
|
|
|
+# 模板下载接口
|
|
|
+@bp.route('/download_template', methods=['GET'])
|
|
|
+def download_template():
|
|
|
+ """
|
|
|
+ 根据给定的表名,下载表的模板(如 CSV 或 Excel 格式)。
|
|
|
+ """
|
|
|
+ table_name = request.args.get('table')
|
|
|
+ if not table_name:
|
|
|
+ return jsonify({'error': '表名参数缺失'}), 400
|
|
|
+
|
|
|
+ columns = get_column_names(table_name)
|
|
|
+ if not columns:
|
|
|
+ return jsonify({'error': f"Table '{table_name}' not found or empty."}), 404
|
|
|
+
|
|
|
+ # 不包括 ID 列
|
|
|
+ if 'id' in columns:
|
|
|
+ columns.remove('id')
|
|
|
+
|
|
|
+ df = pd.DataFrame(columns=columns)
|
|
|
+
|
|
|
+ file_format = request.args.get('format', 'excel').lower()
|
|
|
+ try:
|
|
|
+ if file_format == 'csv':
|
|
|
+ output = BytesIO()
|
|
|
+ df.to_csv(output, index=False, encoding='utf-8')
|
|
|
+ output.seek(0)
|
|
|
+ return send_file(output, as_attachment=True, download_name=f'{table_name}_template.csv',
|
|
|
+ mimetype='text/csv')
|
|
|
+ else:
|
|
|
+ output = BytesIO()
|
|
|
+ df.to_excel(output, index=False, engine='openpyxl')
|
|
|
+ output.seek(0)
|
|
|
+ return send_file(output, as_attachment=True, download_name=f'{table_name}_template.xlsx',
|
|
|
+ mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
|
|
+ except Exception as e:
|
|
|
+ logger.error(f"Failed to generate template: {e}", exc_info=True)
|
|
|
+ return jsonify({'error': '生成模板文件失败'}), 500
|
|
|
+
|