2022-04-10 22:19:50 +08:00
|
|
|
|
from datetime import datetime, timedelta
|
2023-02-18 18:46:54 +08:00
|
|
|
|
from typing import Any, List, Literal, Optional, Tuple, Union
|
2022-04-10 22:19:50 +08:00
|
|
|
|
|
2023-02-18 18:46:54 +08:00
|
|
|
|
from tortoise import fields
|
|
|
|
|
|
from tortoise.functions import Count
|
2022-04-10 22:19:50 +08:00
|
|
|
|
|
2023-02-18 18:46:54 +08:00
|
|
|
|
from services.db_context import Model
|
2022-04-10 22:19:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
2023-02-18 18:46:54 +08:00
|
|
|
|
class ChatHistory(Model):
|
2022-04-10 22:19:50 +08:00
|
|
|
|
|
2023-02-18 18:46:54 +08:00
|
|
|
|
id = fields.IntField(pk=True, generated=True, auto_increment=True)
|
|
|
|
|
|
"""自增id"""
|
2023-04-14 11:45:45 +08:00
|
|
|
|
user_id = fields.CharField(255)
|
2023-02-18 18:46:54 +08:00
|
|
|
|
"""用户id"""
|
2023-04-14 11:45:45 +08:00
|
|
|
|
group_id = fields.CharField(255, null=True)
|
2023-02-18 18:46:54 +08:00
|
|
|
|
"""群聊id"""
|
2023-02-20 22:08:01 +08:00
|
|
|
|
text = fields.TextField(null=True)
|
2023-02-18 18:46:54 +08:00
|
|
|
|
"""文本内容"""
|
2023-02-20 22:08:01 +08:00
|
|
|
|
plain_text = fields.TextField(null=True)
|
2023-02-18 18:46:54 +08:00
|
|
|
|
"""纯文本"""
|
|
|
|
|
|
create_time = fields.DatetimeField(auto_now_add=True)
|
|
|
|
|
|
"""创建时间"""
|
2022-04-26 14:45:04 +08:00
|
|
|
|
|
2023-02-18 18:46:54 +08:00
|
|
|
|
class Meta:
|
|
|
|
|
|
table = "chat_history"
|
|
|
|
|
|
table_description = "聊天记录数据表"
|
2022-04-26 14:45:04 +08:00
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
async def get_group_msg_rank(
|
|
|
|
|
|
cls,
|
2023-04-14 11:45:45 +08:00
|
|
|
|
gid: str | int,
|
2022-04-26 14:45:04 +08:00
|
|
|
|
limit: int = 10,
|
|
|
|
|
|
order: str = "DESC",
|
|
|
|
|
|
date_scope: Optional[Tuple[datetime, datetime]] = None,
|
2023-02-18 18:46:54 +08:00
|
|
|
|
) -> List["ChatHistory"]:
|
2022-04-26 14:45:04 +08:00
|
|
|
|
"""
|
2022-07-23 14:09:17 +08:00
|
|
|
|
说明:
|
2022-04-26 14:45:04 +08:00
|
|
|
|
获取排行数据
|
2022-07-23 14:09:17 +08:00
|
|
|
|
参数:
|
2022-04-26 14:45:04 +08:00
|
|
|
|
:param gid: 群号
|
|
|
|
|
|
:param limit: 获取数量
|
|
|
|
|
|
:param order: 排序类型,desc,des
|
|
|
|
|
|
:param date_scope: 日期范围
|
|
|
|
|
|
"""
|
2023-03-01 23:24:56 +08:00
|
|
|
|
o = "-" if order == "DESC" else ""
|
2023-04-14 11:45:45 +08:00
|
|
|
|
query = cls.filter(group_id=str(gid))
|
2023-03-01 23:24:56 +08:00
|
|
|
|
if date_scope:
|
|
|
|
|
|
query = query.filter(create_time__range=date_scope)
|
2023-02-18 18:46:54 +08:00
|
|
|
|
return list(
|
2023-04-14 11:45:45 +08:00
|
|
|
|
await query.annotate(count=Count("user_id"))
|
2023-03-01 23:24:56 +08:00
|
|
|
|
.order_by(o + "count")
|
2023-04-14 11:45:45 +08:00
|
|
|
|
.group_by("user_id")
|
2023-02-18 18:46:54 +08:00
|
|
|
|
.limit(limit)
|
2023-04-14 11:45:45 +08:00
|
|
|
|
.values_list("user_id", "count")
|
2023-02-20 22:14:40 +08:00
|
|
|
|
) # type: ignore
|
2022-04-26 14:45:04 +08:00
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2023-04-14 11:45:45 +08:00
|
|
|
|
async def get_group_first_msg_datetime(cls, group_id: str | int) -> Optional[datetime]:
|
2022-04-26 14:45:04 +08:00
|
|
|
|
"""
|
2022-07-23 14:09:17 +08:00
|
|
|
|
说明:
|
2022-04-26 14:45:04 +08:00
|
|
|
|
获取群第一条记录消息时间
|
2022-07-23 14:09:17 +08:00
|
|
|
|
参数:
|
2023-04-14 11:45:45 +08:00
|
|
|
|
:param group_id: 群组id
|
2022-04-26 14:45:04 +08:00
|
|
|
|
"""
|
|
|
|
|
|
if (
|
2023-04-14 11:45:45 +08:00
|
|
|
|
message := await cls.filter(group_id=str(group_id))
|
2023-02-18 18:46:54 +08:00
|
|
|
|
.order_by("create_time")
|
|
|
|
|
|
.first()
|
2022-04-26 14:45:04 +08:00
|
|
|
|
):
|
2023-02-18 18:46:54 +08:00
|
|
|
|
return message.create_time
|
2022-04-10 22:19:50 +08:00
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2023-02-18 18:46:54 +08:00
|
|
|
|
async def get_message(
|
2022-04-10 22:19:50 +08:00
|
|
|
|
cls,
|
2023-04-14 11:45:45 +08:00
|
|
|
|
uid: Union[int, str],
|
|
|
|
|
|
gid: Union[int, str],
|
2022-04-10 22:19:50 +08:00
|
|
|
|
type_: Literal["user", "group"],
|
2022-04-26 14:45:04 +08:00
|
|
|
|
msg_type: Optional[Literal["private", "group"]] = None,
|
|
|
|
|
|
days: Optional[Union[int, Tuple[datetime, datetime]]] = None,
|
2023-02-18 18:46:54 +08:00
|
|
|
|
) -> List["ChatHistory"]:
|
2022-04-10 22:19:50 +08:00
|
|
|
|
"""
|
2022-07-23 14:09:17 +08:00
|
|
|
|
说明:
|
2022-04-10 22:19:50 +08:00
|
|
|
|
获取消息查询query
|
2022-07-23 14:09:17 +08:00
|
|
|
|
参数:
|
2023-04-14 11:45:45 +08:00
|
|
|
|
:param uid: 用户id
|
|
|
|
|
|
:param gid: 群聊id
|
2022-04-10 22:19:50 +08:00
|
|
|
|
:param type_: 类型,私聊或群聊
|
|
|
|
|
|
:param msg_type: 消息类型,用户或群聊
|
|
|
|
|
|
:param days: 限制日期
|
|
|
|
|
|
"""
|
|
|
|
|
|
if type_ == "user":
|
2023-04-14 11:45:45 +08:00
|
|
|
|
query = cls.filter(user_id=str(uid))
|
2022-04-10 22:19:50 +08:00
|
|
|
|
if msg_type == "private":
|
2023-02-18 18:46:54 +08:00
|
|
|
|
query = query.filter(group_id__isnull=True)
|
2022-04-10 22:19:50 +08:00
|
|
|
|
elif msg_type == "group":
|
2023-02-18 18:46:54 +08:00
|
|
|
|
query = query.filter(group_id__not_isnull=True)
|
2022-04-10 22:19:50 +08:00
|
|
|
|
else:
|
2023-04-14 11:45:45 +08:00
|
|
|
|
query = cls.filter(group_id=str(gid))
|
2022-04-26 14:45:04 +08:00
|
|
|
|
if uid:
|
2023-04-14 11:45:45 +08:00
|
|
|
|
query = query.filter(user_id=str(uid))
|
2022-04-10 22:19:50 +08:00
|
|
|
|
if days:
|
2022-04-26 14:45:04 +08:00
|
|
|
|
if isinstance(days, int):
|
2023-02-18 18:46:54 +08:00
|
|
|
|
query = query.filter(
|
|
|
|
|
|
create_time__gte=datetime.now() - timedelta(days=days)
|
2022-04-26 14:45:04 +08:00
|
|
|
|
)
|
|
|
|
|
|
elif isinstance(days, tuple):
|
2023-03-02 21:01:11 +08:00
|
|
|
|
query = query.filter(create_time__range=days)
|
2023-02-20 22:14:40 +08:00
|
|
|
|
return await query.all() # type: ignore
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
async def _run_script(cls):
|
2023-03-01 23:24:56 +08:00
|
|
|
|
return [
|
|
|
|
|
|
"alter table chat_history alter group_id drop not null;", # 允许 group_id 为空
|
|
|
|
|
|
"alter table chat_history alter text drop not null;", # 允许 text 为空
|
|
|
|
|
|
"alter table chat_history alter plain_text drop not null;", # 允许 plain_text 为空
|
2023-04-14 11:45:45 +08:00
|
|
|
|
"ALTER TABLE chat_history RENAME COLUMN user_qq TO user_id;", # 将user_id改为user_id
|
|
|
|
|
|
"ALTER TABLE chat_history ALTER COLUMN user_id TYPE character varying(255);",
|
|
|
|
|
|
"ALTER TABLE chat_history ALTER COLUMN group_id TYPE character varying(255);",
|
2023-03-01 23:24:56 +08:00
|
|
|
|
]
|