Sindbad~EG File Manager

Current Path : /home/numerotech/conference.numerotech.com/food_kit_scan_badges/core/model/
Upload File :
Current File : //home/numerotech/conference.numerotech.com/food_kit_scan_badges/core/model/BadgeUserModel.py

from sqlalchemy import create_engine, MetaData, Table, insert, select,update,delete,text
from sqlalchemy.sql import and_, or_
from core import app
import json
# import sqlite3  




engine        = create_engine(app.config['DATABASE_URI'],pool_size=5000,pool_pre_ping=True,pool_recycle=3600)
engine_fk     = create_engine(app.config['DATABASE_URI_FK'],pool_size=5000,pool_pre_ping=True,pool_recycle=3600)
# sqlite_engine = create_engine(app.config['SQLITE_DATABASE_URI'],pool_pre_ping=True,pool_recycle=3600)

class BadgeUserModel():
	def __init__(self):
		try:
			self.meta = MetaData()
				 
		except Exception as e:
			print(e)

	# def createConfTables(self,conf_id,conf_key,curr_dt):
	# 	with engine_fk.connect() as conn:
	# 	#1. conf-delegate-table
	# 	conf_del_table_stmt = text("CREATE TABLE IF NOT EXISTS `delegates_"+conf_key+"` ( `delegate_id` int(11) unsigned zerofill NOT NULL AUTO_INCREMENT,`user_id` int DEFAULT NULL, `delegate_no` int DEFAULT NULL,`conf_id` int DEFAULT NULL, `role` varchar(100) DEFAULT NULL, `full_name` varchar(250) DEFAULT NULL, `email` varchar(70) DEFAULT NULL,  `mobile` varchar(30) DEFAULT NULL,  `city` varchar(70) DEFAULT NULL,  `state` varchar(70) DEFAULT NULL,  `membership_no` varchar(50) DEFAULT NULL, `mc_number` varchar(70) DEFAULT NULL  , `company_name` varchar(600) DEFAULT NULL,  `counter` int DEFAULT NULL,  `batch` varchar(30) DEFAULT NULL,  `is_kit` int DEFAULT '0',  `is_exhibitor` int DEFAULT '0',  `signed_by` varchar(100) DEFAULT NULL,  `comments` varchar(350) DEFAULT NULL,  `signed_on` datetime DEFAULT NULL,  `sync_at` datetime DEFAULT NULL,  `devie_type` varchar(60) DEFAULT NULL,  `user_type` varchar(100) DEFAULT NULL, `created_at` datetime DEFAULT NULL, `updated_at` datetime DEFAULT NULL, `deleted_at` datetime DEFAULT NULL,  `b_color` varchar(45) DEFAULT NULL,  `commitment_content` mediumtext, is_updated INT DEFAULT NULL, scan_limit int(11) default 1, PRIMARY KEY (`delegate_id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;")
	# 	# conf_del_table_stmt = text("CREATE TABLE IF NOT EXISTS `delegates_"+conf_key+"` ( `delegate_id` int(11) unsigned zerofill NOT NULL AUTO_INCREMENT,`user_id` int DEFAULT NULL, `delegate_no` int DEFAULT NULL,`conf_id` int DEFAULT NULL, `role` varchar(100) DEFAULT NULL, `full_name` varchar(250) DEFAULT NULL, `email` varchar(70) DEFAULT NULL,  `mobile` varchar(30) DEFAULT NULL,  `city` varchar(70) DEFAULT NULL,  `state` varchar(70) DEFAULT NULL,  `membership_no` varchar(50) DEFAULT NULL, `mc_number` varchar(70) DEFAULT NULL  , `company_name` varchar(600) DEFAULT NULL,  `counter` int DEFAULT NULL,  `batch` varchar(30) DEFAULT NULL,  `is_kit` int DEFAULT '0',  `is_exhibitor` int DEFAULT '0',  `signed_by` varchar(100) DEFAULT NULL,  `comments` varchar(350) DEFAULT NULL,  `signed_on` datetime DEFAULT NULL,  `sync_at` datetime DEFAULT NULL,  `devie_type` varchar(60) DEFAULT NULL,  `user_type` varchar(100) DEFAULT NULL, `created_at` datetime DEFAULT NULL, `updated_at` datetime DEFAULT NULL, `deleted_at` datetime DEFAULT NULL,  `b_color` varchar(45) DEFAULT NULL,  `commitment_content` mediumtext, PRIMARY KEY (`delegate_id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;")
	# 	result = conn.execute(conf_del_table_stmt)
	# 	#2. Food counter delegate session table

	# 	conf_fcdel_session_stmt = text("CREATE TABLE IF NOT EXISTS `fc_delegates_sessions_"+conf_key+"` (`delegate_session_id` int NOT NULL PRIMARY KEY AUTO_INCREMENT,`delegate_no` int(11),`session_id` int(11),`created_at` datetime,`updated_at` datetime,`deleted_at` datetime,`limit` int DEFAULT '1' );")
	# 	result = conn.execute(conf_fcdel_session_stmt)


	# 	#3. Fodd conter session entry table
	# 	conf_fcdel_sessionentry_stmt = text("CREATE TABLE IF NOT EXISTS `fc_sessions_entry_"+conf_key+"` (`session_entry_id` int unsigned NOT NULL AUTO_INCREMENT,  `delegate_no` int DEFAULT NULL, `session_id` int unsigned DEFAULT NULL,  `created_at` datetime DEFAULT NULL,  `updated_at` datetime DEFAULT NULL,  `deleted_at` datetime DEFAULT NULL,  `is_token` int DEFAULT NULL, `ref_delegate_no` int DEFAULT NULL,  `ref_name` varchar(120) DEFAULT NULL,  `ref_mobile` varchar(30) DEFAULT NULL,`scan_system_name` varchar(120) DEFAULT NULL,  PRIMARY KEY (`session_entry_id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 ;")
	# 	# conf_fcdel_sessionentry_stmt = text("CREATE TABLE IF NOT EXISTS `fc_sessions_entry_"+conf_key+"` (`session_entry_id` int unsigned NOT NULL AUTO_INCREMENT,  `delegate_no` int DEFAULT NULL, `session_id` int unsigned DEFAULT NULL,  `created_at` datetime DEFAULT NULL,  `updated_at` datetime DEFAULT NULL,  `deleted_at` datetime DEFAULT NULL,  `is_token` int DEFAULT NULL, `ref_delegate_no` int DEFAULT NULL,  `ref_name` varchar(120) DEFAULT NULL,  `ref_mobile` varchar(30) DEFAULT NULL,  PRIMARY KEY (`session_entry_id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 ;")
	# 	result = conn.execute(conf_fcdel_sessionentry_stmt)



	# 	#4. Kit counter delegate session table
	# 	conf_kcdel_entry_stmt = text("CREATE TABLE IF NOT EXISTS `kc_delegates_sessions_"+conf_key+"` ( `delegate_session_id` int NOT NULL AUTO_INCREMENT,  `delegate_no` int DEFAULT NULL,  `session_id` int DEFAULT NULL,  `created_at` datetime DEFAULT NULL,  `updated_at` datetime DEFAULT NULL,  `deleted_at` datetime DEFAULT NULL,  `limit` int DEFAULT '1',  PRIMARY KEY (`delegate_session_id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 ;")
	# 	result = conn.execute(conf_kcdel_entry_stmt)


	# 	#5. kit counter session entry table
	# 	conf_kcdel_entry_stmt = text("CREATE TABLE IF NOT EXISTS `kc_sessions_entry_"+conf_key+"` (`session_entry_id` int unsigned NOT NULL AUTO_INCREMENT,  `delegate_no` int DEFAULT NULL,  `session_id` int unsigned DEFAULT NULL,  `created_at` datetime DEFAULT NULL,  `updated_at` datetime DEFAULT NULL,  `deleted_at` datetime DEFAULT NULL,  `is_token` int DEFAULT NULL,  `ref_delegate_no` int DEFAULT NULL,  `ref_name` varchar(120) DEFAULT NULL,  `ref_mobile` varchar(30) DEFAULT NULL,  PRIMARY KEY (`session_entry_id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 ;")
	# 	result = conn.execute(conf_kcdel_entry_stmt)

	# 	conn.close()
	# 	conn_1 = engine.connect()

	# 	del_table_name              = "numerotech_foodkit."+"delegates_"+conf_key
	# 	fc_del_session_table_name   = "numerotech_foodkit."+"fc_delegates_sessions_"+conf_key
	# 	fc_session_entry_table_name = "numerotech_foodkit."+"fc_sessions_entry_"+conf_key

	# 	kc_del_session_table_name   = "numerotech_foodkit."+"kc_delegates_sessions_"+conf_key
	# 	kc_session_entry_table_name = "numerotech_foodkit."+"kc_sessions_entry_"+conf_key
	# 	# update the above 5 table name configure with conference table
	# 	map_conf_table_stmt = text ("update conference set del_table_name = '"+del_table_name+"' ,fc_del_session_table_name = '"+fc_del_session_table_name+"' , fc_session_entry_table_name = '"+fc_session_entry_table_name+"', kc_del_session_table_name = '"+kc_del_session_table_name+"', kc_session_entry_table_name = '"+kc_session_entry_table_name+"'  , updated_at = '"+curr_dt+"'   where conf_id = "+ str(conf_id) + "  ;")
	# 	# print(map_conf_table_stmt)
	# 	result_5 = conn_1.execute(map_conf_table_stmt)
	# 	conn_1.close()
	# 	if result :
	# 		return "success"
	# 	else:
	# 		return None

	# Create table using storeproc
	def createConfTables(self,conf_id,conf_key,curr_dt):
		main_db     = "numerotech_primary_db"
		foodkit_db  = "numerotech_foodkit"
		connection  = engine_fk.raw_connection()
		cursor      = connection.cursor()
		cursor.callproc("create_table_for_conf",[main_db,foodkit_db,conf_id,conf_key])
		if cursor.description :
			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 "success"
				# return results
			else :
				return None 
		else :
			cursor.close()
			connection.commit()
			return None

	# end		


	# Dec 27 ,2022 Ganesan J for upload csv file
	def getDelegatesTableColumn(self,conf_id,del_table_name):	
		with engine.connect() as conn:
			stmt    = text("select * from "+del_table_name+" limit 1;")	
			results = conn.execute(stmt).keys()
			if results : 
				return results
			else:
				return None 

	#  Jan 05,2022
	# Multiple Resutl set
	def insertBulkUploadTempTablePreview(self,conf_id,insert_query,columnss,del_table_name,del_nos):
		sets = []
		try:
			connection = engine.raw_connection()
			cursor = connection.cursor()
			cursor.callproc("usp_delegates_bulk_upload_preview",[conf_id,insert_query,columnss,del_table_name,del_nos])
			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.commit()
			connection.close()
			
		return sets 

					

	def insertBulkUpload(self,conf_id,insert_query,columnss,del_table_name,checked_del_nos):
		with engine.connect() as conn:
			try:
				stmt        = text("INSERT INTO " + del_table_name  +" (" + columnss +")SELECT " + columnss+ " from bulk_upload  where conf_id ="+conf_id +"  ;") ;
				results     = conn.execute(stmt)
				conn.commit()
				return "success"
			except Exception as e:
				return str(e)	

	# Jan 10 ,2023
	def bulkuploadPreview1(self,conf_id,insert_query,columnss,del_table_name,del_nos):
		with engine.connect() as conn:
			try:
				stmt_1 = text("truncate table bulk_upload")
				conn.execute(stmt_1)
				conn.commit()
				stmt        = text(insert_query)
				conn.execute(stmt)
				conn.commit()
				stmt_1      = text("SELECT  " + columnss + " FROM bulk_upload where conf_id  = "+str(conf_id)+ " and   delegate_no is not null  ;" ) 
				result_2 = conn.execute(stmt_1).all()

				results = [dict(r._mapping) for r in result_2] if result_2 else None
				return results
			except Exception as e:
				return str(e)

	def bulkuploadDublicatePreview2(self,conf_id,insert_query,columnss,del_table_name,del_nos):
		with engine.connect() as conn:
			try:
				stmt_1    = text("SELECT  " + columnss + " FROM "+ del_table_name +" where conf_id  = "+str(conf_id)+ " and delegate_no in ( "+del_nos+") and  delegate_no is not null  ;" ) 
				results_1 = conn.execute(stmt_1).all()
				results   = [dict(r._mapping) for r in results_1] if results_1 else None
				return results
			except Exception as e:
				return str(e)	

	def bulkuploadSelected(self,conf_id,insert_query,columnss,del_table_name,del_nos):
		with engine.connect() as conn:
			try:
				stmt    = text("DELETE FROM " + del_table_name + "  where conf_id = " + str(conf_id) + " and delegate_no in (" + del_nos + ") ;")
				conn.execute(stmt)
				stmt_1  = text("INSERT INTO " + del_table_name  +" (" + columnss +")SELECT " + columnss+ " from bulk_upload  where conf_id ="+str(conf_id) +"  and delegate_no in ("+del_nos+") ;") ;
				results = conn.execute(stmt_1)

				# stmt 3 update  the badge_role table
				stmt_3  =text("insert into badge_role (b_role ,conf_id) (select role, "+str(conf_id)+" from "+ del_table_name +
				" where conf_id = "+str(conf_id)+" and role not in (select b_role from  badge_role where conf_id = "+str(conf_id)+"  ) group by role ) ;")
				conn.execute(stmt_3)
				conn.commit()
				if results : 
					return "success"
				else:
					return None 
			except Exception as e:
				return str(e)

	# Badge Auto login 10 ,January 2023
	def checkConfUUID(self,conf_id,conf_uuid):
		with engine.connect() as conn:
			stmt = text("select * from conference where conf_id = "+str(conf_id)+" and conf_uuid = '"+conf_uuid+"'   limit 1;")
			results = conn.execute(stmt)
			result = results.one_or_none()
			return dict(result._mapping) if result else None
		

	def removeAndGetRemainRecords(self,conf_id,del_table_name,del_nos,columnss,remove_delegate_nos):
		with engine.connect() as conn:
			stmt = text("DELETE FROM " +del_table_name + "  where  conf_id = " + str(conf_id) + " and delegate_no in  ("+remove_delegate_nos+ " ) ;" )
			conn.execute(stmt)
			conn.commit()
			stmt_1 = text("SELECT  " + columnss +"  from "+ del_table_name +" where conf_id = "+str(conf_id)+" and delegate_no   in ("+del_nos+")   ;")
			results = conn.execute(stmt_1).all()
			results = [dict(r._mapping) for r in results] if results else None
			return results
			
	
	def GetAllRecordsFromFile(self,conf_id,del_table_name,del_nos,columnss,remove_delegate_nos):
		with engine.connect() as conn:
			stmt_1  = text("SELECT  " + columnss +"  from bulk_upload where conf_id = "+str(conf_id)+"  ;")
			results = conn.execute(stmt_1).all()
			results = [dict(r._mapping) for r in results] if results else None
			return results
				
			
	# Fc scanned records view and delete block -- Santhosh -- August 18,2023
	def FcScannedTestRecords(self,conf_id,ses_entry_table):
		with engine_fk.connect() as conn:
			stmt_1 = text("select count(*) as scanned_count , se.session_id , fc.session_name from "+ses_entry_table+" se inner join fc_sessions fc on fc.session_id = se.session_id group by se.session_id;")
			results = conn.execute(stmt_1).all()
			results = [dict(r._mapping) for r in results] if results else None
			return results

	def delete_test_record(self,del_stmt):
		with engine_fk.connect() as conn:
			safe_update = text("SET SQL_SAFE_UPDATES = 0")
			del_stmt_1  = text(del_stmt)
			conn.execute(safe_update)
			result = conn.execute(del_stmt_1)
			conn.commit()
			return result

	def KcScannedTestRecords(self,conf_id,ses_entry_table):
		with engine_fk.connect() as conn:
			stmt_1 = text("select count(*) as scanned_count , se.session_id , kc.session_name from "+ses_entry_table+" se inner join kc_sessions kc on kc.session_id = se.session_id group by se.session_id;")
			results = conn.execute(stmt_1).all()
			results = [dict(r._mapping) for r in results] if results else None
			return results


	# 2023-08-19 17:41:00 created by Aravinth
	def get_data_for_role_update(self,conf_id,del_table_name):
		with engine.connect() as conn:
			stmt    = text("select d.delegate_id ,d.prefix,d.full_name,d.email,d.role,d.delegate_no,cd.delegate_id as cd_delegate_id,cd.prefix as cd_prefix,cd.full_name as cd_full_name,cd.email as cd_email,cd.role as cd_role,cd.delegate_no as cd_delegate_no from delegates d inner join "+del_table_name+" cd on cd.delegate_no = d.delegate_no where d.conference_id = "+str(conf_id)+" and d.delegate_no is not null and d.role is not null and d.role <> cd.role and d.email=cd.email order by d.delegate_no asc;")
			results = conn.execute(stmt).all()
			results = [dict(r._mapping) for r in results] if results else None
			return results
	
	def conf_del_update_in_data(self,conf_id,del_table_name,delegate_no,updated_at):
		with engine.connect() as conn:
			stmt = text("update "+del_table_name+" cd inner join delegates d on cd. delegate_no  = d.delegate_no and d.conference_id = '"+str(conf_id)+"' set cd.role = d.role ,cd.updated_at = '"+str(updated_at)+"' where d.conference_id = '"+str(conf_id)+"' and d.delegate_no is not null and d.role is not null and d.role  <> cd.role and cd.delegate_no  in ("+delegate_no+") ;")	
			result = conn.execute(stmt)
			conn.commit()
			if result:
				return 'success'
			else :
			   return 'fail'

	def del_update_in_data(self,conf_id,del_table_name,delegate_no,updated_at):
		with engine.connect() as conn:
			stmt = text("update  delegates d inner join "+del_table_name+" cd on cd. delegate_no  = d.delegate_no  and d.conference_id = '"+str(conf_id)+"' and d.email=cd.email set d.role = cd.role ,d.updated_at = '"+str(updated_at)+"' where d.conference_id = "+str(conf_id)+" and d.delegate_no is not null and d.role is not null and d.role  <> cd.role and d.delegate_no in ("+delegate_no+")  ;")	
			result = conn.execute(stmt)
			conn.commit()
			if result:
				return 'success'
			else :
			   return 'fail'
	# backup scan data start

	def getbackupscandata(self,conf_id,ses_entry_table):
		with engine_fk.connect() as conn:
			stmt_1 = text("select count(*) as scanned_count , se.session_id , fc.session_name from "+ses_entry_table+" se inner join fc_sessions fc on fc.session_id = se.session_id group by se.session_id;")
			results = conn.execute(stmt_1).all()
			results = [dict(r._mapping) for r in results] if results else None
			return results
			
	def getbackupscandatabackup(self,conf_id,fc_back_table):
		with engine_fk.connect() as conn:
			stmt_1 = text("select count(*) as scanned_count , se.session_id , fc.session_name,fc.session_key  from "+fc_back_table+" se inner join fc_sessions fc on fc.session_id = se.session_id group by se.session_id;")
			results = conn.execute(stmt_1).all()
			results = [dict(r._mapping) for r in results] if results else None
			return results
			


	#  Jan 05,2022
	# Multiple Resutl set
	def create_backup(self,conf_id,conf_key,session_id):
		sets = []
		try:
			connection = engine_fk.raw_connection()
			cursor = connection.cursor()
			cursor.callproc("fc_data_backup",[conf_id,conf_key,session_id])
			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.commit()
			connection.close()
			
		return sets 

