|
@@ -18,19 +18,7 @@ class ReportField(BaseModel):
|
|
|
name: str
|
|
|
|
|
|
|
|
|
-class ReportCreate(BaseModel):
|
|
|
- table_name: str
|
|
|
- start_time: str
|
|
|
- end_time: str
|
|
|
- status: str
|
|
|
- issued_status: str
|
|
|
- period_type: str
|
|
|
- creator_name: str
|
|
|
- # creator_id: int
|
|
|
- creator_phone:str
|
|
|
- # num_reporters:int
|
|
|
- field_names: List[str] # 用户只传递字段名称
|
|
|
- user_ids: List[int]
|
|
|
+
|
|
|
|
|
|
def get_next_event_id(db: Session):
|
|
|
while True:
|
|
@@ -173,6 +161,18 @@ def create_dynamic_table(table_name: str, field_names: List[str], db: Session):
|
|
|
# 创建表
|
|
|
metadata.create_all(bind=db.bind)
|
|
|
|
|
|
+class ReportCreate(BaseModel):
|
|
|
+ table_name: str
|
|
|
+ start_time: str
|
|
|
+ end_time: str
|
|
|
+ status: str
|
|
|
+ issued_status: str
|
|
|
+ period_type: str
|
|
|
+ creator_name: str
|
|
|
+ # creator_id: int
|
|
|
+ creator_phone:str
|
|
|
+ field_names: List[str] # 用户只传递字段名称
|
|
|
+ user_ids: List[int]
|
|
|
|
|
|
# 新建填报和创建新表的接口
|
|
|
@router.post("/report/")
|
|
@@ -190,13 +190,12 @@ def create_report_and_table(report: ReportCreate, db: Session = Depends(get_db),
|
|
|
|
|
|
# 动态创建新表
|
|
|
create_dynamic_table(data_table_name, report.field_names, db)
|
|
|
-
|
|
|
# 登记填报管理
|
|
|
new_report = ReportManagement(
|
|
|
report_id=get_next_event_id(db),
|
|
|
table_name=report.table_name,
|
|
|
data_table_name=data_table_name,
|
|
|
- start_time=datetime.now(),
|
|
|
+ start_time=start_time,
|
|
|
end_time=datetime.now(),
|
|
|
status=report.status,
|
|
|
issued_status=report.issued_status,
|
|
@@ -233,7 +232,7 @@ class ReportQuery(BaseModel):
|
|
|
status: Optional[List[int]] = None
|
|
|
start_time: Optional[datetime] = None
|
|
|
end_time: Optional[datetime] = None
|
|
|
- issued_status: Optional[List[int]] = None
|
|
|
+ issued_status: Optional[List[int]] = 1
|
|
|
page_num: int = 1
|
|
|
page_size: int = 10
|
|
|
|
|
@@ -313,14 +312,42 @@ async def select_report(
|
|
|
|
|
|
|
|
|
|
|
|
+# class ReportUpdate(BaseModel):
|
|
|
+# table_name: str = None
|
|
|
+# status: int = None
|
|
|
+# # issued_status: int = None
|
|
|
+# period_type: str = None
|
|
|
+# end_time: str = None
|
|
|
+# comments: dict = None # 字典,键为字段名,值为新的备注
|
|
|
+# new_fields: List[dict] = None
|
|
|
+
|
|
|
class ReportUpdate(BaseModel):
|
|
|
- table_name: str = None
|
|
|
- status: int = None
|
|
|
- issued_status: int = None
|
|
|
- period_type: str = None
|
|
|
- end_time: str = None
|
|
|
- comments: dict = None # 字典,键为字段名,值为新的备注
|
|
|
- # creator_id: str = None
|
|
|
+ table_name: Optional[str] = None
|
|
|
+ status: Optional[int] = None
|
|
|
+ period_type: Optional[str] = None
|
|
|
+ end_time: Optional[str] = None
|
|
|
+ comments: Optional[Dict[str, str]] = None
|
|
|
+ new_fields: Optional[List[Dict[str, str]]] = None
|
|
|
+
|
|
|
+ class Config:
|
|
|
+ extra = 'allow'
|
|
|
+
|
|
|
+# 动态添加字段到表
|
|
|
+def add_column_if_not_exists(table_name: str, column_name: str, column_type: str, comment: str, db: Session):
|
|
|
+ inspector = inspect(db.bind)
|
|
|
+ columns = inspector.get_columns(table_name)
|
|
|
+ existing_column_names = {column['name'] for column in columns}
|
|
|
+ if column_name not in existing_column_names:
|
|
|
+ try:
|
|
|
+ db.execute(
|
|
|
+ text(f"""
|
|
|
+ ALTER TABLE {table_name} ADD COLUMN {column_name} {column_type} COMMENT :comment
|
|
|
+ """),
|
|
|
+ {"comment": comment}
|
|
|
+ )
|
|
|
+ except Exception as e:
|
|
|
+ db.rollback()
|
|
|
+ raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
|
|
#修改
|
|
|
@router.put("/report/{report_id}/")
|
|
@@ -330,12 +357,11 @@ async def update_report(
|
|
|
db: Session = Depends(get_db),
|
|
|
creator_id = Depends(valid_access_token)
|
|
|
):
|
|
|
- # 查询要修改的记录
|
|
|
+ # creator_id = '1' # 假设creator_id已经通过某种方式验证
|
|
|
report = db.query(ReportManagement).filter(ReportManagement.report_id == report_id).first()
|
|
|
if not report:
|
|
|
raise HTTPException(status_code=404, detail="Report not found")
|
|
|
|
|
|
- # 验证请求者ID
|
|
|
if report.creator_id != creator_id:
|
|
|
raise HTTPException(status_code=403, detail="没有权限更新此报告")
|
|
|
|
|
@@ -345,8 +371,7 @@ async def update_report(
|
|
|
|
|
|
if update_data.status is not None:
|
|
|
report.status = update_data.status
|
|
|
- if update_data.issued_status is not None:
|
|
|
- report.issued_status = update_data.issued_status
|
|
|
+
|
|
|
if update_data.period_type:
|
|
|
report.period_type = update_data.period_type
|
|
|
|
|
@@ -358,18 +383,61 @@ async def update_report(
|
|
|
for column_name, comment in update_data.comments.items():
|
|
|
db.execute(
|
|
|
text(f"""
|
|
|
- ALTER TABLE {report.data_table_name} CHANGE {column_name} {column_name} VARCHAR(255) COMMENT :comment
|
|
|
+ ALTER TABLE {report.data_table_name} CHANGE {column_name} {column_name} TEXT COMMENT :comment
|
|
|
"""),
|
|
|
{"comment": comment}
|
|
|
)
|
|
|
|
|
|
+ # 添加新字段
|
|
|
+ if update_data.new_fields:
|
|
|
+ for field in update_data.new_fields:
|
|
|
+ column_name = field['field_name']
|
|
|
+ comment = field['comment']
|
|
|
+ add_column_if_not_exists(report.data_table_name, column_name, 'TEXT', comment, db)
|
|
|
+
|
|
|
db.commit()
|
|
|
db.refresh(report)
|
|
|
|
|
|
return {
|
|
|
- "code":200,
|
|
|
- "msg":"操作成功"
|
|
|
+ "code": 200,
|
|
|
+ "msg": "操作成功"
|
|
|
}
|
|
|
+#发布
|
|
|
+@router.put("/report/{report_id}/update_status/")
|
|
|
+async def update_report_status_and_time(
|
|
|
+ report_id: str,
|
|
|
+ db: Session = Depends(get_db),
|
|
|
+ creator_id = Depends(valid_access_token)
|
|
|
+):
|
|
|
+
|
|
|
+
|
|
|
+ # 查询要修改的记录
|
|
|
+ report = db.query(ReportManagement).filter(ReportManagement.report_id == report_id).first()
|
|
|
+ if not report:
|
|
|
+ raise HTTPException(status_code=404, detail="Report not found")
|
|
|
+ print(report.creator_id,creator_id)
|
|
|
+ # 验证请求者ID
|
|
|
+ if str(report.creator_id) != str(creator_id):
|
|
|
+ raise HTTPException(status_code=403, detail="没有权限更新此报告")
|
|
|
+
|
|
|
+ if report.issued_status ==2:
|
|
|
+ raise HTTPException(status_code=403, detail="不可重复发布")
|
|
|
+ # 更新issued_status为2
|
|
|
+ report.issued_status = 2
|
|
|
+
|
|
|
+ # 更新create_time为当前时间
|
|
|
+ report.start_time = datetime.utcnow()
|
|
|
+
|
|
|
+ try:
|
|
|
+ db.commit()
|
|
|
+ db.refresh(report)
|
|
|
+ return {
|
|
|
+ "code": 200,
|
|
|
+ "msg": "操作成功"
|
|
|
+ }
|
|
|
+ except Exception as e:
|
|
|
+ db.rollback()
|
|
|
+ raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
@@ -523,6 +591,8 @@ async def submit_data(
|
|
|
if not submission:
|
|
|
raise HTTPException(status_code=403, detail="用户没有填报权限")
|
|
|
# print(report.creator_id,submit_data.user_id)
|
|
|
+ # if submission.
|
|
|
+
|
|
|
# 将数据写入数据库
|
|
|
for item in submit_data.data:
|
|
|
# 构造插入SQL语句
|
|
@@ -729,7 +799,7 @@ async def get_records_by_creator_and_report(
|
|
|
db: Session = Depends(get_db),
|
|
|
creator_id = Depends(valid_access_token)
|
|
|
):
|
|
|
- # creator_id=1
|
|
|
+ creator_id
|
|
|
# 查询 ReportManagement 表以获取对应记录
|
|
|
report = db.query(ReportManagement).filter(
|
|
|
ReportManagement.creator_id == creator_id,
|