48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
from datetime import datetime
|
|||
|
|
|
||
|
|
from nonebot import require
|
||
|
|
|
||
|
|
require("nonebot_plugin_orm")
|
||
|
|
|
||
|
|
from nonebot_plugin_orm import Model, get_session
|
||
|
|
from sqlalchemy import BigInteger, Date, Integer, select
|
||
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
||
|
|
|
||
|
|
|
||
|
|
class CheckInRecord(Model):
|
||
|
|
"""打卡记录,一行一次打卡,同一天可多次"""
|
||
|
|
|
||
|
|
__tablename__ = "deer_pipe_checkin"
|
||
|
|
|
||
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||
|
|
uid: Mapped[int] = mapped_column(BigInteger, index=True)
|
||
|
|
date: Mapped[datetime.date] = mapped_column(Date, index=True)
|
||
|
|
|
||
|
|
|
||
|
|
# 记录信息
|
||
|
|
async def record_month(uid):
|
||
|
|
now = datetime.now()
|
||
|
|
async with get_session() as session:
|
||
|
|
session.add(CheckInRecord(uid=int(uid), date=now.date()))
|
||
|
|
await session.commit()
|
||
|
|
|
||
|
|
|
||
|
|
# 读取当月信息(返回当月所有打卡的日,同一天多次打卡会有重复项)
|
||
|
|
async def get_records(uid):
|
||
|
|
now = datetime.now()
|
||
|
|
month_start = datetime(now.year, now.month, 1).date()
|
||
|
|
if now.month == 12:
|
||
|
|
month_end = datetime(now.year + 1, 1, 1).date()
|
||
|
|
else:
|
||
|
|
month_end = datetime(now.year, now.month + 1, 1).date()
|
||
|
|
async with get_session() as session:
|
||
|
|
result = await session.execute(
|
||
|
|
select(CheckInRecord.date).where(
|
||
|
|
CheckInRecord.uid == int(uid),
|
||
|
|
CheckInRecord.date >= month_start,
|
||
|
|
CheckInRecord.date < month_end,
|
||
|
|
)
|
||
|
|
)
|
||
|
|
days = result.all()
|
||
|
|
return [row[0].day for row in days]
|