#!/usr/bin/env python3 # -*- coding: utf-8 -*- """王芳2026H1 最终去重总账""" import pymysql, json cfg = json.load(open('db_config.json')) conn = pymysql.connect(host=cfg['host'], port=cfg['port'], user=cfg['user'], password=cfg.get('password'), database=cfg['database'], charset='utf8mb4') cur = conn.cursor() tags = "(note LIKE '%%[支付宝]%%' OR note LIKE '%%[邮储卡]%%' OR note LIKE '%%[微信]%%')" print("=== 支出分类结构(全渠道去重后) ===") cur.execute(f"""SELECT category, SUM(amount), COUNT(*) FROM variable_expenses WHERE member_id=3 AND recorded_date BETWEEN '2026-01-01' AND '2026-06-30' AND {tags} GROUP BY category ORDER BY SUM(amount) DESC""") rows = cur.fetchall() tot = sum(float(r[1]) for r in rows); nrec = sum(r[2] for r in rows) print(f"总支出: {nrec}笔 ¥{tot:,.2f}\n") for c, a, n in rows: print(f" {c:10s} ¥{float(a):>10,.2f} {100*float(a)/tot:5.1f}% ({n}笔)") print("\n=== 收入 ===") cur.execute(f"""SELECT '支付宝' src, COUNT(*), SUM(amount) FROM income_records WHERE member_id=3 AND note LIKE '%%[支付宝]%%' AND recorded_date BETWEEN '2026-01-01' AND '2026-06-30' UNION ALL SELECT '微信', COUNT(*), SUM(amount) FROM income_records WHERE member_id=3 AND note LIKE '%%[微信]%%' AND recorded_date BETWEEN '2026-01-01' AND '2026-06-30'""") for s, n, a in cur.fetchall(): print(f" {s}: {n}笔 ¥{float(a):,.2f}") conn.close()