Sindbad~EG File Manager

Current Path : /home/numerotech/st.aios-scientificcommittee.org/aios-slots/core_old/model/
Upload File :
Current File : //home/numerotech/st.aios-scientificcommittee.org/aios-slots/core_old/model/AIOSSlottingModel.py

from flask import session
from flask import request, Blueprint, jsonify
from sqlalchemy import create_engine, select, MetaData, Table,text
from sqlalchemy.sql import and_, or_

from core import app

engine = create_engine(app.config['DATABASE_URI'],pool_pre_ping=True,pool_recycle=3600)

class AIOSSlottingModel():  
    def __init__(self):
        try:
            self.meta = MetaData()
            self.workshop_admim = Table("workshop_admim", self.meta,autoload_with=engine)
            self.facultyslot = Table("facultyslot", self.meta, autoload_with=engine)
            self.mcourseslots = Table("mcourseslots", self.meta, autoload_with=engine)
            self.delegateslot = Table("delegateslot", self.meta, autoload_with=engine)
            
            
        except Exception as e:
            print(e)

    def Get_Course(self,workshopId):
        with engine.connect() as conn:
            stmt=text("select * from m_workshop where workshopId in ("+str(workshopId)+");")
            # stmt=text("select * from m_workshop ;")
            result = conn.execute(stmt).all()
            results = [dict(r._mapping) for r in result] if result else None
            return results
    
    def Last_login(self,data,admin_id):
        with engine.connect() as conn:
            stmt   = self.workshop_admim.update().where(self.workshop_admim.c.admin_id.in_([admin_id])).values(data)
            result = conn.execute(stmt)
            conn.commit()
            return 'success'

    def Check_login(self,username,password):
        with engine.connect() as conn:
            stmt=text("select * from workshop_admim where user_name='"+str(username)+"' and pwd='"+str(password)+"' ;")
            result=conn.execute(stmt).first()
            results = dict(result._mapping) if result else None
            return results
            
    def get_slot_time(self,course_id):
        with engine.connect() as conn:
            stmt=text("select ms.*,(SELECT COUNT(ds1.SlotId) from delegateslot ds1 where ds.SlotId=ds1.SlotId) as total_slot_done,(SELECT COUNT(fs1.SlotId) from facultyslot fs1 where fs.SlotId=fs1.SlotId) as Faculty_slot   from mcourseslots ms left join  delegateslot ds on ds.SlotId=ms.SlotId  left join facultyslot fs on ms.SlotId=fs.SlotId where ms.WorkshopCourseId="+str(course_id)+" group by ms.SlotId;")
            result=conn.execute(stmt).all()
            results = [dict(r._mapping) for r in result] if result else None
            return results

    def Get_Course_name(self,course_id):
        with engine.connect() as conn:
            stmt=text("select * from workshopcourses c inner join m_workshop m on c.workshopId = m.workshopId where WorkshopCourseId="+str(course_id)+";")
            result=conn.execute(stmt).all()
            results = [dict(r._mapping) for r in result] if result else None
            if results :
                return results
            else:
                return None
        
    def Get_faculties(self,course_id):
        with engine.connect() as conn:
            stmt=text(f"select u.user_id,concat(ifnull(u.prefix,''),ifnull(u.full_name,'')) as full_name,u.membership_no,u.email,u.mobile,(select group_concat(distinct (concat(CourseName,case when t4.Date is not null then concat(' (',ifnull(t4.Date,''),' (',dayname(t4.Date),')  ',ifnull(t4.StartTime,''),' - ',ifnull(t4.EndTime,''),')')else '' end )) SEPARATOR '<br>' ) from facultycourse t1 inner join workshopcourses t2 on t1.course_id = t2.WorkshopCourseId left join facultyslot t3 on t1.user_id = t3.FacultySlotID  left join mcourseslots t4 on t3.SlotId = t4.SlotId and t2.WorkshopCourseId = t4.WorkshopCourseId where t1.user_id = u.user_id  group by t1.user_id) as course_name,(select count(*) from faculty_preference_timing t1 inner join master_slotes t2 on t1.master_slot_id = t2.master_slote_id inner join workshopcourses t3 on t2.Workshop_id = t3.WorkshopId where user_id = u.user_id and t3.WorkshopCourseId = fc.course_id and sloted_course_id is not null ) as sloted_count,(select count(*) from faculty_preference_timing t1 inner join master_slotes t2 on t1.master_slot_id = t2.master_slote_id inner join workshopcourses t3 on t2.Workshop_id = t3.WorkshopId where user_id = u.user_id and t3.WorkshopCourseId = fc.course_id  ) as all_slotes  from facultycourse fc left join facultyslot fs on fc.user_id = fs.FacultySlotID left join mcourseslots ms on ms.WorkshopCourseId = fc.course_id and ms.SlotId = fs.SlotId left join faculty_preference_timing fpt on fc.user_id = fpt.user_id  inner join users u on u.user_id = fc.user_id where course_id = {course_id} and sloted_course_id is null group by fc.user_id;")
            result=conn.execute(stmt).all()
            results = [dict(r._mapping) for r in result] if result else None
            return results
    
    def get_all_faculty_course_slots(self):
        with engine.connect() as conn:
            stmt=text(f"select fc1.user_id,CourseName,concat(' (',ifnull(ms1.Date,''),' (',dayname(ms1.Date),')  ',ifnull(ms1.StartTime,''),' - ',ifnull(ms1.EndTime,''),')') as slot_time from facultycourse fc1 inner join workshopcourses wc1 on fc1.course_id = wc1.WorkshopCourseId  left join facultyslot fs1 on fc1.user_id = fs1.FacultySlotID left join mcourseslots ms1 on ms1.SlotId = fs1.SlotId  and ms1.WorkshopCourseId = wc1.WorkshopCourseId group by CourseName,user_id;")
            result=conn.execute(stmt).all()
            results = [dict(r._mapping) for r in result] if result else None
            return results
            
    
    def Get_faculties_prefered_slots(self,workshop_id,user_id):
        with engine.connect() as conn:
            stmt=text(f"select * from faculty_preference_timing fpt inner join master_slotes ms on fpt.master_slot_id = ms.master_slote_id where user_id = {user_id} and sloted_course_id is null and Workshop_Id = {workshop_id};")
            result=conn.execute(stmt).all()
            results = [dict(r._mapping) for r in result] if result else None
            return results
        
    def create_faculties_prefered_slots(self,workshop_id,course_id,master_slot_id,user_id):
        connection = engine.raw_connection()
        cursor = connection.cursor()
        cursor.callproc("usp_faculty_slotting",[workshop_id,course_id,master_slot_id,user_id])
        columns = [column[0] for column in cursor.description]
        results = []
        for row in cursor.fetchall():
            results.append(dict(zip(columns, row)))
        cursor.close()
        connection.commit()
        if results :
            return results[0]
        else :
            return None

    def Get_Faculty_data(self,full_name):
        with engine.connect() as conn:
            stmt=text("select * from users where full_name like '%{n}%' or mobile like '%{n}%' or email like '%{n}%' or membership_no like '%{n}%';".format(n=full_name))
            result=conn.execute(stmt).all()
            results = [dict(r._mapping) for r in result] if result else None
            return results

    def get_course_data(self,SlotId):
        with engine.connect() as conn:
            stmt=text("select * from mcourseslots where SlotId='"+str(SlotId)+"'")
            result=conn.execute(stmt).first()
            results=dict(result._mapping) if result else None
            return results

    def UpdateSlotData(self,SlotId,data):
        with engine.connect() as conn:
            stmt   = self.mcourseslots.update().where(self.mcourseslots.c.SlotId.in_([SlotId])).values(data)
            result = conn.execute(stmt)
            conn.commit()
            return 'success'
        
    def InsertSlotData(self,data):
        with engine.connect() as conn:
            result = conn.execute(self.mcourseslots.insert(), data)
            conn.commit()
            return "success"
        
    def deleteSlotData(self,slot_id):
        with engine.connect() as conn:
            result = conn.execute(text(f"delete from mcourseslots where SlotId = {slot_id}"))
            conn.commit()
            return "success"

    def Get_workshop_course_Data(self,workshopId):
        sets = []
        try:
            connection = engine.raw_connection()
            cursor = connection.cursor()
            cursor.callproc("get_total_count_data",[workshopId])
            while 1:
                #(column_name, type_, ignore_, ignore_, ignore_, null_ok, column_flags)
                names = [c[0] for c in cursor.description]
                set_ = []
                while 1:
                    row_raw = cursor.fetchone()
                    if row_raw is None:
                        break
                        
                    row = dict(zip(names, row_raw))
                    set_.append(row)
     
                sets.append(list(set_))
     
                if cursor.nextset() is None:
                    break
     
                # nextset() doesn't seem to be sufficiant to tell the end.
                if cursor.description is None:
                    break
        finally:
            # Return the connection to the pool (won't actually close).
            connection.close()
            
        return sets[0]

    def Set_Faculty_Slot(self,data):
        with engine.connect() as conn:
            result = conn.execute(self.facultyslot.insert(), data)
            conn.commit()
            return "success"

    def Set_Delegate_Slot(self,data):
        with engine.connect() as conn:
            result = conn.execute(self.delegateslot.insert(), data)
            conn.commit()
            return "success"
    

    def Get_faculty(self,SlotId):
        with engine.connect() as conn:
            stmt=text("select * from facultyslot f left join users u  on f.FacultySlotID = u.user_id where SlotId ="+str(SlotId)+" ;")
            # stmt=text("SELECT fs.SlotId,u.full_name,u.email,u.mobile FROM users u left join facultyslot fs on fs.FacultySlotID=u.user_id where fs.SlotId='"+str(SlotId)+"' UNION SELECT name as full_name,email,mobile FROM facultyslot where SlotId="+str(SlotId)+";")
            result=conn.execute(stmt).all()
            results=[dict(r._mapping) for r in result] if result else None
            return results

    def get_session_speakers(self,SlotID):
        with engine.connect() as conn:
            stmt = text (f"select * from ((select DelSlotID,SlotId,DelegateNo  as DelegateNo,ifnull(ds.name,wd.name)  as name ,ifnull(ds.email,wd.email)  as email ,ifnull(ds.mobile,wd.mobile)  as mobile,is_present  as is_present,0 as is_faculty from delegateslot ds left join workshop_delegates wd on ds.DelegateNo collate utf8mb4_croatian_ci = wd.delegate_no where SlotId = {SlotID} ) union (select FacSlotId,SlotId,FacultySlotID collate utf8mb4_croatian_ci  as DelegateNo,ifnull(ds.name,concat(ifnull(u.prefix,''),ifnull(u.full_name,''))) collate utf8mb4_croatian_ci  as name ,ifnull(ds.email,u.email) collate utf8mb4_croatian_ci as email ,ifnull(ds.mobile,u.mobile) collate utf8mb4_croatian_ci as mobile ,is_present collate utf8mb4_croatian_ci as is_present ,1 as is_faculty from facultyslot ds left join users u on ds.FacultySlotID = u.user_id  where SlotId = {SlotID})) as delegates;")
            results = conn.execute(stmt).all()
            result = [dict(r._mapping) for r in results] if results else None
            return result
            
    def Get_workshop_date(self):
        with engine.connect() as conn:
            stmt = text ("select date,concat(DATE_FORMAT(Date, '%d-%m-%Y'),' (',dayname(Date),').') as display_date from mcourseslots group by Date;")
            results = conn.execute(stmt).all()
            result = [dict(r._mapping) for r in results] if results else None
            return result
        
    def get_date_wise_session_master(self,workshop_date,workshop_time,workshop_id):
        with engine.connect() as conn:
            stmt = text (f"select *,(select count(*) from delegateslot where SlotId = mc.SlotId) as delegate_sloted  from mcourseslots mc inner join workshopcourses wc ON wc.WorkshopCourseId = mc .WorkshopCourseId WHERE WorkshopId = {workshop_id} AND Date = '{workshop_date}' and  StartTime = '{workshop_time}' group by SlotId order by StartTime,hall")
            results = conn.execute(stmt).all()
            result = [dict(r._mapping) for r in results] if results else None
            return result
        
    def get_date_wise_session(self,workshop_date,workshop_time,workshop_id):
        with engine.connect() as conn:
            stmt = text (f"select * from mcourseslots mc inner join workshopcourses wc ON wc.WorkshopCourseId = mc .WorkshopCourseId WHERE WorkshopId = {workshop_id} AND Date = '{workshop_date}' and  StartTime = '{workshop_time}' order by StartTime,hall")
            results = conn.execute(stmt).all()
            result = [dict(r._mapping) for r in results] if results else None
            return result
    
    def get_session(self,SlotId):
        with engine.connect() as conn:
            stmt = text (f"select * from mcourseslots mc inner join workshopcourses wc on wc.WorkshopCourseId = mc.WorkshopCourseId inner join m_workshop mw on mw.WorkshopId = wc.WorkshopId where mc.SlotId = {SlotId}")
            results = conn.execute(stmt).first()
            result = dict(results._mapping) if results else None
            return result
            
    def update_attendance(self,speaker_data):
        stmt = 'UPDATE delegateslot SET '
        stmt1 = 'UPDATE facultyslot SET '
        keyname = ["is_present"]
        judge_pkid = []
        author_pkid = []
        for j in keyname:
            stmt = stmt + j +" = case "
            stmt1 = stmt1 + j +" = case "
            for i in speaker_data:
                if int(i["is_faculty"]) == 0:
                    stmt = stmt + ' When DelSlotID = ' + str(i["DelSlotID"]) + " then " + i[j]
                    author_pkid.append(i["DelSlotID"])
                else:
                    stmt1 = stmt1 + ' When FacSlotId = ' + str(i["DelSlotID"]) + " then " + i[j]
                    judge_pkid.append(i["DelSlotID"])
            stmt = stmt + ' END, '
            stmt1 = stmt1 + ' END, '
        stmt = stmt[:-2]
        stmt1 = stmt1[:-2]
        author_pkid =  ','.join(str(e) for e in author_pkid)
        judge_pkid =  ','.join(str(e) for e in judge_pkid)
        stmt = stmt + ' Where DelSlotID in (' +str(author_pkid)+' );'
        stmt1 = stmt1 + ' Where FacSlotId in (' +str(judge_pkid)+' );'
        with engine.connect() as conn:
            if author_pkid:
                result = conn.execute(text(stmt))
            if judge_pkid:
                result = conn.execute(text(stmt1))
            conn.commit()
        return "Success"
    
    def Get_Delegates(self,SlotId):
        with engine.connect() as conn:
            stmt=text("SELECT ds.DelSlotID, ds.SlotId, ds.DelegateNo, ds.user_id, ifnull(ds.name,wd.name) as name, ifnull(ds.email,wd.email) as email, ifnull(ds.mobile ,wd.mobile ) as mobile FROM delegateslot ds left join workshop_delegates  wd on ds.DelegateNo collate utf8mb4_croatian_ci  = wd.delegate_no where ds.SlotId="+str(SlotId)+";")
            # stmt=text("SELECT fs.SlotId,u.full_name,u.email,u.mobile FROM users u left join facultyslot fs on fs.FacultySlotID=u.user_id where fs.SlotId='"+str(SlotId)+"' UNION SELECT name as full_name,email,mobile FROM facultyslot where SlotId="+str(SlotId)+";")
            result=conn.execute(stmt).all()
            results=[dict(r._mapping) for r in result] if result else None
            return results

    def Get_faculty_not_slotted_slot_time(self,slotid,course_id):
        with engine.connect() as conn:
            stmt=text(f"""select u.user_id,u.prefix,u.full_name,u.membership_no,u.email,u.mobile,(select count(*) from facultyslot t1 where t1.FacultySlotID = u.user_id ) as faculty_count,(select group_concat(concat(DATE_FORMAT(Date, "%d-%m-%Y"),' (',dayname(Date),') ',StartTime,' - ',EndTime, ' (',CourseName,') ') SEPARATOR '<br>' )  from facultyslot f inner join mcourseslots mcs on f.SlotId = mcs.SlotId inner join workshopcourses mc on mc.WorkshopCourseId = mcs.WorkshopCourseId where f.FacultySlotID = u.user_id) as Course_already_slot from facultycourse fc left join facultyslot fs on fc.user_id = fs.FacultySlotID and fs.SlotId= {slotid} inner join users u on fc.user_id = u.user_id where  fc.course_id = {course_id} and fs.FacSlotId is null; """)
            result=conn.execute(stmt).all()
            results=[dict(r._mapping) for r in result] if result else None
            return results

    def Get_faculty_slot_time(self,SlotId):
        with engine.connect() as conn:
            # stmt=text("select * from workshopcourses wc left join  mcourseslots ms on wc.WorkshopCourseId=ms.WorkshopCourseId where wc.WorkshopCourseId="+str(course_id)+";")
            stmt=text("SELECT * FROM facultyslot fs left join users u  on fs.FacultySlotID = u.user_id  left join mcourseslots ms on fs.SlotId=ms.SlotId left join workshopcourses wc on wc.WorkshopCourseId=ms.WorkshopCourseId where fs.SlotId="+str(SlotId)+" ;")
            result=conn.execute(stmt).all()
            results=[dict(r._mapping) for r in result] if result else None
            return results
    
    def CheckFacultyAlreadyExsits(self,SlotId,name):
        with engine.connect() as conn:
            stmt=text("select count(*) as count from facultyslot where name='"+str(name)+"' and SlotId="+str(SlotId)+";")
            result=conn.execute(stmt).first()
            results = dict(result._mapping) if result else None
            return results
    
    def CheckDelegateAlreadyExsits(self,SlotId,name):
        with engine.connect() as conn:
            stmt=text("select count(*) as count from delegateslot where name='"+str(name)+"' and SlotId="+str(SlotId)+";")
            result=conn.execute(stmt).first()
            results = dict(result._mapping) if result else None
            return results

    def DeleteFacultySlot(self,FacSlotId):
        with engine.connect() as conn:
            stmt=text("DELETE FROM facultyslot WHERE FacSlotId="+str(FacSlotId)+" ;")
            stmt1=text("update faculty_preference_timing set sloted_course_id = NULL WHERE faculty_pre_time_id = (select Prefer_Slot_id from facultyslot WHERE FacSlotId= "+str(FacSlotId)+") ;")
            result=conn.execute(stmt1)
            result=conn.execute(stmt)
            conn.commit()
            if result:
                return "success"
            else:
                return "fail"

    def DeleteDelegateSlot(self,DelSlotID):
        with engine.connect() as conn:
            stmt=text("DELETE FROM delegateslot WHERE DelSlotID="+str(DelSlotID)+" ;")
            result=conn.execute(stmt)
            conn.commit()
            if result:
                return "success"
            else:
                return "fail"


    def get_admin_data(self):
        with engine.connect() as conn:
            stmt=text("select * from workshop_admim ;")
            result=conn.execute(stmt).all()
            results=[dict(r._mapping) for r in result] if result else None
            return results

    def edit_admin(self,admin_id):
        with engine.connect() as conn:
            stmt=text("select * from workshop_admim where admin_id="+str(admin_id)+";")
            result=conn.execute(stmt).first()
            results=dict(result._mapping) if result else None
            return results
            
    def Update_admin_data(self,admin_id,data):
        with engine.connect() as conn:
            stmt   = self.workshop_admim.update().where(self.workshop_admim.c.admin_id.in_([admin_id])).values(data)
            result = conn.execute(stmt)
            conn.commit()
            return 'success'
            
    def add_admin_data(self,data):
        with engine.connect() as conn:
            result = conn.execute(self.workshop_admim.insert(), data)
            conn.commit()
            return "success"

    def get_workshop_name(self):
        with engine.connect() as conn:
            stmt=text("select WorkshopName from m_workshop ;")
            result=conn.execute(stmt).all()
            results=[dict(r._mapping) for r in result] if result else None
            return results

    def get_workshop_id(self,WorkshopName):
        with engine.connect() as conn:
            stmt=text("select workshopId from m_workshop where WorkshopName in ("+str(WorkshopName)+");")
            result=conn.execute(stmt).all()
            results=[dict(r._mapping) for r in result] if result else None
            return results

    def check_user_name(self,user_name,admin_id):
        with engine.connect() as conn:
            if admin_id==0:
                stmt=text("SELECT count(*) as user_name FROM workshop_admim where user_name='"+str(user_name)+"';")
            else:
                stmt=text("SELECT count(*) as user_name FROM workshop_admim where user_name='"+str(user_name)+"' and admin_id!="+str(admin_id)+";")
            result=conn.execute(stmt).first()
            results=dict(result._mapping) if result else None
            return results
        
    def view_delegate_data(self,SlotId):
        with engine.connect() as conn:
            stmt=text("SELECT wd.* FROM delegateslot ds left join workshop_delegates wd on ds.DelegateNo collate utf8mb4_general_ci =wd.delegate_no where ds.SlotId="+str(SlotId)+";")
            result=conn.execute(stmt).all()
            results=[dict(r._mapping) for r in result] if result else None
            return results

    def add_new_faculty(self,data):
        with engine.connect() as conn:
            result = conn.execute(self.facultyslot.insert(), data)
            conn.commit()
            return "success"
    
    def add_new_delegate(self,data):
        with engine.connect() as conn:
            result = conn.execute(self.delegateslot.insert(), data)
            conn.commit()
            return "success"

    def Chosen_delegates(self,course_id):
        with engine.connect() as conn:
            stmt=text("select wd.*,dc.*,cs.* from delegatecourse dc inner join workshop_delegates wd on dc.Delegate_no collate utf8mb4_general_ci = wd.Delegate_no inner join delegateslot ds on ds.DelegateNo collate utf8mb4_general_ci = wd.delegate_no inner join mcourseslots cs on ds.SlotId = cs.SlotId and dc.course_id = WorkshopCourseId where dc.course_id = {};".format(course_id))
            result=conn.execute(stmt).all()
            results=[dict(r._mapping) for r in result] if result else None
            return results

    def Chosen_faculty(self,course_id):
        with engine.connect() as conn:
            stmt=text("select * from facultyslot ds left join users wd on wd.user_id = ds.FacultySlotID  inner join mcourseslots cs on ds.SlotId = cs.SlotId inner join workshopcourses wc on wc.WorkshopCourseId = cs.WorkshopCourseId where cs.WorkshopCourseId = {};".format(course_id))
            result=conn.execute(stmt).all()
            results=[dict(r._mapping) for r in result] if result else None
            return results
    
    def nonchosen_faculty(self,course_id):
        with engine.connect() as conn:
            stmt=text("""select *,(select count(*) from facultyslot where FacultySlotID = dc.user_id) as faculty_count,(select group_concat(concat(DATE_FORMAT(Date, "%d-%m-%Y"),' (',dayname(Date),') ',StartTime,' - ',EndTime, ' (<b>',CourseName,'</b>) ') SEPARATOR '<br>' )  from facultyslot f inner join mcourseslots mcs on f.SlotId = mcs.SlotId inner join workshopcourses mc on mc.WorkshopCourseId = mcs.WorkshopCourseId where f.FacultySlotID = wd.user_id) as Course_already_slot from facultycourse dc inner join users wd on dc.user_id = wd.user_id  where course_id = {} ;""".format(course_id))
            result=conn.execute(stmt).all()
            results=[dict(r._mapping) for r in result] if result else None
            return results
        
    def get_user(self,user_id):
        with engine.connect() as conn:
            stmt=text("select * from users where user_id = {} ;".format(user_id))
            result=conn.execute(stmt).first()
            results=dict(result._mapping) if result else None
            return results
            
    def get_facuty_sloted_list(self,user_id):
        with engine.connect() as conn:
            stmt=text(f"select * from facultyslot fs inner join mcourseslots ms on fs.SlotId = ms.SlotId  inner join workshopcourses d  on ms.WorkshopCourseId = d.WorkshopCourseId inner join m_workshop w on d.workshopId = w.workshopId where FacultySlotID = {user_id};")
            result=conn.execute(stmt).all()
            results=[dict(r._mapping) for r in result] if result else None
            return results

    def nonchosen_deleg(self,course_id):
        with engine.connect() as conn:
            stmt=text("select * from delegatecourse dc inner join workshop_delegates wd on dc.Delegate_no COLLATE utf8mb4_0900_ai_ci = wd.Delegate_no and dc.Delegate_no COLLATE utf8mb4_0900_ai_ci  not in (select ds.DelegateNo from delegateslot ds inner join mcourseslots ms on ds.SlotId = ms.SlotId and ds.course_id = ms.WorkshopCourseId where ms.WorkshopCourseId = dc.course_id) where course_id = {} ;".format(course_id))
            result=conn.execute(stmt).all()
            results=[dict(r._mapping) for r in result] if result else None
            return results

    def get_facuty_slot_list(self,course_id,user_id):
        stmt = text("select m.*,f.FacultySlotID,f.FacSlotId,(select count(*) from facultyslot where slotId = m.SlotId) as total_delegate_count from mcourseslots m left join facultyslot f on f.slotId = m.SlotId and f.FacultySlotID = {}  where WorkshopCourseId = {};".format(user_id,course_id))
        with engine.connect() as conn:
            results = conn.execute(stmt).all()
            results = [dict(r._mapping) for r in results] if results else None
            return results

    def get_slot_list(self,course_id):
        stmt = text("select m.*,(select count(*) from delegateslot where slotId = m.SlotId) as total_delegate_count from mcourseslots m where WorkshopCourseId = {};".format(course_id))
        with engine.connect() as conn:
            results = conn.execute(stmt).all()
            results = [dict(r._mapping) for r in results] if results else None
            return results
    
    def insert_faculties_Slot(self,slot_id,delegate_no):
        stmt = text("insert into facultyslot (SlotId,FacultySlotID) values('{}','{}');".format(slot_id,delegate_no))
        with engine.connect() as conn:
            results = conn.execute(stmt)
            conn.commit()
            return results
        
    def insert_delegate_Slot(self,slot_id,delegate_no,course_id):
        connection = engine.raw_connection()
        cursor = connection.cursor()
        cursor.callproc("delegate_sloting",[delegate_no,course_id,slot_id])
        columns = [column[0] for column in cursor.description]
        results = []
        for row in cursor.fetchall():
            results.append(dict(zip(columns, row)))
        cursor.close()
        connection.commit()
        if results :
            return results[0]
        else :
            return None
        
    def insert_delegate_Slot_v1(self,slot_id,delegate_no,course_id):
        connection = engine.raw_connection()
        cursor = connection.cursor()
        cursor.callproc("delegate_sloting",[delegate_no,course_id,slot_id])
        columns = [column[0] for column in cursor.description]
        results = []
        for row in cursor.fetchall():
            results.append(dict(zip(columns, row)))
        cursor.close()
        connection.commit()
        if results :
            return results[0]
        else :
            return None
    
    def get_date_session_data(self,date,workshop_id):
        stmt = text("select * from mcourseslots cs inner join workshopcourses wc on cs.WorkshopCourseId = wc.WorkshopCourseId where WorkshopId = {} and Date = '{}' order by Date,StartTime ;".format(workshop_id,date))
        with engine.connect() as conn:
            results = conn.execute(stmt).all()
            results = [dict(r._mapping) for r in results] if results else None
            return results
    
    def get_date_session_time_data(self,date,time,workshop_id):
        stmt = text("select * from mcourseslots cs inner join workshopcourses wc on cs.WorkshopCourseId = wc.WorkshopCourseId where WorkshopId = {} and Date = '{}' and StartTime = '{}' order by Date,StartTime ;".format(workshop_id,date,time))
        with engine.connect() as conn:
            results = conn.execute(stmt).all()
            results = [dict(r._mapping) for r in results] if results else None
            return results

    def get_date_session_speaker_time_data(self,date,time,workshop_id):
        stmt = text(f"""(SELECT DelSlotID,ds.SlotId,DelegateNo  COLLATE utf8mb4_general_ci, IFNULL(ds.name, wd.name) COLLATE utf8mb4_general_ci AS name,IFNULL(ds.email, wd.email) COLLATE utf8mb4_general_ci AS email,IFNULL(ds.mobile, wd.mobile) COLLATE utf8mb4_general_ci AS mobile,is_present COLLATE utf8mb4_general_ci as is_present ,0 AS is_faculty,'Delegate' COLLATE utf8mb4_general_ci AS Role FROM delegateslot ds LEFT JOIN workshop_delegates wd ON ds.DelegateNo COLLATE utf8mb4_general_ci = wd.delegate_no COLLATE utf8mb4_general_ci INNER JOIN mcourseslots mcs ON ds.SlotId = mcs.SlotId INNER JOIN workshopcourses wc ON wc.WorkshopCourseId = mcs.WorkshopCourseId WHERE WorkshopId = {workshop_id} AND Date = '{date}' and StartTime = '{time}') UNION (SELECT  FacSlotId,ds.SlotId,FacultySlotID  COLLATE utf8mb4_general_ci, IFNULL(ds.name, concat(ifnull(wd.prefix,''),full_name)) COLLATE utf8mb4_general_ci AS name,IFNULL(ds.email, wd.email) COLLATE utf8mb4_general_ci AS email,IFNULL(ds.mobile, wd.mobile) COLLATE utf8mb4_general_ci AS mobile,is_present COLLATE utf8mb4_general_ci as is_present,1 AS is_faculty,'Faculty' COLLATE utf8mb4_general_ci AS Role FROM facultyslot ds LEFT JOIN users wd ON ds.FacultySlotID COLLATE utf8mb4_general_ci = wd.user_id  COLLATE utf8mb4_general_ci INNER JOIN mcourseslots mcs ON ds.SlotId = mcs.SlotId INNER JOIN workshopcourses wc ON wc.WorkshopCourseId = mcs.WorkshopCourseId WHERE WorkshopId = {workshop_id} AND Date = '{date}' and StartTime = '{time}')""")
        with engine.connect() as conn:
            result = conn.execute(stmt).all()
            results = [dict(r._mapping) for r in result] if result else None
            return results

    def get_date_session_speaker_data(self,date,workshop_id):
        stmt = text(f"""(SELECT DelSlotID,ds.SlotId,DelegateNo  COLLATE utf8mb4_general_ci, IFNULL(ds.name, wd.name) COLLATE utf8mb4_general_ci AS name,IFNULL(ds.email, wd.email) COLLATE utf8mb4_general_ci AS email,IFNULL(ds.mobile, wd.mobile) COLLATE utf8mb4_general_ci AS mobile,is_present COLLATE utf8mb4_general_ci,0 AS is_faculty,'Delegate' COLLATE utf8mb4_general_ci AS Role FROM delegateslot ds LEFT JOIN workshop_delegates wd ON ds.DelegateNo COLLATE utf8mb4_general_ci = wd.delegate_no COLLATE utf8mb4_general_ci INNER JOIN mcourseslots mcs ON ds.SlotId = mcs.SlotId INNER JOIN workshopcourses wc ON wc.WorkshopCourseId = mcs.WorkshopCourseId WHERE WorkshopId = {workshop_id} AND Date = '{date}') UNION (SELECT  FacSlotId,ds.SlotId,FacultySlotID  COLLATE utf8mb4_general_ci, IFNULL(ds.name, concat(ifnull(wd.prefix,''),full_name)) COLLATE utf8mb4_general_ci AS name,IFNULL(ds.email, wd.email) COLLATE utf8mb4_general_ci AS email,IFNULL(ds.mobile, wd.mobile) COLLATE utf8mb4_general_ci AS mobile,is_present COLLATE utf8mb4_general_ci,1 AS is_faculty,'Faculty' COLLATE utf8mb4_general_ci AS Role FROM facultyslot ds LEFT JOIN users wd ON ds.FacultySlotID COLLATE utf8mb4_general_ci = wd.user_id  COLLATE utf8mb4_general_ci INNER JOIN mcourseslots mcs ON ds.SlotId = mcs.SlotId INNER JOIN workshopcourses wc ON wc.WorkshopCourseId = mcs.WorkshopCourseId WHERE WorkshopId = {workshop_id} AND Date = '{date}')""")
        with engine.connect() as conn:
            result = conn.execute(stmt).all()
            results = [dict(r._mapping) for r in result] if result else None
            return results
            
    def master_faculty_list(self,workshop_id):
        stmt = text(f""" (select f.user_id,concat(ifnull(u.prefix,''),ifnull(u.full_name,'')) as full_name,u.membership_no,u.email,u.mobile,group_concat(distinct(concat('-',wc.CourseName)) SEPARATOR '<br>' ) as course_name,group_concat(distinct(concat( case when sloted_course_id is not null then '<span style="background-color: orange;"> - ' else '<span>' end,  DATE_FORMAT(ms.Date, "%d-%m-%Y"),' (',dayname(ms.Date),') ',DATE_FORMAT(ms.Start_time, "%H:%i"),' - ',DATE_FORMAT(ms.End_time, "%H:%i"),case when sloted_course_id is not null then concat(' (', (select CourseName from workshopcourses where WorkshopCourseId = sloted_course_id) ,') ') else " " end ,'</span>')) SEPARATOR '<br>') as slotes 
                from facultycourse  f 
                left join workshopcourses wc on f.course_id = wc.WorkshopCourseId 
                left join faculty_preference_timing fpt on f.user_id = fpt.user_id 
                left join master_slotes ms on fpt.master_slot_id = ms.master_slote_id 
                inner join users u on f.user_id = u.user_id 
                where f.Workshop_id = {workshop_id} group by f.user_id order by full_name)
                union
                (select f.FacultySlotID,name as full_name,u.membership_no,u.email,u.mobile,group_concat(distinct(concat('-',wc.CourseName)) SEPARATOR '<br>' ) as course_name,
                group_concat(distinct(concat( '<span style="background-color: orange;"> - ',  
                DATE_FORMAT(mc.Date, "%d-%m-%Y"),' (',dayname(mc.Date),') ',mc.StartTime,' - ',mc.EndTime,concat(' (', (wc.CourseName) ,') '),'</span>')) SEPARATOR '<br>') as slotes
                from facultyslot f 
                inner join mcourseslots mc on f.SlotId = mc.SlotId
                left join workshopcourses wc on mc.WorkshopCourseId = wc.WorkshopCourseId 
                inner join users u on f.FacultySlotID = u.user_id 
                where wc.WorkshopId = {workshop_id} and f.name is not null group by f.FacultySlotID order by full_name); """)
        with engine.connect() as conn:
            results = conn.execute(stmt).all()
            results = [dict(r._mapping) for r in results] if results else None
            return results
            
    
    def InsertfacultyCourses(self,user_id,course_id,workshop_id):
        stmt = text(f"select * from facultycourse where user_id = {user_id} and course_id = {course_id};")
        insert_stmt = text(f"insert into facultycourse (user_id, Workshop_id, course_id) values({user_id},{workshop_id},{course_id})")
        with engine.connect() as conn:
            results = conn.execute(stmt).first()
            results = dict(results._mapping) if results else None
            if not results:
                results = conn.execute(insert_stmt)
                conn.commit()
            return results
            
    def Get_data_faculty_slot(self,course_id,workshopId,SlotId):
        sets = []
        try:
            connection = engine.raw_connection()
            cursor     = connection.cursor()
            cursor.callproc("usp_get_st_faculty_for_slot",[course_id,workshopId,SlotId])
            while 1:
                names = [c[0] for c in cursor.description]
                set_ = []
                while 1:
                    row_raw = cursor.fetchone()
                    if row_raw is None:
                        break
                    row = dict(zip(names, row_raw))
                    set_.append(row)
                sets.append(list(set_))
                if cursor.nextset() is None:
                    break
                if cursor.description is None:
                    break
        finally:
            connection.close()
        return sets
        
    def get_workshop_delegates(self,workshop_id):
        stmt = text(f"""select wd.delegate_id,dc.delegate_no, membership_no, name, email, mobile,group_concat(distinct CourseName) from workshop_delegates wd 
                        inner join delegatecourse dc on wd.Delegate_no = dc.delegate_no
                        inner join workshopcourses wc on wc.WorkshopCourseId = dc.course_id
                        where wc.WorkshopId = {workshop_id} group by dc.delegate_no;
                        """)
        with engine.connect() as conn:
            results = conn.execute(stmt).all()
            results = [dict(r._mapping) for r in results] if results else None
            return results
    
    def get_workshop_delegates_by_id(self,workshop_id,delegate_ids):
        stmt = text(f"""select wd.delegate_id,dc.delegate_no, membership_no, name, email, mobile,group_concat(distinct CourseName separator '<br>') as courses from workshop_delegates wd 
                        inner join delegatecourse dc on wd.Delegate_no = dc.delegate_no
                        inner join workshopcourses wc on wc.WorkshopCourseId = dc.course_id
                        where wc.WorkshopId = {workshop_id} and wd.delegate_id in ('{delegate_ids}') group by dc.delegate_no;
                        """)
        with engine.connect() as conn:
            results = conn.execute(stmt).all()
            results = [dict(r._mapping) for r in results] if results else None
            return results
            
    def get_workshop_pending_delegates(self,workshop_id):
        stmt = text(f"""select wd.delegate_id,dc.delegate_no, membership_no, name, email, mobile,group_concat(distinct CourseName) from workshop_delegates wd 
                        inner join delegatecourse dc on wd.Delegate_no = dc.delegate_no
                        inner join workshopcourses wc on wc.WorkshopCourseId = dc.course_id
                        where wc.WorkshopId = {workshop_id} and wd.delegate_no in (select distinct Delegate_no
                        from delegatecourse dc left join delegateslot ds on DelegateNo collate utf8mb4_croatian_ci = Delegate_no and dc.course_id = ds.course_id 
                        where ds.course_id is null and Workshop_id = {workshop_id}) 
                        group by dc.delegate_no;
                        """)
        with engine.connect() as conn:
            results = conn.execute(stmt).all()
            results = [dict(r._mapping) for r in results] if results else None
            return results
    
    def get_workshop_pending_delegates_by_id(self,workshop_id,delegate_ids):
        stmt = text(f"""select wd.delegate_id,dc.delegate_no, membership_no, name, email, mobile,group_concat(distinct CourseName separator '<br>') as courses from workshop_delegates wd 
                        inner join delegatecourse dc on wd.Delegate_no = dc.delegate_no
                        inner join workshopcourses wc on wc.WorkshopCourseId = dc.course_id
                        where wc.WorkshopId = {workshop_id} and wd.delegate_id in ('{delegate_ids}') group by dc.delegate_no;
                        """)
        with engine.connect() as conn:
            results = conn.execute(stmt).all()
            results = [dict(r._mapping) for r in results] if results else None
            return results
    
    def get_session_time(self,workshop_date,workshop_id):
        with engine.connect() as conn:
            stmt = text(f"select StartTime,EndTime from mcourseslots m inner join workshopcourses w on m.WorkshopCourseId = w.WorkshopCourseId where WorkshopId = {workshop_id} and Date = '{workshop_date}' group by StartTime order by StartTime;")
            results = conn.execute(stmt).all()
            results = [dict(r._mapping) for r in results] if results else None
            return results

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists