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

72 lines
2.3 KiB

5 years ago
5 years ago
4 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
5 years ago
5 years ago
5 years ago
5 years ago
  1. package models
  2. import (
  3. "time"
  4. "git.ouxuan.net/tommy/osmanthuswine/src/core"
  5. )
  6. const LiveViewerTN = TableNamePrefix + "live_viewer"
  7. type LiveViewer struct {
  8. Id int `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 int `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 int `json:"activity_id" xorm:"not null default 0 comment('活动id') INT(11)"`
  15. LiveConfigId int `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 int) 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").
  52. Join("left", Alias(&Activity{}, "a"),
  53. "a.id=l.activity_id and l.arch_id=a.arch_id").
  54. Where("l.is_delete=0").In("l.live_config_id", ids).
  55. Find(&viewers)
  56. return viewers, err
  57. }
  58. func CountLiveViewerByLiveConfigId(lid interface{}) (int64, error) {
  59. return core.GetXormAuto().Where("is_delete=0 and live_config_id=?", lid).Count(&LiveViewer{})
  60. }
  61. func (t *LiveViewer) ExistByUserId(userId, liveId interface{}) (bool, error) {
  62. return core.GetXormAuto().Where("is_delete=0 and user_id=? and live_config_id=?", userId, liveId).Exist(t)
  63. }