package eta_training_video

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

const (
	VideoOpTypeAdd = iota + 1
	VideoOpTypeEdit
	VideoOpTypePublish
	VideoOpTypeCancelPublish
	VideoOpTypeRemove
)

// EtaTrainingVideoOpLog 视频操作记录表
type EtaTrainingVideoOpLog struct {
	Id                 int       `orm:"column(id);pk"`
	EtaTrainingVideoId int       `description:"视频ID"`
	SysUserId          int       `description:"操作人ID"`
	SysRealName        string    `description:"操作人姓名"`
	OpType             int       `description:"操作类型：1-新增；2-编辑；3-发布；4-取消发布；5-删除"`
	OpData             string    `description:"操作数据"`
	CreateTime         time.Time `description:"创建时间"`
}

func (m *EtaTrainingVideoOpLog) TableName() string {
	return "eta_training_video_op_log"
}

func (m *EtaTrainingVideoOpLog) PrimaryId() string {
	return VideoOpLogsColumns.Id
}

var VideoOpLogsColumns = struct {
	Id                 string
	EtaTrainingVideoId string
	SysUserId          string
	SysRealName        string
	OpType             string
	OpData             string
	CreateTime         string
}{
	Id:                 "id",
	EtaTrainingVideoId: "eta_training_video_id",
	SysUserId:          "sys_user_id",
	SysRealName:        "sys_real_name",
	OpType:             "op_type",
	OpData:             "op_data",
	CreateTime:         "create_time",
}

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

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

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

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