
    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 d dlmZ d d	lmZ  G d
 d      Zy)    )HTTPException)SessionN)Messages)CategoryCreateTblCategory)	GroupBaseTblGroup)TblMainData)
TblStudent)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j                  fdZd	efd
ZdefdZdefdZdedej$                  fdZy)GroupServicedbc                      || _         || _        y N)r   token)selfr   r   s      0/var/www/html/RetailAPI/app/api/group/service.py__init__zGroupService.__init__   s    
    requestc                    K   t        j                  |j                               }t        j                  || j
                         | j
                  j                          t        dt        j                        S w)N1statusmessage)
r   model_validate
model_dumpr	   creater   commitr   r   GROUP_CREAT)r   r   created_users      r   create_groupzGroupService.create_group   sR      //0B0B0DEdgg.S83G3GHHs   A9A;group_idc                    K   t        j                  || j                        }|st        dt        j
                        t        j                  j                  |      S w)N  status_codedetail)	r	   	get_groupr   r   r   GROUP_NOT_FOUNDschemasGroupResposer   r   r$   groups      r   r*   zGroupService.get_group   sI     ""8TWW5Cx7O7OPP##22599s   AAc                    K   t        j                  || j                        }|st        dt        j
                        t        j                  |d   |d         S w)Nr&   r'   r      category_assignedcategory)r	   get_group_categoryr   r   r   r+   r,   GroupCategoryResponse)r   r$   results      r   r5   zGroupService.get_group_category   sP     ,,Xtww?C8P8PQQ,,vaySYZ[S\]]s   AAc                 |  K   t        j                  |      }|j                  t        dt        j
                        t        j                  |j                  || j                        }|st        dt        j
                        | j                  j                          t        dt        j                        S w)Nr&   r'   r   r   )r   r   r$   r   r   r+   r	   updater   r    r   GROUP_UPDATE)r   r   r/   updated_groups       r   update_groupzGroupService.update_group"   s     ((1>>! S9Q9QRR twwGCx7O7OPPS(2G2GHHs   B:B<
group_codec                   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,   StudentInfor   )r   r=   studentsstudents       r   get_group_studentszGroupService.get_group_students,   s`     77==,33J4I4IZ4WX\\^KST8##227;8TTTs   AB&A=:Bc                 
   t        j                  |j                  | j                        }|st	        dt
        j                        |j                  rt        dd      S |j                  }| j                  j                  t               j                  t         j                  |k(  t         j                  |j                  k(        j                         }|rt        dd      S |j                  rt        dd      S t        j                  t!        j"                  |j%                               | j                         |j                  |_        | j                  j'                          t        dt
        j(                        S )	Nr&   r'   0z#Category already assigned by admin.r   zJThis category is already assigned to another group in the same simulation.z(Category already assigned to this group.r   )r	   r*   r$   r   r   r   r+   r3   r   simulation_coder?   r@   choose_categoryfirstr   r   r   r   r   r    GROUP_CATEGORY)r   r   r/   rH   existing_categorys        r   create_categoryzGroupService.create_categoryM   s.   ""7#3#3TWW=C8P8PQQ""!6[\\// GGMM(3::$$7&&'*A*AA
 %' 	 !  7C  D  D""!6`aa>889K9K9MNPTPWPWX")"9"9S(2I2IJJr   c                    | j                   j                  t              j                  t        j                  |k(        j                         }|st        dd      |j                  xs |j                  }|st        dd      | j                   j                  t              j                  t        j                  |k(        j                         }|st        dd      |S )Nr&   Group not foundr'   i  zGroup has no category assignedz%Main data not found for this category)r   r?   r	   r@   r$   rJ   r   r3   r4   r
   format_type)r   r$   r/   r4   	main_datas        r   get_main_data_by_group_servicez+GroupService.get_main_data_by_group_servicea   s    h'..x/@/@H/LMSSUC8IJJ**<ennC8XYYGGMM+.55k6M6MQY6YZ``b	C8_``r   returnc                    | j                   j                  t              j                  t        j                  |k(        j                         }|st        dd      t        j                  |j                  |j                        S )Nr&   rO   r'   r2   )r   r?   r	   r@   r$   rJ   r   r,   r6   r3   r4   r.   s      r   get_group_category_servicez'GroupService.get_group_category_servicem   sg    h'..x/@/@H/LMSSUC8IJJ,,#55^^
 	
r   N)__name__
__module____qualname__r   r   r,   GroupCreater#   intr*   r5   GroupUpdater<   strrE   r   rM   rR   r6   rU    r   r   r   r      s    7 I'*=*= I: :^ ^I'*=*= IU3 UBK~ K(
c 

3 
7;X;X 
r   r   )fastapir   sqlalchemy.ormr   app.api.group.schemasapir/   r,   app.locale.messagesr   app.models.main.categoryr   r   app.models.main.groupr   r	   app.models.main.main_datar
   app.models.main.studentr   app.utils.schemas_utilsr   r   r]   r   r   <module>rh      s/    ! " ' ' ( @ 4 1 . 2i
 i
r   