change_field.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. "property-household/errors"
  13. dbmodel "property-household/model"
  14. pb_v1 "property-household/pb/v1"
  15. )
  16. func checkHouseRentChangeFieldParam(req *pb_v1.HouseRentChangeFieldRequest) error {
  17. switch {
  18. case req.GardenId < 1:
  19. return status.Error(10003, "小区不能为空")
  20. }
  21. return nil
  22. }
  23. //
  24. func HouseRentChangeField(ctx context.Context, req *pb_v1.HouseRentChangeFieldRequest) (reply *pb_v1.HouseRentChangeFieldReply, err error) {
  25. reply = &pb_v1.HouseRentChangeFieldReply{}
  26. // 捕获各个task中的异常并返回给调用者
  27. defer func() {
  28. if r := recover(); r != nil {
  29. err = fmt.Errorf("%+v", r)
  30. e := &status.Status{}
  31. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  32. logger.Error("err",
  33. zap.String("system_err", err.Error()),
  34. zap.Stack("stacktrace"))
  35. }
  36. }
  37. }()
  38. err = checkHouseRentChangeFieldParam(req)
  39. if err != nil {
  40. return nil, err
  41. }
  42. p := dbmodel.THouseRent{}
  43. where := map[string]interface{}{
  44. "garden_id":req.GardenId,
  45. }
  46. values := map[string]interface{}{}
  47. switch {
  48. case req.HouseId == 0 && req.UnitId == 0:
  49. if req.Lnt > 0 {
  50. values["lnt"] = req.Lnt
  51. }
  52. if req.Lat > 0 {
  53. values["lat"] = req.Lat
  54. }
  55. if req.GardenName != "" {
  56. values["garden_name"] = req.GardenName
  57. }
  58. case req.UnitId > 0:
  59. where["unit_id"] = req.UnitId
  60. hasLift := int32(1)
  61. if !req.HasLift {
  62. hasLift = 2
  63. }
  64. values["has_lift"] = hasLift
  65. case req.HouseId > 0:
  66. where["house_id"] = req.HouseId
  67. if req.Layer > 0 {
  68. values["layer"] = req.Layer
  69. }
  70. if req.HouseArea > 0 {
  71. values["house_area"] = req.HouseArea
  72. }
  73. if req.RoomCount > 0 {
  74. values["room_count"] = req.RoomCount
  75. }
  76. if req.HallCount > 0 {
  77. values["hall_count"] = req.HallCount
  78. }
  79. }
  80. if len(values) == 0 {
  81. return reply, nil
  82. }
  83. db := database.DB()
  84. err = p.Update(db, where, values)
  85. if err != nil {
  86. return nil, errors.DataBaseError
  87. }
  88. return reply, nil
  89. }