package implement import ( "applet/app/db/dao" "applet/app/db/model" "applet/app/utils/logx" "reflect" "xorm.io/xorm" ) func NewRoleDb(engine *xorm.Engine, id int) dao.RoleDao { return &RoleDb{ Db: engine, Id: id, } } type RoleDb struct { Db *xorm.Engine Id int `json:"id"` } func (r RoleDb) FindRole() (*[]model.Role, error) { var m []model.Role if err := r.Db.Desc("id").Find(&m); err != nil { return nil, logx.Error(err) } return &m, nil } func (r RoleDb) GetRole() (m *model.Role, err error) { m = new(model.Role) has, err := r.Db.Where("id = ?", r.Id).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (r RoleDb) UpdateRole(m *model.Role, columns ...string) (int64, error) { affected, err := r.Db.Where("id =?", m.Id).Cols(columns...).Update(m) if err != nil { return 0, err } return affected, nil } func (r 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 (r RoleDb) RoleInsert(m *model.Role) (int, error) { _, err := r.Db.InsertOne(m) if err != nil { return 0, err } return m.Id, nil } func (r RoleDb) FindPermissionGroupByRole(roleId int) (list []*dao.RolePermissionGroup, total int64, err error) { total, err = r.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 }