互动
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.

66 lines
2.1 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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. LiveConfigId int64 `json:"live_config_id" xorm:"not null default 0 comment('直播id') INT(11)"`
  15. }
  16. func (t *LiveViewer) TableName() string {
  17. return LiveViewerTN
  18. }
  19. func (t *LiveViewer) Record(uid, aid, lid int64) error {
  20. session := core.GetXormAuto().NewSession()
  21. defer session.Close()
  22. session.Begin()
  23. exist, err := session.Where("is_delete=0 and user_id=? and activity_id=?", uid, aid).Exist(t)
  24. if err != nil {
  25. session.Rollback()
  26. return err
  27. }
  28. if exist {
  29. session.Commit()
  30. return nil
  31. }
  32. t.UserId = uid
  33. t.ActivityId = aid
  34. t.LiveConfigId = lid
  35. _, err = session.InsertOne(t)
  36. if err != nil {
  37. session.Rollback()
  38. return err
  39. }
  40. _, err = session.Where("is_delete=0 and activity_id=?", aid).Incr("watch_num").Update(&LiveConfig{})
  41. if err != nil {
  42. session.Rollback()
  43. return err
  44. }
  45. session.Commit()
  46. return nil
  47. }
  48. func GetLiveViewerByLiveConfigIds(ids interface{}) ([]*LiveViewer, error) {
  49. viewers := make([]*LiveViewer, 0)
  50. err := core.GetXormAuto().Where("is_delete=0").In("live_config_id", ids).Find(&viewers)
  51. return viewers, err
  52. }
  53. func CountLiveViewerByLiveConfigId(lid interface{}) (int64, error) {
  54. return core.GetXormAuto().Where("is_delete=0 and live_config_id=?", lid).Count(&LiveViewer{})
  55. }
  56. func (t *LiveViewer) ExistByUserId(userId, liveId interface{}) (bool, error) {
  57. return core.GetXormAuto().Where("is_delete=0 and user_id=? and live_config_id=?", userId, liveId).Exist(t)
  58. }