#!/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() print("=== 按渠道支出(2026 H1) ===") chan = {'[支付宝]': '支付宝', '[邮储卡]': '邮储卡外部', '[微信]': '微信(去重后)'} for tag, name in chan.items(): cur.execute(f"""SELECT COUNT(*), SUM(amount) FROM variable_expenses WHERE member_id=3 AND recorded_date BETWEEN '2026-01-01' AND '2026-06-30' AND note LIKE '%{tag}%'""") n, a = cur.fetchone() print(f" {name:14s} {n}笔 ¥{float(a):>10.2f}") print("\n=== 汇总(全渠道去重) ===") cur.execute("""SELECT COUNT(*), SUM(amount) FROM variable_expenses WHERE member_id=3 AND recorded_date BETWEEN '2026-01-01' AND '2026-06-30' AND (note LIKE '%[支付宝]%' OR note LIKE '%[邮储卡]%' OR note LIKE '%[微信]%')""") n, a = cur.fetchone() print(f" 支出合计: {n}笔 ¥{float(a):.2f}") print("\n=== 支出分类结构 ===") cur.execute("""SELECT category, SUM(amount), COUNT(*) FROM variable_expenses WHERE member_id=3 AND recorded_date BETWEEN '2026-01-01' AND '2026-06-30' AND (note LIKE '%[支付宝]%' OR note LIKE '%[邮储卡]%' OR note LIKE '%[微信]%') GROUP BY category ORDER BY SUM(amount) DESC""") tot = 0 rows = cur.fetchall() tot = sum(float(r[1]) for r in rows) for c, a, n in rows: print(f" {c:10s} ¥{float(a):>10.2f} {100*float(a)/tot:5.1f}% ({n}笔)") print(f" 总计: ¥{tot:.2f}") print("\n=== 收入 ===") cur.execute("""SELECT COUNT(*), SUM(amount) FROM income_records WHERE member_id=3 AND recorded_date BETWEEN '2026-01-01' AND '2026-06-30' AND note LIKE '%[微信]%'""") n, a = cur.fetchone() print(f" 微信收入: {n}笔 ¥{float(a):.2f}") conn.close()