""" 软件管理接口 """ from fastapi import APIRouter, Depends, Query, Request 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 from app.utils.timezone import beijing_now 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).filter(Software.is_deleted == 0).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, request: Request, 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=beijing_now(), updated_at=beijing_now() ) db.add(software) # 刷新以获取ID db.flush() # 创建历史记录 from app.utils.history import create_software_history, create_operation_log create_software_history(db, software, "INSERT") # 记录操作日志 client_ip = request.client.host if request.client else None create_operation_log( db=db, admin_id=current_admin.id, module="软件管理", action="创建软件", description=f"创建软件:{req.name}({req.code})", new_data={ "code": req.code, "name": req.name, "description": req.description, "daily_price": req.daily_price }, ip_address=client_ip ) # 一次性提交所有更改 db.commit() return {"code": 200, "msg": "创建成功", "data": {"id": software.id}} @router.put("/{software_id}") async def update_software( software_id: int, req: SoftwareUpdate, request: Request, 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": "软件不存在"} # 记录更新前的状态 old_data = { "name": software.name, "description": software.description, "daily_price": float(software.daily_price) } software.name = req.name software.description = req.description software.daily_price = req.daily_price software.updated_at = beijing_now() # 创建历史记录 from app.utils.history import create_software_history, create_operation_log create_software_history(db, software, "UPDATE") # 记录操作日志 client_ip = request.client.host if request.client else None create_operation_log( db=db, admin_id=current_admin.id, module="软件管理", action="更新软件", description=f"更新软件信息:{software.name}({software.code})", old_data=old_data, new_data={ "name": req.name, "description": req.description, "daily_price": req.daily_price }, ip_address=client_ip ) # 一次性提交所有更改 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-禁用"), request: Request = None, 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": "软件不存在"} old_active = software.is_active software.is_active = is_active software.updated_at = beijing_now() # 创建历史记录 from app.utils.history import create_software_history, create_operation_log create_software_history(db, software, "UPDATE") # 记录操作日志 client_ip = request.client.host if request and request.client else None status_text = "启用" if is_active == 1 else "禁用" create_operation_log( db=db, admin_id=current_admin.id, module="软件管理", action="更新软件状态", description=f"{status_text}软件:{software.name}({software.code})", old_data={"is_active": old_active}, new_data={"is_active": is_active}, ip_address=client_ip ) # 一次性提交所有更改 db.commit() return {"code": 200, "msg": "状态更新成功"} @router.delete("/{software_id}") async def delete_software( software_id: int, request: Request, db: Session = Depends(get_db), current_admin = Depends(get_current_admin) ): """删除软件(软删除)""" software = db.query(Software).filter(Software.id == software_id, Software.is_deleted == 0).first() if not software: return {"code": 404, "msg": "软件不存在"} software.is_deleted = 1 software.updated_at = beijing_now() # 创建历史记录 from app.utils.history import create_software_history, create_operation_log create_software_history(db, software, "DELETE") # 记录操作日志 client_ip = request.client.host if request.client else None create_operation_log( db=db, admin_id=current_admin.id, module="软件管理", action="删除软件", description=f"删除软件:{software.name}({software.code})", old_data={ "code": software.code, "name": software.name, "description": software.description, "daily_price": float(software.daily_price), "is_active": software.is_active }, ip_address=client_ip ) # 一次性提交所有更改 db.commit() return {"code": 200, "msg": "删除成功"}