o
    di>9                     @   s  d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
mZ d dlmZ d dlmZ d d	lmZmZ d d
lmZ dd Zdd Zdd Zdd Zdd Zdd Zd+ddZdd Zdd Zdd Zdd  Z d!d" Z!d#d$ Z"d%d& Z#d'd( Z$d)d* Z%dS ),    N)ObjectDoesNotExist)ContentFile)Q)User)ConnectorMasterNetworkOperatorMaster)models)StationMedia)AssetTypeConstantsFileFieldConstants)settingsc                 C   <   t | dkrg S dd | D } tjjj| djddd}|S )z
    Retrieve station IDs associated with a list of connector IDs.

    Args:
        connector_ids (list): A list of connector IDs to query.

    Returns:
        list: A list of station IDs that have the specified connector IDs.
    r   c                 S      g | ]
}|  rt|qS  isdigitint).0connector_idr   r   a/var/www/html/evchargy.com/django-station-ev/station_ev/apps/station/services/station_services.py
<listcomp>   
    z4get_station_ids_by_connector_ids.<locals>.<listcomp>)Zconnector__id__instation__idTflatlenstation_modelStationConnectorsobjectsfiltervalues_list)Zconnector_idsexisting_station_idsr   r   r    get_station_ids_by_connector_ids   s   
r#   c                 C   s,   t jjt| dt|d@ jddd}|S )aU  
    Retrieve station IDs associated with connectors within a specified kilowatt range.

    Args:
        min_kilowatts (float): The minimum kilowatt value.
        max_kilowatts (float): The maximum kilowatt value.

    Returns:
        list: A list of station IDs that have connectors with kilowatt values within the specified range.
    )Zkilowatts__gte)Zkilowatts__lter   Tr   )r   r   r   r    r   r!   )Zmin_kilowattsZmax_kilowattsr"   r   r   r   &get_station_ids_by_connector_kilowatts$   s   r$   c                 C   r   )z
    Retrieve station IDs associated with a list of location IDs.

    Args:
        location_ids (list): A list of location IDs to query.

    Returns:
        list: A list of station IDs that have the specified location IDs.
    r   c                 S   r   r   r   )r   location_idr   r   r   r   C   r   z3get_station_ids_by_location_ids.<locals>.<listcomp>)location__id__inr   Tr   )r   r   StationLocationr   r    r!   )location_idsr"   r   r   r   get_station_ids_by_location_ids6   s   
r)   c                 C   r   )a  
    Retrieve station IDs associated with a list of network operator IDs.

    Args:
        network_operator_ids (list): A list of network operator IDs to query.

    Returns:
        list: A list of station IDs that have the specified network operator IDs.
    r   c                 S   r   r   r   )r   network_operator_idr   r   r   r   ]   s    z;get_station_ids_by_network_operator_ids.<locals>.<listcomp>)Znetwork_operator__id__inr   Tr   r   )Znetwork_operator_idsr"   r   r   r   'get_station_ids_by_network_operator_idsM   s   r+   c                 C   r   )z
    Retrieve station IDs associated with a list of amenity IDs.

    Args:
        amenity_ids (list): A list of amenity IDs to query.

    Returns:
        list: A list of station IDs that have the specified amenity IDs.
    r   c                 S   r   r   r   )r   Z
amenity_idr   r   r   r   x   r   z2get_station_ids_by_amenity_ids.<locals>.<listcomp>)amenities__id__inr   Tr   )r   r   StationAmenitiesr   r    r!   )amenity_idsr"   r   r   r   get_station_ids_by_amenity_idsk   s   r/   c                 C   (   z	t jjj| dW S  ty   Y dS w )z
    Retrieve a station record by its ID.

    Args:
        station_id (int): The ID of the station to retrieve.

    Returns:
        StationMaster: The station record corresponding to the provided station ID, or None if it does not exist.
    idN)r   StationMasterr   getr   )
station_idr   r   r   get_station_by_id   s
   
