广告平台(站长下代理使用)
Você não pode selecionar mais de 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.

role_implement.go 1.7 KiB

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