package eta_training_video

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

// EtaTrainingVideoTagRelate 标签关联表
type EtaTrainingVideoTagRelate struct {
	Id                    int    `orm:"column(id);pk"`
	EtaTrainingVideoId    int    `description:"视频ID"`
	EtaTrainingVideoTagId int    `description:"标签ID"`
	TagName               string `description:"标签名称"`
}

func (m *EtaTrainingVideoTagRelate) TableName() string {
	return "eta_training_video_tag_relate"
}

func (m *EtaTrainingVideoTagRelate) PrimaryId() string {
	return VideoTagRelateColumns.Id
}

var VideoTagRelateColumns = struct {
	Id                    string
	EtaTrainingVideoId    string
	EtaTrainingVideoTagId string
	TagName               string
}{
	Id:                    "id",
	EtaTrainingVideoId:    "eta_training_video_id",
	EtaTrainingVideoTagId: "eta_training_video_tag_id",
	TagName:               "tag_name",
}

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

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

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

func (m *EtaTrainingVideoTagRelate) 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 *EtaTrainingVideoTagRelate) GetItemById(id int) (item *EtaTrainingVideoTagRelate, 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 *EtaTrainingVideoTagRelate) GetItemByCondition(condition string, pars []interface{}) (item *EtaTrainingVideoTagRelate, 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 *EtaTrainingVideoTagRelate) 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 *EtaTrainingVideoTagRelate) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EtaTrainingVideoTagRelate, err error) {
	o := orm.NewOrm()
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := ``
	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 *EtaTrainingVideoTagRelate) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*EtaTrainingVideoTagRelate, err error) {
	o := orm.NewOrm()
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := ``
	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
}

// UpdateTagNameByTagId 更新标签名称
func (m *EtaTrainingVideoTagRelate) UpdateTagNameByTagId(tagId int, tagName string) (err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`UPDATE %s SET %s = ? WHERE %s = ?`, m.TableName(), VideoTagRelateColumns.TagName, VideoTagRelateColumns.EtaTrainingVideoTagId)
	_, err = o.Raw(sql, tagName, tagId).Exec()
	return
}

// RemoveRelateByTagId 根据标签ID移除关联
func (m *EtaTrainingVideoTagRelate) RemoveRelateByTagId(tagId int) (err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ?`, m.TableName(), VideoTagRelateColumns.EtaTrainingVideoTagId)
	_, err = o.Raw(sql, tagId).Exec()
	return
}

// RemoveRelateByVideoId 根据视频ID移除关联
func (m *EtaTrainingVideoTagRelate) RemoveRelateByVideoId(video int) (err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ?`, m.TableName(), VideoTagRelateColumns.EtaTrainingVideoId)
	_, err = o.Raw(sql, video).Exec()
	return
}
