FEAT: Adds a function 'check_record_exists' for checking if the record is already existing, it will only move the logfile in the destionation folder

This commit is contained in:
eloi 2025-03-30 14:20:12 +08:00
parent 8343a3b42b
commit 0e1d2053a9

View File

@ -46,6 +46,16 @@ def extract_data_from_file(file_path):
print(f"Error occurred while reading file {file_path}: {e}") print(f"Error occurred while reading file {file_path}: {e}")
return None, None return None, None
def check_record_exists(conn, cardno, station, machine, status, lastupdate):
query = """
SELECT count(*) as record_count FROM log_pass
WHERE cardno = ? AND station = ? AND machine = ? AND status = ? AND lastupdate = ?
"""
params = (f"{cardno}_1", station, machine, status, lastupdate)
serial_check = pd.read_sql_query(query, conn, params=params)
return int(serial_check['record_count'].iloc[0])
def main_loop(): def main_loop():
try: try:
global cursor global cursor
@ -79,6 +89,15 @@ def main_loop():
else: else:
log_pass_lastupdate = None log_pass_lastupdate = None
exists = check_record_exists(conn, cardno, stage, machine, database_status, log_pass_lastupdate)
if exists > 0:
print(f"Record already exists for cardno: {cardno}, stage: {stage}, machine: {machine}, status: {database_status}, lastupdate: {log_pass_lastupdate}")
shutil.move(file, DESTINATION_DIR + os.path.basename(file))
print('File Moved Successfully!')
else:
# Insert data into the database # Insert data into the database
insert_logpass = "INSERT INTO dbo.log_pass (account, cardno, line, sequence, station, machine, status, lastupdate, lastupdatedby) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)" insert_logpass = "INSERT INTO dbo.log_pass (account, cardno, line, sequence, station, machine, status, lastupdate, lastupdatedby) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
cursor.execute(insert_logpass, ('G5', cardno + '_1', '', '', stage, machine, database_status, log_pass_lastupdate, 'cats')) cursor.execute(insert_logpass, ('G5', cardno + '_1', '', '', stage, machine, database_status, log_pass_lastupdate, 'cats'))
@ -96,7 +115,7 @@ def main_loop():
print('Reject Details inserted successfully') print('Reject Details inserted successfully')
shutil.move(file, DESTINATION_DIR + os.path.basename(file)) shutil.move(file, DESTINATION_DIR + os.path.basename(file))
print('File Moved Successfully') print('File Moved Successfully!')
else: else:
print('No .txt files in the directory found!') print('No .txt files in the directory found!')
else: else: