"""响应数据模型定义"""
from typing import Dict, Optional
from pydantic import BaseModel, Field
class JxlsAnnotations(BaseModel):
"""JXLS批注信息"""
area: str = Field(..., description="jx:area批注")
each: str = Field(..., description="jx:each批注")
class DataStructInfo(BaseModel):
"""数据结构信息"""
collectName: str = Field(..., description="集合变量名称")
itemVariable: str = Field(..., description="循环项变量名")
columnCount: int = Field(..., ge=1, description="列数")
lastCell: str = Field(..., description="最后一个单元格引用")
class GenerateTemplateResponse(BaseModel):
"""生成模板响应模型"""
success: bool = Field(..., description="操作是否成功")
templatePath: Optional[str] = Field(None, description="生成的模板文件路径")
message: str = Field(..., description="响应消息")
jxlsAnnotations: Optional[JxlsAnnotations] = Field(None, description="JXLS批注信息")
dataStruct: Optional[DataStructInfo] = Field(None, description="数据结构信息")
error: Optional[str] = Field(None, description="错误类型")
details: Optional[str] = Field(None, description="错误详细信息")
@classmethod
def success_response(
cls,
template_path: str,
message: str,
jxls_annotations: JxlsAnnotations,
data_struct: DataStructInfo
) -> 'GenerateTemplateResponse':
"""创建成功响应"""
return cls(
success=True,
templatePath=template_path,
message=message,
jxlsAnnotations=jxls_annotations,
dataStruct=data_struct
)
@classmethod
def error_response(
cls,
error: str,
details: str,
message: Optional[str] = None
) -> 'GenerateTemplateResponse':
"""创建错误响应"""
return cls(
success=False,
message=message or f"操作失败: {error}",
error=error,
details=details
)