user_event_flow.go 773 B

12345678910111213141516171819202122232425262728293031323334
  1. package user
  2. import (
  3. "eta/eta_mini_ht_api/models"
  4. "gorm.io/gorm"
  5. "time"
  6. )
  7. type LogType string
  8. const (
  9. Login LogType = "login"
  10. Logout LogType = "logout"
  11. )
  12. type UserEventFlow struct {
  13. ID int `gorm:"primaryKey;autoIncrement"`
  14. UserID int `gorm:"not null"`
  15. EventType LogType `gorm:"type:enum('login','logout');not null"`
  16. EventTime time.Time `gorm:"default:null"`
  17. CreateTime time.Time `gorm:"default:null"`
  18. }
  19. // BeforeCreate 钩子用于在创建记录前设置 CreateTime
  20. func (uef *UserEventFlow) BeforeCreate(tx *gorm.DB) (err error) {
  21. uef.CreateTime = time.Now()
  22. uef.EventTime = time.Now()
  23. return
  24. }
  25. func InsertEventFlow(flow UserEventFlow) error {
  26. db := models.Main()
  27. return db.Model(&UserEventFlow{}).Create(&flow).Error
  28. }