|
@@ -247,6 +247,7 @@ class ReportCreate(BaseModel):
|
|
|
period_type: Optional[str] = Field(None, description="周期,非必填")
|
|
|
field_names: Optional[List[str]] = Field(None, description="字段名称列表,非必填")
|
|
|
|
|
|
+#创建填报
|
|
|
@router.post("/report/")
|
|
|
def create_report_and_table(report: ReportCreate, db: Session = Depends(get_db),
|
|
|
creator_id=Depends(valid_access_token)):
|
|
@@ -489,20 +490,7 @@ def update_table_fields(table_name: str, field_names: List[str], db: Session):
|
|
|
|
|
|
|
|
|
|
|
|
-class ReportUpdate(BaseModel):
|
|
|
- table_name: Optional[str] = None
|
|
|
- end_time: Optional[str] = None
|
|
|
- status: Optional[int] = None
|
|
|
- issued_status: Optional[str] = None
|
|
|
- period_type: Optional[str] = None
|
|
|
- creator_phone: Optional[str] = None
|
|
|
- creator_name: Optional[str] = None
|
|
|
- user_ids: Optional[List[int]] = Field(None, description="字段名称列表,非必填")
|
|
|
- # comments: Optional[Dict[str, str]] = None
|
|
|
- new_fields: Optional[List[str]] = Field(None, description="字段名称列表,非必填")
|
|
|
|
|
|
- class Config:
|
|
|
- extra = 'allow'
|
|
|
|
|
|
|
|
|
|
|
@@ -531,7 +519,20 @@ def drop_table_if_exists(db: Session, table_name: str):
|
|
|
|
|
|
|
|
|
|
|
|
+class ReportUpdate(BaseModel):
|
|
|
+ table_name: Optional[str] = None
|
|
|
+ end_time: Optional[str] = None
|
|
|
+ status: Optional[int] = None
|
|
|
+ issued_status: Optional[str] = None
|
|
|
+ period_type: Optional[str] = None
|
|
|
+ creator_phone: Optional[str] = None
|
|
|
+ creator_name: Optional[str] = None
|
|
|
+ user_ids: Optional[List[int]] = Field(None, description="字段名称列表,非必填")
|
|
|
+ # comments: Optional[Dict[str, str]] = None
|
|
|
+ new_fields: Optional[List[str]] = Field(None, description="字段名称列表,非必填")
|
|
|
|
|
|
+ class Config:
|
|
|
+ extra = 'allow'
|
|
|
|
|
|
|
|
|
#修改
|
|
@@ -564,8 +565,46 @@ async def update_report(
|
|
|
# 这里要添加发布状态判断,如果是暂存状态则跳过,发布状态则要提供字段信息
|
|
|
#
|
|
|
if update_data.issued_status in ['2', 2]:
|
|
|
- # 检查表是否存在
|
|
|
+
|
|
|
+
|
|
|
table_name = report.data_table_name
|
|
|
+
|
|
|
+ # 查询数据库中的用户 ID 和字段信息
|
|
|
+ existing_user_ids = db.query(FormSubmission.user_id).filter(
|
|
|
+ FormSubmission.report_id == report.report_id
|
|
|
+ ).distinct().all()
|
|
|
+ existing_user_ids = [user[0] for user in existing_user_ids]
|
|
|
+
|
|
|
+ existing_field_names = db.execute(
|
|
|
+ text("""
|
|
|
+ SELECT COLUMN_NAME
|
|
|
+ FROM INFORMATION_SCHEMA.COLUMNS
|
|
|
+ WHERE TABLE_NAME = :table_name AND TABLE_SCHEMA = (SELECT DATABASE())
|
|
|
+ """),
|
|
|
+ {"table_name": table_name}
|
|
|
+ ).fetchall()
|
|
|
+ existing_field_names = [field[0] for field in existing_field_names]
|
|
|
+
|
|
|
+ # 检查发布状态下必填字段
|
|
|
+ required_fields = {
|
|
|
+ "end_time": update_data.end_time or report.end_time,
|
|
|
+ "creator_name": update_data.creator_name or report.creator_name,
|
|
|
+ "creator_phone": update_data.creator_phone or report.creator_phone,
|
|
|
+ "user_ids": update_data.user_ids or existing_user_ids,
|
|
|
+ "new_fields": update_data.new_fields or existing_field_names
|
|
|
+ }
|
|
|
+
|
|
|
+ # 检查 user_ids 和 new_fields 是否为空
|
|
|
+ if not required_fields["user_ids"] or len(required_fields["user_ids"]) == 0:
|
|
|
+ return {"code": 400, "msg": "发布状态下,用户 ID 列表不能为空"}
|
|
|
+ if not required_fields["new_fields"] or len(required_fields["new_fields"]) == 0:
|
|
|
+ return {"code": 400, "msg": "发布状态下,字段名称列表不能为空"}
|
|
|
+
|
|
|
+ missing_fields = [field for field, value in required_fields.items() if not value]
|
|
|
+ if missing_fields:
|
|
|
+ return {"code": 400, "msg": f"发布状态下,以下字段为必填项:{', '.join(missing_fields)}"}
|
|
|
+
|
|
|
+ # 检查表是否存在
|
|
|
if not table_exists(db, table_name):
|
|
|
# 如果表不存在,根据 new_fields 创建表
|
|
|
if not update_data.new_fields:
|
|
@@ -576,6 +615,11 @@ async def update_report(
|
|
|
elif table_exists(db, table_name) and update_data.new_fields:
|
|
|
update_table_fields(table_name, update_data.new_fields, db)
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+ report.issued_status = 2
|
|
|
+ report.issued_time = datetime.utcnow()
|
|
|
+
|
|
|
elif update_data.issued_status in ['0', 0]:
|
|
|
if update_data.new_fields:
|
|
|
if len(update_data.new_fields)>0:
|
|
@@ -620,12 +664,12 @@ async def update_report(
|
|
|
if update_data.end_time:
|
|
|
report.end_time = datetime.fromisoformat(update_data.end_time)
|
|
|
|
|
|
- if update_data.issued_status:
|
|
|
- if update_data.issued_status in [2,"2"]:
|
|
|
- # 更新issued_status为2
|
|
|
- # 这里要判断是否
|
|
|
- report.issued_status = 2
|
|
|
- report.issued_time = datetime.utcnow()
|
|
|
+ # if update_data.issued_status:
|
|
|
+ # if update_data.issued_status in [2,"2"]:
|
|
|
+ # # 更新issued_status为2
|
|
|
+ # # 这里要判断是否
|
|
|
+ # report.issued_status = 2
|
|
|
+ # report.issued_time = datetime.utcnow()
|
|
|
|
|
|
if update_data.status:
|
|
|
# print(11111111)
|
|
@@ -653,6 +697,7 @@ async def update_report(
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
#发布
|
|
|
@router.put("/report/{report_id}/update_status/")
|
|
|
async def update_report_status_and_time(
|
|
@@ -709,6 +754,17 @@ async def update_report_status_and_time(
|
|
|
# raise HTTPException(status_code=400, detail=str('信息未填写完整,无法发布'))
|
|
|
return {"code": 400, "msg": "信息未填写完整,无法发布"}
|
|
|
|
|
|
+ # 检查其他必要字段是否完整
|
|
|
+ required_fields = {
|
|
|
+ "end_time": report.end_time,
|
|
|
+ "creator_name": report.creator_name,
|
|
|
+ "creator_phone": report.creator_phone,
|
|
|
+ # "period_type": report.period_type,
|
|
|
+ }
|
|
|
+
|
|
|
+ missing_fields = [field for field, value in required_fields.items() if not value]
|
|
|
+ if missing_fields:
|
|
|
+ return {"code": 400, "msg": f"发布状态下,以下字段为必填项:{', '.join(missing_fields)}"}
|
|
|
# 更新issued_status为2
|
|
|
report.issued_status = 2
|
|
|
|