business.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "go-common/app/admin/main/growup/dao/resource"
  8. "go-common/app/admin/main/growup/model"
  9. "go-common/library/log"
  10. )
  11. var (
  12. // 视频和专栏的权限点
  13. _videoPri = 13
  14. _columnPri = 103
  15. )
  16. // BusPrivilege business privilege
  17. func (s *Service) BusPrivilege(c context.Context, username string, ctypes string) (res []*model.BusRes, err error) {
  18. res = make([]*model.BusRes, 0)
  19. typs := strings.Split(ctypes, ",")
  20. if len(typs) == 0 {
  21. return
  22. }
  23. for _, typ := range typs {
  24. var ctype int
  25. ctype, err = strconv.Atoi(typ)
  26. if err != nil {
  27. log.Error("strconv.Atoi error(%v)", err)
  28. return
  29. }
  30. var r []*model.BusRes
  31. r, err = s.busPrivilege(c, username, ctype)
  32. if err != nil {
  33. log.Error("s.busPrivilege error(%v)", err)
  34. return
  35. }
  36. res = append(res, r...)
  37. }
  38. return
  39. }
  40. func (s *Service) busPrivilege(c context.Context, username string, ctype int) (res []*model.BusRes, err error) {
  41. category, err := s.getBusCategory(c, ctype)
  42. if err != nil {
  43. log.Error("s.getBusCategory error(%v)", err)
  44. return
  45. }
  46. userPri, err := s.GetUserPri(username)
  47. if err != nil {
  48. log.Error("s.GetUserPri error(%v)", err)
  49. return
  50. }
  51. // 获取数据源权限
  52. fatherID := 0
  53. switch ctype {
  54. case 1:
  55. fatherID = _videoPri
  56. case 2:
  57. fatherID = _columnPri
  58. }
  59. allPrivilege, err := s.dao.GetLevelPrivileges(fmt.Sprintf("level = 3 AND father_id = %d", fatherID))
  60. if err != nil {
  61. log.Error("s.dao.GetLevelPrivileges Error(%v)", err)
  62. return
  63. }
  64. res = make([]*model.BusRes, 0)
  65. for _, p := range allPrivilege {
  66. if !userPri[p.ID] {
  67. continue
  68. }
  69. if cid, ok := category[p.Title]; ok {
  70. res = append(res, &model.BusRes{
  71. PrivilegeID: p.ID,
  72. CategoryID: cid,
  73. Name: p.Title})
  74. }
  75. }
  76. return
  77. }
  78. func (s *Service) getBusCategory(c context.Context, ctype int) (categorys map[string]int64, err error) {
  79. categorys = make(map[string]int64)
  80. switch ctype {
  81. case 1:
  82. return resource.VideoCategoryNameToID(c)
  83. case 2:
  84. return resource.ColumnCategoryNameToID(c)
  85. }
  86. return
  87. }