package yb

import (
	"github.com/beego/beego/v2/client/orm"
	"time"
)

// BulletChat 弹幕
type BulletChat struct {
	Id          int       `orm:"column(id);pk" description:"弹幕ID"`
	UserId      int       `description:"用户ID"`
	PrimaryId   int       `description:"视频社区/线上路演ID"`
	Content     string    `description:"弹幕内容"`
	Seconds     float64   `descrption:"时间点(秒)"`
	Color       string    `description:"弹幕颜色"`
	Title       string    `description:"视频标题"`
	Source      int       `description:"来源: 1-视频社区; 2-线上路演"`
	SourceAgent int       `description:"来源: 1-小程序; 2-小程序PC; 3-弘则研究公众号; 4-Web PC"`
	CreateTime  time.Time `description:"发布时间"`
	ModifyTime  time.Time `description:"更新时间"`
	OpAdminId   int       `description:"删除操作管理员ID"`
	IsDeleted   int       `description:"是否已删除: 0-否 1-是"`
	DeleteTime  time.Time `description:"删除时间"`
}

func (item *BulletChat) TableName() string {
	return "yb_bullet_chat"
}

func (item *BulletChat) Add() (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Insert(item)
	if err != nil {
		return
	}
	item.Id = int(lastId)
	return
}

func (item *BulletChat) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(item, cols...)
	return
}

// GetBulletChatById 主键获取弹幕
func GetBulletChatById(id int) (item *BulletChat, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM yb_bullet_chat WHERE id = ? LIMIT 1`
	err = o.Raw(sql, id).QueryRow(&item)
	return
}

// GetBulletChatPageList 获取弹幕分页列表
func GetBulletChatPageList(condition string, pars []interface{}, order string, startSize, pageSize int) (total int, list []*BulletChat, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM yb_bullet_chat WHERE is_deleted = 0 `
	sql += condition
	if order != "" {
		sql += order
	} else {
		sql += ` ORDER BY create_time DESC`
	}
	totalSQl := `SELECT COUNT(1) total FROM (` + sql + `) z`
	if err = o.Raw(totalSQl, pars).QueryRow(&total); err != nil {
		return
	}
	sql += ` LIMIT ?,?`
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
	return
}
