数据库统计增加当月每艘船的作业量

This commit is contained in:
2025-12-29 02:50:25 +08:00
parent 833db895b0
commit 4d2f9302ff
2 changed files with 39 additions and 5 deletions

View File

@@ -191,6 +191,30 @@ class DailyLogsDatabase:
'date_range': {'start': date_range[0], 'end': date_range[1]} 'date_range': {'start': date_range[0], 'end': date_range[1]}
} }
def get_ships_with_monthly_teu(self, year_month: str = None) -> List[Dict]:
"""获取所有船只及其当月TEU总量"""
cursor = self.conn.cursor()
if year_month:
# 按年月筛选
cursor.execute('''
SELECT ship_name, SUM(teu) as monthly_teu
FROM daily_handover_logs
WHERE date LIKE ?
GROUP BY ship_name
ORDER BY monthly_teu DESC
''', (f'{year_month}%',))
else:
# 全部
cursor.execute('''
SELECT ship_name, SUM(teu) as monthly_teu
FROM daily_handover_logs
GROUP BY ship_name
ORDER BY monthly_teu DESC
''')
return [{'ship_name': row[0], 'monthly_teu': row[1]} for row in cursor.fetchall()]
def insert_unaccounted(self, year_month: str, teu: int, note: str = '') -> bool: def insert_unaccounted(self, year_month: str, teu: int, note: str = '') -> bool:
"""插入未统计数据""" """插入未统计数据"""
try: try:

View File

@@ -86,7 +86,7 @@ class OrbitInGUI:
btn_report_today = ttk.Button( btn_report_today = ttk.Button(
left_frame, left_frame,
text="日日报", text="日日报",
command=self.generate_today_report, command=self.generate_today_report,
width=20 width=20
) )
@@ -422,17 +422,27 @@ class OrbitInGUI:
try: try:
db = DailyLogsDatabase() db = DailyLogsDatabase()
stats = db.get_stats() stats = db.get_stats()
# 获取当月船次统计
current_month = datetime.now().strftime('%Y-%m')
ships_monthly = db.get_ships_with_monthly_teu(current_month)
db.close() db.close()
self.log_message(f"总记录数: {stats['total']}") self.log_message(f"总记录数: {stats['total']}")
self.log_message(f"船次数量: {len(stats['ships'])}") self.log_message(f"船次数量: {len(stats['ships'])}")
self.log_message(f"日期范围: {stats['date_range']['start']} ~ {stats['date_range']['end']}") self.log_message(f"日期范围: {stats['date_range']['start']} ~ {stats['date_range']['end']}")
if stats['ships']: if ships_monthly:
self.log_message("") self.log_message("")
self.log_message("船次列表:") self.log_message(f"{current_month}月船次统计:")
for ship in sorted(stats['ships']): total_monthly_teu = 0
self.log_message(f" - {ship}") for ship in ships_monthly:
monthly_teu = ship['monthly_teu'] or 0
total_monthly_teu += monthly_teu
self.log_message(f" {ship['ship_name']}: {monthly_teu}TEU")
self.log_message(f" ---")
self.log_message(f" 本月合计: {total_monthly_teu}TEU")
self.set_status("完成") self.set_status("完成")