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,24 +89,33 @@ def main_loop():
else: else:
log_pass_lastupdate = None log_pass_lastupdate = None
# Insert data into the database exists = check_record_exists(conn, cardno, stage, machine, database_status, log_pass_lastupdate)
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'))
conn.commit()
print('History inserted successfully')
# Insert data into log_repair if failed if exists > 0:
if status == 'FAILED': print(f"Record already exists for cardno: {cardno}, stage: {stage}, machine: {machine}, status: {database_status}, lastupdate: {log_pass_lastupdate}")
check_defect = pd.read_sql_query(f"SELECT defectCode FROM dbo.defectProd2 WHERE description = '{symptoms}'", conn) shutil.move(file, DESTINATION_DIR + os.path.basename(file))
defect_code = check_defect.iloc[0]['defectCode'] print('File Moved Successfully!')
insert_log_repair = "INSERT INTO dbo.log_repair (account, cardno, stage, machine, category, defect, location, details, status, addinfo, serialAffected, component, remarks, rejectDate, rejectUser, repairDate, repairUser, lastupdate, lastupdatedby) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" else:
cursor.execute(insert_log_repair, ('G5', cardno + "_1", stage, machine, "Testing Defects", defect_code, "", symptoms, "0", "", "", "", "", log_pass_lastupdate, lastupdatedby, "", "", log_pass_lastupdate, lastupdatedby))
# Insert data into the database
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'))
conn.commit() conn.commit()
print('Reject Details inserted successfully') print('History inserted successfully')
shutil.move(file, DESTINATION_DIR + os.path.basename(file)) # Insert data into log_repair if failed
print('File Moved Successfully') if status == 'FAILED':
check_defect = pd.read_sql_query(f"SELECT defectCode FROM dbo.defectProd2 WHERE description = '{symptoms}'", conn)
defect_code = check_defect.iloc[0]['defectCode']
insert_log_repair = "INSERT INTO dbo.log_repair (account, cardno, stage, machine, category, defect, location, details, status, addinfo, serialAffected, component, remarks, rejectDate, rejectUser, repairDate, repairUser, lastupdate, lastupdatedby) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
cursor.execute(insert_log_repair, ('G5', cardno + "_1", stage, machine, "Testing Defects", defect_code, "", symptoms, "0", "", "", "", "", log_pass_lastupdate, lastupdatedby, "", "", log_pass_lastupdate, lastupdatedby))
conn.commit()
print('Reject Details inserted successfully')
shutil.move(file, DESTINATION_DIR + os.path.basename(file))
print('File Moved Successfully!')
else: else:
print('No .txt files in the directory found!') print('No .txt files in the directory found!')
else: else: