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

package models
import (
"github.com/ouxuanserver/osmanthuswine/src/core"
"time"
)
const LiveViewerTN = TableNamePrefix + "live_viewer"
type LiveViewer struct {
Id int64 `json:"id" xorm:"not null pk autoincr INT(11)"`
IsDelete bool `json:"-" xorm:"not null default 0 comment('是否删除') TINYINT(1)"`
CreatedAt time.Time `json:"created_at" xorm:"not null created comment('创建时间') DATETIME"`
UpdatedAt time.Time `json:"updated_at" xorm:"not null updated default CURRENT_TIMESTAMP comment('更新时间') TIMESTAMP"`
UserId int64 `json:"user_id" xorm:"not null default 0 comment('用户id') INT(11)"`
ActivityId int64 `json:"activity_id" xorm:"not null default 0 comment('活动id') INT(11)"`
LiveConfigId int64 `json:"live_config_id" xorm:"not null default 0 comment('直播id') INT(11)"`
}
func (t *LiveViewer) TableName() string {
return LiveViewerTN
}
func (t *LiveViewer) Record(uid, aid, lid int64) error {
session := core.GetXormAuto().NewSession()
defer session.Close()
session.Begin()
exist, err := session.Where("is_delete=0 and user_id=? and activity_id=?", uid, aid).Exist(t)
if err != nil {
session.Rollback()
return err
}
if exist {
session.Commit()
return nil
}
t.UserId = uid
t.ActivityId = aid
t.LiveConfigId = lid
_, err = session.InsertOne(t)
if err != nil {
session.Rollback()
return err
}
_, err = session.Where("is_delete=0 and activity_id=?", aid).Incr("watch_num").Update(&LiveConfig{})
if err != nil {
session.Rollback()
return err
}
session.Commit()
return nil
}
func GetLiveViewerByLiveConfigIds(ids interface{}) ([]*LiveViewer, error) {
viewers := make([]*LiveViewer, 0)
err := core.GetXormAuto().Where("is_delete=0").In("live_config_id", ids).Find(&viewers)
return viewers, err
}
func CountLiveViewerByLiveConfigId(lid interface{}) (int64, error) {
return core.GetXormAuto().Where("is_delete=0 and live_config_id=?", lid).Count(&LiveViewer{})
}
func (t *LiveViewer) ExistByUserId(userId, liveId interface{}) (bool, error) {
return core.GetXormAuto().Where("is_delete=0 and user_id=? and live_config_id=?", userId, liveId).Exist(t)
}