package eta_training_video

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

// EtaTrainingVideoClassifyRelate 分类关联表
type EtaTrainingVideoClassifyRelate struct {
	Id                         int    `orm:"column(id);pk"`
	EtaTrainingVideoId         int    `description:"视频ID"`
	EtaTrainingVideoClassifyId int    `description:"分类ID"`
	ClassifyParentId           int    `description:"分类父级ID"`
	ClassifyName               string `description:"分类名称"`
}

func (m *EtaTrainingVideoClassifyRelate) TableName() string {
	return "eta_training_video_classify_relate"
}

func (m *EtaTrainingVideoClassifyRelate) PrimaryId() string {
	return VideoClassifyRelateColumns.Id
}

var VideoClassifyRelateColumns = struct {
	Id                         string
	EtaTrainingVideoId         string
	EtaTrainingVideoClassifyId string
	ClassifyParentId           string
	ClassifyName               string
}{
	Id:                         "id",
	EtaTrainingVideoId:         "eta_training_video_id",
	EtaTrainingVideoClassifyId: "eta_training_video_classify_id",
	ClassifyParentId:           "classify_parent_id",
	ClassifyName:               "classify_name",
}

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

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

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

func (m *EtaTrainingVideoClassifyRelate) 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 *EtaTrainingVideoClassifyRelate) GetItemById(id int) (item *EtaTrainingVideoClassifyRelate, 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 *EtaTrainingVideoClassifyRelate) GetItemByCondition(condition string, pars []interface{}) (item *EtaTrainingVideoClassifyRelate, 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 *EtaTrainingVideoClassifyRelate) 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 *EtaTrainingVideoClassifyRelate) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EtaTrainingVideoClassifyRelate, 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 *EtaTrainingVideoClassifyRelate) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*EtaTrainingVideoClassifyRelate, 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
}

// UpdateClassifyInfoByClassifyId 更新分类冗余
func (m *EtaTrainingVideoClassifyRelate) UpdateClassifyInfoByClassifyId(classifyId, parentId int, classifyName string) (err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`UPDATE %s SET %s = ?, %s = ? WHERE %s = ?`, m.TableName(), VideoClassifyRelateColumns.ClassifyParentId, VideoClassifyRelateColumns.ClassifyName, VideoClassifyRelateColumns.EtaTrainingVideoClassifyId)
	_, err = o.Raw(sql, parentId, classifyName, classifyId).Exec()
	return
}

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