重构前保存现有 apps/ 结构基线,供后续各阶段回滚。 Co-Authored-By: Claude <noreply@anthropic.com>
139 lines
4.3 KiB
Python
139 lines
4.3 KiB
Python
from datetime import datetime, timezone
|
||
|
||
from fastapi.logger import logger
|
||
from sqlalchemy import and_
|
||
|
||
from apps.constants.constants import NOTICE_SOURCE_LIST
|
||
from apps.constants.message import PAGE_SIZE
|
||
from apps.forms.notice import NoticeForm
|
||
from apps.models.notice import Notice
|
||
from config import FASTAPI_IMAGE_URL
|
||
from extensions import get_db_session
|
||
from utils import R
|
||
from utils.rich_text import save_content
|
||
from utils.request import like_contains
|
||
from utils.security import get_user_id
|
||
|
||
|
||
# 查询通知公告分页数据
|
||
|
||
def get_notice_page(request):
|
||
db = get_db_session()
|
||
# 页码
|
||
page = int(request.query_params.get("pageNo", 1))
|
||
# 每页数
|
||
limit = int(request.query_params.get("pageSize", PAGE_SIZE))
|
||
# 实例化查询对象
|
||
query = db.query(Notice).filter(Notice.is_delete == 0)
|
||
# 通知公告标题
|
||
title = request.query_params.get('title')
|
||
if title:
|
||
query = query.filter(Notice.title.like(like_contains(title), escape='\\'))
|
||
# 通知来源:1官方平台 2开源中国 3CSDN官方 4新浪微博
|
||
source = request.query_params.get('source')
|
||
if source:
|
||
query = query.filter(Notice.source == source)
|
||
# 通知状态:1-正常 2-停用
|
||
status = request.query_params.get('status')
|
||
if status:
|
||
query = query.filter(Notice.status == status)
|
||
# 排序
|
||
query = query.order_by(Notice.id.desc())
|
||
# 记录总数
|
||
count = query.count()
|
||
# 分页查询
|
||
notice_list = query.limit(limit).offset((page - 1) * limit).all()
|
||
# 若当前页超出末页(数据被并发删除),自动回退到第一页
|
||
if page > 1 and not notice_list:
|
||
page = 1
|
||
count = query.count()
|
||
notice_list = query.limit(limit).offset(0).all()
|
||
# 实例化结果
|
||
result = []
|
||
# 遍历数据源
|
||
if len(notice_list) > 0:
|
||
for item in notice_list:
|
||
# 对象转字典
|
||
data = item.to_dict()
|
||
# 通知来源描述
|
||
data['source_name'] = NOTICE_SOURCE_LIST.get(item.source)
|
||
# 加入列表
|
||
result.append(data)
|
||
# 返回结果
|
||
return R.ok(data=result, count=count)
|
||
# 根据通知ID查询详情
|
||
|
||
def get_notice_detail(notice_id):
|
||
db = get_db_session()
|
||
# 根据ID查询通知公告
|
||
notice = db.query(Notice).filter(and_(Notice.id == notice_id, Notice.is_delete == 0)).first()
|
||
# 查询结果判空
|
||
if not notice:
|
||
return None
|
||
# 对象转字典
|
||
data = notice.to_dict()
|
||
# 富文本内容
|
||
content = (notice.content or "").replace("[IMG_URL]", FASTAPI_IMAGE_URL)
|
||
data['content'] = content
|
||
# 通知来源
|
||
data['source_name'] = NOTICE_SOURCE_LIST.get(notice.source)
|
||
# 返回结果
|
||
return data
|
||
# 添加通知公告
|
||
|
||
def add_notice(request, data: NoticeForm):
|
||
db = get_db_session()
|
||
# 通知标题
|
||
title = data.title
|
||
# 通知内容
|
||
content = data.content
|
||
# 处理富文本内容
|
||
content = save_content(content, title, "notice")
|
||
|
||
# 表单数据赋值给对象
|
||
notice = Notice(**data.model_dump(exclude={'id'}))
|
||
notice.content = content
|
||
notice.create_user = get_user_id(request)
|
||
# 插入数据
|
||
notice.save()
|
||
# 返回结果
|
||
return R.ok(msg="添加成功")
|
||
|
||
|
||
# 更新通知公告
|
||
|
||
def update_notice(request, data: NoticeForm):
|
||
db = get_db_session()
|
||
# 记录ID判空
|
||
if not data.id or data.id <= 0:
|
||
return R.failed("记录ID不能为空")
|
||
|
||
# 根据ID查询记录
|
||
notice = db.query(Notice).filter(and_(Notice.id == data.id, Notice.is_delete == 0)).first()
|
||
# 查询结果判空
|
||
if not notice:
|
||
return R.failed("记录不存在")
|
||
|
||
# 通知标题
|
||
title = data.title
|
||
# 通知内容
|
||
content = data.content
|
||
# 处理富文本内容
|
||
content = save_content(content, title, "notice")
|
||
# 构造更新数据(排除 id)
|
||
updData = data.model_dump(exclude={'id'})
|
||
updData['content'] = content
|
||
updData['update_user'] = get_user_id(request)
|
||
updData['update_time'] = datetime.now(tz=timezone.utc)
|
||
# 更新记录
|
||
result = db.query(Notice).filter_by(id=data.id).update(updData)
|
||
# 提交数据
|
||
if not result:
|
||
return R.failed("更新失败")
|
||
# 返回结果
|
||
return R.ok(msg="更新成功")
|
||
# 删除通知公告
|
||
|
||
def delete_notice(notice_id):
|
||
return Notice.batch_delete_r(notice_id)
|