# backup scan data end 

# -------------------------------------------------------------------------------------------------
# remove registered delegate start

	def getConfData(self,conf_id,conf_key) :
		with engine.connect() as conn:
			stmt = text("select * from conference where conf_id ="+str(conf_id)+" and conf_key = '"+conf_key+"';")
			result = conn.execute(stmt)
			result = result.one_or_none()
			return dict(result._mapping) if result else None

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

	def get_data_for_remove(self,conf_id,del_table_name,b_role,all_delegate_nos):
		connection  = engine_fk.raw_connection()
		cursor      = connection.cursor()
		cursor.callproc("get_delegates_by_role_and_delnos",[conf_id,del_table_name,b_role,all_delegate_nos])
		if cursor.description :
			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
				# return results
			else :
				return None 
		else :
			cursor.close()
			connection.commit()
			return None



	 # Updated on Step 25,2023
	def removeConfDelegatesByIds(self,conf_id,del_table_name,delegate_ids) :
		with engine_fk.connect() as conn:
			stmt        = text("delete from "+del_table_name+" where conf_id = "+str(conf_id)+" and delegate_id in ("+delegate_ids+")  ;")
			safe_update = text("SET SQL_SAFE_UPDATES = 0 ")
			results     = conn.execute(safe_update)
			result      = conn.execute(stmt)
			conn.commit()

			if result : 
				return "success"
			else:
				return "fail"

