
    0e(                         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 d d
l	mZ d dlT d dlmZ d dlmZ  e            Z G d de          ZdS )    N)selectinload)Iterable)BaseRepository)select)articlePictureTypes)r   )partner_agent)Sequence)Shop)*)StringIO)price_conditionsc                      e Zd ZdZd Z	 	 ddededz  dedz  dedz  dee	j
                 f
d	Zd
e	j        fdZdedee	j                 fdZdedee	j                 fdZdedeej                 fdZedefd            Zdej
        fdZdS )ArticleRepositoryz%
    Repository for article data
    c                 L  K   | j                             t          t          j                                      t          j        j        t          j        k                         d{V }d |	                                D             }t          t          j
        t          j                                      t          j                                      t          t          j
        j                                                t          t          j
        j                                                t          t          j        j                                                t          t          j        j                                                |                     t(          j                            }| j                             |           d{V }t-                      }t/          j        |          }|                    g d           |	                                D ]l}|                    |j        |j        j        j        |j        d|j        |j        |j        |j        j        |j        j        |j        j        j        g
           m|                     d           |S )z`
        Creates a csv export for b2b articles
        :return: path to the export file
        Nc                 (    i | ]}|j         |j        S  )ref_idpath).0pictures     S/Users/daniilossinkin/PycharmProjects/ShopsBackend/src/data/repositories/article.py
<dictcomp>z=ArticleRepository.get_articles_csv_export.<locals>.<dictcomp>   s.     
 
 
 NGL
 
 
    )zModel numberArticleCategoryGenderPictureEANzSize registerSizez
Color codez
Color nameSeason r   )!async_sessionscalarsr   dbr   wheretyper   ARTICLE_IMAGEallArticleDetailr   joinoptionsr   r   colorgroupseason+_ArticleRepository__select_articles_by_shopr   B2Br   csvwriterwriterowarticle_numberdesignationeansize_registersizecodeseek)selfquerypicture_pathsstmtbufferr3   article_details          r   get_articles_csv_exportz)ArticleRepository.get_articles_csv_export   sH     
 (002:$$RZ_8R%RSSU U U U U U U U
 
 99;;
 
 

 r'44bj!!b&6&>??@@b&6&<==>>bj&67788bj&78899t55dh??@@ 	 (0066666666F## ? ? ? 	@ 	@ 	@ $iikk 	 	NOO"1"*0<"* #&"0"'"(-"(4"*1=   " 	Ar   Nshopr-   r9   themereturnc                   K   t          t          j                                      t          j        j                                      t          t          j        j                                                t          j	                                      t          j
                  }|                     |          }|r|t          j
        j        |k    z  }|r|t          j
        j        |k    z  }|r|t          j        j        |k    z  }|                    |          }| j                            |           d{V }d |                                D             S )z
        Get all available articles based on shop
        :param shop: shop constant
        :return: the list of available articles
        Nc                 p    g | ]3}t          j        |j        j        |j        |j        |j                   4S ))namenumbermaterialbrand)schemar   r.   r6   r5   compositionbrand_designation)r   r   s     r   
<listcomp>z2ArticleRepository.get_articles.<locals>.<listcomp>b   sU     
 
 
  N].- ,/	  
 
 
r   )r   r%   r   distinctr5   r,   r   r.   r+   Groupr*   r0   
color_coder9   theme_group_nrr&   r#   r$   r)   )r<   rC   r-   r9   rD   r?   r&   r=   s           r   get_articleszArticleRepository.get_articlesH   s2      rz""233RZ%56677RXR%&&	 	 ..t44 	:R%0E99E 	3R%*d22E 	8RZ.%77Ezz%  (0066666666
 
 !99;;
 
 
 	
r   r=   c                   	
K   | j                             t          j        |j                   d{V }|j        | j                             t          t          j	                  
                    t          j        t          j	        j        t          j        j        k                                  t          j        j        |j        k    t          j        j        |j        k    z                       d{V }d |                                D             t%                    st'          d          dt(          t          j                 ffd	dt          j        f	
fd}t          t          j        t          j                  
                    t          j                                      t1          t          j        j                                                t1          t          j        j                                                t1          t          j        j                                                t1          t          j        j                                                t          j        j        |j        k              }| j                             |           d{V                                                                 }|                      |           d{V 

d         }tC          j"        |j        |j        j#        |j        j        |j$        |j%        |j&        | ||          	          }|S )
z
        Get the color size information of an article
        :param query: article query to determinate the article color size data
        :return:the color size information of by query selected article
        N)onclausec                 (    i | ]}|j         |j        S r   )article_detail_eanprice)r   price_list_items     r   r   z<ArticleRepository.get_article_color_size.<locals>.<dictcomp>   s0     
 
 
 .0E
 
 
r   z:There are no prices for given partner and sales order typearticle_sizesc                 "    fd| D             S )Nc                     g | ]^}|j         d k    t          j        |j        |j        t          j        |j                 |j                  |j        |j                   _S )r   )r9   r8   rY   r7   quantity)r^   rL   ArticleSizer9   
size_groupr   apply_surcharger7   )r   article_sizepricessurcharge_codes     r   rO   z\ArticleRepository.get_article_color_size.<locals>.article_size_to_scheme.<locals>.<listcomp>   s~     	 	 	 !\5JQ5N5N "%*"."9*:6,BR;SUaUfhvww$()2   6O5N5Nr   r   )r[   rc   rd   s    r   article_size_to_schemezHArticleRepository.get_article_color_size.<locals>.article_size_to_scheme   s5    	 	 	 	 	 %2	 	 	 	r   r   c           	      F   g }d         }d | j         D             D ]\  } t          fd| j                             }t          |          s6|                                v r	|         }nd}|                    t          j        |||                     |S )Nr-   c                 >    h | ]}|j         j        |j         j        fS r   )r-   r:   r6   )r   
color_sizes     r   	<setcomp>zOArticleRepository.get_article_color_size.<locals>.get_colors.<locals>.<setcomp>   s6     & & &JT!&
(8(DE& & &r   c                     | j         k    S )N)rR   )ar:   s    r   <lambda>zNArticleRepository.get_article_color_size.<locals>.get_colors.<locals>.<lambda>   s    PT@T r   z#https://via.placeholder.com/700x400)
color_namerR   sizesr   )color_sizesfilterlenkeysappendrL   ArticleColor)	r   colors
color_picsr6   rn   r   r:   re   picturess	         @r   
get_colorsz<ArticleRepository.get_article_color_size.<locals>.get_colors   s    F!'*J& &X_Xk& & &  !k /.v6T6T6T6TV]Vi/j/jkk5zz :??,,,,(.GGCG'#.#'# '	      Mr   )r5   article_namer/   delivery_date_todelivery_date_fromrJ   r   ru   )'r#   get
db_partnerPartner
partner_idrd   r$   r   r%   	Pricelistr+   SalesOrderTypePartnerr:   pricelist_coder&   sales_order_typepartner_numfetchallrq   
ValueErrorr   r*   r   r,   r   r.   r/   ro   r-   r5   uniqueoneload_pictures_for_articlerL   ArticleColorSizer6   date_to	date_fromrM   )r<   r=   partnerprice_listsrx   r?   r   article_pictureschema_resultre   rw   rc   rd   s            @@@@r   get_article_color_sizez(ArticleRepository.get_article_color_sizel   s	      *..z/A5CSTTTTTTTT / !.662<  Z5 l/:3S3bb  d d16%:PP1=AQQS 	
 
 
 
 
 
 
 

 
#.#7#7#9#9
 
 

 6{{ 	[YZZZ
	(2;K2L 
	 
	 
	 
	 
	 
	 
		
 	 	 	 	 	 	 	. rz2#344b&''bj&67788bj&78899bj&<==>>b&6&<==>>rz0E4HHII 	 +33D99999999AACCGGII77@@@@@@@@"9-/"1 2>&$_&0(#:g&&	
 	
 	
 r   c                 Z  K   t          t          j                                                                      t          j                                      t          j                                      |                     |                    }| j	        
                    |           d{V }t                      }g }|                                D ]X}|j        |vrM|                    |j                   |                    t!          j        |j        |j                             Y|S )z
        Get all unique colors of available articles
        :param shop: shop constant
        :return: the list of available articles unique colors
        N)r:   rH   )r   r%   ColorrP   r+   r*   r   r&   r0   r#   r$   setr)   r6   addrs   rL   r:   )r<   rC   r?   r=   ru   resultr-   s          r   rx   zArticleRepository.get_colors   s      bh((**//0@AAFFrzRRXX**4002 2(0066666666YY[[ 	 	E ..

5,---L"Z".     r   c                   K   t          t          j        j                                                                      t          j        j                                      t          j                                      | 	                    |                    }| j
                            |           d{V }d |                                D             S )z
        Get all unique sizes of available articles
        :param shop: shop constant, which determinate
        :return: the list of available articles unique sizes
        Nc                 8    g | ]}t          j        |           S ))r9   )rL   r    )r   r9   s     r   rO   z/ArticleRepository.get_sizes.<locals>.<listcomp>   s%    ???4&&&???r   )r   r%   r*   r9   rP   order_byr+   r   r&   r0   r#   r$   r)   r<   rC   r?   r=   s       r   	get_sizeszArticleRepository.get_sizes   s       r',--

).//RZ  d44T::;;	 	
 (0066666666??599;;????r   c                   K   t          t          j                                      t          j                                      t          j                                      |                     |                    }|                    d          }| j	        
                    |           d{V }|                                                                S )z
        Get all available articles for selected shop
        :param shop: shop constant to determinate the availability
        :return: the list of orm objects representing articles
           N)r   r%   r*   r+   r   r   r&   r0   limitr#   r$   r   r   r   s       r   __get_articlesz ArticleRepository.__get_articles   s       b&'',,RX66;;BJGGMMdNlNlmqNrNrsszz"~~(0066666666||~~&&(((r   c                     t           j        }| t          j        k    r+t          j        j        |k    t          j        j        dk    z  S t          j        j        |k    t          j        j        dk    z  S )Nr   )	shop_configmobi_seasonr   r1   r%   r   	season_nrr*   r^   )rC   r   s     r   __select_articles_by_shopz+ArticleRepository.__select_articles_by_shop   sY    !-48J(K7B<L<UXY<YZZJ(K7B<L<UXY<YZZr   r   c                   K   t          t          j                                      t          j        j        |j        k              }| j                            |           d {V }d |j        D             }t          t          j                                      t          j        j        	                    |                    }| j        
                    |           d {V }|                                }|j        d |D             dS )Nc                 &    h | ]}|j         j        S r   )r-   r:   )r   details     r   ri   z>ArticleRepository.load_pictures_for_article.<locals>.<setcomp>   s    FFF&,#FFFr   c                 (    i | ]}|j         |j        S r   )r   uri)r   	color_pics     r   r   z?ArticleRepository.load_pictures_for_article.<locals>.<dictcomp>  s/         )-  r   )r   r-   )r   r%   r   r&   r   r5   r#   scalarro   in_r$   r)   r   )r<   r   r?   r   ru   r=   color_picturess          r   r   z+ArticleRepository.load_pictures_for_article   s     rz""(():g>T)TUU $ 2 9 9$ ? ???????FF'2EFFFrz""(():)>)>v)F)FGG(0066666666&+ !/  
 
 	
r   )NNN)__name__
__module____qualname____doc__rB   r   strintlistrL   r   rT   GetArticleColorSizer   r   rx   r    r   r
   r%   r*    _ArticleRepository__get_articlesstaticmethodr0   r   r   r   r   r   r      sz        . . .` [_/3"
 "
t "
C$J "
SSWZ "
"%*"
8<V^8L"
 "
 "
 "
HW&2L W W W WrT d6<.@    ,@D @T&+-> @ @ @ @	) 	)(2;K2L 	) 	) 	) 	) [ [ [ [ \[
rz 
 
 
 
 
 
r   r   )r2   sqlalchemy.ormr   typingr   src.data.repositories.baser   
sqlalchemyr   src.data.model.schemasr   rL   r   src.data.model.dbr%   r	   r}   r
   r   src.config.shops_configior   src.data.model.processingr   load_shops_configr   r   r   r   r   <module>r      s2   



 ' ' ' ' ' '       5 5 5 5 5 5       B B B B B B B B + + + + + + 9 9 9 9 9 9       ' ' ' ' ' ' % % % %       6 6 6 6 6 6!!v
 v
 v
 v
 v
 v
 v
 v
 v
 v
r   