o
    Uh|                     @   sz   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mZ d dlmZmZ d dlmZ G dd	 d	ZdS )
    )HTTPException)SessionN)Messages)CustomerLocationBaseTblCustomerLocation)%calculate_total_household_consumption.fill_and_calculate_total_household_consumption)CustomResponsec                   @   sx   e Z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
edejfddZdejfddZdefddZdS )Customer_locationdbc                 C   s   || _ || _d S N)r   token)selfr   r    r   </var/www/html/RetailAPI/app/api/customer_location/service.py__init__
   s   
zCustomer_location.__init__requestc                    s@   t | }t||_t|| j | j  t	dt
jdS )N1statusmessage)r   model_validate
model_dumpr   %total_household_consumption_per_monthr   creater   commitr	   r   CUSTOMER_CREAT)r   r   created_userr   r   r   create_customer_location   s   

z*Customer_location.create_customer_locationcustomer_location_idc                    s.   t || j}|stdtjdtj|S )N  status_codedetail)	r   	get_by_idr   r   r   CUSTOMET_NOT_FOUNDschemascustomerLocationResponser   )r   r   	catchmentr   r   r   get_customer_location   s
   z'Customer_location.get_customer_locationgroup_idc                    s,   | j ttj|k }dd |D S )Nc                 S   s   g | ]}t j|qS r   )r&   r'   r   .0locr   r   r   
<listcomp>   s    zECustomer_location.get_customer_locations_by_group.<locals>.<listcomp>)r   queryr   filterr*   all)r   r*   customer_locationsr   r   r   get_customer_locations_by_group   s   z1Customer_location.get_customer_locations_by_groupreturnc                    sP   | j ttj|k }|stdtjdt	dd |D }t
j||dS )Nr    r!   c                 s   s    | ]	}|j |j V  qd S r   )shopping_frequency_per_monthhousehold_consumption_per_monthr+   r   r   r   	<genexpr>$   s    zACustomer_location.get_total_potential_by_group.<locals>.<genexpr>)r*   r   )r   r/   r   r0   r*   r1   r   r   CUSTOMER_GROUP_NOT_FOUNDsumr&   TotalConsumptionResponse)r   r*   	locationstotal_consumptionr   r   r   get_total_potential_by_group    s   z.Customer_location.get_total_potential_by_groupc                    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 | j  tdtjdS )Nr    r!   T)exclude_unsetr   r   r   )r   r   r   CUSTOMER_ID_REQUIREDr   r$   r   r%   r   r   r   updater   r	   CUSTOMER_UPDATE)r   r   existingupdate_datavalidated_modelr   r   r   update_customer_location'   s   


z*Customer_location.update_customer_locationc                    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	   CUSTOMER_DELETED)r   r   deletedr   r   r   delete_customer_location5   s
   z*Customer_location.delete_customer_locationN)__name__
__module____qualname__r   r   r&   customer_location_creatr   intr)   r3   r:   r=   customerLocationUpdaterE   rI   r   r   r   r   r
   	   s    r
   )fastapir   sqlalchemy.ormr   !app.api.customer_location.schemasapicustomer_locationr&   app.locale.messagesr   !app.models.main.customer_locationr   r   app.utils.common_utilsr   r   app.utils.schemas_utilsr	   r
   r   r   r   r   <module>   s    