# remove registered delegate end
# ---------------------------------------------------------------

# Food & kit session map for custom delegate numbers start

	def getFCSessionData(self,conf_id,conf_key):
		with engine_fk.connect() as conn:
			stmt    = text("SELECT f.session_id,f.session_name,f.session_key,f.start_time,f.end_time,f.is_active from fc_sessions f where conf_id ="+str(conf_id)+" and conf_key ='"+conf_key+"' ;")
			result  = conn.execute(stmt).all()
			results = [dict(r._mapping) for r in result] if result else None
			return results

	def get_delegate_nos_by_name_delno(self,conf_id,del_table_name,full_name,delegate_nos):
		# print(conf_id,del_table_name,full_name,delegate_nos)
		connection  = engine_fk.raw_connection()
		cursor      = connection.cursor()
		cursor.callproc("get_delegates_by_name_and_delnos",[conf_id,del_table_name,full_name,delegate_nos])
		if cursor.description :
			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
			else :
				return None 
		else :
			cursor.close()
			connection.commit()
			return None

	
	# ganesan code start Step 13 
	def insert_custom_delegate_no(self,conf_id,del_session_table_name,del_table_name,session_id,delegate_ids,curr_dt) :
		with engine_fk.connect() as conn:
			try:
				del_nos_stmt = text("select group_concat(delegate_no separator  ',' ) as del_nos from  "+del_table_name+"   where conf_id = "+str(conf_id)+" and delegate_id in ("+str(delegate_ids)+") limit 1 ;")
				result_1     = conn.execute(del_nos_stmt)
				result_1     = result_1.fetchone()
				del_nos      = result_1.del_nos
	 
				# stmt = text("INSERT INTO  "+del_session_table_name+" (session_id,delegate_no,created_at )SELECT  "+str(session_id)+","+str(del_nos)+", '"+curr_dt+"' FROM "+del_table_name+" where delegate_no ="+str(del_nos)+" and conf_id = "+str(conf_id)+" and delegate_no not in (select delegate_no from "+del_session_table_name+" where session_id = "+str(session_id)+" and delegate_no = "+str(del_nos)+" );")

				stmt = text("INSERT INTO  "+del_session_table_name+" (session_id,delegate_no,created_at )SELECT  "+str(session_id)+",delegate_no, '"+curr_dt+"' FROM  "+ del_table_name + " where delegate_id  in ("+str(delegate_ids)+") and conf_id = "+str(conf_id)+" and delegate_no not in (select delegate_no from "+del_session_table_name+" where session_id = "+str(session_id)+" and delegate_no in  ("+str(del_nos)+")  );")
				print(stmt)
				results     = conn.execute(stmt)
				conn.commit()
				return "success"
			except Exception as e:
				return str(e)

	def remove_custom_delegate_no(self,del_table_name,del_session_table_name,session_id,delegate_id) :
		with engine_fk.connect() as conn:
			stmt = text("delete from "+del_session_table_name+" where  session_id = "+str(session_id)+"  and  delegate_no in (select delegate_no from "+del_table_name+" where delegate_id ="+str(delegate_id)+" ) limit 1;")
			safe_update = text("SET SQL_SAFE_UPDATES = 0 ")
			results= conn.execute(safe_update)
			result = conn.execute(stmt)
			conn.commit()
			if result : 
				return "success"
			else:
				return "fail"

	def getkCSessionData(self,conf_id,conf_key):
		with engine_fk.connect() as conn:
			stmt    = text("SELECT f.session_id,f.session_name,f.session_key,f.start_time,f.end_time,f.is_active from kc_sessions f where conf_id ="+str(conf_id)+" and conf_key ='"+conf_key+"' ;")
			result  = conn.execute(stmt).all()
			results = [dict(r._mapping) for r in result] if result else None
			return results

