o
    niI                  
   @   s   d Z ddlmZmZmZmZ ddlZddlm	Z	 ddl
mZ edZdeeeef  defd	d
Z	ddedee deeeef  fddZdS )uy   
sales_sheet_data 테이블 접근.
1s(개인 시트) / 2s(팀 시트) 업로드 후 파싱된 레코드 저장·조회.
    )ListDictAnyOptionalN)logger)get_db_config
Sales_A_DBrecordsreturnc              
   C   sP  | sdS zt jjdi t}| }|d d}d}| D ]f}|d}|d}|d}|d}	|d}
|d	}|d
u sQ|d
u sQ|d
u sQ|	d
u sQ|
d
u rRq|d
urszt|tt	fsbt	|n|}W n t
tfyr   d
}Y nw ||||||	|
|f |d7 }q|  |  |  |W S  ty } z	td|   d
}~ww )u   
    sales_sheet_data 에 레코드 일괄 INSERT.
    uk_record (session_id, sheet_type, period_type, record_type, category) 기준 중복 시 value 갱신.
    r   SET time_zone = '+09:00'z
            INSERT INTO sales_sheet_data (session_id, sheet_type, period_type, record_type, category, value)
            VALUES (%s, %s, %s, %s, %s, %s)
            ON DUPLICATE KEY UPDATE value = VALUES(value), created_at = CURRENT_TIMESTAMP
        
session_id
sheet_typeperiod_typerecord_typecategoryvalueN   zSales sheet insert error:  )mysql	connectorconnectA_DB_CONFIGcursorexecuteget
isinstanceintfloat	TypeError
ValueErrorcommitclose	Exceptionr   error)r	   connr   sqlcountrr   r   r   r   r   r   er   r   ?/home/air/goalskill_sales/back/app/models/sales_sheet_module.pyinsert_sales_sheet_records   sD   






(
r*   r   r   c              
   C   s   z9t jjdi t}|jdd}|d |r"d}||| |f n	d}||| f | }|  |  |W S  tyU } zt	
d|  g W  Y d}~S d}~ww )	uo   
    session_id 기준으로 sales_sheet_data 조회.
    sheet_type 이 있으면 '1s' / '2s' 로 필터.
    T)
dictionaryr   a  
                SELECT id, session_id, sheet_type, period_type, record_type, category, value, created_at
                FROM sales_sheet_data
                WHERE session_id = %s AND sheet_type = %s
                ORDER BY sheet_type, period_type, category, record_type
            a
  
                SELECT id, session_id, sheet_type, period_type, record_type, category, value, created_at
                FROM sales_sheet_data
                WHERE session_id = %s
                ORDER BY sheet_type, period_type, category, record_type
            zSales sheet get error: Nr   )r   r   r   r   r   r   fetchallr!   r"   r   r#   )r   r   r$   r   r%   rowsr(   r   r   r)   get_sales_sheet_by_session7   s$   
r.   )N)__doc__typingr   r   r   r   mysql.connectorr   app.corer   app.core.configr   r   strr   r*   r.   r   r   r   r)   <module>   s    ,