o
    Uh7                     @   s   d dl mZ d dlmZ d dlm  m  mZ d dlm	Z	 d dl
mZ d dlmZmZ d dlmZmZ d dlmZ G d	d
 d
ZdS )    )HTTPException)SessionN)JWTPayloadSchema)Messages)CatchmentPotentialBaseTblCatchmentPotential)3calculate_potential_number_of_household_consumption'fill_and_calculate_potential_households)CustomResponsec                   @   s|   e Zd ZdedefddZdejfddZde	d	ej
fd
dZde	fddZdejfddZde	fddZde	fddZdS )Catchment_Potentialdbtokenc                 C   s   || _ || _d S N)r   r   )selfr   r    r   >/var/www/html/RetailAPI/app/api/catchment_potential/service.py__init__   s   
zCatchment_Potential.__init__requestc                    s@   t | }t||_t|| j | j  t	dt
jdS )N1statusmessage)r   model_validate
model_dumpr   potential_number_of_householdsr   creater   commitr
   r   CATCHMENT_POTENTIAL_CREAT)r   r   created_userr   r   r   create_catchment_potential   s   

z.Catchment_Potential.create_catchment_potentialgroup_idreturnc                    sT   | j ttj|k }|stdtjdt	dd |D }t
j|t|dS )N  status_codedetailc                 s   s    | ]	}|j |j V  qd S r   )number_of_householdspercentage_of_segment.0locr   r   r   	<genexpr>   s    zECatchment_Potential.get_total_consumption_by_group.<locals>.<genexpr>)r    r   )r   queryr   filterr    allr   r   CATCHMENT_NOT_FOUNDsumschemasTotalPotentialResponseint)r   r    	locationstotal_consumptionr   r   r   get_total_consumption_by_group   s   z2Catchment_Potential.get_total_consumption_by_groupcatch_idc                    s.   t || j}|stdtjdtj|S )Nr"   r#   )	r   	get_by_idr   r   r   r/   r1   GetPotentialResponser   )r   r7   	catchmentr   r   r   get_catchment   s
   z!Catchment_Potential.get_catchmentc                    s   |j d u rtdtjdt|j | j}|stdtjd|jdd}t	||}t
di |}|j d us7J t|j || j tdtjdS )Nr"   r#   T)exclude_unsetr   r   r   )r7   r   r   CATCHMENT_ID_REQUIREDr   r8   r   r/   r   r	   r   updater
   CATCHMENT_UPDATE)r   r   existingupdate_dataupdated_datavalidated_modelr   r   r   update_catchment#   s   

z$Catchment_Potential.update_catchmentc                    s,   | j ttj|k }dd |D S )Nc                 S   s   g | ]}t j|qS r   )r1   catchmentPotentialResponser   r(   r   r   r   
<listcomp>2   s    zHCatchment_Potential.get_catchment_poteneial_by_group.<locals>.<listcomp>)r   r,   r   r-   r    r.   )r   r    r   r   r   r    get_catchment_poteneial_by_group0   s   z4Catchment_Potential.get_catchment_poteneial_by_groupc                    s0   t || j}|stdtjdtdtjdS )Nr"   r#   r   r   )r   deleter   r   r   r/   r
   CATCHMENT_DELETED)r   r7   deletedr   r   r   delete_catchment4   s
   z$Catchment_Potential.delete_catchmentN)__name__
__module____qualname__r   r   r   r1   catchmentPotentialCreatr   r3   r2   r6   r;   UpdatePotentialrD   rG   rK   r   r   r   r   r   
   s    r   )fastapir   sqlalchemy.ormr   #app.api.catchment_potential.schemasapicatchment_potentialr1   app.dependency.authanticationr   app.locale.messagesr   #app.models.main.catchment_potentialr   r   app.utils.common_utilsr   r	   app.utils.schemas_utilsr
   r   r   r   r   r   <module>   s    