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

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