role_menu.go 1.3 KB
package models

import (
	"fmt"

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

type RoleMenu struct {
	Id      int64  `orm:"column(id);auto" description:"主键"`
	RoleId  int64  `orm:"column(role_id);null" description:"role.id"`
	MenuId  int    `orm:"column(menu_id);null" description:"menu.id"`
	Opption string `orm:"column(opption);size(500);null" description:"配置"`
	Code    string `orm:"column(code)"`
}

func (t *RoleMenu) TableName() string {
	return "role_menu"
}

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

func GetRoleMenuByRole(roleid int64) ([]RoleMenu, error) {
	var (
		list []RoleMenu
		err  error
	)
	o := orm.NewOrm()
	_, err = o.QueryTable(&RoleMenu{}).
		Filter("role_id", roleid).
		All(&list)
	if err == orm.ErrNoRows {
		return list, nil
	}
	return list, err
}

func GetRoleMenuByCode(roleId int64, code string) (*RoleMenu, error) {
	var data RoleMenu
	o := orm.NewOrm()
	err := o.QueryTable(&RoleMenu{}).
		Filter("role_id", roleId).
		Filter("code", code).
		One(&data)
	fmt.Println(err)
	if err != nil {
		return nil, err
	}
	return &data, nil
}

func AddRoleMenu(m *RoleMenu, om ...orm.Ormer) (id int64, err error) {
	var o orm.Ormer
	if len(om) > 0 {
		o = om[0]
	} else {
		o = orm.NewOrm()
	}
	id, err = o.Insert(m)
	return
}

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