mirror of
https://devops.liangqichi.top/qichi.liang/Orbitin.git
synced 2026-02-10 07:41:29 +08:00
Fix: skip duplicate records instead of accumulating TEU
This commit is contained in:
@@ -108,27 +108,21 @@ class DailyLogsDatabase:
|
||||
self.conn.commit()
|
||||
|
||||
def insert(self, log: Dict) -> bool:
|
||||
"""插入记录(存在则累加TEU,不存在则插入)"""
|
||||
"""插入记录(存在则跳过,不存在则插入)"""
|
||||
try:
|
||||
cursor = self.conn.cursor()
|
||||
# 检查是否已存在,如果存在则累加TEU
|
||||
# 检查是否已存在
|
||||
cursor.execute('''
|
||||
SELECT id, teu FROM daily_handover_logs
|
||||
WHERE date = ? AND shift = ? AND ship_name = ?
|
||||
''', (log['date'], log['shift'], log['ship_name']))
|
||||
existing = cursor.fetchone()
|
||||
|
||||
new_teu = log.get('teu') or 0
|
||||
new_teu = log.get('teu')
|
||||
|
||||
if existing:
|
||||
# 累加TEU
|
||||
old_teu = existing['teu'] or 0
|
||||
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']))
|
||||
# 记录已存在,跳过(不重复添加)
|
||||
return False
|
||||
else:
|
||||
# 插入新记录
|
||||
cursor.execute('''
|
||||
@@ -139,9 +133,9 @@ class DailyLogsDatabase:
|
||||
log['date'], log['shift'], log['ship_name'],
|
||||
log.get('teu'), log.get('efficiency'), log.get('vehicles')
|
||||
))
|
||||
|
||||
self.conn.commit()
|
||||
return True
|
||||
|
||||
except sqlite3.Error as e:
|
||||
print(f"数据库错误: {e}")
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user