appointment_list.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "time"
  16. )
  17. //
  18. func HouseRentAppointmentList(ctx context.Context, req *pb_v1.HouseRentAppointmentListRequest) (reply *pb_v1.HouseRentAppointmentListReply, err error) {
  19. reply = &pb_v1.HouseRentAppointmentListReply{}
  20. // 捕获各个task中的异常并返回给调用者
  21. defer func() {
  22. if r := recover(); r != nil {
  23. err = fmt.Errorf("%+v", r)
  24. e := &status.Status{}
  25. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  26. logger.Error("err",
  27. zap.String("system_err", err.Error()),
  28. zap.Stack("stacktrace"))
  29. }
  30. }
  31. }()
  32. if req.GardenId < 1 && req.Uid < 1 {
  33. return nil, errors.ParamsError
  34. }
  35. if req.Page == 0 {
  36. req.Page = 1
  37. }
  38. if req.PageSize == 0 {
  39. req.PageSize = 10
  40. }
  41. p := dbmodel.THouseRentAppointment{}
  42. where := map[string]interface{}{}
  43. if req.GardenId > 0 {
  44. where["garden_id"] = req.GardenId
  45. }
  46. if req.RentId > 0 {
  47. where["rent_id"] = req.RentId
  48. }
  49. if req.Uid > 0 {
  50. where["uid"] = req.Uid
  51. }
  52. if req.HouseName != "" {
  53. where["house_name"] = req.HouseName
  54. }
  55. if req.Name != "" {
  56. where["name like"] = "%" + req.Name + "%"
  57. }
  58. if req.Phone != "" {
  59. where["phone"] = req.Phone
  60. }
  61. reply.Total, err = p.Count(database.DB(), where, nil)
  62. if err != nil {
  63. return nil, errors.DataBaseError
  64. }
  65. reply.Page = req.Page
  66. if reply.Total == 0 {
  67. return reply, nil
  68. }
  69. list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  70. if err != nil {
  71. return nil, errors.DataBaseError
  72. }
  73. reply.List = make([]*pb_v1.HouseRentAppointmentItem, len(list))
  74. gardenInfosM := map[int64]pb_v1.GardenItem{}
  75. // 小程序需获取小区名
  76. if req.Uid > 0 {
  77. gardenIdsM := map[int64]bool{}
  78. gardenIds := []int64{}
  79. for _, v := range list {
  80. if gardenIdsM[v.GardenId] {
  81. continue
  82. }
  83. gardenIdsM[v.GardenId] = true
  84. gardenIds = append(gardenIds, v.GardenId)
  85. }
  86. gardenInfosM, err = getGardenInfos(gardenIds)
  87. if err != nil {
  88. return nil, err
  89. }
  90. }
  91. now := time.Now()
  92. nowday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  93. for i, v := range list {
  94. reply.List[i] = &pb_v1.HouseRentAppointmentItem{
  95. Id: v.ID,
  96. Name: v.Name,
  97. Phone: v.Phone,
  98. RentId: v.RentId,
  99. Uid: v.Uid,
  100. AppointmentTime: v.AppointmentTime,
  101. HouseName: v.HouseName,
  102. Area: v.Area,
  103. Layer: v.Layer,
  104. Status: v.Status,
  105. GardenId: v.GardenId,
  106. GardenName: gardenInfosM[v.GardenId].GardenName,
  107. }
  108. if v.Status == HouseRentAppointmentWait && v.AppointmentTime < nowday.Unix() && v.AppointmentTime > 0 {
  109. reply.List[i].Status = HouseRentAppointmentExpired
  110. }
  111. }
  112. return reply, nil
  113. }