# Food & kit session map for custom delegate numbers end
# Sep 4-9-23 signup_reports get delegates columns for reports,By Aravinth:
	def getDelegatesColumnForReports(self,conf_id,del_table_name,ses_entry_table,report_name):	
		if report_name and "SIGNUP" in report_name  :
			with engine.connect() as conn:
				stmt    = text("select d.delegate_no,d.full_name,d.email,d.mobile,d.role,d.city,d.state,d.membership_no,d.mc_number,d.signed_on,d.comments,d.signed_by from "+del_table_name+" d limit 1;")
				result = conn.execute(stmt)
				if result:
					records = [dict(zip(row._mapping.keys(), row._mapping)) for row in result]
					return records
				else :
					return None
		else :	
			with engine_fk.connect() as conn:
				stmt    = text("select d.delegate_no,d.full_name,d.email,d.mobile,d.role,d.city,d.state,d.membership_no,d.mc_number,d.created_at from "+del_table_name+ " d   limit 1;")
				result = conn.execute(stmt)
				if result:
					records = [dict(zip(row._mapping.keys(), row._mapping )) for row in result]
					return records
				else :
					return None

# get signup and kit counter report for view and download option
	def getSignupAndKitReport(self,conf_id,del_table_name,group_by_role,group_by_signedon,sel_columns,order_by,is_count_only,session_id,ses_entry_table,date_1,role):
		connection  = engine_fk.raw_connection()
		cursor      = connection.cursor()
		cursor.callproc("usp_get_signup_and_kit_report",[conf_id,del_table_name,group_by_role,group_by_signedon,sel_columns,order_by,is_count_only,session_id,ses_entry_table,date_1,role])
		if cursor.description :
			columns     = [column[0] for column in cursor.description]
			results     = []
			for row in cursor.fetchall():
				results.append(dict(zip(columns, row)))
			cursor.close()
			connection.commit()
			return results if results else None 
		else :
			cursor.close()
			connection.commit()
			return None

	def getKITReportDownload(self,session_id,conf_id,kc_session_entry_table_name,role):
		connection = engine.raw_connection()
		cursor = connection.cursor()
		cursor.callproc("usp_get_kc_download_report",[session_id,conf_id,kc_session_entry_table_name,role])
		if cursor.description :
			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
			else :
				return None
		else :
			cursor.close()
			connection.commit()
			return None
			
	def get_role(self,conf_id,del_table_name):
		with engine_fk.connect() as conn:
			stmt_1 = text("select role from "+del_table_name+" where role not in ('VOLUNTEER' , 'Volunteer','SUPPORT - IT','Support -IT') group by role;")
			results = conn.execute(stmt_1).all()
			results = [dict(r._mapping) for r in results] if results else None
			return results
			
	# Counter wise delegate numbers 
	def get_delegate_no_counter(self,conf_id,del_table_name):
		with engine_fk.connect() as conn:
			stmt    = text("SELECT min(delegate_no) as min_del_number, max(delegate_no) as max_del_number,count(delegate_no)  as total_count, counter from "+del_table_name+"  where conf_id = "+str(conf_id)+" and counter is not null and counter > 0 group by counter order by counter;")
			result  = conn.execute(stmt).all()
			results = [dict(r._mapping) for r in result] if result else None
			return results
			
	def truncateConfDelegates(self,del_table_name) :
		with engine_fk.connect() as conn:
			stmt = text("truncate table "+del_table_name+";")
			safe_update = text("SET SQL_SAFE_UPDATES = 0 ")
			results= conn.execute(safe_update)
			result = conn.execute(stmt)
			conn.commit()
			if result : 
				return "success"
			else:
				return "fail"

	def get_conf_delegate_details(self,del_table_name):
		with engine_fk.connect() as conn:
			stmt    = text("SELECT * from "+del_table_name+" limit 1;")
			result  = conn.execute(stmt).all()
			results = [dict(r._mapping) for r in result] if result else None
			return results


	# remane_delegate_details start
	def get_delegate_details(self,conf_id,del_table_name,name_replace,is_main_del_table):
		with engine.connect() as conn:
			if is_main_del_table > 0 :
				stmt    = text("SELECT delegate_id,delegate_no,prefix,full_name  as old_name,replace(full_name , '"+name_replace+"','') as new_name from "+del_table_name+" where conference_id =  "+str(conf_id)+" and  full_name like '%"+name_replace+"%' and delegate_no is not null;")
			else:
				stmt    = text("SELECT delegate_id,delegate_no,prefix,full_name  as old_name,replace(full_name , '"+name_replace+"','') as new_name from "+del_table_name+" where conf_id =  "+str(conf_id)+" and  full_name like '%"+name_replace+"%' and delegate_no is not null;")
			result  = conn.execute(stmt).all()
			results = [dict(r._mapping) for r in result] if result else None
			return results

	 # Updated on Step 25,2023
	def updateConfDelegatesByIds(self,del_table_name,conf_id,name_replace,delegate_ids,curr_dt,is_main_del_table) :
		with engine.connect() as conn:
			if is_main_del_table > 0 :
				stmt = text("UPDATE  "+del_table_name+" SET full_name = replace(full_name , '"+name_replace+"',''),updated_at = '"+curr_dt+"' where conference_id = "+str(conf_id)+" and  delegate_id in ("+delegate_ids+") and delegate_no is not null ;")
			else:
				stmt = text("UPDATE  "+del_table_name+" SET full_name = replace(full_name, '"+name_replace+"','Dr. '),updated_at = '"+curr_dt+"' where conf_id = "+str(conf_id)+" and  delegate_id in ("+delegate_ids+") and delegate_no is not null;")
			safe_update = text("SET SQL_SAFE_UPDATES = 0 ")
			results     = conn.execute(safe_update)
			result      = conn.execute(stmt)
			conn.commit()
			if result : 
				return "success"
			else:
				return "fail"

