feat: 新增账户服务层,实现账户的增删改查及转账功能,并初始化AI记账、交易服务和预算仓库。
This commit is contained in:
@@ -1407,9 +1407,71 @@ func (s *AIBookkeepingService) GenerateConfirmationCard(session *AISession) *Con
|
||||
card.Date = time.Now().Format("2006-01-02")
|
||||
}
|
||||
|
||||
// Check for budget warnings
|
||||
if params.Amount != nil && *params.Amount > 0 && params.Type == "expense" {
|
||||
card.Warning = s.checkBudgetWarning(session.UserID, params.CategoryID, *params.Amount)
|
||||
}
|
||||
|
||||
return card
|
||||
}
|
||||
|
||||
// checkBudgetWarning checks if the transaction exceeds any budget
|
||||
func (s *AIBookkeepingService) checkBudgetWarning(userID uint, categoryID *uint, amount float64) string {
|
||||
now := time.Now()
|
||||
var budgets []models.Budget
|
||||
|
||||
// Find active budgets
|
||||
// We check:
|
||||
// 1. Budgets specifically for this category
|
||||
// 2. Global budgets (CategoryID is NULL)
|
||||
query := s.db.Where("user_id = ?", userID).
|
||||
Where("start_date <= ?", now).
|
||||
Where("end_date IS NULL OR end_date >= ?", now)
|
||||
|
||||
if categoryID != nil {
|
||||
query = query.Where("category_id = ? OR category_id IS NULL", *categoryID)
|
||||
} else {
|
||||
query = query.Where("category_id IS NULL")
|
||||
}
|
||||
|
||||
if err := query.Find(&budgets).Error; err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
for _, budget := range budgets {
|
||||
// Calculate current period
|
||||
start, end := s.calculateBudgetPeriod(&budget, now)
|
||||
|
||||
// Query spent amount
|
||||
var totalSpent float64
|
||||
q := s.db.Model(&models.Transaction{}).
|
||||
Where("user_id = ? AND type = ? AND transaction_date BETWEEN ? AND ?",
|
||||
userID, models.TransactionTypeExpense, start, end)
|
||||
|
||||
if budget.CategoryID != nil {
|
||||
// Get sub-categories
|
||||
var subCategoryIDs []uint
|
||||
s.db.Model(&models.Category{}).Where("parent_id = ?", *budget.CategoryID).Pluck("id", &subCategoryIDs)
|
||||
categoryIDs := append(subCategoryIDs, *budget.CategoryID)
|
||||
q = q.Where("category_id IN ?", categoryIDs)
|
||||
}
|
||||
// If budget has account restriction, we should ideally check that too,
|
||||
// but we don't always have accountID resolved here perfectly or it might be complex.
|
||||
// For now, focusing on category budgets which are most common.
|
||||
|
||||
q.Select("COALESCE(SUM(amount), 0)").Scan(&totalSpent)
|
||||
|
||||
if totalSpent+amount > budget.Amount {
|
||||
remaining := budget.Amount - totalSpent
|
||||
if remaining < 0 {
|
||||
remaining = 0
|
||||
}
|
||||
return fmt.Sprintf("⚠️ 预算预警:此交易将使【%s】预算超支 (当前剩余 %.2f)", budget.Name, remaining)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// TranscribeAudio transcribes audio and returns text
|
||||
func (s *AIBookkeepingService) TranscribeAudio(ctx context.Context, audioData io.Reader, filename string) (*TranscriptionResult, error) {
|
||||
return s.whisperService.TranscribeAudio(ctx, audioData, filename)
|
||||
@@ -1430,13 +1492,13 @@ func (s *AIBookkeepingService) ConfirmTransaction(ctx context.Context, sessionID
|
||||
|
||||
// Validate required fields
|
||||
if params.Amount == nil || *params.Amount <= 0 {
|
||||
return nil, errors.New("invalid amount")
|
||||
return nil, errors.New("无效的金额")
|
||||
}
|
||||
if params.CategoryID == nil {
|
||||
return nil, errors.New("category not specified")
|
||||
return nil, errors.New("未指定分类")
|
||||
}
|
||||
if params.AccountID == nil {
|
||||
return nil, errors.New("account not specified")
|
||||
return nil, errors.New("未指定账户")
|
||||
}
|
||||
|
||||
// Parse date
|
||||
@@ -1480,7 +1542,7 @@ func (s *AIBookkeepingService) ConfirmTransaction(ctx context.Context, sessionID
|
||||
|
||||
// Critical Check: Prevent negative balance for non-credit accounts
|
||||
if !account.IsCredit && newBalance < 0 {
|
||||
return fmt.Errorf("insufficient balance: account '%s' does not support negative balance (current: %.2f, try: %.2f)",
|
||||
return fmt.Errorf("余额不足:账户“%s”不支持负余额 (当前: %.2f, 尝试扣款: %.2f)",
|
||||
account.Name, account.Balance, *params.Amount)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user