refactor: remove legacy finance module
This commit is contained in:
+11
-37
@@ -2,28 +2,24 @@ package feishu
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ops-assistant/internal/channel"
|
||||
"ops-assistant/internal/core/ops"
|
||||
"ops-assistant/internal/service"
|
||||
"ops-assistant/models"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// DefaultUserID 统一用户ID,使所有平台共享同一份账本
|
||||
// DefaultUserID 统一用户ID
|
||||
const DefaultUserID int64 = 1
|
||||
|
||||
type Bot struct {
|
||||
db *gorm.DB
|
||||
finance *service.FinanceService
|
||||
opsSvc *ops.Service
|
||||
appID string
|
||||
appSecret string
|
||||
@@ -31,10 +27,9 @@ type Bot struct {
|
||||
encryptKey string
|
||||
}
|
||||
|
||||
func NewBot(db *gorm.DB, finance *service.FinanceService, opsSvc *ops.Service, appID, appSecret, verificationToken, encryptKey string) *Bot {
|
||||
func NewBot(db *gorm.DB, opsSvc *ops.Service, appID, appSecret, verificationToken, encryptKey string) *Bot {
|
||||
return &Bot{
|
||||
db: db,
|
||||
finance: finance,
|
||||
opsSvc: opsSvc,
|
||||
appID: appID,
|
||||
appSecret: appSecret,
|
||||
@@ -56,29 +51,29 @@ func (b *Bot) RegisterRoutes(r *gin.Engine) {
|
||||
func (b *Bot) handleWebhook(c *gin.Context) {
|
||||
body, err := io.ReadAll(io.LimitReader(c.Request.Body, 1<<20))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "bad request"})
|
||||
c.JSON(400, gin.H{"error": "bad request"})
|
||||
return
|
||||
}
|
||||
|
||||
// 统一走 channel 包解析,便于后续扩展验签/解密
|
||||
msg, verifyChallenge, err := channel.ParseFeishuInbound(body, b.verificationToken)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
c.JSON(400, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if verifyChallenge != "" {
|
||||
c.JSON(http.StatusOK, gin.H{"challenge": verifyChallenge})
|
||||
c.JSON(200, gin.H{"challenge": verifyChallenge})
|
||||
return
|
||||
}
|
||||
if msg == nil {
|
||||
c.JSON(http.StatusOK, gin.H{"code": 0})
|
||||
c.JSON(200, gin.H{"code": 0})
|
||||
return
|
||||
}
|
||||
|
||||
// 幂等去重
|
||||
var existed models.MessageDedup
|
||||
if err := b.db.Where("platform = ? AND event_id = ?", "feishu", msg.EventID).First(&existed).Error; err == nil {
|
||||
c.JSON(http.StatusOK, gin.H{"code": 0})
|
||||
c.JSON(200, gin.H{"code": 0})
|
||||
return
|
||||
}
|
||||
_ = b.db.Create(&models.MessageDedup{Platform: "feishu", EventID: msg.EventID, ProcessedAt: time.Now()}).Error
|
||||
@@ -91,7 +86,7 @@ func (b *Bot) handleWebhook(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"code": 0})
|
||||
c.JSON(200, gin.H{"code": 0})
|
||||
}
|
||||
|
||||
func (b *Bot) handleText(text string) string {
|
||||
@@ -103,31 +98,10 @@ func (b *Bot) handleText(text string) string {
|
||||
}
|
||||
switch trim {
|
||||
case "帮助", "help", "/help", "菜单", "功能", "/start":
|
||||
return "🛠️ Ops-Assistant\n\n直接发送消费描述即可记账:\n• 午饭 25元\n• 打车 ¥30\n\n📋 命令:记录/查看、今日/今天、统计"
|
||||
case "查看", "记录", "列表", "最近":
|
||||
items, err := b.finance.GetTransactions(DefaultUserID, 10)
|
||||
if err != nil {
|
||||
return "❌ 查询失败"
|
||||
}
|
||||
if len(items) == 0 {
|
||||
return "📭 暂无记录"
|
||||
}
|
||||
var sb strings.Builder
|
||||
sb.WriteString("📋 最近记录:\n\n")
|
||||
for _, item := range items {
|
||||
sb.WriteString(fmt.Sprintf("%s %s %.2f元\n", item.Date, item.Category, item.AmountYuan()))
|
||||
}
|
||||
return sb.String()
|
||||
return "🛠️ Ops-Assistant\n\n模块化运维命令助手,支持:\n• /cpa - CPA 管理\n• /cf - Cloudflare 管理\n• /cfdav - cfdav 管理\n• /sub - 订阅管理\n• /mail - 邮箱管理"
|
||||
default:
|
||||
return "❓ 无法识别,请使用 OPS 命令(发送「帮助」查看说明)。"
|
||||
}
|
||||
|
||||
amount, category, err := b.finance.AddTransaction(DefaultUserID, trim)
|
||||
if err != nil {
|
||||
return "❌ 记账失败,请稍后重试"
|
||||
}
|
||||
if amount == 0 {
|
||||
return "📍 没看到金额,这笔花了多少钱?"
|
||||
}
|
||||
return fmt.Sprintf("✅ 已记入【%s】:%.2f元\n📝 备注:%s", category, float64(amount)/100.0, trim)
|
||||
}
|
||||
|
||||
func maskID(s string) string {
|
||||
|
||||
Reference in New Issue
Block a user