面包店
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

db_role.go 1.9 KiB

9 months ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/utils/logx"
  5. "reflect"
  6. "xorm.io/xorm"
  7. )
  8. type RoleDb struct {
  9. Db *xorm.Engine `json:"db"`
  10. Id int `json:"id"`
  11. }
  12. func (roleDb *RoleDb) Set(id int) { // set方法
  13. roleDb.Db = Db
  14. roleDb.Id = id
  15. }
  16. func (roleDb *RoleDb) FindRole() (*[]model.Role, error) {
  17. var m []model.Role
  18. if err := roleDb.Db.Desc("id").Find(&m); err != nil {
  19. return nil, logx.Error(err)
  20. }
  21. return &m, nil
  22. }
  23. func (roleDb *RoleDb) GetRole() (m *model.Role, err error) {
  24. m = new(model.Role)
  25. has, err := roleDb.Db.Where("id = ?", roleDb.Id).Get(m)
  26. if err != nil {
  27. return nil, logx.Error(err)
  28. }
  29. if has == false {
  30. return nil, nil
  31. }
  32. return m, nil
  33. }
  34. func (roleDb *RoleDb) UpdateRole(m *model.Role, columns ...string) (int64, error) {
  35. affected, err := roleDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
  36. if err != nil {
  37. return 0, err
  38. }
  39. return affected, nil
  40. }
  41. func (roleDb *RoleDb) RoleDeleteBySession(session *xorm.Session, id interface{}) (int64, error) {
  42. if reflect.TypeOf(id).Kind() == reflect.Slice {
  43. return session.In("id", id).Delete(model.Role{})
  44. } else {
  45. return session.Where("id = ?", id).Delete(model.Role{})
  46. }
  47. }
  48. func (roleDb *RoleDb) RoleInsert(m *model.Role) (int, error) {
  49. _, err := roleDb.Db.InsertOne(m)
  50. if err != nil {
  51. return 0, err
  52. }
  53. return m.Id, nil
  54. }
  55. func (roleDb *RoleDb) FindPermissionGroupByRole(roleId int) (list []*RolePermissionGroup, total int64, err error) {
  56. total, err = roleDb.Db.Where("role.id =?", roleId).
  57. Join("LEFT", "role_permission_group", "role_permission_group.role_id = role.id").
  58. Join("LEFT", "permission_group", "permission_group.id = role_permission_group.group_id").
  59. FindAndCount(&list)
  60. return
  61. }
  62. type RolePermissionGroup struct {
  63. model.Role `xorm:"extends"`
  64. model.RolePermissionGroup `xorm:"extends"`
  65. model.PermissionGroup `xorm:"extends"`
  66. }
  67. func (RolePermissionGroup) TableName() string {
  68. return "role"
  69. }