product.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. """Pydantic schemas - 商品管理"""
  2. from pydantic import BaseModel, Field
  3. from typing import Optional
  4. from datetime import datetime
  5. class ProductUnitBase(BaseModel):
  6. """商品单位基础字段"""
  7. name: str = Field(..., max_length=50)
  8. code: str = Field(..., max_length=20)
  9. class ProductUnitCreate(ProductUnitBase):
  10. """创建商品单位请求"""
  11. pass
  12. class ProductUnitResponse(ProductUnitBase):
  13. """商品单位响应"""
  14. id: int
  15. is_active: bool
  16. created_at: datetime
  17. class Config:
  18. from_attributes = True
  19. class ProductCategoryBase(BaseModel):
  20. """商品分类基础字段"""
  21. name: str = Field(..., max_length=100)
  22. parent_id: Optional[int] = None
  23. sort_order: int = 0
  24. class ProductCategoryCreate(ProductCategoryBase):
  25. """创建商品分类请求"""
  26. pass
  27. class ProductCategoryResponse(ProductCategoryBase):
  28. """商品分类响应"""
  29. id: int
  30. is_active: bool
  31. created_at: datetime
  32. class Config:
  33. from_attributes = True
  34. class ProductBase(BaseModel):
  35. """商品基础字段"""
  36. name: str = Field(..., max_length=200)
  37. category_id: Optional[int] = None
  38. unit_id: Optional[int] = None
  39. description: Optional[str] = None
  40. image_url: Optional[str] = None
  41. class ProductCreate(ProductBase):
  42. """创建商品请求"""
  43. pass
  44. class ProductUpdate(BaseModel):
  45. """更新商品请求"""
  46. name: Optional[str] = None
  47. category_id: Optional[int] = None
  48. unit_id: Optional[int] = None
  49. description: Optional[str] = None
  50. image_url: Optional[str] = None
  51. is_active: Optional[bool] = None
  52. class ProductResponse(ProductBase):
  53. """商品响应"""
  54. id: int
  55. is_active: bool
  56. created_at: datetime
  57. unit: Optional[ProductUnitResponse] = None
  58. class Config:
  59. from_attributes = True