list.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package event
  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/parser"
  15. pb_v1 "property-garden/pb/v1"
  16. "property-garden/utils"
  17. "strings"
  18. "time"
  19. )
  20. func householdIsSign(uid int64, eventIds []int64, dbname string) (map[int64]bool, error) {
  21. ret := map[int64]bool{}
  22. if len(eventIds) == 0 {
  23. return ret, nil
  24. }
  25. p := dbmodel.NewEventSign(dbname)
  26. where := map[string]interface{}{
  27. "household_uid": uid,
  28. "event_id in": eventIds,
  29. }
  30. list, err := p.List(database.DB(), where, nil, -1, -1)
  31. if err != nil {
  32. return ret, errors.DataBaseError
  33. }
  34. for _, v := range list {
  35. ret[v.EventId] = true
  36. }
  37. return ret, nil
  38. }
  39. //
  40. func EventList(ctx context.Context, req *pb_v1.EventListRequest) (reply *pb_v1.EventListReply, err error) {
  41. reply = &pb_v1.EventListReply{}
  42. // 捕获各个task中的异常并返回给调用者
  43. defer func() {
  44. if r := recover(); r != nil {
  45. err = fmt.Errorf("%+v", r)
  46. e := &status.Status{}
  47. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  48. logger.Error("err",
  49. zap.String("system_err", err.Error()),
  50. zap.Stack("stacktrace"))
  51. }
  52. }
  53. }()
  54. if req.GardenId < 1 {
  55. return nil, errors.ParamsError
  56. }
  57. if req.Page == 0 {
  58. req.Page = 1
  59. }
  60. if req.PageSize == 0 {
  61. req.PageSize = 10
  62. }
  63. dbname := utils.GetGardenDbName(req.GardenId)
  64. p := dbmodel.NewEvent(dbname)
  65. where := map[string]interface{}{
  66. //"garden_id":req.GardenId,
  67. }
  68. reply.Total, err = p.Count(database.DB(), where, nil)
  69. if err != nil {
  70. return nil, errors.DataBaseError
  71. }
  72. reply.Page = req.Page
  73. if reply.Total == 0 {
  74. return reply, nil
  75. }
  76. list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  77. if err != nil {
  78. return nil, errors.DataBaseError
  79. }
  80. isSignM := map[int64]bool{}
  81. // 小程序查询时,判断住户是否报名该活动
  82. if req.HouseholdUid > 0 {
  83. eventIds := []int64{}
  84. for _, v := range list {
  85. eventIds = append(eventIds, v.ID)
  86. }
  87. isSignM, err = householdIsSign(req.HouseholdUid, eventIds, dbname)
  88. if err != nil {
  89. return nil, err
  90. }
  91. }
  92. reply.List = make([]*pb_v1.EventItem, len(list))
  93. for i, v := range list {
  94. reply.List[i] = &pb_v1.EventItem{
  95. Id: v.ID,
  96. Title: v.Title,
  97. Content: v.Content,
  98. //Pic: strings.Split(v.Pic, ";"),
  99. Addr: v.Addr,
  100. Start: v.Start,
  101. End: v.End,
  102. SignUpEnd: v.SignUpEnd,
  103. PeopleLimit: v.PeopleLimit,
  104. PeopleCount: v.PeopleCount,
  105. Status: 1,
  106. CreatedAt: v.CreatedAt.Format("2006-01-02 15:04:05"),
  107. Sign: isSignM[v.ID],
  108. Fee: v.Fee,
  109. }
  110. if v.Pic != "" {
  111. reply.List[i].Pic = strings.Split(v.Pic, ";")
  112. }
  113. if len(reply.List[i].Pic) == 0 {
  114. reply.List[i].Pic = []string{parser.Conf.Oss.Protocol + "://" + parser.Conf.Oss.Endpoint + "/" + parser.Conf.Oss.FixBucket + "/" + parser.Conf.Oss.RentObj}
  115. }
  116. if v.SignUpEnd <= time.Now().Unix() || v.PeopleLimit <= v.PeopleCount {
  117. reply.List[i].Status = 2
  118. }
  119. }
  120. return reply, nil
  121. }