back.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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_status = 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. return {
  92. "code": 200,
  93. "msg": "信息创建成功",
  94. "data": new_publish_id
  95. }
  96. except Exception as e:
  97. traceback.print_exc()
  98. # 处理异常
  99. raise HTTPException(status_code=500, detail=str(e))
  100. @router.get('/list')
  101. async def get_publish_list(
  102. publish_group: str = Query('', description='发布单位'),
  103. publish_status: str = Query('', description='发布状态的字典键值'),
  104. examine_status: str = Query('', description='审批状态的字典键值'),
  105. sort_by: str = Query('', description='排序字段'),
  106. sort_order: str = Query("asc", description='排序方式'),
  107. page: int = Query(1, gt=0, description='页码'),
  108. page_size: int = Query(10, gt=0, description='pageSize'),
  109. db: Session = Depends(get_db)
  110. ):
  111. try:
  112. # 应用查询条件
  113. where = and_(InfoPublishBase.del_flag == '0')
  114. if publish_status != '':
  115. where = and_(where, InfoPublishBase.publish_status == publish_status)
  116. if examine_status != '':
  117. where = and_(where, InfoPublishBase.examine_status == examine_status)
  118. if publish_group != '':
  119. where = and_(where, InfoPublishBase.publish_group.like('%{}%'.format(publish_group)))
  120. print(where)
  121. # 计算总条目数
  122. q = db.query(func.count(InfoPublishBase.id))
  123. q = q.filter(where)
  124. total = q.scalar()
  125. # 执行分页查询
  126. q = db.query(InfoPublishBase)
  127. q = q.filter(where)
  128. rows = q.order_by(InfoPublishBase.id.desc()).offset((page - 1) * page_size).limit(page_size).all()
  129. data = []
  130. for row in rows:
  131. # 发布申请人
  132. recorded_by = row.recorded_by
  133. user_row = db.query(SysUser).filter(SysUser.user_id == recorded_by).first()
  134. nick_name = ""
  135. dept_name = ""
  136. if user_row is not None:
  137. nick_name = user_row.nick_name
  138. dept_id = user_row.dept_id
  139. dept_row = db.query(SysDept).filter(SysDept.dept_id == dept_id).first()
  140. if dept_row is not None:
  141. dept_name = dept_row.dept_name
  142. # 待处理人
  143. examine_user = "无"
  144. examine_by = row.examine_by
  145. user_row = db.query(SysUser).filter(SysUser.user_id == examine_by).first()
  146. if user_row is not None:
  147. examine_user = user_row.nick_name
  148. data.append({
  149. "id": row.id,
  150. "title": row.title,
  151. "publish_group": row.publish_group,
  152. "content": row.content,
  153. "publish_time": get_datetime_str(row.publish_time),
  154. "publish_channel": row.publish_channel,
  155. "nick_name": nick_name,
  156. "dept_name": dept_name,
  157. "examine_user": examine_user,
  158. "publish_status": db_dict.get_dict_label(db, "mm_publish_status", row.publish_status),
  159. "examine_status": db_dict.get_dict_label(db, "mm_examine_status", row.examine_status),
  160. "user_count": row.user_count,
  161. "user_ok_count": row.user_ok_count,
  162. "user_err_count": row.user_err_count,
  163. "user_sending_count": row.user_sending_count
  164. })
  165. # 返回结果
  166. return {
  167. "code": 200,
  168. "msg": "查询成功",
  169. "data": data,
  170. "total": total
  171. }
  172. except Exception as e:
  173. # 处理异常
  174. traceback.print_exc()
  175. raise HTTPException(status_code=500, detail=str(e))
  176. @router.get('/edit')
  177. async def get_edit_info(
  178. request: Request,
  179. info_id: str = Query(None, description='信息ID'),
  180. db: Session = Depends(get_db)):
  181. row = db.query(InfoPublishBase).filter(InfoPublishBase.id == info_id).first()
  182. data = get_model_dict(row)
  183. data['add_time'] = get_datetime_str(data['add_time'])
  184. data['publish_time'] = get_datetime_str(data['publish_time'])
  185. data["examines"] = []
  186. rows = db.query(InfoPublishBase).filter(InfoPublishExamine.publish_id == info_id).filter(InfoPublishExamine.del_flag == '0').all()
  187. for row in rows:
  188. data["examines"].append({
  189. "examine_type": row.examine_type,
  190. "examine_sub_type": row.examine_sub_type,
  191. "content": row.content,
  192. "examine_time": get_datetime_str(row.examine_time),
  193. "user_id": row.user_id,
  194. "user_name": row.user_name,
  195. "nick_name": row.nick_name
  196. })
  197. return {
  198. "code": 200,
  199. "msg": "查询成功",
  200. "data": data
  201. }
  202. @router.post('/edit')
  203. async def post_edit_info(
  204. request: Request,
  205. body = Depends(remove_xss_json),
  206. db: Session = Depends(get_db),
  207. user_id = Depends(valid_access_token)):
  208. info_id = body['info_id']
  209. del body['info_id']
  210. body['recorded_by'] = user_id
  211. db.query(InfoPublishBase).filter(InfoPublishBase.id == info_id).update(body)
  212. db.commit()
  213. return {
  214. "code": 200,
  215. "msg": "保存信息成功"
  216. }
  217. @router.post('/examine')
  218. async def post_examine_info(
  219. request: Request,
  220. body = Depends(remove_xss_json),
  221. db: Session = Depends(get_db),
  222. user_id = Depends(valid_access_token)):
  223. user_row = db.query(SysUser).filter(SysUser.user_id == user_id).first()
  224. new_examine = InfoPublishExamine(
  225. pubish_id = body['info_id'],
  226. examine_type = body['examine_type'],
  227. examine_sub_type = body['examine_sub_type'],
  228. content = body['content'],
  229. examine_time = datetime.now(),
  230. user_id = user_id,
  231. user_name = user_row.user_name,
  232. nick_name = user_row.nick_name
  233. )
  234. db.add(new_examine)
  235. db.commit()
  236. return {
  237. "code": 200,
  238. "msg": "保存审批记录成功"
  239. }
  240. @router.get("send_list")
  241. async def get_send_list(
  242. info_id: str = Query('', description='信息ID'),
  243. keywords: str = Query('', description='关键字'),
  244. sort_by: str = Query('', description='排序字段'),
  245. sort_order: str = Query("asc", description='排序方式'),
  246. page: int = Query(1, gt=0, description='页码'),
  247. page_size: int = Query(10, gt=0, description='pageSize'),
  248. db: Session = Depends(get_db)
  249. ):
  250. try:
  251. # 应用查询条件
  252. where = ()
  253. where = and_(where, InfoPublishResponses.publish_id == info_id)
  254. # 计算总条目数
  255. q = db.query(func.count(InfoPublishResponses.id))
  256. q = q.filter(where)
  257. total = q.scalar()
  258. # 执行分页查询
  259. q = db.query(InfoPublishResponses)
  260. q = q.filter(where)
  261. rows = q.order_by(InfoPublishResponses.id.desc()).offset((page - 1) * page_size).limit(page_size).all()
  262. data = []
  263. for row in rows:
  264. data.append({
  265. "user_id": row.user_id,
  266. "user_name": row.user_name,
  267. "nick_name": row.nick_name,
  268. "dept_name": row.dept_name,
  269. "sent_status": row.sent_status,
  270. "sent_time": get_datetime_str(row.sent_time),
  271. "response_type": row.response_type
  272. })
  273. # 返回结果
  274. return {
  275. "code": 200,
  276. "msg": "查询成功",
  277. "data": data,
  278. "total": total
  279. }
  280. except Exception as e:
  281. # 处理异常
  282. traceback.print_exc()
  283. raise HTTPException(status_code=500, detail=str(e))