down.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. "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-household/errors"
  14. dbmodel "property-household/model"
  15. pb_v1 "property-household/pb/v1"
  16. )
  17. func checkHouseRentDownParam(req *pb_v1.HouseRentDownRequest) error {
  18. switch {
  19. case req.Id < 1:
  20. return status.Error(10003, "id不能为空")
  21. case req.GardenId < 1:
  22. return status.Error(10003, "小区不能为空")
  23. }
  24. return nil
  25. }
  26. //
  27. func HouseRentDown(ctx context.Context, req *pb_v1.HouseRentDownRequest) (reply *pb_v1.HouseRentDownReply, err error) {
  28. reply = &pb_v1.HouseRentDownReply{}
  29. // 捕获各个task中的异常并返回给调用者
  30. defer func() {
  31. if r := recover(); r != nil {
  32. err = fmt.Errorf("%+v", r)
  33. e := &status.Status{}
  34. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  35. logger.Error("err",
  36. zap.String("system_err", err.Error()),
  37. zap.Stack("stacktrace"))
  38. }
  39. }
  40. }()
  41. err = checkHouseRentDownParam(req)
  42. if err != nil {
  43. return nil, err
  44. }
  45. p := dbmodel.THouseRent{}
  46. where := map[string]interface{}{
  47. "id": req.Id,
  48. }
  49. err = p.Find(database.DB(), where)
  50. if err != nil && err != gorm.ErrRecordNotFound {
  51. return nil, errors.DataBaseError
  52. }
  53. if p.ID == 0 {
  54. return nil, errors.ErrRecordNotFound
  55. }
  56. if p.ApproveStatus != 2 {
  57. return nil, status.Error(10003, "当前状态不能下架")
  58. }
  59. values := map[string]interface{}{
  60. "approve_status": 4,
  61. }
  62. db := database.DB().Begin()
  63. err = p.Update(db, where, values)
  64. if err != nil {
  65. db.Rollback()
  66. return nil, errors.DataBaseError
  67. }
  68. p.ApproveStatus = 4
  69. err = houseRentSync(p, false, req.GardenId, -1)
  70. if err != nil {
  71. db.Rollback()
  72. return nil, err
  73. }
  74. db.Commit()
  75. return reply, nil
  76. }