
    jJ8                     p   d dl Z d dlZd dlZd dlmZ d dlmZ dej        fdZde	dedefdZ
d	ej        dz  dee	ef         dz  fd
Zd	ej        dz  dee	ef         dz  fdZd	ej        dz  dee	ef         dz  fdZde	dee	ef         dz  fdZdee	ef         ddfdZdde	de	dedeee	ef                  fdZde	dee	ef         dz  fdZdee	ef         ddfdZdee	ef         ddfdZd de	de	dedeee	ef                  fdZdee	ef         ddfdZd de	de	dedeee	ef                  fdZdS )!    N)Any)SQLITE_DB_PATHreturnc                      t           j                            t                    } | rt          j        | d           t          j        t                    }t
          j        |_        |S )NT)exist_ok)	ospathdirnamer   makedirssqlite3connectRowrow_factory)parentconns     I/home/longshao/multi-rider-rag/modules/dispatch/services/store_service.py_connectr   	   sP    W__^,,F +
FT****?>**D{DK    valuedefaultc                     t          | pd                                          }|s|S 	 t          j        |          S # t          $ r |cY S w xY w)N )strstripjsonloads	Exception)r   r   raws      r   _parse_jsonr      sc    
ekr


 
 
"
"C z#   s   = AArowc                 (    | d S t          |           S N)dict)r    s    r   _row_to_auth_sessionr$      s    
{t99r   c                 f   | d S t          |           }t          |                    d                    |d<   t          |                    d          pdi           |d<   t          |                    d          pdi           |d<   t          |                    d          pdi           |d<   |S )	Nis_mockdraft_payload_jsonr   draft_payloadidentity_payload_jsonidentity_payloaddispatch_response_jsondispatch_response)r#   boolgetr   r    items     r   _row_to_queue_itemr1   "   s    
{t99D488I..//DO'1E(F(F(L"bQQD*4884K+L+L+RPRTVWWD	 +DHH5M,N,N,TRTVX Y YD	Kr   c                     | d S t          |           }t          |                    d          pdi           |d<   t          |                    d          pdi           |d<   |S )Nrequest_payload_jsonr   request_payloadresponse_payload_jsonresponse_payload)r#   r   r.   r/   s     r   _row_to_recordr7   -   sm    
{t99D)$((3I*J*J*PbRTUUD	*4884K+L+L+RPRTVWWD	Kr   	owner_keyc                     t                      5 }|                    d| f                                          }d d d            n# 1 swxY w Y   t          |          S )Nz@SELECT * FROM dispatch_auth_sessions WHERE owner_key = ? LIMIT 1)r   executefetchoner$   )r8   r   r    s      r   get_dispatch_auth_sessionr<   6   s    	 tllNL
 
 (** 	              
  $$$   *AA	A	session_itemc                 
   |                      dd          |                      dd          |                      dd          |                      dd          |                      dd          |                      dd          t          |                      d	          pd
          |                      d          |                      d          t          |                      d          pd
          |                      dd          |                      d          rdnd
|                      dd          d}t                      5 }|                    d|           |                                 d d d            d S # 1 swxY w Y   d S )Nr8   r   owner_ipusernameaccess_tokenrefresh_token
token_typeBearer
expires_inr   
expires_atauthenticated_ts
updated_tsstatuspendingr&      
last_error)r8   r@   rA   rB   rC   rD   rF   rG   rH   rI   rJ   r&   rM   ax  
            INSERT INTO dispatch_auth_sessions (
                owner_key, owner_ip, username, access_token, refresh_token, token_type,
                expires_in, expires_at, authenticated_ts, updated_ts, status, is_mock, last_error
            )
            VALUES (
                :owner_key, :owner_ip, :username, :access_token, :refresh_token, :token_type,
                :expires_in, :expires_at, :authenticated_ts, :updated_ts, :status, :is_mock, :last_error
            )
            ON CONFLICT(owner_key) DO UPDATE SET
                owner_ip = excluded.owner_ip,
                username = excluded.username,
                access_token = excluded.access_token,
                refresh_token = excluded.refresh_token,
                token_type = excluded.token_type,
                expires_in = excluded.expires_in,
                expires_at = excluded.expires_at,
                authenticated_ts = excluded.authenticated_ts,
                updated_ts = excluded.updated_ts,
                status = excluded.status,
                is_mock = excluded.is_mock,
                last_error = excluded.last_error
            )r.   intr   r:   commit)r>   payloadr   s      r   save_dispatch_auth_sessionrQ   ?   s   !%%k266 $$Z44 $$Z44$((<<%))/2>>"&&|X>>,**<88=A>>"&&|44(,,-?@@,**<88=A>>""8Y77$((33:11"&&|R88 G 
 t. 1	
 	
 	
4 	7                 s    +E88E<?E<d   r@   limitc                    t          dt          t          |pd          d                    }t                      5 }|                    d| ||f                                          }d d d            n# 1 swxY w Y   d |D             S )NrL   rR   i  z
            SELECT *
            FROM dispatch_queue
            WHERE owner_key = ?
               OR (COALESCE(owner_key, '') = '' AND owner_ip = ?)
            ORDER BY updated_ts DESC, created_ts DESC, id DESC
            LIMIT ?
            c                 0    g | ]}|t          |          S r"   )r1   .0r    s     r   
