package models

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

// EtaVersionUpdateLog ETA版本更新日志表
type EtaVersionUpdateLog struct {
	Id         int       `orm:"column(id);pk"`
	Version    string    `description:"版本号"`
	Content    string    `description:"更新内容"`
	UpdateDate time.Time `description:"更新日期"`
	CreateTime time.Time
	ModifyTime time.Time
}

func (m *EtaVersionUpdateLog) TableName() string {
	return "eta_version_update_log"
}

func (m *EtaVersionUpdateLog) PrimaryId() string {
	return "id"
}

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

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

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

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

// EtaVersionUpdateLogItem 更新日志信息
type EtaVersionUpdateLogItem struct {
	Id         int
	Version    string `description:"版本号"`
	Content    string `description:"更新内容"`
	UpdateDate string `description:"更新日期"`
	CreateTime string
	ModifyTime string
}

// EtaVersionUpdateLogListResp 更新日志列表响应体
type EtaVersionUpdateLogListResp struct {
	List   []*EtaVersionUpdateLogItem
	Paging *paging.PagingItem
}

// EtaVersionUpdateLogAddReq 新增更新日志请求体
type EtaVersionUpdateLogAddReq struct {
	Version    string `description:"版本号"`
	Content    string `description:"更新内容"`
	UpdateDate string `description:"更新日期"`
}

// EtaVersionUpdateLogEditReq 编辑更新日志请求体
type EtaVersionUpdateLogEditReq struct {
	Id int
	EtaVersionUpdateLogAddReq
}

// EtaVersionUpdateLogDelReq 删除更新日志请求体
type EtaVersionUpdateLogDelReq struct {
	Id int
}
