approve.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package house_rent
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "gorm.io/gorm"
  9. "property-household/errors"
  10. "property-household/impl/v1/public_msg"
  11. dbmodel "property-household/model"
  12. pb_v1 "property-household/pb/v1"
  13. "time"
  14. "git.getensh.com/common/gopkgs/database"
  15. "git.getensh.com/common/gopkgs/logger"
  16. "go.uber.org/zap"
  17. "google.golang.org/grpc/status"
  18. )
  19. func checkHouseRentApproveParam(req *pb_v1.HouseRentApproveRequest) error {
  20. switch {
  21. case req.Id < 1:
  22. return status.Error(10003, "id不能为空")
  23. case req.GardenId < 1:
  24. return status.Error(10003, "小区不能为空")
  25. }
  26. return nil
  27. }
  28. //
  29. func HouseRentApprove(ctx context.Context, req *pb_v1.HouseRentApproveRequest) (reply *pb_v1.HouseRentApproveReply, err error) {
  30. reply = &pb_v1.HouseRentApproveReply{}
  31. // 捕获各个task中的异常并返回给调用者
  32. defer func() {
  33. if r := recover(); r != nil {
  34. err = fmt.Errorf("%+v", r)
  35. e := &status.Status{}
  36. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  37. logger.Error("err",
  38. zap.String("system_err", err.Error()),
  39. zap.Stack("stacktrace"))
  40. }
  41. }
  42. }()
  43. err = checkHouseRentApproveParam(req)
  44. if err != nil {
  45. return nil, err
  46. }
  47. p := dbmodel.THouseRent{}
  48. where := map[string]interface{}{
  49. "id": req.Id,
  50. }
  51. err = p.Find(database.DB(), where)
  52. if err != nil && err != gorm.ErrRecordNotFound {
  53. return nil, errors.DataBaseError
  54. }
  55. if p.ID == 0 {
  56. return nil, errors.ErrRecordNotFound
  57. }
  58. now := time.Now()
  59. approveStatus := HouseholdRentStatusFail
  60. if req.Status {
  61. approveStatus = HouseholdRentStatusSuccess
  62. }
  63. values := map[string]interface{}{
  64. "approve_status": approveStatus,
  65. "approved_at": now,
  66. "feedback": req.Feedback,
  67. }
  68. db := database.DB().Begin()
  69. err = p.Update(db, where, values)
  70. if err != nil {
  71. db.Rollback()
  72. return nil, errors.DataBaseError
  73. }
  74. p.ApproveStatus = int32(approveStatus)
  75. p.ApprovedAt = now
  76. p.Feedback = req.Feedback
  77. increase := 0
  78. if req.Status {
  79. increase = 1
  80. }
  81. err = houseRentSync(p, false, req.GardenId, increase)
  82. if err != nil {
  83. db.Rollback()
  84. return nil, err
  85. }
  86. db.Commit()
  87. serviceInfo := public_msg.ServiceInfo{
  88. Name: "委托发布房源",
  89. State: "已通过",
  90. }
  91. if !req.Status {
  92. serviceInfo.State = "未通过(" + req.Feedback + ")"
  93. }
  94. public_msg.SendMsgWx(p.HouseholdUid, p.GardenId, serviceInfo)
  95. return reply, nil
  96. }