智慧食堂
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

81 linhas
1.9 KiB

  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. }