package eta_training_video

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

// EtaTrainingVideoViewLog 视频操作记录表
type EtaTrainingVideoViewLog struct {
	Id                     int       `orm:"column(id);pk"`
	EtaTrainingVideoId     int       `description:"视频ID"`
	EtaBusinessId          int       `description:"商家ID"`
	EtaBusinessCodeEncrypt string    `description:"商家编码encrypt"`
	CreateTime             time.Time `description:"创建时间"`
}

func (m *EtaTrainingVideoViewLog) TableName() string {
	return "eta_training_video_view_log"
}

func (m *EtaTrainingVideoViewLog) PrimaryId() string {
	return VideoViewLogColumns.Id
}

var VideoViewLogColumns = struct {
	Id                     string
	EtaTrainingVideoId     string
	EtaBusinessId          string
	EtaBusinessCodeEncrypt string
	CreateTime             string
}{
	Id:                     "id",
	EtaTrainingVideoId:     "eta_training_video_id",
	EtaBusinessId:          "eta_business_id",
	EtaBusinessCodeEncrypt: "eta_business_code_encrypt",
	CreateTime:             "create_time",
}

func (m *EtaTrainingVideoViewLog) Create() (err error) {
	o := orm.NewOrm()
	id, err := o.Insert(m)
	if err != nil {
		return
	}
	m.Id = int(id)
	return
}

func (m *EtaTrainingVideoViewLog) CreateMulti(items []*EtaTrainingVideoViewLog) (err error) {
	if len(items) == 0 {
		return
	}
	o := orm.NewOrm()
	_, err = o.InsertMulti(len(items), items)
	return
}

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

func (m *EtaTrainingVideoViewLog) Del() (err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	_, err = o.Raw(sql, m.Id).Exec()
	return
}

func (m *EtaTrainingVideoViewLog) GetItemById(id int) (item *EtaTrainingVideoViewLog, err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	err = o.Raw(sql, id).QueryRow(&item)
	return
}

func (m *EtaTrainingVideoViewLog) GetItemByCondition(condition string, pars []interface{}) (item *EtaTrainingVideoViewLog, err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
	err = o.Raw(sql, pars).QueryRow(&item)
	return
}

func (m *EtaTrainingVideoViewLog) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func (m *EtaTrainingVideoViewLog) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EtaTrainingVideoViewLog, err error) {
	o := orm.NewOrm()
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := `ORDER BY create_time DESC`
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

func (m *EtaTrainingVideoViewLog) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*EtaTrainingVideoViewLog, err error) {
	o := orm.NewOrm()
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := `ORDER BY create_time DESC`
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}
