o
    jjy.                     @   s*   d dl m Z mZ d dlZG dd dZdS )    )datetime	timedeltaNc                   @   s   e Zd ZdejfddZd3ddZdd Zd	d
 Zdd Z	d4ddZ
d4ddZd4ddZd4ddZdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd5d'd(Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 ZdS )6CheckinManagerconnc                 C   s   || _ | | _d S N)r   cursorcur)selfr    r
   ,/home/dore/onebot/Bot_Ero/core/db/checkin.py__init__   s   zCheckinManager.__init__Nc                 C   s<   t  d}|D ]}| jd||||f q	| j  d S )N%Y-%m-%d %H:%M:%Sz\INSERT INTO checkin_records (user_id, checkin_date, content, message_id) VALUES (?, ?, ?, ?))r   nowstrftimer   executer   commit)r	   user_idimages
message_id	today_strimgr
   r
   r   insert
   s   
zCheckinManager.insertc                 C   sT   t dD ]}t|dt|d d}t| | jd||df q| j	  d S )N   %Y-%m-%ddays%Y-%m-%d 12:00:00MINSERT INTO checkin_records (user_id, checkin_date, content) VALUES (?, ?, ?)remedy_checkin)
ranger   strptimer   r   printr   r   r   r   )r	   r   	day_starticheckin_dater
   r
   r   remedy_week   s   zCheckinManager.remedy_weekc                 C   s4   t |dd}| jd||df | j  d S )Nr   r   r   r   )r   r    r   r   r   r   r   )r	   r   day_strr$   r
   r
   r   
remedy_day   s   zCheckinManager.remedy_dayc                 C   s6   | d}| d}| j d|||f | j  }|S )Nz-01-01 00:00:00z-12-31 23:59:59z
            SELECT * FROM checkin_records
            WHERE user_id = ?
            AND checkin_date BETWEEN ? AND ?
            ORDER BY checkin_date DESC
            r   r   fetchall)r	   r   year
start_dateend_daterowsr
   r
   r   search_year%   s   


	zCheckinManager.search_year c                 C   s    | j d||f | j  }|S )Nz
            SELECT * FROM checkin_records
            WHERE user_id = ?
            ORDER BY checkin_date DESC
            LIMIT ?
            r(   )r	   r   limitr-   r
   r
   r   
search_all4   s   
	zCheckinManager.search_allc                 C   s   | j d|f | j  }|S )Nzr
            SELECT * FROM checkin_records
            ORDER BY checkin_date DESC
            LIMIT ?
            r(   )r	   r0   r-   r
   r
   r   all_recordsA   s   
zCheckinManager.all_recordsc                 C   s"   | j d|||f | j  }|S )Nz
        SELECT * FROM checkin_records
        WHERE checkin_date BETWEEN ? AND ?
        ORDER BY checkin_date DESC
        LIMIT ?
        r(   )r	   r+   r,   r0   r-   r
   r
   r   search_rangeM   s
   
zCheckinManager.search_rangec                 C   s$   | j d||||f | j  }|S )Nz
        SELECT * FROM checkin_records
        WHERE user_id = ?
        AND checkin_date BETWEEN ? AND ?
        ORDER BY checkin_date DESC
        LIMIT ?
        r(   )r	   r   r+   r,   r0   r-   r
   r
   r   search_user_rangeW   s
   

z CheckinManager.search_user_rangec                 C   s   | j d|f | j  d S )NzJ
            DELETE FROM checkin_records
            WHERE id = ?
        )r   r   r   r   )r	   	target_idr
   r
   r   deleteb   s   zCheckinManager.deletec                 C   s   | j d||f | j  S )Nz
            SELECT * FROM checkin_records
            WHERE user_id = ? AND message_id = ?
            ORDER BY id ASC
            r(   )r	   r   r   r
   r
   r   
get_by_msgi   s
   
zCheckinManager.get_by_msgc                 C   s(   | j d||f | j j}| j  |S )Nzf
            DELETE FROM checkin_records
            WHERE user_id = ? AND message_id = ?
            )r   r   rowcountr   r   )r	   r   r   nr
   r
   r   delete_by_msgt   s   
zCheckinManager.delete_by_msgc                 C   sP   | d}t |dtdd }|d}| jdt|||f | j d uS )Nz	 00:00:00r      r   z%Y-%m-%d 00:00:00z
            SELECT 1
            FROM checkin_records
            WHERE user_id = ?
            AND checkin_date >= ?
            AND checkin_date < ?
            LIMIT 1
        )r   r    r   r   r   r   intfetchone)r	   r   date_strstartend_dtendr
   r
   r   has_on_date   s   

zCheckinManager.has_on_datec                    s  d | j  }|d|f | }|sdddddS t fdd|D }d}d}tdt|D ]}|| ||d  tdd krL|d7 }t||}q1d}q1|d	 }d}	tt|d
 d	d	D ]}|| |tdd kru|	d7 }	|| }q_ tdd |D }
d}d}tdt|
D ]>}|
|d  \}}|
| \}}t	
||d }|tdd }| d d
 \}}||f||fkr|d7 }t||}qd}q|
d	 \}}d}t	
||d }tt|
d
 d	d	D ]#}|tdd }| d d
 \}}|