# remane_delegate_details end

# badge and kit not collect report start
	def confDelegateNotSignin(self,conf_id,del_table_name,is_count_only):
		with engine_fk.connect() as conn:
			if is_count_only and is_count_only > 0 :
				stmt    = text("select count(d.delegate_no) as delegate_count from "+del_table_name + " d where d.conf_id= "+str(conf_id)+" and d.delegate_no is not null and d.full_name is not null "
				+ " and d.role not in ('GUEST OF HONOR','BANQUET - DAY 2','BANQUET - DAY 1', 'EXHIBITOR','TRADE', 'VOLUNTEER', 'Event Crew' , 'TRADE DELEGATE' ,'SUPPORT - IT' , 'Support - AV','AV-TEAM','Photographer' )  limit 1")
				result = conn.execute(stmt).one_or_none()
				return dict(result._mapping) if result else None
			else :
				stmt    = text("select d.delegate_id,d.delegate_no,d.full_name,d.email,d.mobile,d.mc_number,d.membership_no,"
				+ " d.state,d.counter,d.batch,d.role from "+del_table_name + " d where d.conf_id= "+str(conf_id)+" and d.delegate_no is not null and d.full_name is not null "
				+ " and d.role not in ('GUEST OF HONOR','BANQUET - DAY 2','BANQUET - DAY 1', 'EXHIBITOR','TRADE', 'VOLUNTEER', 'Event Crew' , 'TRADE DELEGATE' ,'SUPPORT - IT' , 'Support - AV','AV-TEAM','Photographer' ) ")
			
				result  = conn.execute(stmt).all()

				results = [dict(r._mapping) for r in result] if result else None
				return results			

	# strt badge kit not get 
	def badge_kit_not_collected_report(self,conf_id,del_table_name,ses_entry_table,is_badge_not_collect,is_kit_not_collect):
		connection  = engine_fk.raw_connection()
		cursor      = connection.cursor()
		cursor.callproc("usp_badge_kit_not_collected_report",[conf_id,del_table_name,ses_entry_table,is_badge_not_collect,is_kit_not_collect])
		if cursor.description :
			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
				# return results
			else :
				return None 
		else :
			cursor.close()
			connection.commit()
			return None

	def get_delegates(self,del_table_name,conf_id,delegate_id):
		with engine_fk.connect() as conn:
			stmt    = text("SELECT * from "+del_table_name+" where conf_id ="+str(conf_id)+" and delegate_id = "+str(delegate_id)+" ;")
			result = conn.execute(stmt)
			result = result.one_or_none()
			return dict(result._mapping) if result else None
	

	# badge and kit not collect report end
	def updateDelNoPrefix(self,conf_id,del_table_name, role,del_no_prefix):
		with engine_fk.connect() as conn:
			count_1 = 0
			stmt    = text(" SET SQL_SAFE_UPDATES = 0 ;")
			conn.execute(stmt)
			conn.commit()
			stmt_1      = text(" select count(delegate_no) as count_1 from  "+ del_table_name +"   WHERE role = '"+role+"' and conf_id = "+ str(conf_id)+ "  limit 1; ")
			result      = conn.execute(stmt_1).fetchone()
			if result :
				count_1 = result.count_1
				if count_1 > 0 :
					stmt = text(" UPDATE "+ del_table_name +"  set del_no_prefix = '"+del_no_prefix+"' WHERE role = '"+role+"' and conf_id = "+ str(conf_id)+ " ; ")
					conn.execute(stmt)
					conn.commit()
					return "Successfully updated. Affected row count is " + str(count_1)+ "."
				else :
					return "There is no records to updated. "
			else :
				return "There is no records to updated. "
	# registered delegates report start
	def get_register_delegates(self,sel_columns,del_table_name,conf_id):
		with engine_fk.connect() as conn:
			stmt_1 = text("select "+sel_columns+" from "+del_table_name+" where conf_id = "+str(conf_id)+" and full_name is not null order by delegate_no asc ;")
			print(stmt_1)
			results = conn.execute(stmt_1).all()
			results = [dict(r._mapping) for r in results] if results else None
			return results
	# registered delegates report end
