package eta_business

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

// EtaBusinessMenuIcon 菜单icon表
type EtaBusinessMenuIcon struct {
	IconId     int       `orm:"column(icon_id);pk"`
	IconPath   string    `description:"icon地址"`
	CreateTime time.Time `description:"创建时间"`
}

func (m *EtaBusinessMenuIcon) TableName() string {
	return "eta_business_menu_icon"
}

func (m *EtaBusinessMenuIcon) PrimaryId() string {
	return "icon_id"
}

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

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

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

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

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

// EtaBusinessMenuIconItem 菜单icon
type EtaBusinessMenuIconItem struct {
	IconId   int
	IconPath string `description:"icon地址"`
}

// EtaBusinessMenuIconAddReq 新增icon请求体
type EtaBusinessMenuIconAddReq struct {
	IconPath string `description:"icon地址"`
}
