|
@@ -0,0 +1,191 @@
|
|
|
+#!/usr/bin/env python3
|
|
|
+# -*- coding: utf-8 -*-
|
|
|
+
|
|
|
+from fastapi import APIRouter, Request, Depends, Query, HTTPException, status
|
|
|
+from fastapi.responses import JSONResponse
|
|
|
+from common.security import valid_access_token
|
|
|
+from pydantic import BaseModel
|
|
|
+from database import get_db
|
|
|
+from sqlalchemy.orm import Session
|
|
|
+from typing import List
|
|
|
+from models import *
|
|
|
+from utils import *
|
|
|
+from utils.ry_system_util import *
|
|
|
+import json
|
|
|
+from common.db import db_user
|
|
|
+from sqlalchemy.sql import func
|
|
|
+from common.enc import mpfun, sys_dept_data
|
|
|
+from common.auth_user import *
|
|
|
+from common.db import db_czrz
|
|
|
+import traceback
|
|
|
+
|
|
|
+router = APIRouter()
|
|
|
+
|
|
|
+@router.post('')
|
|
|
+async def notice_create(
|
|
|
+ db: Session = Depends(get_db),
|
|
|
+ user_id: int = Depends(valid_access_token),
|
|
|
+ body = Depends(remove_xss_json)
|
|
|
+):
|
|
|
+ try:
|
|
|
+ noticeTitle = body['noticeTitle']
|
|
|
+ noticeType = body['noticeType']
|
|
|
+ noticeContent = body['noticeContent']
|
|
|
+ status = body['status']
|
|
|
+ remark = body['remark']
|
|
|
+
|
|
|
+ new_notice = SysNotice(
|
|
|
+ notice_title = noticeTitle,
|
|
|
+ notice_type = noticeType,
|
|
|
+ notice_content = noticeContent,
|
|
|
+ status = status,
|
|
|
+ remark = remark,
|
|
|
+ create_by = user_id,
|
|
|
+ update_by = user_id,
|
|
|
+ create_dept = db_user.get_user_info(db, user_id).dept_id
|
|
|
+ )
|
|
|
+ db.add(new_notice)
|
|
|
+ db.commit()
|
|
|
+ db.refresh(new_notice)
|
|
|
+
|
|
|
+ db.commit()
|
|
|
+ return {"code": 200, "msg": "创建成功", "data": None}
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ traceback.print_exc()
|
|
|
+ raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
|
|
+
|
|
|
+
|
|
|
+@router.get('/list')
|
|
|
+async def get_list(
|
|
|
+ request: Request,
|
|
|
+ noticeTitle: str = Query(None, max_length=100),
|
|
|
+ noticeType: str = Query(None, max_length=100),
|
|
|
+ pageNum: int = Query(1, gt=0, description="页码"),
|
|
|
+ pageSize: int = Query(10, gt=0, le=100, description="每页大小"),
|
|
|
+ db: Session = Depends(get_db),
|
|
|
+ user_id = Depends(valid_access_token),
|
|
|
+):
|
|
|
+ query = db.query(SysNotice)
|
|
|
+ query = query.filter(SysNotice.status == '0')
|
|
|
+ if noticeTitle:
|
|
|
+ query = query.filter(SysNotice.notice_title.like(f'%{noticeTitle}%'))
|
|
|
+ if noticeType:
|
|
|
+ query = query.filter(SysNotice.notice_type == noticeType)
|
|
|
+
|
|
|
+ total_count = query.count()
|
|
|
+
|
|
|
+ offset = (pageNum - 1) * pageSize
|
|
|
+ query = query.order_by(SysNotice.create_time.desc()).offset(offset).limit(pageSize)
|
|
|
+ notice_list = query.all()
|
|
|
+
|
|
|
+ # 将模型实例转换为字典
|
|
|
+ czrz_list_dict = [{
|
|
|
+ "noticeId": item.notice_id,
|
|
|
+ "noticeTitle": item.notice_title,
|
|
|
+ "noticeType": item.notice_type,
|
|
|
+ "noticeContent": item.notice_content,
|
|
|
+ "status": item.status,
|
|
|
+ "createTime": item.create_time.strftime('%Y-%m-%d %H:%M:%S') if item.create_time else '',
|
|
|
+ "createByName": db_user.get_nick_name_by_id(db, item.create_by),
|
|
|
+ } for item in notice_list]
|
|
|
+
|
|
|
+
|
|
|
+ return {
|
|
|
+ "code": 200,
|
|
|
+ "msg": "操作成功",
|
|
|
+ "rows": czrz_list_dict,
|
|
|
+ 'pages': (total_count + pageSize - 1) // pageSize,
|
|
|
+ 'total': total_count,
|
|
|
+ "currentPage": pageNum,
|
|
|
+ "pageSize": pageSize,
|
|
|
+ }
|
|
|
+
|
|
|
+@router.delete('/{notice_id}')
|
|
|
+async def delete(
|
|
|
+ notice_id: int,
|
|
|
+ db: Session = Depends(get_db),
|
|
|
+ body = Depends(remove_xss_json),
|
|
|
+ user_id = Depends(valid_access_token)
|
|
|
+):
|
|
|
+ try:
|
|
|
+ query = db.query(SysNotice)
|
|
|
+ query = query.filter(SysNotice.notice_id == notice_id)
|
|
|
+
|
|
|
+ post_to_delete = query.first()
|
|
|
+ if not post_to_delete:
|
|
|
+ detail = "公告不存在"
|
|
|
+ raise HTTPException(status_code=404, detail="公告不存在")
|
|
|
+ db.delete(post_to_delete)
|
|
|
+ db.commit()
|
|
|
+ return {
|
|
|
+ "code": 200,
|
|
|
+ "msg": "公告删除成功"
|
|
|
+ }
|
|
|
+ except Exception as e:
|
|
|
+ db.rollback()
|
|
|
+ if str(e)=='':
|
|
|
+ e = detail
|
|
|
+ raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e))
|
|
|
+
|
|
|
+
|
|
|
+@router.get('/{notice_id}')
|
|
|
+async def get_notice_info( notice_id:int,
|
|
|
+ db: Session = Depends(get_db),
|
|
|
+ user_id: int = Depends(valid_access_token)):
|
|
|
+
|
|
|
+ try:
|
|
|
+ # 构建查询
|
|
|
+ query = db.query(SysNotice)
|
|
|
+ # 应用查询条件
|
|
|
+ query = query.filter(SysNotice.notice_id==notice_id)
|
|
|
+
|
|
|
+ notices = query.all()
|
|
|
+
|
|
|
+ # 将查询结果转换为列表形式的字典
|
|
|
+ notice_list = notice_list_to_dict(notices, db)
|
|
|
+
|
|
|
+ # 返回结果
|
|
|
+ return {
|
|
|
+ "code": 200,
|
|
|
+ "msg": "成功",
|
|
|
+ "data": notice_list[0]
|
|
|
+ }
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ traceback.print_exc()
|
|
|
+ raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
|
|
+
|
|
|
+@router.put('')
|
|
|
+async def postupdate(
|
|
|
+ db: Session = Depends(get_db),
|
|
|
+ user_id: int = Depends(valid_access_token),
|
|
|
+ body = Depends(remove_xss_json)
|
|
|
+):
|
|
|
+ try:
|
|
|
+ noticeId = body['noticeId']
|
|
|
+
|
|
|
+ query = db.query(SysNotice)
|
|
|
+ query = query.filter(SysNotice.notice_id == noticeId)
|
|
|
+ notice = query.first()
|
|
|
+ if not notice :
|
|
|
+ return JSONResponse(status_code=410, content={
|
|
|
+ 'code': 410,
|
|
|
+ 'msg': f'公告{noticeId}不存在'
|
|
|
+ })
|
|
|
+
|
|
|
+ notice.notice_title = body['noticeTitle']
|
|
|
+ notice.notice_content = body['noticeContent']
|
|
|
+ notice.notice_type = body['noticeType']
|
|
|
+ notice.status = body['status']
|
|
|
+ notice.remark = body['remark']
|
|
|
+ notice.update_by = user_id
|
|
|
+ notice.update_time = datetime.now()
|
|
|
+ db.commit()
|
|
|
+
|
|
|
+ return {"code": 200, "msg": "更新成功", "data": None}
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ traceback.print_exc()
|
|
|
+ raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
|
|
+
|