o
    >jj8  ã                   @   sò   d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZmZ dZdZej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d„ Z#dd„ Z$dd „ Z%d!d"„ Z&d#d$„ Z'd%S )&é    )Útransaction)Útimezone)ÚLanguage)Ú
CollectionÚCurrencyÚProductÚProductCollectionÚProductLanguageÚProductOptionÚProductOptionLanguageÚProductOptionValueÚProductOptionValueLanguageÚSellerÚShippingCostÚSKUÚSKUImageÚSKUOptionValueÚSKUPrice)Ú"aliexpress_ds_product_get_responseÚ,aliexpress_ds_product_wholesale_get_responseÚali_expressc           *      C   sæ  t | ƒ}| d¡p
i }t| d¡dƒ}t| ƒ}i }g }	|D ]"}
t|
 d¡p&dƒ}t| |¡ƒ}|r5|du r6q|||< |	 |
¡ q|	sFtdƒ‚|	}t| |ƒ}t	j
j|d�}tj
j|d�}t ¡ }t|ƒ}| d	¡pii  d
¡pq| d¡}|du rztdƒ‚d|› �}tj
 ¡ j|d�}|j|d� ¡ }|du rœ|jdd� ¡ }|du r­tj
j|dd|||d�}nd|_d|_||_||_|jdd� tj
j||d� ¡ }|du rÜtj
j||| d¡||d� n| d¡|_||_|jdd� tj
j||d� ¡ sÿtj
j||||d� i }i }t|ƒ}dd„ |D ƒ}tj
j|d�j|d�  ¡  |D �]R}
|
 d ¡}|du�r.t|ƒnd}tj
j||d!� ¡ }|du �rMtj
j||t!|
ƒ||d"�}nt!|
ƒ|_"||_|jd#d� t#|
 d$¡�pl|
 d%¡�pl|
 d&¡ƒt#|
 d&¡ƒd'œ}t$j
j%||i |¥||d(œ¥d)�\}}|�sž|d* |_&|d+ |_'||_|jd,d� t(j
j|d-�  ¡  t)j
j|d-�  ¡  g } t|
 d.¡d/ƒ}!|!D ]/}"t*|||"||ƒ}#t+|#||"||ƒ}$t(j
j||$||d0� |" d1¡}%|%�rê|%d2k�rê|  |%¡ �q¼| �sõ|dd3… } t, -| ¡D ]}%t)j
j||%||d4� �qú|t|
 d¡ƒ }t.j
