update.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package system_permission
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "git.getensh.com/common/gopkgs/database"
  9. "git.getensh.com/common/gopkgs/logger"
  10. "go.uber.org/zap"
  11. "google.golang.org/grpc/status"
  12. "gorm.io/gorm"
  13. "property-common/errors"
  14. dbmodel "property-common/model"
  15. pb_v1 "property-common/pb/v1"
  16. "strings"
  17. "time"
  18. )
  19. func checkSystemPermissionUpdateParam(req *pb_v1.SystemPermissionUpdateRequest) error {
  20. switch {
  21. case req.Code == "":
  22. return status.Error(10003, "节点code不能为空")
  23. case req.Name == "":
  24. return status.Error(10003, "权限名称不能为空")
  25. case req.Router == "":
  26. return status.Error(10003, "路由不能为空")
  27. case req.Path == "":
  28. return status.Error(10003, "组件路径不能为空")
  29. }
  30. return nil
  31. }
  32. //
  33. func SystemPermissionUpdate(ctx context.Context, req *pb_v1.SystemPermissionUpdateRequest) (reply *pb_v1.SystemPermissionUpdateReply, err error) {
  34. reply = &pb_v1.SystemPermissionUpdateReply{}
  35. // 捕获各个task中的异常并返回给调用者
  36. defer func() {
  37. if r := recover(); r != nil {
  38. err = fmt.Errorf("%+v", r)
  39. e := &status.Status{}
  40. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  41. logger.Error("err",
  42. zap.String("system_err", err.Error()),
  43. zap.Stack("stacktrace"))
  44. }
  45. }
  46. }()
  47. // 参数检查
  48. err = checkSystemPermissionUpdateParam(req)
  49. if err != nil {
  50. return nil, err
  51. }
  52. now := time.Now()
  53. p := &dbmodel.TPropertySystemPermission{}
  54. where := map[string]interface{}{
  55. "code": req.Code,
  56. }
  57. values := map[string]interface{}{
  58. "name": req.Name,
  59. "router": req.Router,
  60. "path": req.Path,
  61. "applete_path": req.AppletePath,
  62. "updated_at": now,
  63. }
  64. old := &dbmodel.TPropertySystemPermission{}
  65. err = old.Find(database.DB(), where)
  66. if err != nil && err != gorm.ErrRecordNotFound {
  67. return nil, errors.DataBaseError
  68. }
  69. if old.ID == 0 {
  70. return nil, errors.ErrRecordNotFound
  71. }
  72. reply.Origin = &pb_v1.SystemPermissionUpdateRequest{
  73. Code: old.Code,
  74. Router: old.Router,
  75. Path: old.Path,
  76. AppletePath: old.AppletePath,
  77. Name: old.Name,
  78. }
  79. db := database.DB().Begin()
  80. err = p.Update(db, where, values)
  81. if err != nil {
  82. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  83. db.Rollback()
  84. return nil, status.Error(10003, "路由和方法已存在")
  85. }
  86. db.Rollback()
  87. return nil, errors.DataBaseError
  88. }
  89. if err = delSystemPermissionFromRedis(); err != nil {
  90. db.Rollback()
  91. return nil, err
  92. }
  93. db.Commit()
  94. return reply, nil
  95. }