fix: First 改 Find 消除 record not found 日志噪音

- GetDailyEnergizeExp 用 Find + Limit(1) 替代 First,空结果不触发 GORM ErrRecordNotFound
- 移除不再需要的 errors 导入
- 数据库检查确认三张表均存在,daily_exp_summaries 为空属正常状态
This commit is contained in:
2026-06-01 12:49:26 +08:00
parent 610277c8c7
commit 5248e91a3d

View File

@ -1,7 +1,6 @@
package repository
import (
"errors"
"time"
"metazone.cc/metalab/internal/model"
@ -83,15 +82,15 @@ func (r *EnergyRepo) CreateEnergizeLog(log *model.PostEnergizeLog) error {
// GetDailyEnergizeExp 获取用户当日通过赋能获得的经验值
// 无记录时返回 nil, nil调用方当作 exp=0 处理)
func (r *EnergyRepo) GetDailyEnergizeExp(userID uint, date time.Time) (*model.DailyExpSummary, error) {
var s model.DailyExpSummary
err := r.db.Where("user_id = ? AND date = ?", userID, date).First(&s).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
var summaries []model.DailyExpSummary
err := r.db.Where("user_id = ? AND date = ?", userID, date).Limit(1).Find(&summaries).Error
if err != nil {
return nil, err
}
return &s, nil
if len(summaries) == 0 {
return nil, nil
}
return &summaries[0], nil
}
// UpsertDailyExp 创建或更新每日经验汇总