pause idel
This commit is contained in:
93
engine/engine_components/regles.py
Normal file
93
engine/engine_components/regles.py
Normal file
@ -0,0 +1,93 @@
|
||||
# regles.py
|
||||
from engine.logger import log_info, log_debug
|
||||
# -------------------------------------------------
|
||||
# Règle de priorité : remplace, rejette, ou force tour
|
||||
# -------------------------------------------------
|
||||
def resolve_priority(
|
||||
cur,
|
||||
cnx,
|
||||
detail_table,
|
||||
tp_id,
|
||||
dossard,
|
||||
new_timestamp,
|
||||
new_priority,
|
||||
det_id,
|
||||
tp_config
|
||||
):
|
||||
|
||||
# -------------------------------------------------
|
||||
# Lecture de la fenêtre backup pour priorité
|
||||
# -------------------------------------------------
|
||||
cfg = (
|
||||
tp_config[tp_id]["config"]
|
||||
.get("config_tp", {})
|
||||
.get("clef_temp_bbk", [])
|
||||
)
|
||||
|
||||
backup_window = 0
|
||||
if cfg:
|
||||
try:
|
||||
backup_window = int(cfg[0]["value1"])
|
||||
except:
|
||||
backup_window = 0
|
||||
|
||||
log_debug(f"[resolve_priority] backup_window={backup_window}")
|
||||
|
||||
# -------------------------------------------------
|
||||
# Si backup_window = 0 → toujours accepter
|
||||
# -------------------------------------------------
|
||||
if backup_window <= 0:
|
||||
return 1, None, None, None
|
||||
|
||||
# -------------------------------------------------
|
||||
# Recherche d'une lecture existante dans la fenêtre
|
||||
# -------------------------------------------------
|
||||
cur.execute(f"""
|
||||
SELECT detail_id, controller_priority, timestamp_read, tour
|
||||
FROM `{detail_table}`
|
||||
WHERE tp_id = %s
|
||||
AND cla_info_dossard = %s
|
||||
AND status = 1
|
||||
AND ABS(timestamp_read - %s) <= %s
|
||||
LIMIT 1
|
||||
""", (tp_id, dossard, str(new_timestamp), backup_window))
|
||||
|
||||
row = cur.fetchone()
|
||||
|
||||
# -------------------------------------------------
|
||||
# Aucun doublon → accepter
|
||||
# -------------------------------------------------
|
||||
if not row:
|
||||
return 1, None, None, None
|
||||
|
||||
old_id = row["detail_id"]
|
||||
old_priority = row["controller_priority"]
|
||||
old_tour = row["tour"]
|
||||
|
||||
# -------------------------------------------------
|
||||
# Comparaison des priorités
|
||||
# -------------------------------------------------
|
||||
|
||||
# Nouvelle lecture moins prioritaire → rejet
|
||||
if new_priority > old_priority:
|
||||
return 0, "LOWER_PRIORITY", det_id, None
|
||||
|
||||
# Nouvelle lecture plus prioritaire → remplacer
|
||||
if new_priority < old_priority:
|
||||
|
||||
cur.execute(f"""
|
||||
UPDATE `{detail_table}`
|
||||
SET
|
||||
status = 0,
|
||||
reason_code = 'REPLACED_BY_HIGHER_PRIORITY',
|
||||
reason_ref_id = %s
|
||||
WHERE detail_id = %s
|
||||
""", (det_id, old_id))
|
||||
|
||||
cnx.commit()
|
||||
|
||||
forced_tour = old_tour
|
||||
return 1, None, None, forced_tour
|
||||
|
||||
# Même priorité → rejet
|
||||
return 0, "EQUAL_PRIORITY", det_id, None
|
||||
Reference in New Issue
Block a user