package models

import (
	"time"

	"github.com/astaxie/beego/orm"
)

type Menu struct {
	Id           int       `orm:"column(id);auto" description:"主键"`
	Name         string    `orm:"column(name);size(10)" description:"菜单名称"`
	Icon         string    `orm:"column(icon);size(128)" description:"菜单图标地址"`
	ParentId     int       `orm:"column(parent_id)" description:"上一级菜单 id"`
	Type         int       `orm:"column(type)" description:"类型:1为 pc 2为app"`
	Url          string    `orm:"column(url);size(128)" description:"菜单地址"`
	SeniorStatus int       `orm:"column(senior_status)" description:"类型:是否有高级设置 1是 0 否"`
	Code         string    `orm:"column(code);size(128)" description:"菜单编码"`
	Sort         int       `orm:"column(sort)" description:"同级菜单序号 "`
	CreateTime   time.Time `orm:"column(create_time);type(timestamp);null" description:"创建时间"`
	Enabled      int8      `orm:"column(enabled)" description:"是否有效"`
}

func (t *Menu) TableName() string {
	return "menu"
}

func init() {
	orm.RegisterModel(new(Menu))
}

// GetMenuById retrieves Menu by Id. Returns error if
// Id doesn't exist
func GetMenuById(id int) (v *Menu, err error) {
	o := orm.NewOrm()
	v = &Menu{Id: id}
	if err = o.Read(v); err == nil {
		return v, nil
	}
	return nil, err
}

func GetMenuByIds(ids []int64) (v []*Menu, err error) {
	o := orm.NewOrm()
	_, err = o.QueryTable(&Menu{}).
		Filter("id__in", ids).
		All(&v)
	if err == orm.ErrNoRows {
		return v, nil
	}
	return v, nil
}