j|d5� ¡ }&t/| d6¡ƒt0| d7¡ƒt0| d8¡ƒt#| d9¡ƒ�p5d:d;œ}'|&du �rOt.j
jd>d<|i|'¤||d(œ¤Ž �q|' 1¡ D ]\}(})t2|&|(|)ƒ �qS||&_|&jg |' 3¡ ¢d=‘R d� �q|S )?z7Save an AliExpress product response in the shop models.Úae_item_base_info_dtoÚae_item_sku_info_dtosÚae_item_sku_info_d_t_oÚsku_idÚ Nz/productData does not contain a SKU with freight©ÚidÚproduct_id_converter_resultÚmain_product_idÚ
product_idz)productData does not contain a product idÚali_express_©ÚkeyName)ÚsellerId_idT)ÚsellerId__isnullF)r#   Ú	isDigitalÚisFreer$   ÚcreatedDateÚupdatedDate)r&   r'   ÚsellerIdr)   ©Úupdate_fields)Ú	productIdÚ
languageIdÚsubject)r-   r.   Únamer(   r)   ©r0   r)   )r-   ÚcollectionId)r-   r2   r(   r)   c                 S   s   h | ]}t | d ¡pdƒ’qS )Úsku_attrr   )ÚstrÚget)Ú.0ÚskuData© r8   úl/var/www/snowflake_co_dev_github/snow_flake_back_end_deploy/mama_care_api/ali_express_ds/pms/save_product.pyÚ	<setcomp>}   s    ÿzsaveProduct.<locals>.<setcomp>)r-   )Ú
serial__inr3   )r-   Úserial)r-   r<   Ústockr(   r)   )r=   r)   Úoffer_sale_priceÚoffer_bulk_sale_priceÚ	sku_price)Ú	salePriceÚoriginalPrice)r(   r)   )ÚskuIdÚ
currencyIdÚdefaultsrA   rB   )rA   rB   r)   )rC   Úae_sku_property_dtosÚae_sku_property_d_t_o)rC   ÚproductOptionValueIdr(   r)   Ú	sku_imageÚ0é   )rC   ÚimageUrlr(   r)   )Ú
skuPriceIdÚfree_shippingÚmin_delivery_daysÚmax_delivery_daysÚshipping_fee_centg        )ÚisFreeShippingÚminDeliveryDaysÚmaxDeliveryDaysÚcostrM   r)   r8   )4Ú_getProductResultr5   Ú	_getItemsÚ_getFreightBySkur4   Ú_getDeliveryOptionÚappendÚ
ValueErrorÚ_getCurrencyr   Úobjectsr   r   ÚnowÚ_getSellerIdr   Úselect_for_updateÚfilterÚfirstÚcreater&   r'   r$   r)   Úsaver	   r0   r   ÚexistsÚ_getProductImagesr   ÚexcludeÚdeleteÚ	_getStockr=   Ú_toFloatr   Úget_or_createrA   rB   r   r   Ú_getOrCreateOptionÚ_getOrCreateOptionValueÚdictÚfromkeysr   Ú_toBoolÚ_toIntOrNoneÚitemsÚsetattrÚkeys)*ÚproductDatarD   r.   r2   ÚresultÚbaseInfoÚskuDataListÚfreightBySkuÚdeliveryBySkuÚdeliverableSkuDataListr7   rC   ÚdeliveryOptionÚcurrencyÚlanguageÚ
collectionr^   r*   r-   Ú
productKeyÚproductQueryÚproductÚproductLanguageÚ	optionMapÚoptionValueMapÚproductImagesÚdeliverableSerialsr<   ÚskuÚpriceValuesÚskuPriceÚskuPriceCreatedÚ	skuImagesÚskuPropertiesÚskuPropertyÚoptionÚoptionValuerL   ÚshippingCostÚshippingValuesÚfieldÚvaluer8   r8   r9   ÚsaveProduct   sš  þ
þ
ÿþú	ÿþýûþýüÿÿ


û
ÿýúÿý
ý	

ÿþûûü

€
üÿþÿÿø


ÿþü
ÿr•   c                 C   sj   t | tƒs	tdƒ‚|  d| ¡}t |tƒstdƒ‚tD ]}||v r.| |¡p&i  d¡p+i   S q| d|¡S )Nz productData must be a dictionaryÚproductJsonz,productData.productJson must be a dictionaryrv   )Ú
isinstancern   r[   r5   ÚPRODUCT_RESPONSE_KEYS)ru   r–   ÚresponseKeyr8   r8   r9   rV   ü   s   

ÿrV   c                 C   sX   t |  d¡pdƒ ¡ }|r%tjj|d� ¡ }|d u r#t d|› d�¡‚|S tjj|d�S )NÚtargetCurrencyr   )Úcode__iexactzCurrency with code "z" does not exist.r   )r4   r5   Ústripr   r]   ra   rb   ÚDoesNotExist)ru   rD   rš   r}   r8   r8   r9   r\     s   
ÿr\   c                 C   s@   t | tƒr|| v r| | } t | tƒr| S t | tƒr| r| gS g S ©N)r—   rn   Úlist)Ú	containerÚitemKeyr8   r8   r9   rW     s   
rW   c           
      C   sÚ   |  d¡}t|  d¡pdƒ}|d urd|› �n|}||vritjj| |d� ¡ }|d u r6tjj| |||d�}n	||_|jdd� t	jj||d	� ¡ }	|	d u rYt	jj|||||d
� n||	_
||	_|	jdd� |||< || S )NÚsku_property_idÚsku_property_namer�   Úoption_)r-   r#   )r-   r#   r(   r)   )r)   r+   )ÚproductOptionIdr.   )r¥   r.   r0   r(   r)   r1   )r5   r4   r
   r]   ra   rb   rc   r)   rd   r   r0   )
