|
@@ -239,98 +239,86 @@ def create_dynamic_table(table_name: str, field_names: List[str], db: Session):
|
|
|
####表名必填,其它非必填
|
|
|
class ReportCreate(BaseModel):
|
|
|
table_name: str = Field(..., description="表单名称,必填")
|
|
|
- end_time: str = Field(..., description="结束时间,必填,格式为 ISO8601")
|
|
|
- # status: str = Field(..., description="状态,必填")
|
|
|
+ end_time: Optional[str] = Field(None, description="结束时间,非必填,格式为 ISO8601")
|
|
|
issued_status: str = Field(..., description="发布状态,必填")
|
|
|
- creator_name: str = Field(..., description="创建者姓名,必填")
|
|
|
- creator_phone: str = Field(..., description="创建者电话,必填")
|
|
|
- user_ids: List[int] = Field(..., description="用户 ID 列表,必填")
|
|
|
-
|
|
|
+ creator_name: Optional[str] = Field(None, description="创建者姓名,非必填")
|
|
|
+ creator_phone: Optional[str] = Field(None, description="创建者电话,非必填")
|
|
|
+ user_ids: Optional[List[int]] = Field(None, description="用户 ID 列表,非必填")
|
|
|
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)
|
|
|
- ):
|
|
|
+ creator_id=Depends(valid_access_token)):
|
|
|
try:
|
|
|
+ # 验证 table_name 是否必填
|
|
|
+ if not report.table_name:
|
|
|
+ return {"code": 400, "msg": "表单名称(table_name)是必填项"}
|
|
|
+
|
|
|
# 获取当前时间并格式化为 YYYYMMDDHHMMSS
|
|
|
current_time_str = datetime.now().strftime("%Y%m%d%H%M%S")
|
|
|
# 动态生成 data_table_name
|
|
|
- table_name_pinyin=''
|
|
|
- for i in range(len(lazy_pinyin(report.table_name, style=Style.FIRST_LETTER))):
|
|
|
- table_name_pinyin += ''.join(lazy_pinyin(report.table_name, style=Style.FIRST_LETTER)[i]).lower()
|
|
|
+ table_name_pinyin = ''.join(lazy_pinyin(report.table_name, style=Style.FIRST_LETTER)).lower()
|
|
|
data_table_name = f"tbxt_{table_name_pinyin}_{current_time_str}"
|
|
|
- # 待发布状态(即暂时)需领处理,如果是待发布(issued_status 1待发布 2已发布),
|
|
|
- # 那么只需要填联系人姓名,联系电话,填报人,截止时间和表名
|
|
|
- # 可以先不创表,但是填报人先报上?
|
|
|
- if str(report.issued_status) == '2' and not report.field_names:
|
|
|
- #这里有bug
|
|
|
- return {"code": 400, "msg": "发布状态下,需要填写字段信息"}
|
|
|
- # raise HTTPException(status_code=403, detail='发布状态下,需要填写字段信息')
|
|
|
-
|
|
|
- if str(report.issued_status) == '2':
|
|
|
-
|
|
|
- # 登记填报管理
|
|
|
- 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(),
|
|
|
- end_time=report.end_time,
|
|
|
- status=0,
|
|
|
- issued_status=report.issued_status,
|
|
|
- collection_status=0,#未收取
|
|
|
- period_type=report.period_type,
|
|
|
- creator_name=report.creator_name,
|
|
|
- creator_id=creator_id,
|
|
|
- creator_phone=report.creator_phone,
|
|
|
- num_reporters = len(report.user_ids),
|
|
|
- issued_time = datetime.now()
|
|
|
- )
|
|
|
- db.add(new_report)
|
|
|
- db.commit()
|
|
|
- db.refresh(new_report)
|
|
|
- else:
|
|
|
- # 登记填报管理
|
|
|
- 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(),
|
|
|
- end_time=report.end_time,
|
|
|
- status=0,
|
|
|
- issued_status=report.issued_status,
|
|
|
- collection_status=0, # 未收取
|
|
|
- period_type=report.period_type,
|
|
|
- creator_name=report.creator_name,
|
|
|
- creator_id=creator_id,
|
|
|
- creator_phone=report.creator_phone,
|
|
|
- num_reporters=len(report.user_ids)
|
|
|
- )
|
|
|
- db.add(new_report)
|
|
|
- db.commit()
|
|
|
- db.refresh(new_report)
|
|
|
|
|
|
- if report.field_names :
|
|
|
- if len(report.field_names) > 0:
|
|
|
- # 动态创建新表
|
|
|
- create_dynamic_table(data_table_name, report.field_names, db)
|
|
|
+ # 根据 issued_status 判断必填字段
|
|
|
+ if report.issued_status == "2": # 发布状态
|
|
|
+ # 验证发布状态下必填字段
|
|
|
+ if not all([report.end_time, report.creator_name, report.creator_phone, report.user_ids, report.field_names]):
|
|
|
+ missing_fields = []
|
|
|
+ if not report.end_time:
|
|
|
+ missing_fields.append("end_time")
|
|
|
+ if not report.creator_name:
|
|
|
+ missing_fields.append("creator_name")
|
|
|
+ if not report.creator_phone:
|
|
|
+ missing_fields.append("creator_phone")
|
|
|
+ if not report.user_ids:
|
|
|
+ missing_fields.append("user_ids")
|
|
|
+ if not report.field_names:
|
|
|
+ missing_fields.append("field_names")
|
|
|
+ return {"code": 400, "msg": f"发布状态下,以下字段为必填项:{', '.join(missing_fields)}"}
|
|
|
+ elif report.issued_status != "1": # 如果 issued_status 不是 1 或 2,返回错误
|
|
|
+ return {"code": 400, "msg": "issued_status 必须为 1(待发布)或 2(已发布)"}
|
|
|
+
|
|
|
+ # 登记填报管理
|
|
|
+ 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(),
|
|
|
+ end_time=datetime.fromisoformat(report.end_time) if report.end_time else None,
|
|
|
+ status=0,
|
|
|
+ issued_status=report.issued_status,
|
|
|
+ collection_status=0, # 未收取
|
|
|
+ period_type=report.period_type,
|
|
|
+ creator_name=report.creator_name,
|
|
|
+ creator_id=creator_id,
|
|
|
+ creator_phone=report.creator_phone,
|
|
|
+ num_reporters=len(report.user_ids) if report.user_ids else 0,
|
|
|
+ issued_time=datetime.now() if report.issued_status == "2" else None
|
|
|
+ )
|
|
|
+ db.add(new_report)
|
|
|
+ db.commit()
|
|
|
+ db.refresh(new_report)
|
|
|
+
|
|
|
+ # 如果有字段信息,则动态创建新表
|
|
|
+ if report.field_names and len(report.field_names) > 0:
|
|
|
+ create_dynamic_table(data_table_name, report.field_names, db)
|
|
|
|
|
|
# 为每个用户创建填报记录
|
|
|
- for user_id in report.user_ids:
|
|
|
- submission = FormSubmission(
|
|
|
- report_id=new_report.report_id,
|
|
|
- user_id=user_id,
|
|
|
- submission_status=0 # 默认状态为未填报
|
|
|
- )
|
|
|
- db.add(submission)
|
|
|
- db.commit()
|
|
|
+ if report.user_ids:
|
|
|
+ for user_id in report.user_ids:
|
|
|
+ submission = FormSubmission(
|
|
|
+ report_id=new_report.report_id,
|
|
|
+ user_id=user_id,
|
|
|
+ submission_status=0 # 默认状态为未填报
|
|
|
+ )
|
|
|
+ db.add(submission)
|
|
|
+ db.commit()
|
|
|
+
|
|
|
return {
|
|
|
"code": 200,
|
|
|
- "msg":"创建成功"
|
|
|
+ "msg": "创建成功"
|
|
|
}
|
|
|
except Exception as e:
|
|
|
raise HTTPException(status_code=400, detail=str(e))
|