software.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. """
  2. 软件管理接口
  3. """
  4. from fastapi import APIRouter, Depends, Query
  5. from sqlalchemy.orm import Session
  6. from pydantic import BaseModel
  7. from app.database import get_db
  8. from app.api.deps import get_current_admin
  9. from app.models.software import Software
  10. from datetime import datetime
  11. router = APIRouter()
  12. class SoftwareCreate(BaseModel):
  13. """创建软件请求"""
  14. code: str
  15. name: str
  16. description: str | None = None
  17. daily_price: float
  18. class SoftwareUpdate(BaseModel):
  19. """更新软件请求"""
  20. name: str
  21. description: str | None = None
  22. daily_price: float
  23. @router.get("")
  24. async def get_software_list(
  25. db: Session = Depends(get_db),
  26. current_admin = Depends(get_current_admin)
  27. ):
  28. """获取软件列表"""
  29. software_list = db.query(Software).all()
  30. return {
  31. "code": 200,
  32. "msg": "成功",
  33. "data": [
  34. {
  35. "id": s.id,
  36. "code": s.code,
  37. "name": s.name,
  38. "description": s.description,
  39. "daily_price": float(s.daily_price),
  40. "is_active": bool(s.is_active),
  41. "created_at": s.created_at.isoformat() if s.created_at else None,
  42. "updated_at": s.updated_at.isoformat() if s.updated_at else None
  43. }
  44. for s in software_list
  45. ]
  46. }
  47. @router.post("")
  48. async def create_software(
  49. req: SoftwareCreate,
  50. db: Session = Depends(get_db),
  51. current_admin = Depends(get_current_admin)
  52. ):
  53. """创建软件"""
  54. # 检查是否已存在
  55. existing = db.query(Software).filter(Software.code == req.code).first()
  56. if existing:
  57. return {"code": 400, "msg": "软件特征码已存在"}
  58. software = Software(
  59. code=req.code,
  60. name=req.name,
  61. description=req.description,
  62. daily_price=req.daily_price,
  63. is_active=1,
  64. created_at=datetime.now(),
  65. updated_at=datetime.now()
  66. )
  67. db.add(software)
  68. db.commit()
  69. return {"code": 200, "msg": "创建成功", "data": {"id": software.id}}
  70. @router.put("/{software_id}")
  71. async def update_software(
  72. software_id: int,
  73. req: SoftwareUpdate,
  74. db: Session = Depends(get_db),
  75. current_admin = Depends(get_current_admin)
  76. ):
  77. """更新软件"""
  78. software = db.query(Software).filter(Software.id == software_id).first()
  79. if not software:
  80. return {"code": 404, "msg": "软件不存在"}
  81. software.name = req.name
  82. software.description = req.description
  83. software.daily_price = req.daily_price
  84. software.updated_at = datetime.now()
  85. db.commit()
  86. return {"code": 200, "msg": "更新成功"}
  87. @router.put("/{software_id}/status")
  88. async def update_software_status(
  89. software_id: int,
  90. is_active: int = Query(..., description="状态:1-启用,0-禁用"),
  91. db: Session = Depends(get_db),
  92. current_admin = Depends(get_current_admin)
  93. ):
  94. """启用/禁用软件"""
  95. software = db.query(Software).filter(Software.id == software_id).first()
  96. if not software:
  97. return {"code": 404, "msg": "软件不存在"}
  98. software.is_active = is_active
  99. software.updated_at = datetime.now()
  100. db.commit()
  101. return {"code": 200, "msg": "状态更新成功"}
  102. @router.delete("/{software_id}")
  103. async def delete_software(
  104. software_id: int,
  105. db: Session = Depends(get_db),
  106. current_admin = Depends(get_current_admin)
  107. ):
  108. """删除软件"""
  109. software = db.query(Software).filter(Software.id == software_id).first()
  110. if not software:
  111. return {"code": 404, "msg": "软件不存在"}
  112. db.delete(software)
  113. db.commit()
  114. return {"code": 200, "msg": "删除成功"}