123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- from fastapi import APIRouter, Request, Depends, HTTPException, Query
- from sqlalchemy.exc import IntegrityError
- from fastapi.responses import HTMLResponse, FileResponse
- from fastapi.responses import JSONResponse
- from database import get_db
- from sqlalchemy import text, exists, and_, or_, not_
- from sqlalchemy.orm import Session
- from models import *
- import json
- import os
- from sqlalchemy import create_engine, select
- from typing import Optional
- from utils.StripTagsHTMLParser import *
- from common.db import db_event_management, db_user, db_area, db_emergency_plan
- from common.security import valid_access_token
- import traceback
- from utils import *
- from datetime import datetime, timedelta
- from common import YzyApi
- from common.db import db_dict
- from urllib.parse import quote
- import base64
- from config import settings
- router = APIRouter()
- EXAMINE_TYPE_DICT = {
- 0: "草稿",
- 10: "提交",
- 20: "领导审批",
- 30: "重新提交"
- }
- EXAMINE_SUB_TYPE_DICT = {
- 0: "草稿",
- 10: "提交",
- 20: "待审批",
- 21: "审批通过",
- 22: "审批不通过",
- 30: "重新提交"
- }
- # 信息发布创建
- @router.post('/create')
- async def create_emergency_plan(
- db: Session = Depends(get_db),
- body = Depends(remove_xss_json),
- user_id = Depends(valid_access_token)
- ):
- try:
- dept_id = 0
- dept_name = ''
- user_row = db.query(SysUser).filter(SysUser.user_id == user_id).first()
- user_name = user_row.user_name
- nick_name = user_row.nick_name
- dept_id = user_row.dept_id
- dept_row = db.query(SysDept).filter(SysDept.dept_id == dept_id).first()
- dept_name = dept_row.dept_name
- new_publish = InfoPublishBase(
- title = body['title'],
- publish_group = body['publish_group'],
- template_id = body['template_id'],
- content = body['content'],
- recorded_by = user_id,
- del_flag = '0',
- dept_id = dept_id,
- dept_name = dept_name,
- add_time = datetime.now(),
- response_type = body['response_type'],
- publish_time = body['publish_time'],
- examine_by = body['examine_by'],
- publish_status = 0,
- examine_status = 0,
- publish_channel = body['publish_channel'],
- user_count = body['user_count'],
- user_ok_count = 0,
- user_err_count = 0,
- user_sending_count = 0
- )
- db.add(new_publish)
- db.commit()
- db.refresh(new_publish)
- new_publish_id = new_publish.id
- # 发送人员
- for u in body['users']:
- send_user_id = u['user_id']
- send_nick_name = u['nick_name']
- send_user_name = ''
- send_dept_name = ''
- user_row = db.query(SysUser).filter(SysUser.user_id == send_user_id).first()
- if user_row is not None:
- send_user_name = user_row.user_name
- send_dept_row = db.query(SysDept).filter(SysDept.dept_id == user_row.dept_id).first()
- send_dept_name = send_dept_row.dept_name
-
- new_resp = InfoPublishResponses(
- publish_id = new_publish_id,
- user_id = send_user_id,
- user_name = send_user_name,
- nick_name = send_nick_name,
- dept_name = send_dept_name,
- sent_status = 0,
- response_type = body['response_type'],
- publish_channel = body['publish_channel']
- )
- db.add(new_resp)
- db.commit()
-
- # 附件
- if 'attachs' in body:
- infopublish_files = [
- InfoPublishFile(
- file_name=fileName["name"],
- storage_file_name=fileName["url"],
- file_path=f'/data/upload/mergefile/uploads/{fileName["url"]}',
- file_size=os.path.getsize(f'/data/upload/mergefile/uploads/{fileName["url"]}'),
- foreign_key=str(new_publish_id),
- from_scenario="infopublish_attach_file",
- update_time=datetime.now(),
- create_time=datetime.now(),
- create_by=user_id,
- create_dept=dept_id,
- del_flag='0',
- status=0,
- )
- for fileName in body['attachs']
- ]
- db.add_all(infopublish_files)
- db.commit()
-
- # 审批附件
- if 'examine_attachs' in body:
- infopublish_files = [
- InfoPublishFile(
- file_name=fileName["name"],
- storage_file_name=fileName["url"],
- file_path=f'/data/upload/mergefile/uploads/{fileName["url"]}',
- file_size=os.path.getsize(f'/data/upload/mergefile/uploads/{fileName["url"]}'),
- foreign_key=str(new_publish_id),
- from_scenario="infopublish_examine_attach_file",
- update_time=datetime.now(),
- create_time=datetime.now(),
- create_by=user_id,
- create_dept=dept_id,
- del_flag='0',
- status=0,
- )
- for fileName in body['examine_attachs']
- ]
- db.add_all(infopublish_files)
- db.commit()
- # 审批记录
- infopublish_examine = InfoPublishExamine(
- publish_id = new_publish_id,
- examine_type = 10, # 提交
- examine_sub_type = 10, # 提交
- examine_time = datetime.now(),
- content = '',
- user_id = user_id,
- user_name = user_name,
- nick_name = nick_name,
- del_flag = '0'
- )
- db.add(infopublish_examine)
- db.commit()
- # 改草稿、待审批状态
- db.query(InfoPublishBase).filter(InfoPublishBase.id == new_publish_id).update({"publish_status": 1, "examine_status": 1})
- db.commit()
- return {
- "code": 200,
- "msg": "信息创建成功",
- "data": new_publish_id
- }
- except Exception as e:
- traceback.print_exc()
- # 处理异常
- raise HTTPException(status_code=500, detail=str(e))
- # 信息发布分页查询
- @router.get('/list')
- async def get_publish_list(
- publish_group: str = Query('', description='发布单位'),
- publish_status: str = Query('', description='发布状态的字典键值'),
- examine_status: str = Query('', description='审批状态的字典键值'),
- dispose_status: str = Query('', description='处理状态的字典键值'),
- content: str = Query('', description='信息内容'),
- sort_by: str = Query('', description='排序字段'),
- sort_order: str = Query("asc", description='排序方式'),
- page: int = Query(1, gt=0, description='页码'),
- page_size: int = Query(10, gt=0, description='pageSize'),
- db: Session = Depends(get_db),
- user_id = Depends(valid_access_token)
- ):
- try:
- # 应用查询条件
- where = and_(InfoPublishBase.del_flag == '0')
- if content != '':
- where = and_(where, InfoPublishBase.content.like('%{}%'.format(content)))
- if publish_status not in ['', '0'] :
- where = and_(where, InfoPublishBase.publish_status == publish_status)
- if examine_status not in ['', '0'] :
- where = and_(where, InfoPublishBase.examine_status == examine_status)
- if publish_group != '':
- where = and_(where, InfoPublishBase.publish_group.like('%{}%'.format(publish_group)))
- if dispose_status not in ['', '0'] :
- where = and_(where, InfoPublishBase.examine_status == 1, InfoPublishBase.examine_by == user_id)
- print(where)
-
- # 计算总条目数
- q = db.query(func.count(InfoPublishBase.id))
- q = q.filter(where)
- total = q.scalar()
-
- # 执行分页查询
- q = db.query(InfoPublishBase)
- q = q.filter(where)
- rows = q.order_by(InfoPublishBase.id.desc()).offset((page - 1) * page_size).limit(page_size).all()
- data = []
- for row in rows:
-
- # 发布申请人
- recorded_by = row.recorded_by
-
- user_row = db.query(SysUser).filter(SysUser.user_id == recorded_by).first()
- nick_name = ""
- dept_name = ""
-
- if user_row is not None:
- nick_name = user_row.nick_name
- dept_id = user_row.dept_id
- dept_row = db.query(SysDept).filter(SysDept.dept_id == dept_id).first()
- if dept_row is not None:
- dept_name = dept_row.dept_name
- # 待处理人
- examine_user = "无"
- examine_by = row.examine_by
- user_row = db.query(SysUser).filter(SysUser.user_id == examine_by).first()
- if user_row is not None:
- examine_user = user_row.nick_name
- data.append({
- "id": row.id,
- "title": row.title,
- "info_type": row.info_type,
- "publish_group": row.publish_group,
- "content": row.content,
- "publish_time": get_datetime_str(row.publish_time),
- "publish_channel": row.publish_channel,
- "nick_name": nick_name,
- "dept_name": dept_name,
- "examine_user": examine_user,
- "publish_status": db_dict.get_dict_label(db, "mm_publish_status", row.publish_status),
- "examine_status": db_dict.get_dict_label(db, "mm_examine_status", row.examine_status),
- "user_count": row.user_count,
- "user_ok_count": row.user_ok_count,
- "user_err_count": row.user_err_count,
- "user_sending_count": row.user_sending_count,
- "is_my_edit": (row.examine_status == 0 or row.examine_status == 9) and row.recorded_by == user_id, # 是否我的编辑事项
- "is_my_examine": row.examine_status == 1 and int(row.examine_by) == user_id # 是否我的审批事项
- })
- # 返回结果
- return {
- "code": 200,
- "msg": "查询成功",
- "data": data,
- "total": total
- }
- except Exception as e:
- # 处理异常
- traceback.print_exc()
- raise HTTPException(status_code=500, detail=str(e))
- # 信息发布查看
- @router.get('/edit')
- async def get_edit_info(
- request: Request,
- info_id: str = Query(None, description='信息ID'),
- db: Session = Depends(get_db)):
- row = db.query(InfoPublishBase).filter(InfoPublishBase.id == info_id).first()
- data = get_model_dict(row)
- data['add_time'] = get_datetime_str(data['add_time'])
- data['publish_time'] = get_datetime_str(data['publish_time'])
- # 反馈 && 未反馈
- data['feedback_count'] = db.query(InfoPublishResponses).filter(and_(InfoPublishResponses.publish_id == info_id, InfoPublishResponses.sent_status > 0, InfoPublishResponses.response_type > 0)).count()
- data['unresponsive_count'] = db.query(InfoPublishResponses).filter(and_(InfoPublishResponses.publish_id == info_id, InfoPublishResponses.sent_status > 0, InfoPublishResponses.response_type > 0)).count()
- # 附件
- rows = db.query(InfoPublishFile).filter(and_(InfoPublishFile.from_scenario=="infopublish_attach_file", InfoPublishFile.foreign_key == info_id, InfoPublishFile.del_flag == '0')).all()
- data['attachs'] = [
- {
- "name": row.file_name,
- "url": row.storage_file_name
- }
- for row in rows
- ]
- # 审批附件
- rows = db.query(InfoPublishFile).filter(and_(InfoPublishFile.from_scenario=="infopublish_examine_attach_file", InfoPublishFile.foreign_key == info_id, InfoPublishFile.del_flag == '0')).all()
- data['examine_attachs'] = [
- {
- "name": row.file_name,
- "url": row.storage_file_name
- }
- for row in rows
- ]
- data["examines"] = []
- rows = db.query(InfoPublishExamine).filter(InfoPublishExamine.publish_id == info_id).filter(InfoPublishExamine.del_flag == '0').all()
- for row in rows:
- data["examines"].append({
- "examine_type": EXAMINE_TYPE_DICT[row.examine_type],
- "examine_sub_type": EXAMINE_SUB_TYPE_DICT[row.examine_sub_type],
- "content": row.content,
- "examine_time": get_datetime_str(row.examine_time),
- "user_id": row.user_id,
- "user_name": row.user_name,
- "nick_name": row.nick_name
- })
- return {
- "code": 200,
- "msg": "查询成功",
- "data": data
- }
- # 信息发布编辑保存
- @router.post('/edit')
- async def post_edit_info(
- request: Request,
- body = Depends(remove_xss_json),
- db: Session = Depends(get_db),
- user_id = Depends(valid_access_token)):
- try:
- id = body['id']
- remove_req_param(body, 'info_id')
- examines = body['examines']
- remove_req_param(body, 'examines')
- body['recorded_by'] = user_id
- db.query(InfoPublishBase).filter(InfoPublishBase.id == id).update(body)
- db.commit()
- return {
- "code": 200,
- "msg": "保存信息成功"
- }
- except Exception as e:
- # 处理异常
- traceback.print_exc()
- raise HTTPException(status_code=500, detail=str(e))
- # 信息发布提交审核
- @router.post('/examine')
- async def post_examine_info(
- request: Request,
- body = Depends(remove_xss_json),
- db: Session = Depends(get_db),
- user_id = Depends(valid_access_token)):
- user_row = db.query(SysUser).filter(SysUser.user_id == user_id).first()
- new_examine = InfoPublishExamine(
- pubish_id = body['info_id'],
- examine_type = body['examine_type'],
- examine_sub_type = body['examine_sub_type'],
- content = body['content'],
- examine_time = datetime.now(),
- user_id = user_id,
- user_name = user_row.user_name,
- nick_name = user_row.nick_name
- )
-
- db.add(new_examine)
- db.commit()
- return {
- "code": 200,
- "msg": "保存审批记录成功"
- }
- # 信息发布查看发送列表
- @router.get("/sent_list")
- async def get_sent_list(
- info_id: str = Query('', description='信息ID'),
- channel: str = Query('', description='渠道'),
- keywords: str = Query('', description='关键字'),
- sort_by: str = Query('', description='排序字段'),
- sort_order: str = Query("asc", description='排序方式'),
- page: int = Query(1, gt=0, description='页码'),
- page_size: int = Query(10, gt=0, description='pageSize'),
- db: Session = Depends(get_db)
- ):
- try:
- # 应用查询条件
- where = and_(InfoPublishResponses.publish_id == info_id)
- if channel != '':
- where = and_(where, InfoPublishResponses.publish_channel.like('%{}%'.format(channel)))
- # 计算总条目数
- q = db.query(func.count(InfoPublishResponses.id))
- q = q.filter(where)
- total = q.scalar()
-
- # 执行分页查询
- q = db.query(InfoPublishResponses)
- q = q.filter(where)
- rows = q.order_by(InfoPublishResponses.id.desc()).offset((page - 1) * page_size).limit(page_size).all()
- data = [
- {
- "user_id": row.user_id,
- "user_name": row.user_name,
- "nick_name": row.nick_name,
- "dept_name": row.dept_name,
- "sent_status": row.sent_status,
- "sent_time": get_datetime_str(row.sent_time),
- "response_type": row.response_type,
- "publish_channel": row.publish_channel,
- "yzy_account": row.yzy_account
- }
- for row in rows
- ]
- # 返回结果
- return {
- "code": 200,
- "msg": "查询成功",
- "data": data,
- "total": total
- }
- except Exception as e:
- # 处理异常
- traceback.print_exc()
- raise HTTPException(status_code=500, detail=str(e))
-
- # 列出可用模板
- @router.post("/template_list")
- def template_list(db: Session = Depends(get_db)):
- try:
- rows = db.query(InfoPublishTemplate).filter(InfoPublishTemplate.del_flag == '0').all()
- data = [
- {
- "id": row.id,
- "name": row.name,
- "content": row.content
- }
- for row in rows
- ]
- return {
- "code": 200,
- "msg": "查询成功",
- "data": data,
- "total": len(data)
- }
- except Exception as e:
- # 处理异常
- traceback.print_exc()
- raise HTTPException(status_code=500, detail=str(e))
-
- # 提交审批
- @router.post("/submit_examine")
- async def submit_examine(
- db: Session = Depends(get_db),
- body = Depends(remove_xss_json),
- user_id = Depends(valid_access_token)
- ):
- try:
- user_row = db.query(SysUser).filter(SysUser.user_id == user_id).first()
-
- info_id = body['info_id']
- examine_type = body['examine_type']
- content = body['content']
- # 审批通过
- if examine_type == 'approved':
- new_examine = InfoPublishExamine(
- publish_id = info_id,
- examine_type = 20,
- examine_sub_type = 21,
- content = content,
- examine_time = datetime.now(),
- user_id = user_id,
- user_name = user_row.user_name,
- nick_name = user_row.nick_name
- )
- db.add(new_examine)
- db.commit()
- db.query(InfoPublishBase).filter(InfoPublishBase.id == info_id).update({"publish_status": 3, "examine_status": 2})
- db.commit()
- # 审批不通过
- elif examine_type == 'rejected':
- new_examine = InfoPublishExamine(
- publish_id = info_id,
- examine_type = 20,
- examine_sub_type = 22,
- content = content,
- examine_time = datetime.now(),
- user_id = user_id,
- user_name = user_row.user_name,
- nick_name = user_row.nick_name
- )
- db.add(new_examine)
- db.commit()
- db.query(InfoPublishBase).filter(InfoPublishBase.id == info_id).update({"publish_status": 0, "examine_status": 0})
- db.commit()
- return {
- "code": 200,
- "msg": "审批成功"
- }
- except Exception as e:
- # 处理异常
- traceback.print_exc()
- raise HTTPException(status_code=500, detail=str(e))
|