change_field.go 2.7 KB

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