info.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. pb_v1 "property-garden/pb/v1"
  15. "property-garden/utils"
  16. )
  17. func checkGardenHouseRentInfoParam(req *pb_v1.GardenHouseRentInfoRequest) error {
  18. if req.GardenId < 1 {
  19. return status.Error(10003, "小区不能为空")
  20. }
  21. return nil
  22. }
  23. type HouseRentInfo struct {
  24. Count int64
  25. RentPrice int64
  26. RoomArea float64
  27. }
  28. //
  29. func GardenHouseRentInfo(ctx context.Context, req *pb_v1.GardenHouseRentInfoRequest) (reply *pb_v1.GardenHouseRentInfoReply, err error) {
  30. reply = &pb_v1.GardenHouseRentInfoReply{}
  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 = checkGardenHouseRentInfoParam(req)
  44. if err != nil {
  45. return nil, err
  46. }
  47. dbname := utils.GetGardenDbName(req.GardenId)
  48. p := dbmodel.NewHouseRent(dbname)
  49. where := map[string]interface{}{
  50. "approve_status": 2,
  51. }
  52. str := "count(*) as count, sum(rent_price) as rent_price, sum(room_area) as room_area"
  53. result := []HouseRentInfo{}
  54. err = p.Select(database.DB(), where, nil, str, &result)
  55. if err != nil {
  56. return nil, errors.DataBaseError
  57. }
  58. if len(result) == 0 {
  59. return reply, nil
  60. }
  61. reply.RentCount = result[0].Count
  62. reply.AvgPrice = result[0].RentPrice / int64(result[0].RoomArea)
  63. return reply, nil
  64. }