role.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package role
  2. import (
  3. "errors"
  4. "zhiyuan/models"
  5. "zhiyuan/pkg/app"
  6. "zhiyuan/pkg/db"
  7. "zhiyuan/pkg/utils"
  8. "zhiyuan/services/admin"
  9. "zhiyuan/services/auth"
  10. "zhiyuan/services/form"
  11. )
  12. var Role models.Role
  13. func GetList(where map[string]interface{}, fields []string, page app.Page, retVal interface{}) ([]*models.Role, error) {
  14. if page.PageNum > 0 && page.PageSize > 0 {
  15. where["_limit"] = db.GetOffset(uint(page.PageNum), uint(page.PageSize))
  16. }
  17. return Role.GetMulti(where, fields, retVal)
  18. }
  19. func GetOne(where map[string]interface{}, fields []string, retVal interface{}) (*models.Role, error) {
  20. return Role.GetOne(where, fields, retVal)
  21. }
  22. func GetInfoByID(id int, fields []string, retVal interface{}) (*models.Role, error) {
  23. return GetOne(map[string]interface{}{"id": id}, fields, retVal)
  24. }
  25. func Count(where map[string]interface{}) (int64, error) {
  26. return db.Count(Role.TableName(), where)
  27. }
  28. func Add(form form.RoleAdd) (int64, error) {
  29. if CheckRoleDuplicate(utils.ToStr(form.Name)) {
  30. return 0, errors.New("role already exists")
  31. }
  32. authList, _ := auth.GetList(map[string]interface{}{"id in": form.AuthIds}, []string{"id, auth"}, app.Page{}, nil)
  33. roleMap := map[string]interface{}{
  34. "name": form.Name,
  35. "auth_ids": utils.JoinIntSlice(form.AuthIds, ","),
  36. "auth_list": utils.JoinSliceMap(authList, "auth", ","),
  37. "data_auth": form.DataAuth,
  38. }
  39. roleID, err := db.InsertOne(Role.TableName(), roleMap)
  40. if err != nil {
  41. return 0, nil
  42. }
  43. return roleID, nil
  44. }
  45. func EditByID(form form.RoleAdd, id int) error {
  46. roleInfo, err := GetInfoByID(id, nil, nil)
  47. if roleInfo == nil {
  48. return errors.New("invalid auth id")
  49. }
  50. if roleInfo.Name != form.Name && CheckRoleDuplicate(form.Name) {
  51. return errors.New("role already exists")
  52. }
  53. authList, _ := auth.GetList(map[string]interface{}{"id in": form.AuthIds}, []string{"id, auth"}, app.Page{}, nil)
  54. roleMap := map[string]interface{}{
  55. "name": form.Name,
  56. "auth_ids": utils.JoinIntSlice(form.AuthIds, ","),
  57. "auth_list": utils.JoinSliceMap(authList, "auth", ","),
  58. "data_auth": form.DataAuth,
  59. }
  60. _, err = db.Update(Role.TableName(), map[string]interface{}{"id": id}, roleMap)
  61. admin.ClearAuthCacheByRole(id)
  62. return err
  63. }
  64. func CheckRoleDuplicate(name string) bool {
  65. roleInfo, err := GetOne(map[string]interface{}{"name": name}, nil, nil)
  66. return roleInfo != nil && err == nil
  67. }
  68. func DeleteByID(id int) error {
  69. authInfo, _ := GetInfoByID(id, nil, nil)
  70. if authInfo == nil {
  71. return errors.New("invalid auth id")
  72. }
  73. _, err := db.Delete(Role.TableName(), map[string]interface{}{"id": id})
  74. return err
  75. }
  76. func GetNameByID(roleIds []string) []string {
  77. roleList, _ := GetList(map[string]interface{}{"id in": roleIds}, []string{"id, name"}, app.Page{}, nil)
  78. roleNames := make([]string, 0)
  79. for _, v := range roleList {
  80. roleNames = append(roleNames, v.Name)
  81. }
  82. return roleNames
  83. }