Files
njts-accounting-core/cleanup_duplicate_data.py
2026-02-20 15:47:27 +09:00

84 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import psycopg2
conn = psycopg2.connect(
host="192.168.0.61",
port=55432,
database="njts_acct",
user="njts_app",
password="njts_app2025"
)
cur = conn.cursor()
print("=" * 120)
print("【清理步骤】删除通过复制创建的不正确数据")
print("=" * 120)
# 识别所有应该删除的记录:
# ID 4447月31日 遠達ビル)- 这是6月数据复制到7月的版本
# 以及其他类似的复制数据
delete_list = [
(444, "2025-07-31", "遠達ビル 事務所費用", "6月原始数据的7月复制版"),
]
print("\n识别的应该删除的记录:\n")
for entry_id, date, description, reason in delete_list:
print(f"ID={entry_id} | {date} | {description}")
print(f" 理由: {reason}")
print()
# 实际删除之前先验证
print("\n" + "=" * 120)
print("【验证】使用DELETE指令删除这些记录")
print("=" * 120)
for entry_id, date, description, reason in delete_list:
print(f"\n删除 ID={entry_id}...")
# 先删除journal_lines
cur.execute("DELETE FROM journal_lines WHERE journal_entry_id = %s", (entry_id,))
lines_deleted = cur.rowcount
print(f" ✓ journal_lines: {lines_deleted}行删除")
# 再删除journal_entries
cur.execute("DELETE FROM journal_entries WHERE journal_entry_id = %s", (entry_id,))
entries_deleted = cur.rowcount
print(f" ✓ journal_entries: {entries_deleted}件删除")
conn.commit()
print("\n" + "=" * 120)
print("【完成】数据已删除")
print("=" * 120)
# 验证删除结果
print("\n验证遠達ビル 事務所費用现在的状态:\n")
cur.execute("""
SELECT
je.journal_entry_id,
je.entry_date,
je.description,
COALESCE(SUM(jl.debit), 0) as debit_total,
COALESCE(SUM(jl.credit), 0) as credit_total
FROM journal_entries je
LEFT JOIN journal_lines jl ON je.journal_entry_id = jl.journal_entry_id
WHERE je.description LIKE '%遠達ビル 事務所費用%'
AND je.is_deleted = false
GROUP BY je.journal_entry_id, je.entry_date, je.description
ORDER BY je.entry_date
""")
results = cur.fetchall()
if results:
for entry_id, entry_date, description, debit, credit in results:
print(f"ID={entry_id} | {entry_date} | 借={debit:>10,.0f} 貸={credit:>10,.0f}")
else:
print("未找到遠達ビル相关记录(或全部已删除)")
cur.close()
conn.close()
print("\n✓ 操作完成")