enable.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "time"
  17. )
  18. func checkSystemPermissionEnableParam(req *pb_v1.SystemPermissionEnableRequest) error {
  19. switch {
  20. case req.Code == "":
  21. return status.Error(10003, "节点code不能为空")
  22. case req.Enable == 0:
  23. return status.Error(10003, "是否发布")
  24. }
  25. return nil
  26. }
  27. //
  28. func SystemPermissionEnable(ctx context.Context, req *pb_v1.SystemPermissionEnableRequest) (reply *pb_v1.SystemPermissionEnableReply, err error) {
  29. reply = &pb_v1.SystemPermissionEnableReply{}
  30. // 捕获各个task中的异常并返回给调用者
  31. defer func() {
  32. if r := recover(); r != nil {
  33. err = fmt.Errorf("%+v", r)
  34. e := &status.Status{}
  35. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  36. logger.Error("err",
  37. zap.String("system_err", err.Error()),
  38. zap.Stack("stacktrace"))
  39. }
  40. }
  41. }()
  42. // 参数检查
  43. err = checkSystemPermissionEnableParam(req)
  44. if err != nil {
  45. return nil, err
  46. }
  47. now := time.Now()
  48. p := &dbmodel.TPropertySystemPermission{}
  49. where := map[string]interface{}{
  50. "code": req.Code,
  51. }
  52. values := map[string]interface{}{
  53. "enable": req.Enable,
  54. "updated_at": now,
  55. "enable_at": 0,
  56. }
  57. old := &dbmodel.TPropertySystemPermission{}
  58. err = old.Find(database.DB(), where)
  59. if err != nil && err != gorm.ErrRecordNotFound {
  60. return nil, errors.DataBaseError
  61. }
  62. if old.ID == 0 {
  63. return nil, errors.ErrRecordNotFound
  64. }
  65. if req.Enable == old.Enable {
  66. return reply, nil
  67. }
  68. if req.Enable == 1 {
  69. values["enable_at"] = now.Unix()
  70. }
  71. db := database.DB().Begin()
  72. err = p.Update(db, where, values)
  73. if err != nil {
  74. db.Rollback()
  75. return nil, errors.DataBaseError
  76. }
  77. if err = delSystemPermissionFromRedis(); err != nil {
  78. db.Rollback()
  79. return nil, err
  80. }
  81. db.Commit()
  82. return reply, nil
  83. }