package models

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

// MetaInfo 表示 meta_infos 表的模型
// Permission 表示权限表的结构
type Permission struct {
	Id           int       `description:"column:id;primary_key;autoIncrement"`
	PermissionId int       `description:"column:permission_id"`
	Name         string    `description:"column:name"`
	ParentId     int       `description:"column:parent_id"`
	RiskLevel    string    `description:"column:risk_level"`
	Sort         int       `description:"column:sort"`
	CreateTime   time.Time `description:"column:create_time"`
	UpdateTime   time.Time `description:"column:update_time"`
}

func (p *Permission) TableName() string {
	return "permissions"
}
func (p *Permission) Insert() (insert int64, err error) {
	o := orm.NewOrm()
	insert, err = o.Insert(p)
	return
}
func GetPermissionList() (items []*Permission, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM permissions ORDER BY sort ASC`
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

func GetPermissionByPermissionId(permissionId int) (items *Permission, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM permissions  where permission_id =?`
	err = o.Raw(sql, permissionId).QueryRow(&items)
	return
}

func (p *Permission) SetPermissionRiskLevel() (err error) {
	o := orm.NewOrm()
	_, err = o.Update(p, "RiskLevel")
	return
}
