package models

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

type OutLink struct {
	Id           int       `orm:"column(id);pk;auto" description:"自增序号"`
	Title        string    `description:"菜单名称"`
	Url          string    `description:"链接地址"`
	SysAdminId   int       `description:"创建人ID"`
	SysAdminName string    `description:"创建人姓名"`
	CreateTime   time.Time `description:"创建时间"`
	ModifyTime   time.Time `description:"修改时间"`
}

type OutLinkItem struct {
	Id         int    `description:"自增序号"`
	Title      string `description:"菜单名称"`
	Url        string `description:"链接地址"`
	CreateTime string `description:"创建时间"`
	ModifyTime string `description:"修改时间"`
}

type OutLinkListResp struct {
	List   []*OutLinkItem
	Paging *paging.PagingItem `description:"分页数据"`
}

type OutLinkSaveReq struct {
	Id    int    `description:"序号"`
	Title string `description:"菜单名称"`
	Url   string `description:"链接地址"`
}

type OutLinkReq struct {
	Id int `description:"序号"`
}

func (ol *OutLink) TableName() string {
	return "out_link"
}

func (ol *OutLink) Create() (err error) {
	o := orm.NewOrm()
	_, err = o.Insert(ol)
	if err != nil {
		return
	}
	return
}

func (ol *OutLink) GetItemById(id int) (err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM out_link WHERE id = ? LIMIT 1`
	err = o.Raw(sql, id).QueryRow(&ol)
	return
}

func (ol *OutLink) GetItemsByCondition(condition string, pars []interface{}, orderRule string) (items []*OutLink, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM out_link WHERE 1=1 `
	sql += condition
	order := ` ORDER BY create_time ASC`
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql += order
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

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

func (ol *OutLink) Del() (err error) {
	o := orm.NewOrm()
	sql := `DELETE FROM out_link WHERE id = ? LIMIT 1`
	_, err = o.Raw(sql, ol.Id).Exec()
	return
}

func (ol *OutLink) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, ol.TableName(), condition)
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func (ol *OutLink) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*OutLink, err error) {
	o := orm.NewOrm()
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := `ORDER BY create_time ASC`
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, ol.TableName(), condition, order)
	totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z`
	if err = o.Raw(totalSql, pars).QueryRow(&total); err != nil {
		return
	}
	sql += ` LIMIT ?,?`
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}