approve.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package company
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "git.getensh.com/common/gopkgs/database"
  9. "gorm.io/gorm"
  10. "property-company/consts"
  11. "property-company/errors"
  12. dbmodel "property-company/model"
  13. pb_v1 "property-company/pb/v1"
  14. "time"
  15. "git.getensh.com/common/gopkgs/logger"
  16. "go.uber.org/zap"
  17. "google.golang.org/grpc/status"
  18. )
  19. //
  20. func CompanyApprove(ctx context.Context, req *pb_v1.CompanyApproveRequest) (reply *pb_v1.CompanyApproveReply, err error) {
  21. reply = &pb_v1.CompanyApproveReply{}
  22. // 捕获各个task中的异常并返回给调用者
  23. defer func() {
  24. if r := recover(); r != nil {
  25. err = fmt.Errorf("%+v", r)
  26. e := &status.Status{}
  27. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  28. logger.Error("err",
  29. zap.String("system_err", err.Error()),
  30. zap.Stack("stacktrace"))
  31. }
  32. }
  33. }()
  34. if req.Id < 0 {
  35. return nil, errors.ParamsError
  36. }
  37. now := time.Now()
  38. p := &dbmodel.TCompany{}
  39. where := map[string]interface{}{
  40. "id":req.Id,
  41. }
  42. values := map[string]interface{}{
  43. "updated_at":now,
  44. "approved_at":now,
  45. "approve_status":consts.ApproveStatusSuccess,
  46. "feedback":req.Feedback,
  47. }
  48. err = p.Find(database.DB(), where)
  49. if err != nil {
  50. if err != gorm.ErrRecordNotFound {
  51. return nil, errors.DataBaseError
  52. }
  53. }
  54. if p.ID == 0 {
  55. return nil, errors.ErrRecordNotFound
  56. }
  57. if p.ApproveStatus == consts.ApproveStatusSuccess || p.ApproveStatus == consts.ApproveStatusFail {
  58. return nil, errors.ApproveStatusError
  59. }
  60. if !req.Status {
  61. values["approve_status"] = consts.ApproveStatusFail
  62. // 审批未通过,重复标志设未null,可以再次添加同账号或同社会信用码
  63. values["uniq_flag"] = nil
  64. }
  65. db := database.DB().Begin()
  66. err = p.Update(db, where, values)
  67. if err != nil {
  68. db.Rollback()
  69. return nil, errors.DataBaseError
  70. }
  71. if !req.Status {
  72. user := dbmodel.TUser{}
  73. where := map[string]interface{}{
  74. "user":p.User,
  75. }
  76. err = user.Delete(db, where)
  77. if err != nil {
  78. db.Rollback()
  79. return nil, errors.DataBaseError
  80. }
  81. }
  82. db.Commit()
  83. return reply, nil
  84. }