| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- """
- 软件管理接口
- """
- from fastapi import APIRouter, Depends, Query
- from sqlalchemy.orm import Session
- from pydantic import BaseModel
- from app.database import get_db
- from app.api.deps import get_current_admin
- from app.models.software import Software
- from datetime import datetime
- router = APIRouter()
- class SoftwareCreate(BaseModel):
- """创建软件请求"""
- code: str
- name: str
- description: str | None = None
- daily_price: float
- class SoftwareUpdate(BaseModel):
- """更新软件请求"""
- name: str
- description: str | None = None
- daily_price: float
- @router.get("")
- async def get_software_list(
- db: Session = Depends(get_db),
- current_admin = Depends(get_current_admin)
- ):
- """获取软件列表"""
- software_list = db.query(Software).all()
-
- return {
- "code": 200,
- "msg": "成功",
- "data": [
- {
- "id": s.id,
- "code": s.code,
- "name": s.name,
- "description": s.description,
- "daily_price": float(s.daily_price),
- "is_active": bool(s.is_active),
- "created_at": s.created_at.isoformat() if s.created_at else None,
- "updated_at": s.updated_at.isoformat() if s.updated_at else None
- }
- for s in software_list
- ]
- }
- @router.post("")
- async def create_software(
- req: SoftwareCreate,
- db: Session = Depends(get_db),
- current_admin = Depends(get_current_admin)
- ):
- """创建软件"""
- # 检查是否已存在
- existing = db.query(Software).filter(Software.code == req.code).first()
- if existing:
- return {"code": 400, "msg": "软件特征码已存在"}
-
- software = Software(
- code=req.code,
- name=req.name,
- description=req.description,
- daily_price=req.daily_price,
- is_active=1,
- created_at=datetime.now(),
- updated_at=datetime.now()
- )
- db.add(software)
- db.commit()
-
- return {"code": 200, "msg": "创建成功", "data": {"id": software.id}}
- @router.put("/{software_id}")
- async def update_software(
- software_id: int,
- req: SoftwareUpdate,
- db: Session = Depends(get_db),
- current_admin = Depends(get_current_admin)
- ):
- """更新软件"""
- software = db.query(Software).filter(Software.id == software_id).first()
- if not software:
- return {"code": 404, "msg": "软件不存在"}
-
- software.name = req.name
- software.description = req.description
- software.daily_price = req.daily_price
- software.updated_at = datetime.now()
- db.commit()
-
- return {"code": 200, "msg": "更新成功"}
- @router.put("/{software_id}/status")
- async def update_software_status(
- software_id: int,
- is_active: int = Query(..., description="状态:1-启用,0-禁用"),
- db: Session = Depends(get_db),
- current_admin = Depends(get_current_admin)
- ):
- """启用/禁用软件"""
- software = db.query(Software).filter(Software.id == software_id).first()
- if not software:
- return {"code": 404, "msg": "软件不存在"}
-
- software.is_active = is_active
- software.updated_at = datetime.now()
- db.commit()
-
- return {"code": 200, "msg": "状态更新成功"}
- @router.delete("/{software_id}")
- async def delete_software(
- software_id: int,
- db: Session = Depends(get_db),
- current_admin = Depends(get_current_admin)
- ):
- """删除软件"""
- software = db.query(Software).filter(Software.id == software_id).first()
- if not software:
- return {"code": 404, "msg": "软件不存在"}
-
- db.delete(software)
- db.commit()
-
- return {"code": 200, "msg": "删除成功"}
|