syncPermsLogic.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package pub
  2. import (
  3. "context"
  4. "time"
  5. "perms-system-server/internal/consts"
  6. permModel "perms-system-server/internal/model/perm"
  7. "perms-system-server/internal/response"
  8. "perms-system-server/internal/svc"
  9. "perms-system-server/internal/types"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type SyncPermsLogic struct {
  13. logx.Logger
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. }
  17. func NewSyncPermsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SyncPermsLogic {
  18. return &SyncPermsLogic{
  19. Logger: logx.WithContext(ctx),
  20. ctx: ctx,
  21. svcCtx: svcCtx,
  22. }
  23. }
  24. func (l *SyncPermsLogic) SyncPerms(req *types.SyncPermsReq) (resp *types.SyncPermsResp, err error) {
  25. product, err := l.svcCtx.SysProductModel.FindOneByAppKey(l.ctx, req.AppKey)
  26. if err != nil {
  27. return nil, response.ErrUnauthorized("无效的appKey")
  28. }
  29. if product.AppSecret != req.AppSecret {
  30. return nil, response.ErrUnauthorized("appSecret验证失败")
  31. }
  32. if product.Status != consts.StatusEnabled {
  33. return nil, response.ErrForbidden("产品已被禁用")
  34. }
  35. existingMap, err := l.svcCtx.SysPermModel.FindMapByProductCode(l.ctx, product.Code)
  36. if err != nil {
  37. return nil, err
  38. }
  39. now := time.Now().Unix()
  40. var added, updated int64
  41. codes := make([]string, 0, len(req.Perms))
  42. var toInsert []*permModel.SysPerm
  43. var toUpdate []*permModel.SysPerm
  44. for _, item := range req.Perms {
  45. codes = append(codes, item.Code)
  46. existing, ok := existingMap[item.Code]
  47. if !ok {
  48. toInsert = append(toInsert, &permModel.SysPerm{
  49. ProductCode: product.Code,
  50. Name: item.Name,
  51. Code: item.Code,
  52. Remark: item.Remark,
  53. Status: consts.StatusEnabled,
  54. CreateTime: now,
  55. UpdateTime: now,
  56. })
  57. added++
  58. continue
  59. }
  60. if existing.Name != item.Name || existing.Remark != item.Remark || existing.Status != consts.StatusEnabled {
  61. existing.Name = item.Name
  62. existing.Remark = item.Remark
  63. existing.Status = consts.StatusEnabled
  64. existing.UpdateTime = now
  65. toUpdate = append(toUpdate, existing)
  66. updated++
  67. }
  68. }
  69. if len(toInsert) > 0 {
  70. if err := l.svcCtx.SysPermModel.BatchInsert(l.ctx, toInsert); err != nil {
  71. return nil, err
  72. }
  73. }
  74. if len(toUpdate) > 0 {
  75. if err := l.svcCtx.SysPermModel.BatchUpdate(l.ctx, toUpdate); err != nil {
  76. return nil, err
  77. }
  78. }
  79. disabled, err := l.svcCtx.SysPermModel.DisableNotInCodes(l.ctx, product.Code, codes, now)
  80. if err != nil {
  81. return nil, err
  82. }
  83. if added > 0 || updated > 0 || disabled > 0 {
  84. l.svcCtx.UserDetailsLoader.CleanByProduct(l.ctx, product.Code)
  85. }
  86. return &types.SyncPermsResp{
  87. Added: added,
  88. Updated: updated,
  89. Disabled: disabled,
  90. }, nil
  91. }