back.py 11 KB

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