feat: add email_send_log table to track email sends and registration status
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
50
internal/storage/dao/email_send_log.go
Normal file
50
internal/storage/dao/email_send_log.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type EmailSendStatus int8
|
||||
|
||||
const (
|
||||
EmailSendStatusSent EmailSendStatus = 0 // 已发送,用户未注册
|
||||
EmailSendStatusRegistered EmailSendStatus = 1 // 用户已完成注册
|
||||
)
|
||||
|
||||
type EmailSendLog struct {
|
||||
BaseModel
|
||||
Email string `gorm:"column:email;type:varchar(255);not null;comment:邮箱地址" json:"email"`
|
||||
Status EmailSendStatus `gorm:"column:status;type:tinyint;not null;default:0;comment:状态: 0=已发送未注册 1=已注册" json:"status"`
|
||||
}
|
||||
|
||||
func (e *EmailSendLog) TableName() string {
|
||||
return "email_send_log"
|
||||
}
|
||||
|
||||
type EmailSendLogDao struct{}
|
||||
|
||||
func NewEmailSendLogDao() *EmailSendLogDao {
|
||||
return &EmailSendLogDao{}
|
||||
}
|
||||
|
||||
func (d *EmailSendLogDao) Create(tx *gorm.DB, log *EmailSendLog) error {
|
||||
return tx.Create(log).Error
|
||||
}
|
||||
|
||||
func (d *EmailSendLogDao) GetLatestByEmail(tx *gorm.DB, email string) (*EmailSendLog, error) {
|
||||
var record EmailSendLog
|
||||
err := tx.Where("email = ?", email).Order("id DESC").First(&record).Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &record, nil
|
||||
}
|
||||
|
||||
func (d *EmailSendLogDao) MarkRegistered(tx *gorm.DB, email string) error {
|
||||
return tx.Model(&EmailSendLog{}).
|
||||
Where("email = ? AND status = ?", email, EmailSendStatusSent).
|
||||
Update("status", EmailSendStatusRegistered).Error
|
||||
}
|
||||
Reference in New Issue
Block a user