|
@@ -0,0 +1,434 @@
|
|
|
|
+from fastapi import APIRouter, Request, Depends, HTTPException, Query, BackgroundTasks,status
|
|
|
|
+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 random
|
|
|
|
+from sqlalchemy import create_engine, select
|
|
|
|
+from typing import Optional
|
|
|
|
+from utils.StripTagsHTMLParser import *
|
|
|
|
+from common.db import db_dept, db_user, db_area, db_emergency_plan, db_msg_center, db_yzy
|
|
|
|
+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
|
|
|
|
+from extensions import logger
|
|
|
|
+from utils.ry_system_util import *
|
|
|
|
+from common.enc import mpfun
|
|
|
|
+
|
|
|
|
+router = APIRouter()
|
|
|
|
+
|
|
|
|
+@router.get('/common/dept/list')
|
|
|
|
+async def get_list(
|
|
|
|
+ # request: Request,
|
|
|
|
+ deptName: str = Query(None, max_length=100),
|
|
|
|
+ deptCategory:str = Query(None, max_length=100),
|
|
|
|
+ status: str = Query(None, max_length=100),
|
|
|
|
+ db: Session = Depends(get_db),
|
|
|
|
+ body = Depends(remove_xss_json),
|
|
|
|
+ user_id = Depends(valid_access_token)
|
|
|
|
+):
|
|
|
|
+ query = db.query(SysDept)
|
|
|
|
+ query = query.filter(SysDept.dept_id >= 3)
|
|
|
|
+ query = query.filter(SysDept.del_flag != '2')
|
|
|
|
+ if deptName:
|
|
|
|
+ query = query.filter(SysDept.dept_name.like(f'%{deptName}%'))
|
|
|
|
+ if deptCategory:
|
|
|
|
+ query = query.filter(SysDept.dept_category.like(f'%{deptCategory}%'))
|
|
|
|
+ if status:
|
|
|
|
+ query = query.filter(SysDept.status.like(f'%{status}%'))
|
|
|
|
+
|
|
|
|
+ dept_list = query.all()
|
|
|
|
+ # 将模型实例转换为字典
|
|
|
|
+ dept_list_dict = [{
|
|
|
|
+ "deptId": dept.dept_id,
|
|
|
|
+ "deptName": dept.dept_name,
|
|
|
|
+ "ancestors": dept.ancestors,
|
|
|
|
+ "deptCategory": dept.dept_category,
|
|
|
|
+ "orderNum": dept.order_num,
|
|
|
|
+ "parentId": dept.parent_id,
|
|
|
|
+ "parentName": dept.parent_name,
|
|
|
|
+ } for dept in dept_list]
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ return {
|
|
|
|
+ "code": 200,
|
|
|
|
+ "data": dept_list_dict,
|
|
|
|
+
|
|
|
|
+ "msg": "操作成功"
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+@router.get("/system/dept/list")
|
|
|
|
+async def get_system_dept_list(
|
|
|
|
+ request: Request,
|
|
|
|
+ eventId: str,
|
|
|
|
+ db: Session = Depends(get_db),
|
|
|
|
+ body = Depends(remove_xss_json),
|
|
|
|
+ user_id = Depends(valid_access_token)
|
|
|
|
+):
|
|
|
|
+ dept_list_dict = get_dept_list_by_event_id(db, eventId)
|
|
|
|
+
|
|
|
|
+ return {
|
|
|
|
+ "code": 200,
|
|
|
|
+ "data": dept_list_dict,
|
|
|
|
+
|
|
|
|
+ "msg": "操作成功"
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+@router.post("/system/dept/delete")
|
|
|
|
+async def delete_system_dept(
|
|
|
|
+ request: Request,
|
|
|
|
+ db: Session = Depends(get_db),
|
|
|
|
+ body = Depends(remove_xss_json),
|
|
|
|
+ user_id = Depends(valid_access_token)
|
|
|
|
+):
|
|
|
|
+ eventId = body['eventId']
|
|
|
|
+ deptId = body['deptId']
|
|
|
|
+ db.query(CommandSystemDept).filter(and_(CommandSystemDept.event_id == eventId, CommandSystemDept.id == deptId, CommandSystemDept.dept_category == "temp")).delete()
|
|
|
|
+ db.query(CommandSystemUser).filter(and_(CommandSystemUser.event_id == eventId, CommandSystemUser.system_dept_id == deptId, CommandSystemUser.user_category == "temp")).delete()
|
|
|
|
+ db.commit()
|
|
|
|
+
|
|
|
|
+ return {
|
|
|
|
+ "code": 200,
|
|
|
|
+ "msg": "操作成功"
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+@router.post("/system/user/checked")
|
|
|
|
+async def check_users(
|
|
|
|
+ request: Request,
|
|
|
|
+ db: Session = Depends(get_db),
|
|
|
|
+ body = Depends(remove_xss_json),
|
|
|
|
+):
|
|
|
|
+ try:
|
|
|
|
+ eventId = body['eventId']
|
|
|
|
+ nodes = body['nodes']
|
|
|
|
+ for node in nodes:
|
|
|
|
+ userId = node['userId']
|
|
|
|
+ db.query(CommandSystemUser).filter(and_(CommandSystemUser.event_id == eventId, CommandSystemUser.user_id == userId, CommandSystemUser.user_category == "temp")).delete()
|
|
|
|
+ if node['checked'] == True:
|
|
|
|
+ new_user = CommandSystemUser(
|
|
|
|
+ user_id = node['userId'],
|
|
|
|
+ dept_id = node['deptId'],
|
|
|
|
+ dept_name = node['deptName'],
|
|
|
|
+ user_name = node['userName'],
|
|
|
|
+ nick_name = node['nickName'],
|
|
|
|
+ system_dept_id = node['systemDeptId'],
|
|
|
|
+ user_category = "temp",
|
|
|
|
+ event_id = eventId,
|
|
|
|
+ order_num = unixstamp()
|
|
|
|
+ )
|
|
|
|
+ db.add(new_user)
|
|
|
|
+ db.commit()
|
|
|
|
+
|
|
|
|
+ if node['checked'] == False:
|
|
|
|
+ pass
|
|
|
|
+
|
|
|
|
+ return {
|
|
|
|
+ "code": 200,
|
|
|
|
+ "msg": "操作成功"
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ except Exception as e:
|
|
|
|
+ traceback.print_exc()
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+@router.get("/system/user/list")
|
|
|
|
+async def get_system_user_list(
|
|
|
|
+ request: Request,
|
|
|
|
+ eventId: str,
|
|
|
|
+ deptId: str,
|
|
|
|
+ nickName: str = Query(None, description='用户姓名'),
|
|
|
|
+ db: Session = Depends(get_db),
|
|
|
|
+ body = Depends(remove_xss_json),
|
|
|
|
+ user_id = Depends(valid_access_token)
|
|
|
|
+):
|
|
|
|
+ try:
|
|
|
|
+ row = db.query(CommandSystemDept).filter(CommandSystemDept.id == int(deptId)).first()
|
|
|
|
+ systemDeptId = row.id
|
|
|
|
+ deptCategory = row.dept_category
|
|
|
|
+ deptId = row.dept_id
|
|
|
|
+
|
|
|
|
+ if deptCategory == '0':
|
|
|
|
+ # 虚拟部门,总指挥或者副总指挥,其他部门已被tree下来忽略
|
|
|
|
+ query = db.query(CommandSystemUser)
|
|
|
|
+ query = query.filter(and_(CommandSystemUser.event_id == eventId, CommandSystemUser.system_dept_id == deptId))
|
|
|
|
+ if nickName:
|
|
|
|
+ query =query.filter(CommandSystemUser.nick_name.like(f'%{nickName}%'))
|
|
|
|
+
|
|
|
|
+ user_list = []
|
|
|
|
+ users = query.all()
|
|
|
|
+ for user in users:
|
|
|
|
+ user_info = {
|
|
|
|
+ "id": user.id,
|
|
|
|
+ "userId": user.user_id,
|
|
|
|
+ "systemDeptId": user.system_dept_id,
|
|
|
|
+ "deptId": user.dept_id,
|
|
|
|
+ "deptName": user.dept_name,
|
|
|
|
+ "userName": user.user_name,
|
|
|
|
+ "nickName": user.nick_name,
|
|
|
|
+ "position": user.position
|
|
|
|
+ }
|
|
|
|
+ user_list.append(user_info)
|
|
|
|
+ # 返回结果
|
|
|
|
+ return {
|
|
|
|
+ "code": 200,
|
|
|
|
+ "msg": "成功用户列表",
|
|
|
|
+ "rows": user_list,
|
|
|
|
+ "selected_rows": user_list
|
|
|
|
+ }
|
|
|
|
+ else:
|
|
|
|
+ # 真实部门
|
|
|
|
+ # 查询部门用户
|
|
|
|
+ def get_dept_chli(dept_list : list,parent_id : int):
|
|
|
|
+ depts = parent_id_get_dept_info(db,parent_id)
|
|
|
|
+ if depts:
|
|
|
|
+ for dept in depts:
|
|
|
|
+ dept_list.append(dept.dept_id)
|
|
|
|
+ get_dept_chli(dept_list, dept.dept_id)
|
|
|
|
+ return dept_list
|
|
|
|
+
|
|
|
|
+ query = db.query(SysUser).filter(and_(SysUser.status == 0))
|
|
|
|
+ query = query.filter(SysUser.dept_id.in_(get_dept_chli([deptId],deptId)))
|
|
|
|
+ query = query.order_by(SysUser.create_time.desc())
|
|
|
|
+ users = query.all()
|
|
|
|
+
|
|
|
|
+ user_list = []
|
|
|
|
+ for user in users:
|
|
|
|
+ user_info = {
|
|
|
|
+ "userId": user.user_id,
|
|
|
|
+ "deptId": user.dept_id,
|
|
|
|
+ "deptName": user.dept_name,
|
|
|
|
+ "userName": mpfun.dec_data(user.user_name),
|
|
|
|
+ "nickName": user.nick_name,
|
|
|
|
+ "systemDeptId": systemDeptId,
|
|
|
|
+ "position": ""
|
|
|
|
+ }
|
|
|
|
+ user_list.append(user_info)
|
|
|
|
+
|
|
|
|
+ # 查询已勾选用户
|
|
|
|
+ query = db.query(CommandSystemUser)
|
|
|
|
+ query = query.filter(CommandSystemUser.system_dept_id == systemDeptId)
|
|
|
|
+ if nickName:
|
|
|
|
+ query =query.filter(CommandSystemUser.nick_name.like(f'%{nickName}%'))
|
|
|
|
+
|
|
|
|
+ users = query.all()
|
|
|
|
+
|
|
|
|
+ # 将查询结果转换为列表形式的字典
|
|
|
|
+ selected_user_list = []
|
|
|
|
+ for user in users:
|
|
|
|
+ user_info = {
|
|
|
|
+ "id": user.id,
|
|
|
|
+ "userId": user.user_id,
|
|
|
|
+ "systemDeptId": user.system_dept_id,
|
|
|
|
+ "deptId": user.dept_id,
|
|
|
|
+ "deptName": user.dept_name,
|
|
|
|
+ "userName": user.user_name,
|
|
|
|
+ "nickName": user.nick_name,
|
|
|
|
+ "position": user.position
|
|
|
|
+ }
|
|
|
|
+ selected_user_list.append(user_info)
|
|
|
|
+
|
|
|
|
+ # 返回结果
|
|
|
|
+ return {
|
|
|
|
+ "code": 200,
|
|
|
|
+ "msg": "成功用户列表",
|
|
|
|
+ "rows": user_list,
|
|
|
|
+ "selected_rows": selected_user_list
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ except Exception as e:
|
|
|
|
+ traceback.print_exc()
|
|
|
|
+ raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def get_dept_list_by_event_id(db: Session, eventId: str):
|
|
|
|
+
|
|
|
|
+ init_system_dept_list_by_event_id(db, eventId)
|
|
|
|
+
|
|
|
|
+ query = db.query(CommandSystemDept)
|
|
|
|
+ query = query.filter(CommandSystemDept.event_id == eventId)
|
|
|
|
+ query = query.filter(CommandSystemDept.del_flag != '2')
|
|
|
|
+ dept_list = query.all()
|
|
|
|
+
|
|
|
|
+ dept_list_dict = [{
|
|
|
|
+ "id": dept.id,
|
|
|
|
+ "deptId": dept.dept_id,
|
|
|
|
+ "deptName": dept.dept_name,
|
|
|
|
+ "ancestors": dept.ancestors,
|
|
|
|
+ "deptCategory": dept.dept_category,
|
|
|
|
+ "orderNum": dept.order_num,
|
|
|
|
+ "parentId": dept.parent_id,
|
|
|
|
+ "parentName": dept.parent_name
|
|
|
|
+ } for dept in dept_list]
|
|
|
|
+
|
|
|
|
+ return dept_list_dict
|
|
|
|
+
|
|
|
|
+@router.post("/system/filtered/dept/list")
|
|
|
|
+async def get_list(request: Request,
|
|
|
|
+ db: Session = Depends(get_db),
|
|
|
|
+ body = Depends(remove_xss_json)):
|
|
|
|
+
|
|
|
|
+ eventId = body['eventId']
|
|
|
|
+ nodes = body['nodes']
|
|
|
|
+
|
|
|
|
+ # 成员单位节点
|
|
|
|
+ parent_id = get_member_parent_id(db, eventId)
|
|
|
|
+ print('成员单位节点:', parent_id)
|
|
|
|
+
|
|
|
|
+ ok_nodes = []
|
|
|
|
+ for node in nodes:
|
|
|
|
+ deptName = node['deptName']
|
|
|
|
+
|
|
|
|
+ # 只能是部门才可以加上去
|
|
|
|
+ if deptName.find("局") != -1 :
|
|
|
|
+ ok_nodes.append(node)
|
|
|
|
+
|
|
|
|
+ if len(ok_nodes) > 0:
|
|
|
|
+ # 删除之前的成员单位
|
|
|
|
+
|
|
|
|
+ # 将勾选的XXX局等节点加到成员单位节点下
|
|
|
|
+ i = 0
|
|
|
|
+ for n in ok_nodes:
|
|
|
|
+
|
|
|
|
+ row = db.query(CommandSystemDept).filter(and_(CommandSystemDept.event_id == eventId, CommandSystemDept.dept_id == n['deptId'])).first()
|
|
|
|
+ if row is None:
|
|
|
|
+ new_dept = CommandSystemDept(
|
|
|
|
+ dept_id = n['deptId'],
|
|
|
|
+ parent_id = parent_id,
|
|
|
|
+ parent_name = '成员部门',
|
|
|
|
+ ancestors = '',
|
|
|
|
+ dept_name = n['deptName'],
|
|
|
|
+ dept_category = "temp",
|
|
|
|
+ event_id = eventId,
|
|
|
|
+ order_num = 10 * i,
|
|
|
|
+ del_flag = '0'
|
|
|
|
+ )
|
|
|
|
+ db.add(new_dept)
|
|
|
|
+ db.commit()
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ dept_list_dict = get_dept_list_by_event_id(db, eventId)
|
|
|
|
+
|
|
|
|
+ return {
|
|
|
|
+ "code": 200,
|
|
|
|
+ "data": dept_list_dict,
|
|
|
|
+
|
|
|
|
+ "msg": "操作成功"
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+# 成员单位节点
|
|
|
|
+def get_member_parent_id(db: Session, eventId: str):
|
|
|
|
+ row = db.query(CommandSystemDept).filter(and_(CommandSystemDept.event_id == eventId, CommandSystemDept.dept_category == 'member_root')).first()
|
|
|
|
+ parent_id = row.dept_id
|
|
|
|
+ return parent_id
|
|
|
|
+
|
|
|
|
+def init_system_dept_list_by_event_id(db: Session, eventId: str):
|
|
|
|
+ c = db.query(CommandSystemDept).filter(CommandSystemDept.event_id == eventId).count()
|
|
|
|
+ if c > 0:
|
|
|
|
+ return
|
|
|
|
+
|
|
|
|
+ # 初始部门
|
|
|
|
+ rows = db.query(CommandSystemDept).filter(and_(CommandSystemDept.event_id == "0", CommandSystemDept.del_flag == '0')).all()
|
|
|
|
+ for row in rows:
|
|
|
|
+ info = get_model_dict(row)
|
|
|
|
+ info['event_id'] = eventId
|
|
|
|
+ del info['id']
|
|
|
|
+
|
|
|
|
+ new_dept = CommandSystemDept(**info)
|
|
|
|
+ db.add(new_dept)
|
|
|
|
+ db.commit()
|
|
|
|
+
|
|
|
|
+ # 初始人员(总指挥和副总指挥)
|
|
|
|
+ rows = db.query(CommandSystemUser).filter(and_(CommandSystemUser.event_id == "0")).all()
|
|
|
|
+ for row in rows:
|
|
|
|
+ info = get_model_dict(row)
|
|
|
|
+ info['event_id'] = eventId
|
|
|
|
+ del info['id']
|
|
|
|
+
|
|
|
|
+ new_user = CommandSystemUser(**info)
|
|
|
|
+ db.add(new_user)
|
|
|
|
+ db.commit()
|
|
|
|
+
|
|
|
|
+ # 获取成员单位节点
|
|
|
|
+ member_parent_id = get_member_parent_id(db, eventId)
|
|
|
|
+ print('成员单位节点:', member_parent_id)
|
|
|
|
+
|
|
|
|
+ # 初始化应急预案人员
|
|
|
|
+ row = db.query(EventBase).filter(EventBase.event_code == eventId).first()
|
|
|
|
+ if row is None:
|
|
|
|
+ return
|
|
|
|
+
|
|
|
|
+ plan_id = row.plan_id
|
|
|
|
+ if plan_id is None or plan_id == '':
|
|
|
|
+ return
|
|
|
|
+
|
|
|
|
+ # 匹配预案相关部门(以下代码,对数据匹配行要求高)
|
|
|
|
+ rows = db.query(EmergencyUnit).filter(EmergencyUnit.plan_id == plan_id).order_by(EmergencyUnit.dept_order.asc()).all()
|
|
|
|
+ for row in rows:
|
|
|
|
+ dept_id = row.dept_id
|
|
|
|
+ dept_name = row.dept_name
|
|
|
|
+ dept_order = row.dept_order
|
|
|
|
+
|
|
|
|
+ system_dept_id = 0
|
|
|
|
+ dept_row = db.query(CommandSystemDept).filter(and_(CommandSystemDept.event_id == eventId, CommandSystemDept.dept_id == dept_id)).first()
|
|
|
|
+ if dept_row is None:
|
|
|
|
+ new_dept = CommandSystemDept(
|
|
|
|
+ dept_id = dept_id,
|
|
|
|
+ parent_id = member_parent_id,
|
|
|
|
+ parent_name = '成员部门',
|
|
|
|
+ ancestors = '',
|
|
|
|
+ dept_name = dept_name,
|
|
|
|
+ dept_category = "1",# 设置为固定部门
|
|
|
|
+ event_id = eventId,
|
|
|
|
+ order_num = dept_order,
|
|
|
|
+ del_flag = '0'
|
|
|
|
+ )
|
|
|
|
+ db.add(new_dept)
|
|
|
|
+ db.commit()
|
|
|
|
+ db.refresh(new_dept)
|
|
|
|
+ system_dept_id = new_dept.id
|
|
|
|
+ else:
|
|
|
|
+ system_dept_id = dept_row.id
|
|
|
|
+
|
|
|
|
+ # 匹配部门对应的预案联系人
|
|
|
|
+ contact_row = db.query(EmergencyContactInfo).filter(and_(EmergencyContactInfo.unit_id == dept_id, EmergencyContactInfo.del_flag == '0')).first()
|
|
|
|
+ if contact_row is not None:
|
|
|
|
+ db_dept.get_dept_name_by_id(db, dept_id)
|
|
|
|
+ contact_name = contact_row.contact_name
|
|
|
|
+ position = contact_row.position
|
|
|
|
+ yzy_account = mpfun.dec_data(contact_row.yue_gov_ease_phone)
|
|
|
|
+
|
|
|
|
+ user_name = ''
|
|
|
|
+ nick_name = contact_name
|
|
|
|
+ user_id = db_user.get_user_id_by_phonenumber(db, yzy_account)
|
|
|
|
+ if user_id != -1:
|
|
|
|
+ user_info = db_user.get_user_info(db, user_id)
|
|
|
|
+ user_name = user_info.user_name
|
|
|
|
+ nick_name = user_info.nick_name
|
|
|
|
+
|
|
|
|
+ new_user = CommandSystemUser(
|
|
|
|
+ user_id = user_id,
|
|
|
|
+ dept_id = dept_id,
|
|
|
|
+ dept_name = dept_name,
|
|
|
|
+ user_name = user_name,
|
|
|
|
+ nick_name = nick_name,
|
|
|
|
+ system_dept_id = system_dept_id,
|
|
|
|
+ user_category = "1",# 设置为固定人
|
|
|
|
+ event_id = eventId,
|
|
|
|
+ position = position,
|
|
|
|
+ order_num = unixstamp()
|
|
|
|
+ )
|
|
|
|
+ db.add(new_user)
|
|
|
|
+ db.commit()
|