# badge and kit not collect report end

	def get_email_ids(self,conf_id):
		with engine_fk.connect() as conn:
			stmt    = text("select * from conf_reports where conf_id = "+str(conf_id)+";")
			result  = conn.execute(stmt).all()
			results = [dict(r._mapping) for r in result] if result else None
			return results
			
	def UnsignupButScannedList(self,conf_id,del_nos,del_table_name,kc_entry_table,fc_entry_table,fcbackup_table,is_bulk_signup,is_view_bulk_signup_data,is_manual_signup):
		connection  = engine_fk.raw_connection()
		cursor      = connection.cursor()
		cursor.callproc("badge_not_collected_but_attended_list",[conf_id,del_nos,del_table_name,kc_entry_table,fc_entry_table,fcbackup_table,is_bulk_signup,is_view_bulk_signup_data,is_manual_signup])
		if cursor.description :
			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
			else :
				return None 
		else :
			cursor.close()
			connection.commit()
			return None
	
	
		#  Jan 05,2022
	# Multiple Resutl set
	def Getsearch_delegate_data(self,conf_id,del_nos,search_data,del_table_name,kc_entry_table,fc_entry_table,fcbackup_table):
		sets = []
		try:
			connection = engine_fk.raw_connection()
			cursor = connection.cursor()
			cursor.callproc("del_records_from_all_table",[conf_id,del_nos,search_data,del_table_name,kc_entry_table,fc_entry_table,fcbackup_table])
			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.commit()
			connection.close()
			
		return sets 
		
	
	def getkCSessionData(self,conf_id,conf_key):
		with engine_fk.connect() as conn:
			stmt    = text("SELECT f.session_id,f.session_name,f.session_key,f.start_time,f.end_time,f.is_active from kc_sessions f where conf_id ="+str(conf_id)+" and conf_key ='"+conf_key+"' ;")
			result  = conn.execute(stmt).all()
			results = [dict(r._mapping) for r in result] if result else None
			return results


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