del.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. "property-common/errors"
  13. dbmodel "property-common/model"
  14. pb_v1 "property-common/pb/v1"
  15. )
  16. //
  17. func SystemPermissionDel(ctx context.Context, req *pb_v1.SystemPermissionDelRequest) (reply *pb_v1.SystemPermissionDelReply, err error) {
  18. reply = &pb_v1.SystemPermissionDelReply{}
  19. // 捕获各个task中的异常并返回给调用者
  20. defer func() {
  21. if r := recover(); r != nil {
  22. err = fmt.Errorf("%+v", r)
  23. e := &status.Status{}
  24. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  25. logger.Error("err",
  26. zap.String("system_err", err.Error()),
  27. zap.Stack("stacktrace"))
  28. }
  29. }
  30. }()
  31. // 参数检查
  32. if req.Code == "" {
  33. return nil, errors.ParamsError
  34. }
  35. p := &dbmodel.TPropertySystemPermission{}
  36. where := map[string]interface{}{
  37. "code like": req.Code + "%",
  38. }
  39. old := &dbmodel.TPropertySystemPermission{}
  40. list, err := old.List(database.DB(), where, nil, -1, -1)
  41. if err != nil {
  42. return nil, errors.DataBaseError
  43. }
  44. db := database.DB().Begin()
  45. err = p.Delete(db, where)
  46. if err != nil {
  47. db.Rollback()
  48. return nil, errors.DataBaseError
  49. }
  50. for _, v := range list {
  51. item := &pb_v1.SystemPermissionUpdateRequest{
  52. Code: v.Code,
  53. Router: v.Router,
  54. Path: v.Path,
  55. AppletePath: v.AppletePath,
  56. Name: v.Name,
  57. }
  58. reply.Origin = append(reply.Origin, item)
  59. }
  60. if err = delSystemPermissionFromRedis(); err != nil {
  61. db.Rollback()
  62. return nil, err
  63. }
  64. ap := dbmodel.TApplicationPermission{}
  65. where = map[string]interface{}{
  66. "permission_code": req.Code,
  67. }
  68. err = ap.Delete(db, where)
  69. if err != nil {
  70. db.Rollback()
  71. return nil, errors.DataBaseError
  72. }
  73. db.Commit()
  74. return reply, nil
  75. }