Przeglądaj źródła

增加消息查询、删除,修改发送时间判断

zhangnaiwen 2 lat temu
rodzic
commit
936d7fd150
1 zmienionych plików z 76 dodań i 14 usunięć
  1. 76 14
      src/app/api/message.py

+ 76 - 14
src/app/api/message.py

@@ -3,7 +3,7 @@ import json
 
 from flask import request, jsonify, current_app
 from flask_restx import Resource, Namespace, reqparse
-from sqlalchemy import select, insert, update
+from sqlalchemy import select, insert, update, delete
 from sqlalchemy.orm import Session
 
 from app.defines import StatesCode, Module, OperationType
@@ -12,7 +12,7 @@ from app.modle.users import User
 from app.utils.jwt_util import login_required
 from app.utils.save_log import save_log
 from app.utils.message_mq import add_message
-from app.utils.util import to_dict
+from app.utils.util import to_dict, cn_now
 from config import Config
 
 ns = Namespace('message', description='消息管理接口')
@@ -47,6 +47,45 @@ class MessageTypeApi(Resource):
             return jsonify(code=StatesCode.UNKNOWN_ERROR, message=str(e))
 
 
+message_search = reqparse.RequestParser(bundle_errors=True)
+message_search.add_argument(name='name', type=str, location='args', required=False, help='消息名')
+message_search.add_argument(name='type', type=str, location='args', required=False, help='类型')
+message_search.add_argument(name='start_time', type=str, location='args', required=False, help='开始时间')
+message_search.add_argument(name='end_time', type=str, location='args', required=False, help='结束时间')
+
+
+@ns.route('/search')
+class MessageSearchApi(Resource):
+
+    @ns.doc(description='消息查询')
+    @ns.expect(message_search)
+    def get(self):
+        """消息搜索"""
+        name = request.args.get('name')
+        message_type = request.args.get('type')
+        start_time = request.args.get('start_time')
+        end_time = request.args.get('end_time')
+
+        try:
+            with Session(current_app.engine) as session:
+                stmt = select(Message)
+                if name:
+                    stmt = stmt.where(Message.name == name)
+                if message_type:
+                    stmt = stmt.where(Message.type == message_type)
+                if start_time and end_time:
+                    stmt = stmt.where(Message.send_time.between(start_time, end_time))
+
+                results = session.execute(stmt).scalars().all()
+
+            save_log(request, Module.MESSAGE, OperationType.INQUIRE, StatesCode.SUCCESS)
+
+            return jsonify(code=StatesCode.SUCCESS, message='成功', data=to_dict(results))
+        except Exception as e:
+            save_log(request, Module.MESSAGE, OperationType.INQUIRE, StatesCode.UNKNOWN_ERROR)
+            return jsonify(code=StatesCode.UNKNOWN_ERROR, message=str(e))
+
+
 message_details = reqparse.RequestParser(bundle_errors=True)
 message_details.add_argument(name='message_id', type=int, location='args', required=False, help='消息id')
 
@@ -54,9 +93,10 @@ message = reqparse.RequestParser(bundle_errors=True)
 
 message.add_argument(name='message_id', type=str, location='form', required=False, help='消息id')
 message.add_argument(name='name', type=str, location='form', required=False, help='消息名称')
-message.add_argument(name='pipeline', type=str, location='form', required=False, help='通道配置')
-message.add_argument(name='staff', type=str, location='form', required=False, help='人员配置: all role:'' company:'' ')
-message.add_argument(name='send_time', type=str, location='form', required=False, help='发送时间 immediately 2020-10-20 10:10:10')
+message.add_argument(name='pipeline', type=str, location='form', required=False, help='通道配置:instation')
+message.add_argument(name='staff', type=str, location='form', required=False, help='人员配置: all role:id company:id')
+message.add_argument(name='send_time', type=str, location='form', required=False,
+                     help='发送时间 :immediately、 2020-10-20 10:10:10')
 message.add_argument(name='title', type=str, location='form', required=False, help='标题')
 message.add_argument(name='content', type=str, location='form', required=False, help='内容')
 message.add_argument(name='style', type=str, location='form', required=False, help='样式')
@@ -126,8 +166,8 @@ class MessageApi(Resource):
                 else:
                     return jsonify(code=StatesCode.UNKNOWN_ERROR, message='无效的人员配置')
 
-            if send_time != 'immediately':
-                datetime.datetime.strptime(send_time, '%Y-%m-%d %H:%M:%S')
+            # if send_time != 'immediately':
+            #     send_time = datetime.datetime.strptime(send_time, '%Y-%m-%d %H:%M:%S')
 
             # 添加数据库
             with Session(current_app.engine) as session:
@@ -135,7 +175,7 @@ class MessageApi(Resource):
                     name=name,
                     pipeline=pipeline,
                     staff=staff,
-                    send_time=send_time,
+                    send_time=cn_now() if send_time == 'immediately' else send_time,
                     title=title,
                     content=content,
                     style=style,
@@ -190,6 +230,9 @@ class MessageApi(Resource):
         style = request.form.get('style')
         message_type = request.form.get('type')
 
+        if not message_id:
+            return jsonify(code=StatesCode.UNKNOWN_ERROR, message='消息id不能为空')
+
         if pipeline != 'instation':
             return jsonify(code=StatesCode.UNKNOWN_ERROR, message='仅支持站内通知 instation')
 
@@ -212,11 +255,8 @@ class MessageApi(Resource):
                 else:
                     return jsonify(code=StatesCode.UNKNOWN_ERROR, message='无效的人员配置')
 
-            try:
-                if send_time != 'immediately':
-                    datetime.datetime.strptime(send_time, '%Y-%m-%d %H:%M:%S')
-            except Exception:
-                return jsonify(code=StatesCode.UNKNOWN_ERROR, message='时间格式错误。例:2023-10-10 12:12:12')
+            # if send_time != 'immediately':
+            #     send_time = datetime.datetime.strptime(send_time, '%Y-%m-%d %H:%M:%S')
 
             # 添加数据库
             with Session(current_app.engine) as session:
@@ -224,7 +264,7 @@ class MessageApi(Resource):
                     name=name,
                     pipeline=pipeline,
                     staff=staff,
-                    send_time=send_time,
+                    send_time=cn_now() if send_time == 'immediately' else send_time,
                     title=title,
                     content=content,
                     style=style,
@@ -261,3 +301,25 @@ class MessageApi(Resource):
         except Exception as e:
             save_log(request, Module.MESSAGE, OperationType.UPDATE, StatesCode.UNKNOWN_ERROR)
             return jsonify(code=StatesCode.UNKNOWN_ERROR, message=str(e))
+
+    @ns.doc(id='delete_message', description='删除消息')
+    @ns.expect(message)
+    def delete(self):
+        """删除消息"""
+        message_id = request.form.get('message_id')
+
+        if message_id is None:
+            return jsonify(code=StatesCode.UNKNOWN_ERROR, message='消息id不能为空')
+
+        try:
+            with Session(current_app.engine) as session:
+                stmt = delete(Message).where(Message.id == message_id)
+                session.execute(stmt)
+                session.commit()
+
+            save_log(request, Module.MESSAGE, OperationType.DELETE, StatesCode.SUCCESS)
+
+            return jsonify(code=StatesCode.SUCCESS, message='成功')
+        except Exception as e:
+            save_log(request, Module.MESSAGE, OperationType.DELETE, StatesCode.UNKNOWN_ERROR)
+            return jsonify(code=StatesCode.UNKNOWN_ERROR, message=str(e))