__init__.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from fastapi import APIRouter, Request, Depends, Query, HTTPException, status
  4. from database import get_db
  5. from sqlalchemy.orm import Session
  6. from sqlalchemy import not_
  7. from fastapi.responses import JSONResponse
  8. from models import *
  9. from utils import *
  10. from utils.ry_system_util import *
  11. from common.security import valid_access_token
  12. from common.enc import mpfun, sys_post_data, sys_user_post_data
  13. from common.auth_user import *
  14. from common.db import db_czrz
  15. import traceback
  16. router = APIRouter()
  17. @router.post('')
  18. async def postcreate(
  19. db: Session = Depends(get_db),
  20. user_id: int = Depends(valid_access_token),
  21. body = Depends(remove_xss_json)
  22. ):
  23. try:
  24. deptId = body['deptId']
  25. postCategory = body['postCategory']
  26. postCode = body['postCode']
  27. postName = body['postName']
  28. postSort = body['postSort']
  29. status = body['status']
  30. remark = body['remark']
  31. new_post = SysPost(
  32. dept_id=deptId,
  33. post_category=postCategory,
  34. post_code=postCode,
  35. post_name=postName,
  36. post_sort=postSort,
  37. status=status,
  38. remark=remark,
  39. create_by=user_id,
  40. create_dept=0,
  41. sign=''
  42. )
  43. db.add(new_post)
  44. db.commit()
  45. db.refresh(new_post)
  46. sys_post_data.sign_table()
  47. db.commit()
  48. return {"code": 200, "msg": "创建成功", "data": None}
  49. except Exception as e:
  50. traceback.print_exc()
  51. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
  52. @router.get('/list')
  53. async def get_list(
  54. request: Request,
  55. belongDeptId: str = Query(None, max_length=100),
  56. postCode: str = Query(None, max_length=100),
  57. postName:str = Query(None, max_length=100),
  58. postCategory:str = Query(None, max_length=100),
  59. deptId:str = Query(None, max_length=100),
  60. status: str = Query(None, max_length=100),
  61. db: Session = Depends(get_db),
  62. body = Depends(remove_xss_json),
  63. user_id = Depends(valid_access_token)
  64. ):
  65. query = db.query(SysPost)
  66. if belongDeptId:
  67. query = query.filter(SysPost.dept_id == int(belongDeptId))
  68. if deptId:
  69. query = query.filter(SysPost.dept_id == int(deptId))
  70. if postCode:
  71. query = query.filter(SysPost.post_code.like(f'%{postCode}%'))
  72. if postName:
  73. query = query.filter(SysPost.post_name.like(f'%{postName}%'))
  74. if postCategory:
  75. query = query.filter(SysPost.post_category.like(f'%{postCategory}%'))
  76. if status:
  77. query = query.filter(SysPost.status.like(f'%{status}%'))
  78. def get_det_name(dept_id):
  79. dept_info = dept_id_get_dept_info(db, dept_id)
  80. return dept_info.dept_name if dept_info is not None else ''
  81. post_list = query.all()
  82. # 将模型实例转换为字典
  83. dept_list_dict = [{
  84. "postId": post.post_id,
  85. "postName": post.post_name,
  86. "postCode": post.post_code,
  87. "postCategory": post.post_category,
  88. "postSort": post.post_sort,
  89. "deptId": post.dept_id,
  90. "deptName": get_det_name(post.dept_id),
  91. "createDept": post.create_dept,
  92. "createBy": post.create_by,
  93. "status": post.status,
  94. "remark": post.remark,
  95. "createTime": post.create_time.strftime('%Y-%m-%d %H:%M:%S') if post.create_time else '',
  96. } for post in post_list]
  97. return {
  98. "code": 200,
  99. "rows": dept_list_dict,
  100. "msg": "操作成功"
  101. }
  102. @router.get('/optionselect')
  103. async def get_optionselect(
  104. deptId: int = Query(0),
  105. db: Session = Depends(get_db),
  106. ):
  107. try:
  108. # 构建查询
  109. query = db.query(SysPost)
  110. query = query.filter(SysPost.status == 0)
  111. # 计算总记录数
  112. posts = query.all()
  113. # 转换为字典
  114. posts = dept_id_get_dept_post(db, deptId)
  115. # 构建返回结果
  116. result = {
  117. "data": posts,
  118. "code": 200,
  119. "msg": "查询成功"
  120. }
  121. return result
  122. except Exception as e:
  123. # 处理异常
  124. raise HTTPException(status_code=500, detail=str(e))
  125. @router.put('')
  126. async def postupdate(
  127. db: Session = Depends(get_db),
  128. user_id: int = Depends(valid_access_token),
  129. body = Depends(remove_xss_json)
  130. ):
  131. try:
  132. postId = body['postId']
  133. query = db.query(SysPost)
  134. query = query.filter(SysPost.post_id == postId)
  135. post = query.first()
  136. if not post :
  137. return JSONResponse(status_code=410, content={
  138. 'code': 410,
  139. 'msg': f'岗位{postId}不存在'
  140. })
  141. post.dept_id = body['deptId']
  142. post.post_category = body['postCategory']
  143. post.post_code = body['postCode']
  144. post.post_name = body['postName']
  145. post.post_sort = body['postSort']
  146. post.status = body['status']
  147. post.remark = body['remark']
  148. post.sign = ''
  149. db.commit()
  150. sys_post_data.sign_table()
  151. return {"code": 200, "msg": "更新成功", "data": None}
  152. except Exception as e:
  153. traceback.print_exc()
  154. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
  155. @router.get('/{postId}')
  156. async def postId( postId:int,
  157. db: Session = Depends(get_db),
  158. user_id: int = Depends(valid_access_token)):
  159. try:
  160. # 构建查询
  161. query = db.query(SysPost)
  162. # 应用查询条件
  163. query = query.filter(SysPost.post_id==postId)
  164. posts = query.all()
  165. # 将查询结果转换为列表形式的字典
  166. post_list = post_list_to_dict(posts, db)
  167. # 返回结果
  168. return {
  169. "code": 200,
  170. "msg": "成功",
  171. "data": post_list[0]
  172. }
  173. except Exception as e:
  174. traceback.print_exc()
  175. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
  176. @router.delete('/{post_id}')
  177. async def delete(
  178. post_id: int,
  179. db: Session = Depends(get_db),
  180. body = Depends(remove_xss_json),
  181. user_id = Depends(valid_access_token)
  182. ):
  183. try:
  184. query = db.query(SysPost)
  185. query = query.filter(SysPost.post_id == post_id)
  186. post_to_delete = query.first()
  187. if not post_to_delete:
  188. detail = "岗位不存在"
  189. raise HTTPException(status_code=404, detail="岗位不存在")
  190. db.delete(post_to_delete)
  191. db.commit()
  192. return {
  193. "code": 200,
  194. "msg": "岗位删除成功"
  195. }
  196. except Exception as e:
  197. db.rollback()
  198. if str(e)=='':
  199. e = detail
  200. raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e))