order_approve.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package application
  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-system/errors"
  14. "property-system/impl/v1/permission"
  15. dbmodel "property-system/model"
  16. pb_v1 "property-system/pb/v1"
  17. "strings"
  18. )
  19. func checkApplicationOrderApproveParam(req *pb_v1.ApplicationOrderApproveRequest) error {
  20. switch {
  21. case req.OrderId < 1:
  22. return status.Error(10003, "id不能为空")
  23. }
  24. return nil
  25. }
  26. func UpdateGardenPermissions(gardenId int64, permissions []*pb_v1.SystemPermissionNotTreeItem, db *gorm.DB) error {
  27. p := dbmodel.TGardenApproved{}
  28. where := map[string]interface{}{
  29. "id": gardenId,
  30. }
  31. err := p.Find(db, where)
  32. if err != nil && err != gorm.ErrRecordNotFound {
  33. return errors.DataBaseError
  34. }
  35. if p.ID == 0 {
  36. return errors.ErrRecordNotFound
  37. }
  38. origin := strings.Split(p.PermissionCodes, ";")
  39. originM := map[string]bool{}
  40. for _, v := range origin {
  41. originM[v] = true
  42. }
  43. for _, v := range permissions {
  44. if originM[v.Code] {
  45. continue
  46. }
  47. origin = append(origin, v.Code)
  48. }
  49. values := map[string]interface{}{
  50. "permission_codes": strings.Join(origin, ";"),
  51. }
  52. err = p.Update(db, where, values)
  53. permission.GardenPermissionTreeClearRedis(gardenId)
  54. if err != nil {
  55. return errors.DataBaseError
  56. }
  57. return nil
  58. }
  59. func ApproveHandle(req *pb_v1.ApplicationOrderApproveRequest, db *gorm.DB) error {
  60. p := dbmodel.TApplicationOrder{}
  61. where := map[string]interface{}{
  62. "id": req.OrderId,
  63. }
  64. err := p.Find(database.DB(), where)
  65. if err != nil && err != gorm.ErrRecordNotFound {
  66. return errors.DataBaseError
  67. }
  68. if p.ID == 0 {
  69. return errors.ErrRecordNotFound
  70. }
  71. if p.Status != 1 {
  72. return status.Error(10003, "状态错误")
  73. }
  74. status := 2
  75. if !req.Status {
  76. status = 3
  77. }
  78. values := map[string]interface{}{
  79. "status": status,
  80. "feedback": req.Feedback,
  81. }
  82. p.ID = 0
  83. err = p.Update(db, where, values)
  84. if err != nil {
  85. return errors.DataBaseError
  86. }
  87. application := pb_v1.ApplicationInfo{}
  88. json.Unmarshal([]byte(p.ApplicationContent), &application)
  89. return UpdateGardenPermissions(p.GardenId, application.Permissions, db)
  90. }
  91. //
  92. func ApplicationOrderApprove(ctx context.Context, req *pb_v1.ApplicationOrderApproveRequest) (reply *pb_v1.ApplicationOrderApproveReply, err error) {
  93. reply = &pb_v1.ApplicationOrderApproveReply{}
  94. // 捕获各个task中的异常并返回给调用者
  95. defer func() {
  96. if r := recover(); r != nil {
  97. err = fmt.Errorf("%+v", r)
  98. e := &status.Status{}
  99. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  100. logger.Error("err",
  101. zap.String("system_err", err.Error()),
  102. zap.Stack("stacktrace"))
  103. }
  104. }
  105. }()
  106. err = checkApplicationOrderApproveParam(req)
  107. if err != nil {
  108. return nil, err
  109. }
  110. db := database.DB().Begin()
  111. if err = ApproveHandle(req, db); err != nil {
  112. db.Rollback()
  113. return nil, err
  114. }
  115. db.Commit()
  116. return reply, nil
  117. }