package eta_training_video

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

// EtaTrainingVideoTag 视频标签表
type EtaTrainingVideoTag struct {
	EtaTrainingVideoTagId int       `orm:"column(eta_training_video_tag_id);pk"`
	TagName               string    `description:"标签名称"`
	SysUserId             int       `description:"创建人ID"`
	SysRealName           string    `description:"创建人姓名"`
	VideoTotal            int       `description:"关联视频数"`
	CreateTime            time.Time `description:"创建时间"`
	ModifyTime            time.Time `description:"更新时间"`
}

func (m *EtaTrainingVideoTag) TableName() string {
	return "eta_training_video_tag"
}

func (m *EtaTrainingVideoTag) PrimaryId() string {
	return VideoTagColumns.EtaTrainingVideoTagId
}

var VideoTagColumns = struct {
	EtaTrainingVideoTagId string
	TagName               string
	SysUserId             string
	SysRealName           string
	VideoTotal            string
	CreateTime            string
	ModifyTime            string
}{
	EtaTrainingVideoTagId: "eta_training_video_tag_id",
	TagName:               "tag_name",
	SysUserId:             "sys_user_id",
	SysRealName:           "sys_real_name",
	VideoTotal:            "video_total",
	CreateTime:            "create_time",
	ModifyTime:            "modify_time",
}

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

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

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

func (m *EtaTrainingVideoTag) 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.EtaTrainingVideoTagId).Exec()
	return
}

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

func (m *EtaTrainingVideoTag) UpdateVideoTotal(tagId int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE eta_training_video_tag SET video_total = (
				SELECT COUNT(1) FROM eta_training_video_tag_relate WHERE eta_training_video_tag_id = ?
			) WHERE eta_training_video_tag_id = ?`
	_, err = o.Raw(sql, tagId, tagId).Exec()
	return
}

// RemoveVideoTotalByVideoId 更新标签的视频引用数
func (m *EtaTrainingVideoTag) RemoveVideoTotalByVideoId(videoId int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE eta_training_video_tag SET video_total = video_total-1 WHERE eta_training_video_tag_id IN (
				SELECT DISTINCT eta_training_video_tag_id FROM eta_training_video_tag_relate WHERE eta_training_video_id = ?
			) AND video_total > 0`
	_, err = o.Raw(sql, videoId).Exec()
	return
}

// EtaTrainingVideoTagAddReq 新增标签请求体
type EtaTrainingVideoTagAddReq struct {
	TagName string `description:"标签名称"`
}

// EtaTrainingVideoTagEditReq 编辑标签请求体
type EtaTrainingVideoTagEditReq struct {
	TagId int `description:"标签ID"`
	EtaTrainingVideoTagAddReq
}

// EtaTrainingVideoTagRemoveReq 删除标签请求体
type EtaTrainingVideoTagRemoveReq struct {
	TagId int `description:"标签ID"`
}

// EtaTrainingVideoTagPageListResp 标签分页列表响应体
type EtaTrainingVideoTagPageListResp struct {
	List   []*EtaTrainingVideoTagItem `description:"标签列表数据"`
	Paging *paging.PagingItem         `description:"分页数据"`
}

// EtaTrainingVideoTagItem 标签信息
type EtaTrainingVideoTagItem struct {
	TagId      int    `description:"标签ID"`
	TagName    string `description:"标签名称"`
	VideoTotal int    `description:"关联视频数"`
	CreateTime string `description:"创建时间"`
	ModifyTime string `description:"更新时间"`
}
