
    {2i"                         d dl m Z  d dlZd dlZd dlmZmZ d dl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 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!  G d d      Z"y)    )datetimeN)HTTPException
UploadFile)Session)StudentService)JWTPayloadSchema)Messages)TblCategorySimulation)TblGroup)TblInstitution)SimulationBaseTblSimulation)TblUser)TblUserSimulation)CustomResponsec                       e Zd ZdedefdZ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fdZde
fdZde
fdZde
fdZde
fdZy)SimulationServicedbtokenc                      || _         || _        y N)r   r   )selfr   r   s      A/var/www/html/retail-simulation-api/app/api/simulation/service.py__init__zSimulationService.__init__   s    
    c                 |   | j                   j                  t              j                  t        j                  j                               j                         }|r|j                  sy|j                  }|j                  d      }t        |      dk7  s|d   j                         syt        |d         dz   }d|dS )NzSIM-001-      zSIM-03d)r   queryr   order_bysimulation_iddescfirstsimulation_codesplitlenisdigitint)r   last_simulation	last_codepartscurrent_numbers        r   generate_simulation_codez*SimulationService.generate_simulation_code   s    GGMM-(Xm116689UW 	
 o&E&E#33	$u:?%("2"2"4U1X*nS)**r   requestc                 f  K   | j                         }|j                         }||d<   t        j                  |      }t	        di |j                         }| j
                  j                  |       | j
                  j                          | j
                  j                  |       | j
                  j                  t              j                  t        j                  j                               j                         }t        |j                   |j                        }| j
                  j                  |       | j
                  j                          | j
                  j                  |       d}t#        j$                  |d       t'        j(                         }	|	j*                  }
d|
_        g d}|
j/                  |       |
j/                  ddddd|dddg	       |}| d	}t"        j0                  j3                  ||      }|	j5                  |       t7        d
t8        j:                        S w)Nr&   )r#   user_iddownload_excelT)exist_okzSimulation Data)	
first_name	last_namestudent_emailroll_no
group_coder&   
group_namenumber_of_memberscategory_assigned z.xlsx1statusmessage )r/   
model_dumpr   model_validater   r   addcommitrefreshr!   r   r"   r2   r$   r%   r   r#   osmakedirsopenpyxlWorkbookactivetitleappendpathjoinsaver   r	   SIMULATION_CREAT)r   r0   r&   datacreated_simulationnew_simulationusernew_linkSAVE_FOLDERwbwsheaderssimulation_code_filenamefilepaths                  r   create_simulationz#SimulationService.create_simulation)   s    779!!#"1+::4@&I);)F)F)HIN#'ww}}W%..w/C/C/EFLLN$(66LL
 	H!'
K$/ YY$

 			'
		BBBREF*&'u-77<<X6
--
 	
s   H/H1r#   c                    K   t        j                  || j                        }t        j                  j                  |      S wr   )r   	get_by_idr   schemasSimulationResponserD   r   r#   
simulations      r   get_simulationz SimulationService.get_simulationX   s3     ",,]DGGD
))88DDs   A Ar&   c                   K   | j                   j                  t              j                  t        j                  |k(        j                         }|sdg iS t        | j                   | j                        }g }|D ]  }|j                  |j                         d {   }|j                  |j                  |j                  |j                  |j                  |j                  |j                  |j                  |d        d|iS 7 jw)Ngroup)group_idr:   r<   categoryr;   r&   r9   student)r   r!   r   filterr&   allr   r   get_simulation_group_studentr9   rN   ri   r:   r<   rj   r;   )r   r&   groupsstudent_serviceresultgrk   s          r   get_groups_by_simulation_codez/SimulationService.get_groups_by_simulation_code\   s     GGMM(#VH,,?@SU 	
 R= ($**=A+HHVVGMMJJll%&%8%8JJ%&%8%8#$#4#4ll"
 
     Ws   BDDA+Dc                   K   | j                   j                  t              j                  t        j                  |k(        j                         }|D cg c]d  }t        j                  |j                  |j                  |j                  |j                  |j                  |j                  |j                        f c}S c c}w w)N)ri   r:   r<   rj   r;   r&   r9   )r   r!   r   rl   r&   rm   rb   GroupResponseri   r:   r<   rj   r;   r9   )r   r&   ro   rr   s       r   get_groups_by_simulation_idz-SimulationService.get_groups_by_simulation_id   s      GGMM(#VH,,?@SU	 	" 
  !!<<"#"5"5"#"5"5 ! 1 1<< 
 	
 
s   ACA)C =Cend_datec                   K   | j                   j                  t              j                  t        j                  |k(        j                         }|st        dd      S ||_        | j                   j                          t        dd      S w)N-1Simulation not foundr?   r>   zEnd Date Updated Successfully)	r   r!   r   rl   r#   r%   r   rw   rF   )r   r#   rw   	end_datess       r   update_end_datez!SimulationService.update_end_date   sm     GGMM-0778S8SWd8dekkm	!7MNN%	S2QRRs   BBc                   K   | j                   j                  t              j                  t        j                  |k(        j                         }|st        dd      S |j                  dk(  rId|_        | j                   j                          | j                   j                  |       t        dd      S |j                  dk(  rt        dd      S |j                  d	k(  rt        dd
      S t        dd|j                         S w)Nry   rz   r?   PendingActiver>   zThe Simulation is Activez The Simulation is Already Active	Completed#The Simulation is Already ComplatedUnknown status: 
