back.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from fastapi import APIRouter, Request, Depends, HTTPException, Query
  4. from sqlalchemy.exc import IntegrityError
  5. from fastapi.responses import HTMLResponse, FileResponse
  6. from fastapi.responses import JSONResponse
  7. from database import get_db
  8. from sqlalchemy import text, exists, and_, or_, not_
  9. from sqlalchemy.orm import Session
  10. from models import *
  11. import json
  12. import random
  13. from sqlalchemy import create_engine, select
  14. from typing import Optional
  15. from utils.StripTagsHTMLParser import *
  16. from common.db import db_event_management, db_user, db_area, db_emergency_plan
  17. from common.security import valid_access_token
  18. import traceback
  19. from utils import *
  20. from datetime import datetime, timedelta
  21. from common import YzyApi
  22. from common.db import db_dict
  23. from urllib.parse import quote
  24. import base64
  25. from config import settings
  26. router = APIRouter()
  27. @router.post('/create')
  28. async def create_emergency_plan(
  29. db: Session = Depends(get_db),
  30. body = Depends(remove_xss_json),
  31. user_id = Depends(valid_access_token)
  32. ):
  33. try:
  34. dept_id = 0
  35. dept_name = ''
  36. user_row = db.query(SysUser).filter(SysUser.user_id == user_id).first()
  37. dept_id = user_row.dept_id
  38. dept_row = db.query(SysDept).filter(SysDept.dept_id == dept_id).first()
  39. dept_name = dept_row.dept_name
  40. new_publish = InfoPublishBase(
  41. title = body['title'],
  42. publish_group = body['publish_group'],
  43. template_id = body['template_id'],
  44. content = body['content'],
  45. recorded_by = user_id,
  46. del_flag = '0',
  47. dept_id = dept_id,
  48. dept_name = dept_name,
  49. add_time = datetime.now(),
  50. response_type = body['response_type'],
  51. publish_time = body['publish_time'],
  52. examine_by = body['examine_by'],
  53. publish_status = 0,
  54. examine_status = 0,
  55. publish_channel = body['publish_channel'],
  56. user_count = body['user_count'],
  57. user_ok_count = 0,
  58. user_err_count = 0,
  59. user_sending_count = 0
  60. )
  61. db.add(new_publish)
  62. db.commit()
  63. db.refresh(new_publish)
  64. new_publish_id = new_publish.id
  65. # 发送人员
  66. for u in body['users']:
  67. user_id = u['user_id']
  68. user_row = db.query(SysUser).filter(SysUser.user_id == user_id).first()
  69. dept_id = user_row.dept_id
  70. dept_row = db.query(SysDept).filter(SysDept.dept_id == dept_id).first()
  71. dept_name = dept_row.dept_name
  72. new_resp = InfoPublishResponses(
  73. publish_id = new_publish_id,
  74. user_id = user_id,
  75. user_name = user_row.user_name,
  76. nick_name = user_row.nick_name,
  77. dept_name = dept_row.dept_name,
  78. sent_time = 0,
  79. response_type = body['response_type']
  80. )
  81. db.add(new_resp)
  82. db.commit()
  83. # 附件
  84. if 'attachs' in body:
  85. for n in body['attachs']:
  86. pass
  87. # 审批附件
  88. if 'examine_attachs' in body:
  89. for n in body['examine_attachs']:
  90. pass
  91. except Exception as e:
  92. traceback.print_exc()
  93. # 处理异常
  94. raise HTTPException(status_code=500, detail=str(e))
  95. @router.get('/list')
  96. async def get_publish_list(
  97. publish_group: str = Query('', description='发布单位'),
  98. publish_status: str = Query('', description='发布状态的字典键值'),
  99. examine_status: str = Query('', description='审批状态的字典键值'),
  100. sort_by: str = Query('', description='排序字段'),
  101. sort_order: str = Query("asc", description='排序方式'),
  102. page: int = Query(1, gt=0, description='页码'),
  103. page_size: int = Query(10, gt=0, description='pageSize'),
  104. db: Session = Depends(get_db)
  105. ):
  106. try:
  107. # 应用查询条件
  108. where = and_(InfoPublishBase.del_flag == '0')
  109. if publish_status != '':
  110. where = and_(where, InfoPublishBase.publish_status == publish_status)
  111. if examine_status != '':
  112. where = and_(where, InfoPublishBase.examine_status == examine_status)
  113. if publish_group != '':
  114. where = and_(where, InfoPublishBase.publish_group.like('%{}%'.format(publish_group)))
  115. print(where)
  116. # 计算总条目数
  117. q = db.query(func.count(InfoPublishBase.id))
  118. q = q.filter(where)
  119. total = q.scalar()
  120. # 执行分页查询
  121. q = db.query(InfoPublishBase)
  122. q = q.filter(where)
  123. rows = q.order_by(InfoPublishBase.id.desc()).offset((page - 1) * page_size).limit(page_size).all()
  124. data = []
  125. for row in rows:
  126. # 发布申请人
  127. recorded_by = row.recorded_by
  128. user_row = db.query(SysUser).filter(SysUser.user_id == recorded_by).first()
  129. nick_name = ""
  130. dept_name = ""
  131. if user_row is not None:
  132. nick_name = user_row.nick_name
  133. dept_id = user_row.dept_id
  134. dept_row = db.query(SysDept).filter(SysDept.dept_id == dept_id).first()
  135. if dept_row is not None:
  136. dept_name = dept_row.dept_name
  137. # 待处理人
  138. examine_user = "无"
  139. examine_by = row.examine_by
  140. user_row = db.query(SysUser).filter(SysUser.user_id == examine_by).first()
  141. if user_row is not None:
  142. examine_user = user_row.nick_name
  143. data.append({
  144. "id": row.id,
  145. "title": row.title,
  146. "publish_group": row.publish_group,
  147. "content": row.content,
  148. "publish_time": get_datetime_str(row.publish_time),
  149. "publish_channel": row.publish_channel,
  150. "nick_name": nick_name,
  151. "dept_name": dept_name,
  152. "examine_user": examine_user,
  153. "publish_status": db_dict.get_dict_label(db, "mm_publish_status", row.publish_status),
  154. "examine_status": db_dict.get_dict_label(db, "mm_examine_status", row.examine_status),
  155. "user_count": row.user_count,
  156. "user_ok_count": row.user_ok_count,
  157. "user_err_count": row.user_err_count,
  158. "user_sending_count": row.user_sending_count
  159. })
  160. # 返回结果
  161. return {
  162. "code": 200,
  163. "msg": "查询成功",
  164. "data": data,
  165. "total": total
  166. }
  167. except Exception as e:
  168. # 处理异常
  169. traceback.print_exc()
  170. raise HTTPException(status_code=500, detail=str(e))