rent_sell_count.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package garden
  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. dbmodel "property-system/model"
  15. pb_v1 "property-system/pb/v1"
  16. )
  17. func checkGardenRentSellCountParam(req *pb_v1.GardenRentSellCountRequest) error {
  18. switch {
  19. case req.GardenId < 1:
  20. return status.Error(10003, "请选择要更改的小区")
  21. }
  22. return nil
  23. }
  24. //
  25. func GardenRentSellCount(ctx context.Context, req *pb_v1.GardenRentSellCountRequest) (reply *pb_v1.GardenRentSellCountReply, err error) {
  26. reply = &pb_v1.GardenRentSellCountReply{}
  27. // 捕获各个task中的异常并返回给调用者
  28. defer func() {
  29. if r := recover(); r != nil {
  30. err = fmt.Errorf("%+v", r)
  31. e := &status.Status{}
  32. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  33. logger.Error("err",
  34. zap.String("system_err", err.Error()),
  35. zap.Stack("stacktrace"))
  36. }
  37. }
  38. }()
  39. err = checkGardenRentSellCountParam(req)
  40. if err != nil {
  41. return nil, err
  42. }
  43. if req.SellIncrease == 0 && req.RentIncrease == 0 {
  44. return reply, nil
  45. }
  46. p := &dbmodel.TGardenApproved{}
  47. where := map[string]interface{}{
  48. "id": req.GardenId,
  49. }
  50. err = p.Find(database.DB(), where)
  51. if err != nil && err != gorm.ErrRecordNotFound {
  52. return nil, errors.DataBaseError
  53. }
  54. if p.ID == 0 {
  55. return nil, errors.GardenNotExist
  56. }
  57. garden := dbmodel.TGardenApproved{}
  58. values := map[string]interface{}{}
  59. if req.RentIncrease > 0 {
  60. values["house_rent_count"] = gorm.Expr("house_rent_count + ?", req.RentIncrease)
  61. } else if req.RentIncrease < 0 {
  62. values["house_rent_count"] = gorm.Expr("house_rent_count - ?", -req.RentIncrease)
  63. }
  64. if req.SellIncrease > 0 {
  65. values["sell_rent_count"] = gorm.Expr("sell_rent_count + ?", req.SellIncrease)
  66. } else if req.SellIncrease < 0 {
  67. values["sell_rent_count"] = gorm.Expr("sell_rent_count - ?", req.SellIncrease)
  68. }
  69. where = map[string]interface{}{
  70. "id": req.GardenId,
  71. }
  72. err = garden.Update(database.DB(), where, values)
  73. if err != nil {
  74. return nil, errors.DataBaseError
  75. }
  76. return reply, nil
  77. }