r   r!   r   rl   r#   r%   r   r@   rF   rG   rd   s      r   run_simulationz SimulationService.run_simulation   s     WW]]=1889T9TXe9eflln
!7MNN	) (JGGNNGGOOJ'!6PQQ(*!7YZZ+-!7\]]!9I*J[J[I\7]^^   DD	c                   K   | j                   j                  t              j                  t        j                  |k(        j                         }|st        dd      S |j                  dk(  rt        dd      S |j                  dk(  rId|_        | j                   j                          | j                   j                  |       t        dd	      S |j                  dk(  rt        dd
      S t        dd|j                         S w)Nry   rz   r?   r~   zThe Simulation is Pendingr   r   r>   zThe Simulation is Complatedr   r   r   rd   s      r   end_simulationz SimulationService.end_simulation   s     WW]]=1889T9TXe9eflln
!7MNN	)!7RSS(* +JGGNNGGOOJ'!6STT+-!7\]]!9I*J[J[I\7]^^r   c                    K   t        j                  || j                        }|st        dt        j
                        S t        dt        j                        S w)Nry   r?   r>   )r   deleter   r   r	   SIMULATION_NOT_FOUNDSIMULATION_DELETED)r   r#   deleteds      r   delete_simulationz#SimulationService.delete_simulation   sE     &&}dgg>!x7T7TUUS(2M2MNNs   AAinstitution_idc                   K   | j                   j                  t              j                  t        j                  |k(        j                         }|st        dd      | j                   j                  t              j                  t        j                  |j                  k(        j                         }|st        dd      | j                   j                  t              j                  t        j                  |j                  k(        j                         }|st        dd      |j                  |j                  z
  }||j                  dS w)Ni  zInstitution Not Found)status_codedetailzSimulation ID Not FoundzSimulation Not Found)subtract_totalinstitution_name)r   r!   r   rl   r   r%   r   r   r2   admin_idr   r#   members_countmembersinstitutions_name)r   r   get_institutionrf   get_simr   s         r   get_simulation_institutionz,SimulationService.get_simulation_institution   s	    ''--7>>~?\?\`n?nouuwC8OPP'89@@ARAZAZ^m^v^vAvw}}C8QRR''--.55m6Q6QUcUqUq6qrxxzC8NOO(66H - / A A
 	
s   EEN)__name__
__module____qualname__r   r   r   r/   rb   SimulationCreater_   r*   rf   strrs   rv   r   r|   r   r   r   r   rB   r   r   r   r      s    7 +; +"-
w/G/G -
^E3 E!3 !F
 
.S# S S_ _ _ _"OS O
c 
r   r   )#r   rH   shutilfastapir   r   rJ   sqlalchemy.ormr   app.api.simulation.schemasapire   rb   app.api.student.servicer   app.dependency.authanticationr   app.locale.messagesr	   #app.models.main.category_simulationr
   app.models.main.groupr   app.models.main.institutionr   app.models.main.simulationr   r   app.models.main.userr   app.models.main.user_simulationr   app.utils.schemas_utilsr   r   rB   r   r   <module>r      sG     	  -  " , , 2 : ( E * 6 D ( = 2A
 A
r   