互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.4 KiB

5 years ago
  1. package models
  2. import (
  3. "github.com/ouxuanserver/osmanthuswine/src/core"
  4. "time"
  5. )
  6. const LiveViewerTN = TableNamePrefix + "live_viewer"
  7. type LiveViewer struct {
  8. Id int64 `json:"id" xorm:"not null pk autoincr INT(11)"`
  9. IsDelete bool `json:"-" xorm:"not null default 0 comment('是否删除') TINYINT(1)"`
  10. CreatedAt time.Time `json:"created_at" xorm:"not null created comment('创建时间') DATETIME"`
  11. UpdatedAt time.Time `json:"updated_at" xorm:"not null updated default CURRENT_TIMESTAMP comment('更新时间') TIMESTAMP"`
  12. UserId int64 `json:"user_id" xorm:"not null default 0 comment('用户id') INT(11)"`
  13. ActivityId int64 `json:"activity_id" xorm:"not null default 0 comment('活动id') INT(11)"`
  14. }
  15. func (t *LiveViewer) TableName() string {
  16. return LiveViewerTN
  17. }
  18. func (t *LiveViewer) Record(uid, aid int64) error {
  19. session := core.GetXormAuto().NewSession()
  20. defer session.Close()
  21. session.Begin()
  22. exist, err := session.Where("is_delete=0 and user_id=? and activity_id=?", uid, aid).Exist(t)
  23. if err != nil {
  24. session.Rollback()
  25. return err
  26. }
  27. if exist {
  28. session.Commit()
  29. return nil
  30. }
  31. t.UserId = uid
  32. t.ActivityId = aid
  33. _, err = session.InsertOne(t)
  34. if err != nil {
  35. session.Rollback()
  36. return err
  37. }
  38. _, err = session.Where("is_delete=0 and activity_id=?", aid).Incr("watch_num").Update(&LiveConfig{})
  39. if err != nil {
  40. session.Rollback()
  41. return err
  42. }
  43. session.Commit()
  44. return nil
  45. }