| ||fkr|d7 }|}q ||	||dS )Nr   z
            SELECT checkin_date
            FROM checkin_records
            WHERE user_id = ?
            ORDER BY checkin_date ASC
        r   )longest_dailycurrent_dailylongest_weeklycurrent_weeklyc                    s*   h | ]}t |d   tdd  qS )r      )hours)r   r    r   date).0rowDATE_FORMATr
   r   	<setcomp>   s    z)CheckinManager.streaks.<locals>.<setcomp>r;   r      c                 S   s    h | ]}|  j|  jfqS r
   )isocalendarr*   week)rJ   dr
   r
   r   rN      s    )weeks)r   r   r   r)   sortedr   lenr   maxr   fromisocalendarrI   rQ   )r	   r   r   r-   datesrC   rD   r#   todaycurrent_daily_realrT   rE   rF   	prev_year	prev_week	curr_year	curr_week	prev_datenext_week_date	next_year	next_week	last_year	last_weekcurrent_weekly_real	test_dateexpected_yearexpected_weekr
   rL   r   streaks   sv   

zCheckinManager.streaksc                 C   sF   | j dt|||f | j  }|d u s|d d u rdS t|d S )Nz
            SELECT COUNT(DISTINCT substr(datetime(checkin_date, '-8 hours'), 1, 10))
            FROM checkin_records
            WHERE user_id = ?
            AND checkin_date >= ?
            AND checkin_date < ?
        r   r   r   r<   r=   )r	   r   start_datetime_strend_datetime_strrK   r
   r
   r   
count_days   s
   
$zCheckinManager.count_daysc                 C   sB   | j dt|f | j  }|d u s|d d u rdS t|d S )Nz
            SELECT COUNT(DISTINCT substr(checkin_date, 1, 10))
            FROM checkin_records
            WHERE user_id = ?
        r   rk   )r	   r   rK   r
   r
   r   count_all_days   s
   
$zCheckinManager.count_all_daysc                 C   s<   | j dt|t|f | j  }|d u rdS t|d S )Nzu
            SELECT used_count
            FROM user_remedy_usage
            WHERE year = ? AND user_id = ?
        r   rk   )r	   r*   r   rK   r
   r
   r   remedy_used   s
   
zCheckinManager.remedy_usedr;   c                 C   s.   | j dt|t|t|f | j  d S )Nz
            INSERT INTO user_remedy_usage (year, user_id, used_count)
            VALUES (?, ?, ?)
            ON CONFLICT(year, user_id) DO UPDATE SET used_count = used_count + excluded.used_count
        )r   r   r<   r   r   )r	   r*   r   incr
   r
   r   add_remedy_used  s   zCheckinManager.add_remedy_usedc                 C   s@   | j dt|t|t df | j jdk}| j	  |S )Nz
            INSERT OR IGNORE INTO user_weekly_streak_reward_claims (user_id, week_start, claimed_at)
            VALUES (?, ?, ?)
        r   r   
r   r   r<   strr   r   r   r8   r   r   )r	   r   
week_startokr
   r
   r   claim_weekly  s   
zCheckinManager.claim_weeklyc              
   C   sL   | j dt|t|t|t|t df | j jdk}| j	  |S )Nz
            INSERT OR IGNORE INTO user_attendance_reward_claims (user_id, reward_type, period_key, points, claimed_at)
            VALUES (?, ?, ?, ?, ?)
        r   r   rs   )r	   r   reward_type
period_keypointsrv   r
   r
   r   claim_attendance  s   

zCheckinManager.claim_attendancec                 C   sl   | j dt|t|t|f | j  }|sdS t|d }| j dt|t|t|f | j  |S )Nz
            SELECT points
            FROM user_attendance_reward_claims
            WHERE user_id = ? AND reward_type = ? AND period_key = ?
            LIMIT 1
        r   z
            DELETE FROM user_attendance_reward_claims
            WHERE user_id = ? AND reward_type = ? AND period_key = ?
        r   r   r<   rt   r=   r   r   )r	   r   rx   ry   rK   rz   r
   r
   r   revoke_attendance$  s   

z CheckinManager.revoke_attendancec                 C   s   | j dt|t|| df | j  }|d u s |d d u r"dnt|d }|dkr.dS | j dt|t|| df | j  |S )Nz
            SELECT COALESCE(SUM(points), 0)
            FROM user_attendance_reward_claims
            WHERE user_id = ? AND reward_type = ? AND period_key LIKE ?
        %r   z
            DELETE FROM user_attendance_reward_claims
            WHERE user_id = ? AND reward_type = ? AND period_key LIKE ?
        r|   )r	   r   rx   period_prefixrK   totalr
   r
   r   revoke_attendance_prefix6  s   
$
z'CheckinManager.revoke_attendance_prefixc                 C   s   | j dt|t|t|t|f | j  }|d u s"|d d u r$dnt|d }|dkr0dS | j dt|t|t|t|f | j  |S )Nz
            SELECT COALESCE(SUM(points), 0)
            FROM user_attendance_reward_claims
            WHERE user_id = ?
            AND reward_type = ?
            AND period_key >= ?
            AND period_key < ?
        r   z
            DELETE FROM user_attendance_reward_claims
            WHERE user_id = ?
            AND reward_type = ?
            AND period_key >= ?
            AND period_key < ?
        r|   )r	   r   rx   	start_keyend_keyrK   r   r
   r
   r   revoke_attendance_rangeG  s   
$
z&CheckinManager.revoke_attendance_ranger   )r/   )r;   )__name__
__module____qualname__sqlite3
Connectionr   r   r%   r'   r.   r1   r2   r3   r4   r6   r7   r:   rB   rj   rn   ro   rp   rr   rw   r{   r}   r   r   r
   r
   r
   r   r      s0    
	





Y	
		r   )r   r   r   r   r
   r
   r
   r   <module>   s    