software.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. """
  2. 软件管理接口
  3. """
  4. from fastapi import APIRouter, Depends, Query, Request
  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. from app.utils.timezone import beijing_now
  12. router = APIRouter()
  13. class SoftwareCreate(BaseModel):
  14. """创建软件请求"""
  15. code: str
  16. name: str
  17. description: str | None = None
  18. daily_price: float
  19. class SoftwareUpdate(BaseModel):
  20. """更新软件请求"""
  21. name: str
  22. description: str | None = None
  23. daily_price: float
  24. @router.get("")
  25. async def get_software_list(
  26. db: Session = Depends(get_db),
  27. current_admin = Depends(get_current_admin)
  28. ):
  29. """获取软件列表"""
  30. software_list = db.query(Software).filter(Software.is_deleted == 0).all()
  31. return {
  32. "code": 200,
  33. "msg": "成功",
  34. "data": [
  35. {
  36. "id": s.id,
  37. "code": s.code,
  38. "name": s.name,
  39. "description": s.description,
  40. "daily_price": float(s.daily_price),
  41. "is_active": bool(s.is_active),
  42. "created_at": s.created_at.isoformat() if s.created_at else None,
  43. "updated_at": s.updated_at.isoformat() if s.updated_at else None
  44. }
  45. for s in software_list
  46. ]
  47. }
  48. @router.post("")
  49. async def create_software(
  50. req: SoftwareCreate,
  51. request: Request,
  52. db: Session = Depends(get_db),
  53. current_admin = Depends(get_current_admin)
  54. ):
  55. """创建软件"""
  56. # 检查是否已存在
  57. existing = db.query(Software).filter(Software.code == req.code).first()
  58. if existing:
  59. return {"code": 400, "msg": "软件特征码已存在"}
  60. software = Software(
  61. code=req.code,
  62. name=req.name,
  63. description=req.description,
  64. daily_price=req.daily_price,
  65. is_active=1,
  66. created_at=beijing_now(),
  67. updated_at=beijing_now()
  68. )
  69. db.add(software)
  70. # 刷新以获取ID
  71. db.flush()
  72. # 创建历史记录
  73. from app.utils.history import create_software_history, create_operation_log
  74. create_software_history(db, software, "INSERT")
  75. # 记录操作日志
  76. client_ip = request.client.host if request.client else None
  77. create_operation_log(
  78. db=db,
  79. admin_id=current_admin.id,
  80. module="软件管理",
  81. action="创建软件",
  82. description=f"创建软件:{req.name}({req.code})",
  83. new_data={
  84. "code": req.code,
  85. "name": req.name,
  86. "description": req.description,
  87. "daily_price": req.daily_price
  88. },
  89. ip_address=client_ip
  90. )
  91. # 一次性提交所有更改
  92. db.commit()
  93. return {"code": 200, "msg": "创建成功", "data": {"id": software.id}}
  94. @router.put("/{software_id}")
  95. async def update_software(
  96. software_id: int,
  97. req: SoftwareUpdate,
  98. request: Request,
  99. db: Session = Depends(get_db),
  100. current_admin = Depends(get_current_admin)
  101. ):
  102. """更新软件"""
  103. software = db.query(Software).filter(Software.id == software_id).first()
  104. if not software:
  105. return {"code": 404, "msg": "软件不存在"}
  106. # 记录更新前的状态
  107. old_data = {
  108. "name": software.name,
  109. "description": software.description,
  110. "daily_price": float(software.daily_price)
  111. }
  112. software.name = req.name
  113. software.description = req.description
  114. software.daily_price = req.daily_price
  115. software.updated_at = beijing_now()
  116. # 创建历史记录
  117. from app.utils.history import create_software_history, create_operation_log
  118. create_software_history(db, software, "UPDATE")
  119. # 记录操作日志
  120. client_ip = request.client.host if request.client else None
  121. create_operation_log(
  122. db=db,
  123. admin_id=current_admin.id,
  124. module="软件管理",
  125. action="更新软件",
  126. description=f"更新软件信息:{software.name}({software.code})",
  127. old_data=old_data,
  128. new_data={
  129. "name": req.name,
  130. "description": req.description,
  131. "daily_price": req.daily_price
  132. },
  133. ip_address=client_ip
  134. )
  135. # 一次性提交所有更改
  136. db.commit()
  137. return {"code": 200, "msg": "更新成功"}
  138. @router.put("/{software_id}/status")
  139. async def update_software_status(
  140. software_id: int,
  141. is_active: int = Query(..., description="状态:1-启用,0-禁用"),
  142. request: Request = None,
  143. db: Session = Depends(get_db),
  144. current_admin = Depends(get_current_admin)
  145. ):
  146. """启用/禁用软件"""
  147. software = db.query(Software).filter(Software.id == software_id).first()
  148. if not software:
  149. return {"code": 404, "msg": "软件不存在"}
  150. old_active = software.is_active
  151. software.is_active = is_active
  152. software.updated_at = beijing_now()
  153. # 创建历史记录
  154. from app.utils.history import create_software_history, create_operation_log
  155. create_software_history(db, software, "UPDATE")
  156. # 记录操作日志
  157. client_ip = request.client.host if request and request.client else None
  158. status_text = "启用" if is_active == 1 else "禁用"
  159. create_operation_log(
  160. db=db,
  161. admin_id=current_admin.id,
  162. module="软件管理",
  163. action="更新软件状态",
  164. description=f"{status_text}软件:{software.name}({software.code})",
  165. old_data={"is_active": old_active},
  166. new_data={"is_active": is_active},
  167. ip_address=client_ip
  168. )
  169. # 一次性提交所有更改
  170. db.commit()
  171. return {"code": 200, "msg": "状态更新成功"}
  172. @router.delete("/{software_id}")
  173. async def delete_software(
  174. software_id: int,
  175. request: Request,
  176. db: Session = Depends(get_db),
  177. current_admin = Depends(get_current_admin)
  178. ):
  179. """删除软件(软删除)"""
  180. software = db.query(Software).filter(Software.id == software_id, Software.is_deleted == 0).first()
  181. if not software:
  182. return {"code": 404, "msg": "软件不存在"}
  183. software.is_deleted = 1
  184. software.updated_at = beijing_now()
  185. # 创建历史记录
  186. from app.utils.history import create_software_history, create_operation_log
  187. create_software_history(db, software, "DELETE")
  188. # 记录操作日志
  189. client_ip = request.client.host if request.client else None
  190. create_operation_log(
  191. db=db,
  192. admin_id=current_admin.id,
  193. module="软件管理",
  194. action="删除软件",
  195. description=f"删除软件:{software.name}({software.code})",
  196. old_data={
  197. "code": software.code,
  198. "name": software.name,
  199. "description": software.description,
  200. "daily_price": float(software.daily_price),
  201. "is_active": software.is_active
  202. },
  203. ip_address=client_ip
  204. )
  205. # 一次性提交所有更改
  206. db.commit()
  207. return {"code": 200, "msg": "删除成功"}