r6   c                 C   s`   | r$t jjj| |d\}}|s"| D ]
\}}t||| q|  |S t jjjdi |}|S )a  
    Create or update a station record based on scrap ID and validated data.

    Args:
        scarp_id (str): The scrap ID to identify the station.
        validated_data (dict): Validated data containing station details.

    Returns:
        StationMaster: The created or updated station record.

    This function creates or updates a station record based on the scrap ID. If a station with the given scrap ID exists,
    it updates the existing record with the provided data. If not, it creates a new station record.
    )scarp_iddefaultsNr   )r   r3   r   get_or_createitemssetattrsavecreate)r7   validated_datastationcreatedkeyvaluer   r   r   handle_stations   s   
rC   c                 C   s   t jjj| |d\}}|S )a  
    Create or retrieve a station user association.

    Args:
        station (StationMaster): The station record to associate with the user.
        user (User): The user to associate with the station.

    Returns:
        StationUser: The created or retrieved station user association.

    This function creates or retrieves a station user association, linking a station with a user.
    )r?   user)r   StationUserr   r9   )r?   rD   station_userr@   r   r   r   handle_stations_user   s   
rG   c              
   C   s   t jjj| djddd}dd |D }t|t| }t jjj| |d  |D ]}zt jjj| |d W q) tt	fyA   Y q)w d	S )
a?  
    Handle amenities for a station, updating or creating StationAmenities records.

    Args:
        station (StationMaster): The station for which amenities are being managed.
        new_locations (list of LocationMaster): The list of new locations to be associated with the station.

    Returns:
        None
    r?   Zlocation__idTr   c                 S      g | ]}|j qS r   r1   )r   locationr   r   r   r          z$handle_locations.<locals>.<listcomp>)r?   r&   )r?   rJ   N)
r   r'   r   r    r!   setdeleter9   r   
ValueError)r?   Znew_locationsZexisting_location_idsr(   Zlocations_to_deleterJ   r   r   r   handle_locations   s(   
rO   c                 C   sn   t jjj| djddd}dd |D }t|t| }t jjj| |d  |D ]}t jjj| |d q)d	S )
a@  
    Handle amenities for a station, updating or creating StationAmenities records.

    Args:
        station (StationMaster): The station for which amenities are being managed.
        new_amenities (list of AmenitiesMaster): The list of new amenities to be associated with the station.

    Returns:
        None
    rH   Zamenities__idTr   c                 S   rI   r   r1   )r   amenityr   r   r   r      rK   z$handle_amenities.<locals>.<listcomp>)r?   r,   )r?   	amenitiesN)r   r-   r   r    r!   rL   rM   r9   )r?   Znew_amenitiesZexisting_amenity_idsr.   Zamenity_to_deleterP   r   r   r   handle_amenities   s    rR   c                 C   sJ   t jjj| d  |D ]}t jjj| |d|d|dd qdS )a  
    Handle connectors for a station, updating StationConnectors records.

    Args:
        station (StationMaster): The station for which connectors are being managed.
        new_connectors (list of dict): The list of new connectors data to be associated with the station.
            Each item in the list is a dictionary with keys 'connector', 'kilowatts', and 'make' for the connector data.

    Returns:
        None
    rH   	connector	kilowattsnetwork_operatorr?   rS   rT   rU   N)r   r   r   r    rM   r=   r4   )r?   new_connectorsconnector_datar   r   r   handle_update_connectors  s   rY   c                 C   s>   |D ]}|d D ]}t jjj| |d |d |d d qqdS )a  
    Handle connectors for a station, updating or creating StationConnectors records.

    Args:
        station (StationMaster): The station for which connectors are being managed.
        new_connectors (list of dict): The list of new connectors data to be associated with the station.
            Each item in the list is a dictionary with keys 'connector', 'kilowatts', and 'make' for the connector data.

    Returns:
        None
    outletsrS   rT   makerV   N)r   r   r   r=   )r?   rW   rX   outletr   r   r   handle_connectors#  s   r]   c           	      C   s~   |  d}|  d}|  d}|  d}|  d}|r=|r=d|i}|r'||d< |s+|r1|p.||d< tjj||d\}}|S dS )a8  
    Get or create a network operator based on the provided data.

    Args:
        network_operator_data (dict): Data for the network operator,
        including 'id', 'name', 'phone', 'url', and 'action_url'.

    Returns:
        NetworkOperatorMaster: The created or existing network operator instance.
    r2   namephoneurl
action_urltype_idr8   N)r4   r   r   r9   )	Znetwork_operator_dataZ
network_idr^   r_   r`   ra   r8   rU   r=   r   r   r   get_or_create_network_operator<  s    





rd   c                 C   s>   |  d}|  d}|r|rtjj|||dd\}}|S dS )a   
    Get or create a connector based on the provided data.

    Args:
        connector_data (dict): Data for the connector, including 'connector' and 'connector_name'.

    Returns:
        ConnectorMaster: The created or existing connector instance.
    rS   connector_name)r^   
short_namerb   N)r4   r   r   r9   )rX   r   re   rS   r=   r   r   r   get_or_create_connector\  s   



rg   c                 C   r0   )z
    Get or create a connector based on the provided data.

    Args:
        suggest_id (int): id of SuggestStation tabel

    Returns:
        ConnectorMaster: To get existing SuggestStation instance.
    r1   N)r   SuggestStationr   r4   r   )Z
suggest_idr   r   r   get_suggest_stations  s
   ri   c              
   C   s   |rozYt j}|t|kr|d | }|D ]D}|d}|dd  }|tjv rXt|}|j	dkrX|j
}t||dd d}	tjj|d}tjj| |	tj|dd}
|
  qW d S  tyn } zW Y d }~d S d }~ww d S )	Nr`   .   )r^   )pkT)r?   asset
asset_typerD   is_verified)r   EXTRACT_STATION_IMAGE_COUNTr   r4   splitlowerr   IMAGE_FORMATSrequestsstatus_codecontentr   r   r   r	   r=   r
   IMAGEr<   	Exception)r?   rD   Zstation_photosZextract_image_countZphotoZfirst_photo_url	extensionresponserw   Zcontent_filestation_mediaer   r   r   handle_station_media  s>   



r~   )NN)&ru   django.core.exceptionsr   django.core.files.baser   django.db.modelsr   Zapps.account.modelsr   apps.master.modelsr   r   apps.stationr   r   Zapps.station.modelsr	   base.constantsr
   r   
station_evr   r#   r$   r)   r+   r/   r6   rC   rG   rO   rR   rY   r]   rd   rg   ri   r~   r   r   r   r   <module>   s4    
%! 