r‚   r~   rŽ   r„   r^   ÚoptionIdÚ
optionNameÚ	optionKeyr�   ÚoptionLanguager8   r8   r9   rl   #  sL   
þýüþýûrl   c                 C   sþ   |  d¡}t|  d¡pdƒ}t|  d¡p|ƒ}|d ur d|› �n|}| j|f}	|	|vr{tjj| |d� ¡ }
|
d u rEtjj| ||||d�}
n||
_||
_	|
j
dd	� tjj|
|d
� ¡ }|d u rktjj|
||||d� n||_||_	|j
dd	� |
||	< ||	 S )NÚproperty_value_idÚsku_property_valuer”   Úproperty_value_definition_nameÚvalue_)r¥   r#   )r¥   r#   r”   r(   r)   )r”   r)   r+   )rH   r.   )rH   r.   r0   r(   r)   r1   )r5   r4   r   r   r]   ra   rb   rc   r”   r)   rd   r   r0   )r�   r~   rŽ   r…   r^   ÚvalueIdr”   Ú	valueNameÚvalueKeyÚmapKeyr�   ÚoptionValueLanguager8   r8   r9   rm   M  sT   

þýûþýûrm   c                 C   sH   |   d¡pi   d¡pd}t|tƒrdd„ |D ƒS dd„ t|ƒ d¡D ƒS )NÚae_multimedia_info_dtoÚ
image_urlsr   c                 S   s   g | ]
}|rt |ƒ ¡ ‘qS r8   )r4   rœ   ©r6   rL   r8   r8   r9   Ú
<listcomp>„  s    z%_getProductImages.<locals>.<listcomp>c                 S   s   g | ]}|r|  ¡ ‘qS r8   )rœ   rµ   r8   r8   r9   r¶   …  s    ú;)r5   r—   rŸ   r4   Úsplit)rv   Ú	imageUrlsr8   r8   r9   rf   �  s   
rf   c                 C   s$   t | tƒsi S dd„ |  dg ¡D ƒS )Nc                 S   s:   i | ]}t |tƒr| d ¡durt| d ¡ƒ| d¡“qS )rC   NÚfreightJson)r—   rn   r5   r4   )r6   Úitemr8   r8   r9   Ú
<dictcomp>Œ  s    ýþz$_getFreightBySku.<locals>.<dictcomp>ÚfreightResults)r—   rn   r5   )ru   r8   r8   r9   rX   ˆ  s
   

þrX   c                 C   sf   t | tƒsd S |  d| ¡}t |tƒr| d|¡ni }tt |tƒr'| d¡p&i ni dƒ}|r1|d S d S )NÚ$aliexpress_ds_freight_query_responserv   Údelivery_optionsÚdelivery_option_d_t_or   )r—   rn   r5   rW   )rº   Úresponserv   Úoptionsr8   r8   r9   rY   “  s   
€ þrY   c                 C   s0   t jjtd� ¡ }|r|jS t jjt| | d�jS )Nr"   )r#   r(   r)   )r   r]   ra   Ú
SELLER_KEYrb   r   rc   )r^   Úsellerr8   r8   r9   r_      s   ýür_   c                 C   s6   |   d¡du r	dS |   d¡}|d u r|   d¡}t|ƒS )NÚ	sku_stockFr   Úsku_available_stockÚipm_sku_stock)r5   Ú_toInt)r7   r=   r8   r8   r9   ri   ¬  s   

ri   c              	   C   s$   zt | ƒW S  ttfy   Y d S w rž   )ÚfloatÚ	TypeErrorr[   ©r”   r8   r8   r9   rj   ¶  ó
   
ÿrj   c              	   C   s$   zt | ƒW S  ttfy   Y dS w )Nr   )ÚintrÊ   r[   rË   r8   r8   r9   rÈ   ½  rÌ   rÈ   c                 C   s   | d u s| dkr
d S t | ƒS )Nr   )rÈ   rË   r8   r8   r9   rq   Ä  s   rq   c                 C   s"   t | tƒr| S t| ƒ ¡  ¡ dv S )N)Ú1ÚtrueÚyes)r—   Úboolr4   rœ   ÚlowerrË   r8   r8   r9   rp   Ê  s   
rp   N)(Ú	django.dbr   Údjango.utilsr   Úbasic.modelsr   Úshop.modelsr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r˜   rÃ   Úatomicr•   rV   r\   rW   rl   rm   rf   rX   rY   r_   ri   rj   rÈ   rq   rp   r8   r8   r8   r9   Ú<module>   s.    D
 ]*4
