template.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from fastapi import APIRouter, Request, Depends, Query, HTTPException, status,WebSocket,WebSocketDisconnect
  4. from common.security import valid_access_token,valid_websocket_token
  5. from fastapi.responses import JSONResponse
  6. from sqlalchemy.orm import Session
  7. from sqlalchemy.sql import func
  8. from common.auth_user import *
  9. from sqlalchemy import text
  10. from pydantic import BaseModel
  11. from common.BigDataCenterAPI import *
  12. from database import get_db
  13. from typing import List
  14. from models import *
  15. from utils import *
  16. from utils.spatial import *
  17. from utils.ry_system_util import *
  18. from common.websocketManager import *
  19. import json
  20. import traceback
  21. router = APIRouter()
  22. def template_id_get_template_info(db,id):
  23. query = db.query(TpPatternTemplate)
  24. query = query.filter_by(template_id = id,del_flag = '0')
  25. return query.first()
  26. @router.post("/create")
  27. async def create_template(
  28. user_id=Depends(valid_access_token),
  29. body = Depends(remove_xss_json),
  30. db: Session = Depends(get_db)
  31. ):
  32. try:
  33. new_template = TpPatternTemplate(
  34. template_id=new_guid(),
  35. name=body['name'],
  36. value=body['value'],
  37. order_num=body['order_num'],
  38. visible=body['visible'],
  39. create_by = user_id
  40. )
  41. db.add(new_template)
  42. db.commit()
  43. return {"code": 200, "msg": "创建成功", "data": None}
  44. except Exception as e:
  45. traceback.print_exc()
  46. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
  47. @router.put("/update/{id}")
  48. async def update_pattern(
  49. id :str ,
  50. user_id=Depends(valid_access_token),
  51. body=Depends(remove_xss_json),
  52. db: Session = Depends(get_db)
  53. ):
  54. try:
  55. update_template = template_id_get_template_info(db,id)
  56. if not update_template:
  57. return JSONResponse(status_code=404,content={"code":404,"msg":"template not found"})
  58. update_template.name = body['name']
  59. update_template.value = body['value']
  60. update_template.order_num=body['order_num']
  61. update_template.visible=body['visible']
  62. update_template.update_by = user_id
  63. db.commit()
  64. return {"code": 200, "msg": "更新成功"}
  65. except Exception as e:
  66. traceback.print_exc()
  67. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
  68. @router.get("/info/{id}")
  69. async def get_pattern_info(
  70. id: str,
  71. db: Session = Depends(get_db)
  72. ):
  73. try:
  74. info = template_id_get_template_info(db,id)
  75. if not info:
  76. return JSONResponse(status_code=404,content={"code":404,"msg":"template not found"})
  77. data = {"template_id": info.template_id,
  78. "name": info.name,
  79. "value": info.value,
  80. "order_num": info.order_num,
  81. "visible": info.visible,
  82. "create_time": info.create_time}
  83. return {"code": 200, "msg": "获取成功", "data": data}
  84. except Exception as e:
  85. traceback.print_exc()
  86. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
  87. @router.get("/list")
  88. async def get_pattern_list(
  89. name: str = Query(None, description='名称'),
  90. page: int = Query(1, gt=0, description='页码'),
  91. pageSize: int = Query(None, gt=0, description='每页条目数量'),
  92. db: Session = Depends(get_db)
  93. ):
  94. try:
  95. query = db.query(TpPatternTemplate)
  96. query = query.filter_by(del_flag='0')
  97. if name:
  98. query = query.filter(TpPatternTemplate.name.like(f'%{name}%'))
  99. total_items = query.count()
  100. # 排序
  101. if pageSize is None:
  102. pageSize=total_items
  103. query = query.order_by(TpPatternTemplate.order_num.asc())
  104. # 执行分页查询
  105. lists = query.offset((page - 1) * pageSize).limit(pageSize).all()
  106. data = [{"template_id": info.template_id,
  107. "name": info.name,
  108. "value": info.value,
  109. "order_num": info.order_num,
  110. "visible": info.visible,
  111. "create_time": info.create_time} for info in lists]
  112. return {"code": 200, "msg": "查询成功", "data": data,
  113. "total": total_items,
  114. "page": page,
  115. "pageSize": pageSize,
  116. "totalPages": (total_items + pageSize - 1) // pageSize
  117. }
  118. except Exception as e:
  119. traceback.print_exc()
  120. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
  121. @router.put('/changeVisible')
  122. async def change_classification_visible(
  123. db: Session = Depends(get_db),
  124. body=Depends(remove_xss_json),
  125. user_id=Depends(valid_access_token)
  126. ):
  127. try:
  128. template_id = body['template_id']
  129. visible = body['visible']
  130. info = template_id_get_template_info(db, template_id)
  131. if not info:
  132. return JSONResponse(status_code=404, content={"code": 404, "msg": "classification not found"})
  133. info.visible= visible
  134. info.update_by=user_id
  135. db.commit()
  136. return {
  137. "code": 200,
  138. "msg": "操作成功"
  139. }
  140. except Exception as e:
  141. # 处理异常
  142. traceback.print_exc()
  143. raise HTTPException(status_code=500, detail=str(e))
  144. @router.delete("/delete/{id}")
  145. async def delete_pattern(
  146. id: str,
  147. db: Session = Depends(get_db)
  148. ):
  149. try:
  150. # 检查图案是否存在
  151. info = template_id_get_template_info(db, id)
  152. if not info:
  153. return JSONResponse(status_code=404, content={"code": 404, "msg": "template not found"})
  154. info.del_flag='2'
  155. db.commit()
  156. return {"code": 200, "msg": "删除成功"}
  157. except Exception as e:
  158. traceback.print_exc()
  159. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")