package db import ( "applet/app/db/model" "applet/app/utils/logx" "reflect" "xorm.io/xorm" ) type RoleDb struct { Db *xorm.Engine `json:"db"` Id int `json:"id"` } func (roleDb *RoleDb) Set(id int) { // set方法 roleDb.Db = Db roleDb.Id = id } func (roleDb *RoleDb) FindRole() (*[]model.Role, error) { var m []model.Role if err := roleDb.Db.Desc("id").Find(&m); err != nil { return nil, logx.Error(err) } return &m, nil } func (roleDb *RoleDb) GetRole() (m *model.Role, err error) { m = new(model.Role) has, err := roleDb.Db.Where("id = ?", roleDb.Id).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (roleDb *RoleDb) UpdateRole(m *model.Role, columns ...string) (int64, error) { affected, err := roleDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m) if err != nil { return 0, err } return affected, nil } func (roleDb *RoleDb) RoleDeleteBySession(session *xorm.Session, id interface{}) (int64, error) { if reflect.TypeOf(id).Kind() == reflect.Slice { return session.In("id", id).Delete(model.Role{}) } else { return session.Where("id = ?", id).Delete(model.Role{}) } } func (roleDb *RoleDb) RoleInsert(m *model.Role) (int, error) { _, err := roleDb.Db.InsertOne(m) if err != nil { return 0, err } return m.Id, nil } func (roleDb *RoleDb) FindPermissionGroupByRole(roleId int) (list []*RolePermissionGroup, total int64, err error) { total, err = roleDb.Db.Where("role.id =?", roleId). Join("LEFT", "role_permission_group", "role_permission_group.role_id = role.id"). Join("LEFT", "permission_group", "permission_group.id = role_permission_group.group_id"). FindAndCount(&list) return } type RolePermissionGroup struct { model.Role `xorm:"extends"` model.RolePermissionGroup `xorm:"extends"` model.PermissionGroup `xorm:"extends"` } func (RolePermissionGroup) TableName() string { return "role" }