
    	,h|                         d dl mZ d dlmZ d dlmc mc 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	      Zy)
    )HTTPException)SessionN)Messages)CustomerLocationBaseTblCustomerLocation)%calculate_total_household_consumption.fill_and_calculate_total_household_consumption)CustomResponsec                       e Zd ZdefdZdej                  fdZdefdZ	defdZ
ded	ej                  fd
Zdej                  fdZdefdZy)Customer_locationdbc                      || _         || _        y N)r   token)selfr   r   s      </var/www/html/RetailAPI/app/api/customer_location/service.py__init__zCustomer_location.__init__
   s    
    requestc                   K   t        j                  |j                               }t        |      |_        t        j                  || j                         | j                  j                          t        dt        j                        S w)N1statusmessage)r   model_validate
model_dumpr   %total_household_consumption_per_monthr   creater   commitr
   r   CUSTOMER_CREAT)r   r   created_users      r   create_customer_locationz*Customer_location.create_customer_location   sb     +::7;M;M;OP=bco=p:""<9S(2I2IJJs   B	Bcustomer_location_idc                    K   t        j                  || j                        }|st        dt        j
                        t        j                  j                  |      S w)N  status_codedetail)	r   	get_by_idr   r   r   CUSTOMET_NOT_FOUNDschemascustomerLocationResponser   )r   r#   	catchments      r   get_customer_locationz'Customer_location.get_customer_location   sJ     '112FP	Cx7R7RSS//>>yIIs   AAgroup_idc                   K   | j                   j                  t              j                  t        j                  |k(        j                         }|D cg c]!  }t        j                  j                  |      # c}S c c}w wr   )	r   queryr   filterr/   allr+   r,   r   )r   r/   customer_locationslocs       r   get_customer_locations_by_groupz1Customer_location.get_customer_locations_by_group   sf     "ggmm,?@GGH[HdHdhpHpquuwPbcPb00??DPbcccs   AB&A=:Breturnc                 0  K   | j                   j                  t              j                  t        j                  |k(        j                         }|st        dt        j                        t        d |D              }t        j                  ||      S w)Nr%   r&   c              3   N   K   | ]  }|j                   |j                  z    y wr   )shopping_frequency_per_monthhousehold_consumption_per_month).0r5   s     r   	<genexpr>zACustomer_location.get_total_potential_by_group.<locals>.<genexpr>$   s%     |r{kn @ @3CfCf fr{s   #%)r/   r   )r   r1   r   r2   r/   r3   r   r   CUSTOMER_GROUP_NOT_FOUNDsumr+   TotalConsumptionResponse)r   r/   	locationstotal_consumptions       r   get_total_potential_by_groupz.Customer_location.get_total_potential_by_group    sv     WW]]#67>>?R?[?[_g?ghlln	C8Y8YZZ|r{||//hyzzs   BBc                   K   |j                   t        dt        j                        t	        j
                  |j                   | j                        }|st        dt        j                        |j                  d      }t        ||      }t        di |}|j                   J t	        j                  |j                   || j                         | j                  j                          t        dt        j                        S w)Nr%   r&   T)exclude_unsetr   r    )r#   r   r   CUSTOMER_ID_REQUIREDr   r)   r   r*   r   r	   r   updater   r
   CUSTOMER_UPDATE)r   r   existingupdate_datavalidated_models        r   update_customer_locationz*Customer_location.update_customer_location'   s     ''/C8U8UVV&001M1MtwwWC8S8STT((t(<D[RZ[.==33???""?#G#GZ^ZaZabS(2J2JKKs   DDc                    K   t        j                  || j                        }|st        dt        j
                        t        dt        j                        S w)Nr%   r&   r   r   )r   deleter   r   r   r*   r
   CUSTOMER_DELETED)r   r#   deleteds      r   delete_customer_locationz*Customer_location.delete_customer_location5   sF     %,,-A477KCx7R7RSSS(2K2KLLs   AAN)__name__
__module____qualname__r   r   r+   customer_location_creatr"   intr.   r6   r@   rC   customerLocationUpdaterM   rR   rF   r   r   r   r   	   s|    7 Kg6U6U KJ Jdc d{3 {7CcCc {Lg6T6T LM3 Mr   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   rF   r   r   <module>rb      s)    ! " 3 3 ( V x 20M 0Mr   