Fix: skip duplicate records instead of accumulating TEU

This commit is contained in:
2025-12-29 02:32:46 +08:00
parent 5a6aee333c
commit 5df530a528

View File

@@ -108,27 +108,21 @@ class DailyLogsDatabase:
self.conn.commit() self.conn.commit()
def insert(self, log: Dict) -> bool: def insert(self, log: Dict) -> bool:
"""插入记录(存在则累加TEU,不存在则插入)""" """插入记录(存在则跳过,不存在则插入)"""
try: try:
cursor = self.conn.cursor() cursor = self.conn.cursor()
# 检查是否已存在如果存在则累加TEU # 检查是否已存在
cursor.execute(''' cursor.execute('''
SELECT id, teu FROM daily_handover_logs SELECT id, teu FROM daily_handover_logs
WHERE date = ? AND shift = ? AND ship_name = ? WHERE date = ? AND shift = ? AND ship_name = ?
''', (log['date'], log['shift'], log['ship_name'])) ''', (log['date'], log['shift'], log['ship_name']))
existing = cursor.fetchone() existing = cursor.fetchone()
new_teu = log.get('teu') or 0 new_teu = log.get('teu')
if existing: if existing:
# 累加TEU # 记录已存在,跳过(不重复添加)
old_teu = existing['teu'] or 0 return False
total_teu = old_teu + new_teu
cursor.execute('''
UPDATE daily_handover_logs
SET teu = ?, vehicles = ?, created_at = CURRENT_TIMESTAMP
WHERE id = ?
''', (total_teu, log.get('vehicles'), existing['id']))
else: else:
# 插入新记录 # 插入新记录
cursor.execute(''' cursor.execute('''
@@ -139,9 +133,9 @@ class DailyLogsDatabase:
log['date'], log['shift'], log['ship_name'], log['date'], log['shift'], log['ship_name'],
log.get('teu'), log.get('efficiency'), log.get('vehicles') log.get('teu'), log.get('efficiency'), log.get('vehicles')
)) ))
self.conn.commit() self.conn.commit()
return True return True
except sqlite3.Error as e: except sqlite3.Error as e:
print(f"数据库错误: {e}") print(f"数据库错误: {e}")
return False return False