feat: 添加图表统计功能

- TG: /chart 本月分类饼图, /week 近7天消费柱状图
- QQ: 统计/报表 本月文本统计
- 新增 go-chart 依赖生成 PNG 图表
- 新增 GetCategoryStats/GetDailyStats 查询方法
This commit is contained in:
2026-02-15 21:52:03 +08:00
parent ebe8d92c75
commit bac7a7b708
6 changed files with 317 additions and 7 deletions

View File

@@ -7,6 +7,7 @@ import (
"strings"
"time"
xchart "xiaji-go/internal/chart"
"xiaji-go/internal/service"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
@@ -61,10 +62,10 @@ func (b *TGBot) handleMessage(msg *tgbotapi.Message) {
switch {
case text == "/start":
reply = "🦞 欢迎使用虾记记账!\n\n直接发送消费描述即可记账例如\n• 午饭 25元\n• 打车 ¥30\n• 买咖啡15块\n\n命令\n/list - 查看最近记录\n/help - 帮助"
reply = "🦞 欢迎使用虾记记账!\n\n直接发送消费描述即可记账例如\n• 午饭 25元\n• 打车 ¥30\n• 买咖啡15块\n\n命令\n/list - 查看最近记录\n/today - 今日汇总\n/chart - 本月图表\n/help - 帮助"
case text == "/help":
reply = "📖 使用说明:\n\n直接发送带金额的文本即可自动记账。\n系统会自动识别金额和消费分类。\n\n支持格式\n• 午饭 25元\n• ¥30 打车\n• 买水果15块\n\n命令\n/list - 最近10条记录\n/today - 今日汇总\n/start - 欢迎信息"
reply = "📖 使用说明:\n\n直接发送带金额的文本即可自动记账。\n系统会自动识别金额和消费分类。\n\n支持格式\n• 午饭 25元\n• ¥30 打车\n• 买水果15块\n\n命令\n/list - 最近10条记录\n/today - 今日汇总\n/chart - 本月消费图表\n/week - 近7天每日趋势\n/start - 欢迎信息"
case text == "/today":
today := time.Now().Format("2006-01-02")
@@ -85,6 +86,14 @@ func (b *TGBot) handleMessage(msg *tgbotapi.Message) {
reply = sb.String()
}
case text == "/chart":
b.sendMonthlyChart(chatID)
return
case text == "/week":
b.sendWeeklyChart(chatID)
return
case text == "/list":
items, err := b.finance.GetTransactions(DefaultUserID, 10)
if err != nil {
@@ -122,3 +131,79 @@ func (b *TGBot) handleMessage(msg *tgbotapi.Message) {
log.Printf("发送消息失败 chat=%d: %v", chatID, err)
}
}
// sendMonthlyChart 发送本月分类饼图
func (b *TGBot) sendMonthlyChart(chatID int64) {
now := time.Now()
dateFrom := now.Format("2006-01") + "-01"
dateTo := now.Format("2006-01-02")
title := fmt.Sprintf("%d年%d月消费分类", now.Year(), now.Month())
stats, err := b.finance.GetCategoryStats(DefaultUserID, dateFrom, dateTo)
if err != nil || len(stats) == 0 {
m := tgbotapi.NewMessage(chatID, "📭 本月暂无消费数据")
b.api.Send(m)
return
}
imgData, err := xchart.GeneratePieChart(stats, title)
if err != nil {
log.Printf("生成饼图失败: %v", err)
m := tgbotapi.NewMessage(chatID, "❌ 图表生成失败")
b.api.Send(m)
return
}
// 计算总计文字
var total int64
var totalCount int
for _, s := range stats {
total += s.Total
totalCount += s.Count
}
caption := fmt.Sprintf("📊 %s\n💰 共 %d 笔,合计 %.2f 元", title, totalCount, float64(total)/100.0)
photo := tgbotapi.NewPhoto(chatID, tgbotapi.FileBytes{Name: "chart.png", Bytes: imgData})
photo.Caption = caption
if _, err := b.api.Send(photo); err != nil {
log.Printf("发送图表失败 chat=%d: %v", chatID, err)
}
}
// sendWeeklyChart 发送近7天每日消费柱状图
func (b *TGBot) sendWeeklyChart(chatID int64) {
now := time.Now()
dateFrom := now.AddDate(0, 0, -6).Format("2006-01-02")
dateTo := now.Format("2006-01-02")
title := fmt.Sprintf("近7天消费趋势 (%s ~ %s)", dateFrom[5:], dateTo[5:])
stats, err := b.finance.GetDailyStats(DefaultUserID, dateFrom, dateTo)
if err != nil || len(stats) == 0 {
m := tgbotapi.NewMessage(chatID, "📭 近7天暂无消费数据")
b.api.Send(m)
return
}
imgData, err := xchart.GenerateBarChart(stats, title)
if err != nil {
log.Printf("生成柱状图失败: %v", err)
m := tgbotapi.NewMessage(chatID, "❌ 图表生成失败")
b.api.Send(m)
return
}
// 总计
var total int64
var totalCount int
for _, s := range stats {
total += s.Total
totalCount += s.Count
}
caption := fmt.Sprintf("📈 %s\n💰 共 %d 笔,合计 %.2f 元", title, totalCount, float64(total)/100.0)
photo := tgbotapi.NewPhoto(chatID, tgbotapi.FileBytes{Name: "chart.png", Bytes: imgData})
photo.Caption = caption
if _, err := b.api.Send(photo); err != nil {
log.Printf("发送图表失败 chat=%d: %v", chatID, err)
}
}