|
@@ -92,12 +92,12 @@ async def get_report_structure(
|
|
|
table_structures = []
|
|
|
column_order = [] # 用于存储字段的顺序
|
|
|
for row in table_structure_query.fetchall():
|
|
|
- if row[0] not in ['collect_status', 'create_id', 'id', 'user_id', 'add_time']:
|
|
|
+ if row[0] not in ['collect_status', 'create_id', 'id', 'user_id', 'add_time','temporarily_store']:
|
|
|
table_structures.append(TableStructure(column_name=row[0], comment=row[1]))
|
|
|
column_order.append(row[0]) # 保存字段顺序
|
|
|
|
|
|
# 查询表中的数据,排除指定字段
|
|
|
- excluded_columns = ['collect_status', 'create_id', 'id', 'user_id', 'add_time']
|
|
|
+ excluded_columns = ['collect_status', 'create_id', 'id', 'user_id', 'add_time','temporarily_store']
|
|
|
columns_to_select = ", ".join(column_order) # 使用字段顺序
|
|
|
table_data_with_headers = []
|
|
|
# print("字段:",columns_to_select)
|
|
@@ -218,6 +218,8 @@ def create_dynamic_table(table_name: str, field_names: List[str], db: Session):
|
|
|
Column('create_id', Integer,comment="创建者ID"),
|
|
|
Column('collect_status', Boolean,comment="收取结果"),
|
|
|
Column('add_time', DateTime, server_default=func.now(),comment="添加时间"),
|
|
|
+ # Column('temporarily_store', Integer, default=0,comment="暂存状态"),
|
|
|
+ Column('temporarily_store', Integer, server_default='0', comment="暂存状态"),
|
|
|
extend_existing=True
|
|
|
)
|
|
|
|
|
@@ -459,7 +461,7 @@ def update_table_fields(table_name: str, field_names: List[str], db: Session):
|
|
|
existing_column_names = {col['name'] for col in existing_columns}
|
|
|
|
|
|
# 定义需要保留的基础字段
|
|
|
- columns_to_keep = {'id', 'user_id', 'create_id', 'collect_status','add_time'}
|
|
|
+ columns_to_keep = {'id', 'user_id', 'create_id', 'collect_status','add_time','temporarily_store'}
|
|
|
existing_column_names -= columns_to_keep # 排除基础字段
|
|
|
# print(existing_column_names)
|
|
|
# print(field_names)
|
|
@@ -715,7 +717,7 @@ async def update_report_status_and_time(
|
|
|
table_structures = []
|
|
|
column_order = [] # 用于存储字段的顺序
|
|
|
for row in table_structure_query.fetchall():
|
|
|
- if row[0] not in ['collect_status', 'create_id', 'id', 'user_id', 'add_time']:
|
|
|
+ if row[0] not in ['collect_status', 'create_id', 'id', 'user_id', 'add_time','temporarily_store']:
|
|
|
table_structures.append(TableStructure(column_name=row[0], comment=row[1]))
|
|
|
column_order.append(row[0]) # 保存字段顺序
|
|
|
# print(table_structures)
|
|
@@ -853,7 +855,7 @@ async def get_report_fields(
|
|
|
# 构造返回结果
|
|
|
result_fields = []
|
|
|
for column in columns:
|
|
|
- if column['name'] not in ['collect_status', 'create_id', 'id', 'user_id']:
|
|
|
+ if column['name'] not in ['collect_status', 'create_id', 'id', 'user_id','temporarily_store']:
|
|
|
result_field = {
|
|
|
"field_name": column['name'],
|
|
|
"field_comment": column.get('comment', '无注释')
|
|
@@ -862,7 +864,7 @@ async def get_report_fields(
|
|
|
|
|
|
# 构造返回结果
|
|
|
result_items = []
|
|
|
- excluded_columns = ['id', 'user_id', 'create_id', 'collect_status']
|
|
|
+ excluded_columns = ['id', 'user_id', 'create_id', 'collect_status','temporarily_store']
|
|
|
|
|
|
# 构建查询SQL
|
|
|
query_sql = text(f"""
|
|
@@ -925,29 +927,24 @@ async def submit_data(
|
|
|
):
|
|
|
# 检查用户ID和填报ID是否提供
|
|
|
if not user_id or not submit_data.report_id:
|
|
|
- return {"code":400,"msg":"填报ID是必填项"}
|
|
|
- # raise HTTPException(status_code=400, detail="填报ID是必填项")
|
|
|
+ return {"code": 400, "msg": "填报ID是必填项"}
|
|
|
|
|
|
# 获取对应填报ID的数据表名称
|
|
|
report = db.query(ReportManagement).filter(ReportManagement.report_id == submit_data.report_id).first()
|
|
|
if not report:
|
|
|
return {"code": 400, "msg": "未找到对应的填报ID"}
|
|
|
- # raise HTTPException(status_code=404, detail="未找到对应的填报ID")
|
|
|
|
|
|
data_table_name = report.data_table_name
|
|
|
if not data_table_name:
|
|
|
return {"code": 400, "msg": "未找到对应的数据表名称"}
|
|
|
- # raise HTTPException(status_code=404, detail="未找到对应的数据表名称")
|
|
|
|
|
|
- if report.issued_status not in [2,'2']:
|
|
|
+ if report.issued_status not in [2, '2']:
|
|
|
return {"code": 400, "msg": "当前未发布,不可填写"}
|
|
|
- # raise HTTPException(status_code=404, detail="当前未发布,不可填写")
|
|
|
|
|
|
is_collection = report.collection_status
|
|
|
|
|
|
if is_collection == 2 or is_collection == '2':
|
|
|
return {"code": 400, "msg": "管理员已收取信息,无法填写"}
|
|
|
- # raise HTTPException(status_code=404, detail="管理员已收取信息,无法填写")
|
|
|
|
|
|
# 检查用户是否有权限填报
|
|
|
submission = db.query(FormSubmission).filter(
|
|
@@ -956,37 +953,108 @@ async def submit_data(
|
|
|
).first()
|
|
|
if not submission:
|
|
|
return {"code": 400, "msg": "用户没有填报权限"}
|
|
|
- # raise HTTPException(status_code=403, detail="用户没有填报权限")
|
|
|
-
|
|
|
|
|
|
current_time = datetime.now()
|
|
|
|
|
|
if report.end_time < current_time:
|
|
|
return {"code": 400, "msg": "填写时间已过"}
|
|
|
- # raise HTTPException(status_code=403, detail="填写时间已过")
|
|
|
-
|
|
|
|
|
|
# 将数据写入数据库
|
|
|
for item in submit_data.data:
|
|
|
# 构造插入SQL语句
|
|
|
columns = ', '.join(list(item.keys()) + ['create_id', 'user_id', 'collect_status'])
|
|
|
values = ', '.join(
|
|
|
- [f":{k}" for k in item.keys()] + [f"'{report.creator_id}'", f"'{user_id}'", '1'])
|
|
|
+ [f":{k}" for k in item.keys()] + [f"'{report.creator_id}'", f"'{user_id}'", '1']
|
|
|
+ )
|
|
|
sql = f"INSERT INTO {data_table_name} ({columns}) VALUES ({values})"
|
|
|
# 执行插入操作
|
|
|
db.execute(text(sql), item)
|
|
|
|
|
|
+ # 修改成已填写状态
|
|
|
submission.submission_status = 1
|
|
|
db.add(submission)
|
|
|
+
|
|
|
+ # 删除暂存的记录
|
|
|
+ delete_sql = f"DELETE FROM {data_table_name} WHERE temporarily_store = 1 AND user_id = '{user_id}'"
|
|
|
+ db.execute(text(delete_sql))
|
|
|
+
|
|
|
# 提交事务
|
|
|
db.commit()
|
|
|
|
|
|
return {
|
|
|
- "code":200,
|
|
|
+ "code": 200,
|
|
|
"msg": "数据提交成功"
|
|
|
}
|
|
|
|
|
|
|
|
|
+#数据保存
|
|
|
+@router.post("/save_data")
|
|
|
+async def save_data(
|
|
|
+ db: Session = Depends(get_db),
|
|
|
+ save_data: SubmitData = Body(...),
|
|
|
+ user_id = Depends(valid_access_token)
|
|
|
+):
|
|
|
+ # 检查用户ID和填报ID是否提供
|
|
|
+ if not user_id or not save_data.report_id:
|
|
|
+ return {"code": 400, "msg": "填报ID是必填项"}
|
|
|
+
|
|
|
+ # 获取对应填报ID的数据表名称
|
|
|
+ report = db.query(ReportManagement).filter(ReportManagement.report_id == save_data.report_id).first()
|
|
|
+ if not report:
|
|
|
+ return {"code": 400, "msg": "未找到对应的填报ID"}
|
|
|
+
|
|
|
+ data_table_name = report.data_table_name
|
|
|
+ if not data_table_name:
|
|
|
+ return {"code": 400, "msg": "未找到对应的数据表名称"}
|
|
|
+
|
|
|
+ if report.issued_status not in [2, '2']:
|
|
|
+ return {"code": 400, "msg": "当前未发布,不可填写"}
|
|
|
+
|
|
|
+ is_collection = report.collection_status
|
|
|
+
|
|
|
+ if is_collection == 2 or is_collection == '2':
|
|
|
+ return {"code": 400, "msg": "管理员已收取信息,无法填写"}
|
|
|
+
|
|
|
+ # 检查用户是否有权限填报
|
|
|
+ submission = db.query(FormSubmission).filter(
|
|
|
+ FormSubmission.report_id == save_data.report_id,
|
|
|
+ FormSubmission.user_id == str(user_id) # 确保user_id是字符串类型
|
|
|
+ ).first()
|
|
|
+ if not submission:
|
|
|
+ return {"code": 400, "msg": "用户没有填报权限"}
|
|
|
+
|
|
|
+ current_time = datetime.now()
|
|
|
+
|
|
|
+ if report.end_time < current_time:
|
|
|
+ return {"code": 400, "msg": "填写时间已过"}
|
|
|
+
|
|
|
+ # 检查目标表是否包含 temporarily_store 字段
|
|
|
+ inspector = inspect(db.bind)
|
|
|
+ columns = inspector.get_columns(data_table_name)
|
|
|
+ column_names = [col['name'] for col in columns]
|
|
|
+ if 'temporarily_store' not in column_names:
|
|
|
+ return {"code": 400, "msg": "目标表中缺失字段 'temporarily_store',保存失败"}
|
|
|
+
|
|
|
+
|
|
|
+ # 将数据写入数据库
|
|
|
+ for item in save_data.data:
|
|
|
+ # 构造插入SQL语句
|
|
|
+ columns = ', '.join(list(item.keys()) + ['create_id', 'user_id', 'collect_status', 'temporarily_store'])
|
|
|
+ values = ', '.join(
|
|
|
+ [f":{k}" for k in item.keys()] + [f"'{report.creator_id}'", f"'{user_id}'", '1', '1']
|
|
|
+ )
|
|
|
+ sql = f"INSERT INTO {data_table_name} ({columns}) VALUES ({values})"
|
|
|
+ # 执行插入操作
|
|
|
+ db.execute(text(sql), item)
|
|
|
+
|
|
|
+ # 提交事务
|
|
|
+ db.commit()
|
|
|
+
|
|
|
+ return {
|
|
|
+ "code": 200,
|
|
|
+ "msg": "数据暂存成功"
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
@@ -1042,7 +1110,7 @@ async def get_submission_status(
|
|
|
|
|
|
# 构造返回结果
|
|
|
result_items = []
|
|
|
- excluded_columns = ['id', 'user_id', 'create_id', 'collect_status']
|
|
|
+ excluded_columns = ['id', 'user_id', 'create_id', 'collect_status','temporarily_store']
|
|
|
|
|
|
# 构建查询SQL
|
|
|
query_sql = text(f"""
|
|
@@ -1071,12 +1139,22 @@ async def get_submission_status(
|
|
|
return result
|
|
|
|
|
|
|
|
|
+# 辅助函数:检查表是否存在
|
|
|
+def table_exists1(inspector: Inspector, table_name: str) -> bool:
|
|
|
+ return inspector.has_table(table_name)
|
|
|
+
|
|
|
+
|
|
|
# 辅助函数:根据字段备注获取表中所有匹配字段名
|
|
|
def get_columns_with_comment_like(
|
|
|
- inspector: Inspector, table_name: str, comment_like: str
|
|
|
+ inspector: Inspector, table_name: str, comment_like: str
|
|
|
) -> List[str]:
|
|
|
+ # 检查表是否存在
|
|
|
+ if not table_exists1(inspector, table_name):
|
|
|
+ return [] # 或者可以选择抛出异常
|
|
|
+
|
|
|
columns = inspector.get_columns(table_name)
|
|
|
- matching_columns = [column['name'] for column in columns if column.get('comment') and comment_like in column['comment']]
|
|
|
+ matching_columns = [column['name'] for column in columns if
|
|
|
+ column.get('comment') and comment_like in column['comment']]
|
|
|
return matching_columns
|
|
|
|
|
|
# 辅助函数:检查是否有字段备注匹配
|
|
@@ -1137,7 +1215,8 @@ async def get_reports_by_creator(
|
|
|
current_time = datetime.now()
|
|
|
|
|
|
if end_time < current_time and collection_status in [0,'0'] and issued_status in [2,'2']:
|
|
|
- # print("符合自动收取")
|
|
|
+ # print("符合自动收
|
|
|
+ # 取")
|
|
|
report.collection_status=2
|
|
|
report.collection_time = current_time
|
|
|
|
|
@@ -1231,7 +1310,6 @@ async def get_records_by_creator_and_report(
|
|
|
db: Session = Depends(get_db),
|
|
|
creator_id = Depends(valid_access_token)
|
|
|
):
|
|
|
- # creator_id
|
|
|
# 查询 ReportManagement 表以获取对应记录
|
|
|
report = db.query(ReportManagement).filter(
|
|
|
ReportManagement.creator_id == creator_id,
|
|
@@ -1241,21 +1319,10 @@ async def get_records_by_creator_and_report(
|
|
|
# 如果没有找到记录,返回404
|
|
|
if not report:
|
|
|
return {"code": 404, "msg": "Report not found"}
|
|
|
- # raise HTTPException(status_code=404, detail="Report not found")
|
|
|
|
|
|
# 如果没有 data_table_name,返回404
|
|
|
if not report.data_table_name:
|
|
|
return {"code": 404, "msg": "Data table name not found"}
|
|
|
- # raise HTTPException(status_code=404, detail="Data table name not found")
|
|
|
-
|
|
|
- # 查询工单表所有信息,并关联用户表匹配到用户名字
|
|
|
- query_sql = text(f"""
|
|
|
- SELECT w.*, u.user_name
|
|
|
- FROM {report.data_table_name} w
|
|
|
- LEFT JOIN sys_user u ON w.user_id = u.user_id
|
|
|
- """)
|
|
|
- result = db.execute(query_sql)
|
|
|
- rows = result.fetchall()
|
|
|
|
|
|
# 使用SQLAlchemy的inspect功能来获取表的列信息
|
|
|
inspector = inspect(db.bind)
|
|
@@ -1268,18 +1335,39 @@ async def get_records_by_creator_and_report(
|
|
|
# 构造字段信息
|
|
|
columns_info = []
|
|
|
for column in column_names:
|
|
|
- if column not in ['id', 'user_id', 'create_id', 'collect_status']:
|
|
|
+ if column not in ['id', 'user_id', 'create_id', 'collect_status', 'temporarily_store']:
|
|
|
columns_info.append({
|
|
|
'prop': column,
|
|
|
'label': column_comments.get(column, '')
|
|
|
})
|
|
|
|
|
|
+ # 构造查询SQL语句
|
|
|
+ if 'temporarily_store' in column_names:
|
|
|
+
|
|
|
+ # 如果存在 temporarily_store 字段,过滤掉暂存状态的记录
|
|
|
+ query_sql = text(f"""
|
|
|
+ SELECT w.*, u.user_name
|
|
|
+ FROM {report.data_table_name} w
|
|
|
+ LEFT JOIN sys_user u ON w.user_id = u.user_id
|
|
|
+ WHERE w.temporarily_store != 1
|
|
|
+ """)
|
|
|
+ else:
|
|
|
+
|
|
|
+ query_sql = text(f"""
|
|
|
+ SELECT w.*, u.user_name
|
|
|
+ FROM {report.data_table_name} w
|
|
|
+ LEFT JOIN sys_user u ON w.user_id = u.user_id
|
|
|
+ """)
|
|
|
+
|
|
|
+ result = db.execute(query_sql)
|
|
|
+ rows = result.fetchall()
|
|
|
+
|
|
|
# 构造返回结果
|
|
|
rows_data = []
|
|
|
for row in rows:
|
|
|
# 过滤掉不需要的列,并添加到结果中
|
|
|
filtered_row = {column: row[idx] for idx, column in enumerate(column_names)
|
|
|
- if column not in ['id', 'user_id', 'create_id', 'collect_status']}
|
|
|
+ if column not in ['id', 'user_id', 'create_id', 'collect_status', 'temporarily_store']}
|
|
|
filtered_row['user_name'] = row[-1] # 添加用户昵称
|
|
|
rows_data.append(filtered_row)
|
|
|
|
|
@@ -1303,7 +1391,6 @@ async def get_records_by_creator_and_report(
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
from fastapi import status
|
|
|
@router.get("/export_to_excel")
|
|
|
@router.post("/export_to_excel")
|
|
@@ -1329,7 +1416,7 @@ async def export_to_excel(
|
|
|
columns = inspector.get_columns(data_table_name)
|
|
|
|
|
|
# 提取用户填报的字段注释
|
|
|
- user_report_columns = [col for col in columns if col['name'] not in ['id', 'create_id', 'collect_status', 'add_time', 'user_id']]
|
|
|
+ user_report_columns = [col for col in columns if col['name'] not in ['id', 'create_id', 'collect_status', 'add_time', 'user_id','temporarily_store']]
|
|
|
column_comments = [col.get('comment', '') for col in user_report_columns]
|
|
|
|
|
|
# 构建查询SQL,关联 sys_user 表获取 nick_name
|