<listcomp>z'list_dispatch_queue.<locals>.<listcomp>{   s"    GGGss##r   maxminrN   r   r:   fetchallr8   r@   rS   
safe_limitr   rowss         r   list_dispatch_queuer`   m   s    QC--s3344J	 t|| *-

 

 (** 	               HGtGGGG   ,A44A8;A8queue_idc                     t                      5 }|                    d| f                                          }d d d            n# 1 swxY w Y   t          |          S )Nz1SELECT * FROM dispatch_queue WHERE id = ? LIMIT 1)r   r:   r;   r1   )rb   r   r    s      r   get_dispatch_queue_itemrd   ~   s    	 tll?K
 
 (** 	              
 c"""r=   r0   c                    i d|                      dd          d|                      dd          d|                      dd          d|                      dd          d|                      dd          d|                      dd          d|                      dd          d	|                      d	d          d
|                      d
d          dt          |                      d          pd          d|                      dd          d|                      dd          d|                      dd          dt          |                      d          pd          d|                      dd          d|                      dd          d|                      dd          i d|                      dd          d|                      dd          d|                      dd          d|                      dd          d|                      dd          d|                      dd          d|                      dd          d|                      dd          d|                      dd          d|                      dd          dt          j        |                      d           pi d!"          d#t          j        |                      d$          pi d!"          d%t          j        |                      d&          pi d!"          d'|                      d'd          d(t          |                      d(          pd          d)t          |                      d)          pd          }t                      5 }|                    d*|           |                                 d d d            d S # 1 swxY w Y   d S )+Nidr   r8   r@   source_job_idsource_asset_idsource_job_typesource_namesource_type
asset_name
face_indexr   person_nameperson_id_noperson_phonesimilarity_scoreillegal_typesssj_dmsssj_mcssfj_dmssfj_mczbpcs_dmzbpcs_mcdzmcrwdyidsjcslydispatch_statusrK   
sms_statusrM   r'   r(   Fensure_asciir)   r*   r+   r,   sms_preview
created_tsrI   a7  
            INSERT INTO dispatch_queue (
                id, owner_key, owner_ip, source_job_id, source_asset_id, source_job_type, source_name, source_type,
                asset_name, face_index, person_name, person_id_no, person_phone, similarity_score, illegal_type,
                sssj_dm, sssj_mc, ssfj_dm, ssfj_mc, zbpcs_dm, zbpcs_mc, dzmc,
                rwdyid, sjcsly, dispatch_status, sms_status, last_error,
                draft_payload_json, identity_payload_json, dispatch_response_json, sms_preview,
                created_ts, updated_ts
            )
            VALUES (
                :id, :owner_key, :owner_ip, :source_job_id, :source_asset_id, :source_job_type, :source_name, :source_type,
                :asset_name, :face_index, :person_name, :person_id_no, :person_phone, :similarity_score, :illegal_type,
                :sssj_dm, :sssj_mc, :ssfj_dm, :ssfj_mc, :zbpcs_dm, :zbpcs_mc, :dzmc,
                :rwdyid, :sjcsly, :dispatch_status, :sms_status, :last_error,
                :draft_payload_json, :identity_payload_json, :dispatch_response_json, :sms_preview,
                :created_ts, :updated_ts
            )
            ON CONFLICT(id) DO UPDATE SET
                owner_key = excluded.owner_key,
                owner_ip = excluded.owner_ip,
                source_job_id = excluded.source_job_id,
                source_asset_id = excluded.source_asset_id,
                source_job_type = excluded.source_job_type,
                source_name = excluded.source_name,
                source_type = excluded.source_type,
                asset_name = excluded.asset_name,
                face_index = excluded.face_index,
                person_name = excluded.person_name,
                person_id_no = excluded.person_id_no,
                person_phone = excluded.person_phone,
                similarity_score = excluded.similarity_score,
                illegal_type = excluded.illegal_type,
                sssj_dm = excluded.sssj_dm,
                sssj_mc = excluded.sssj_mc,
                ssfj_dm = excluded.ssfj_dm,
                ssfj_mc = excluded.ssfj_mc,
                zbpcs_dm = excluded.zbpcs_dm,
                zbpcs_mc = excluded.zbpcs_mc,
                dzmc = excluded.dzmc,
                rwdyid = excluded.rwdyid,
                sjcsly = excluded.sjcsly,
                dispatch_status = excluded.dispatch_status,
                sms_status = excluded.sms_status,
                last_error = excluded.last_error,
                draft_payload_json = excluded.draft_payload_json,
                identity_payload_json = excluded.identity_payload_json,
                dispatch_response_json = excluded.dispatch_response_json,
                sms_preview = excluded.sms_preview,
                created_ts = excluded.created_ts,
                updated_ts = excluded.updated_ts
            )r.   rN   floatr   dumpsr   r:   rO   )r0   rP   r   s      r   upsert_dispatch_queue_itemr      sy   "dhhtR  "TXXk2.." 	DHHZ,," 	/266	"
 	488$5r::" 	488$5r::" 	txxr22" 	txxr22" 	dhh|R00" 	c$((<005A66" 	txxr22" 	44" 	44" 	E$((+=">">"C!DD" 	44"  	488Ir**!"" 	488Ir**#" "$ 	488Ir**%"& 	488Ir**'"( 	DHHZ,,)"* 	DHHZ,,+", 	$$-". 	$((8R((/"0 	$((8R((1"2 	488$5yAA3"4 	dhh|Y775"6 	dhh|R007"8 	dj/)B)B)HbW\]]]9": 	 DHH5G,H,H,NB]b!c!c!c;"< 	!$*TXX6I-J-J-Pb_d"e"e"e="> 	txxr22?"@ 	c$((<005A66A"B 	c$((<005A66C" "GF 
 7t2f i5	
 5	
 5	
l 	o7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7s   +N99N= N=recordc                    |                      dd          |                      dd          |                      dd          |                      dd          |                      dd          t          j        |                      d          pi d	
          t          j        |                      d          pi d	
          |                      dd          t          |                      d          pd          d	}t	                      5 }|                    d|           |                                 d d d            d S # 1 swxY w Y   d S )Nrf   r   rb   r8   r@   rJ   rK   r4   Fr~   r6   error_messager   r   )	rf   rb   r8   r@   rJ   r3   r5   r   r   am  
            INSERT INTO dispatch_records (
                id, queue_id, owner_key, owner_ip, status,
                request_payload_json, response_payload_json, error_message, created_ts
            )
            VALUES (
                :id, :queue_id, :owner_key, :owner_ip, :status,
                :request_payload_json, :response_payload_json, :error_message, :created_ts
            )
            ON CONFLICT(id) DO UPDATE SET
                queue_id = excluded.queue_id,
                owner_key = excluded.owner_key,
                owner_ip = excluded.owner_ip,
                status = excluded.status,
                request_payload_json = excluded.request_payload_json,
                response_payload_json = excluded.response_payload_json,
                error_message = excluded.error_message,
                created_ts = excluded.created_ts
            r.   r   r   rN   r   r:   rO   r   rP   r   s      r   save_dispatch_recordr      sv   jjr""JJz2..ZZR00JJz2..**Xy11 $
6::6G+H+H+NB]b c c c!%FJJ7I,J,J,Pb_d!e!e!eOR88&**\227a88
 
G 
 t& )	
 	
 	
, 	/                 s   +D??EE   c                    t          dt          t          |pd          d                    }t                      5 }|                    d| ||f                                          }d d d            n# 1 swxY w Y   d |D             S )NrL   r      z
            SELECT *
            FROM dispatch_records
            WHERE owner_key = ?
               OR (COALESCE(owner_key, '') = '' AND owner_ip = ?)
            ORDER BY created_ts DESC, id DESC
            LIMIT ?
            c                 0    g | ]}|t          |          S r"   r7   rV   s     r   rX   z)list_dispatch_records.<locals>.<listcomp>  !    CCCC3?N3???r   rY   r]   s         r   list_dispatch_recordsr         QC,,c2233J	 t|| *-

 

 (** 	               DC4CCCCra   c                    |                      dd          |                      dd          |                      dd          |                      dd          |                      dd          |                      dd          |                      dd	          t          j        |                      d
          pi d          t          j        |                      d          pi d          |                      dd          t          |                      d          pd          d}t	                      5 }|                    d|           |                                 d d d            d S # 1 swxY w Y   d S )Nrf   r   rb   r8   r@   mobilecontentrJ   rK   r4   Fr~   r6   r   r   r   )rf   rb   r8   r@   r   r   rJ   r3   r5   r   r   a  
            INSERT INTO dispatch_sms_records (
                id, queue_id, owner_key, owner_ip, mobile, content, status,
                request_payload_json, response_payload_json, error_message, created_ts
            )
            VALUES (
                :id, :queue_id, :owner_key, :owner_ip, :mobile, :content, :status,
                :request_payload_json, :response_payload_json, :error_message, :created_ts
            )
            ON CONFLICT(id) DO UPDATE SET
                queue_id = excluded.queue_id,
                owner_key = excluded.owner_key,
                owner_ip = excluded.owner_ip,
                mobile = excluded.mobile,
                content = excluded.content,
                status = excluded.status,
                request_payload_json = excluded.request_payload_json,
                response_payload_json = excluded.response_payload_json,
                error_message = excluded.error_message,
                created_ts = excluded.created_ts
            r   r   s      r   save_dispatch_sms_recordr     s   jjr""JJz2..ZZR00JJz2..**Xr**::i,,**Xy11 $
6::6G+H+H+NB]b c c c!%FJJ7I,J,J,Pb_d!e!e!eOR88&**\227a88 G 
 t* -	
 	
 	
0 	3                 s   1+E))E-0E-c                    t          dt          t          |pd          d                    }t                      5 }|                    d| ||f                                          }d d d            n# 1 swxY w Y   d |D             S )NrL   r   r   z
            SELECT *
            FROM dispatch_sms_records
            WHERE owner_key = ?
               OR (COALESCE(owner_key, '') = '' AND owner_ip = ?)
            ORDER BY created_ts DESC, id DESC
            LIMIT ?
            c                 0    g | ]}|t          |          S r"   r   rV   s     r   rX   z-list_dispatch_sms_records.<locals>.<listcomp>T  r   r   rY   r]   s         r   list_dispatch_sms_recordsr   F  r   ra   )rR   )r   )r   r   r   typingr   shared.config.configr   
Connectionr   r   r   r   r#   r$   r1   r7   r<   rQ   rN   listr`   rd   r   r   r   r   r    r   r   <module>r      s:    				        / / / / / /'$    s S S    gkD0 T#s(^d5J    GK$. 4S>D3H    d* tCH~/D    % %c3h$1F % % % %+T#s(^ + + + + +\H H3 H# Hc HDQUVY[^V^Q_L` H H H H"#c #d38nt.C # # # #[T#s(^ [ [ [ [ [|#c3h #D # # # #LD DS DC D DTRVWZ\_W_R`Ma D D D D"'T#s(^ ' ' ' ' 'TD D D DC DQUVZ[^`c[cVdQe D D D D D Dr   