
    j                         d dl mZmZ d dlmZ d dlmZ dedefdZdde	de
fd	Zdde	dee
         fd
Zdde	dee
         fdZdS )    )build_age_exists_clauseget_age_filter_threshold)logger	query_all
age_clausereturnc                     d|  dS )Na  
        SELECT TO_CHAR(a."ajxx_fasj", 'YYYY-MM') AS month,
               COUNT(DISTINCT a."ajxx_ajbh") AS count
        FROM "ywdata"."zq_zfba_ajxx" a
        WHERE a."ajxx_fasj" >= CURRENT_DATE - make_interval(months => %(months)s)
          AND a."ajxx_fasj" IS NOT NULL
          zO
        GROUP BY TO_CHAR(a."ajxx_fasj", 'YYYY-MM')
        ORDER BY month
     )r   s    J/home/longshao/multi-rider-rag/modules/dashboard/services/trend_service.py_case_trend_sqlr      s     	 	 	 	 	       monthsc                 V   d| i}t                      dk    r t          t          d          |          ddS t          t          t          dd                    |          }|r|ddS t	          j        dt          |                     t          t          d          |          d	dS )
Nr   r    F)pointsdegradedaxz7Case trend fallback triggered, primary returned %d rowsT)r   r   r   r   r   infolen)r   paramsrowss      r   get_case_trendr      s    F!!Q&&#OB$7$7@@eTTT_%<S#%F%FGGPPD 3E222
KI3t99UUU 3 3V<<$OOOr   c                 *    d}t          |d| i          S )NaP  
        SELECT TO_CHAR(calc_time, 'YYYY-MM') AS month,
               COUNT(DISTINCT zjhm) AS count
        FROM "jcgkzx_monitor"."wcnr_score_history"
        WHERE calc_time >= CURRENT_DATE - make_interval(months => %(months)s)
          AND total_score >= 60
        GROUP BY TO_CHAR(calc_time, 'YYYY-MM')
        ORDER BY month
    r   r   r   sqls     r   get_person_trendr      s     C S8V,---r   c                 *    d}t          |d| i          S )Na:  
        SELECT TO_CHAR(calc_time, 'YYYY-MM') AS month,
               ROUND(AVG(total_score), 1) AS avg_score
        FROM "jcgkzx_monitor"."wcnr_score_history"
        WHERE calc_time >= CURRENT_DATE - make_interval(months => %(months)s)
        GROUP BY TO_CHAR(calc_time, 'YYYY-MM')
        ORDER BY month
    r   r   r   s     r   get_score_trendr!   ,   s     C S8V,---r   N)r   )shared.age_filterr   r   shared.config.configr   shared.db.kingbaser   strr   intdictr   listr   r!   r   r   r   <module>r)      s    O O O O O O O O ' ' ' ' ' ' ( ( ( ( ( (
 
 
 
 
 
	P 	P3 	P 	P 	P 	P 	P
. 
.S 
.$t* 
. 
. 
. 
.	. 	.C 	.d 	. 	. 	. 	. 	. 	.r   