list.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package visitor
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "git.getensh.com/common/gopkgs/database"
  7. "git.getensh.com/common/gopkgs/logger"
  8. "go.uber.org/zap"
  9. "google.golang.org/grpc/status"
  10. "property-device/errors"
  11. dbmodel "property-device/model"
  12. "property-device/pb"
  13. pb_v1 "property-device/pb/v1"
  14. "time"
  15. )
  16. func checkGateVisitorListParam(req *pb_v1.GateVisitorListRequest) error {
  17. if req.Id == 0 {
  18. if req.DeviceId == 0 && req.Uid == 0 {
  19. return status.Error(10003, "设备和用户不能同时为空")
  20. }
  21. }
  22. if req.Page == 0 {
  23. req.Page = 1
  24. }
  25. if req.PageSize == 0 {
  26. req.PageSize = 10
  27. }
  28. return nil
  29. }
  30. func getGateInfo(ids []int64) (map[int64]dbmodel.TGate, error) {
  31. ret := map[int64]dbmodel.TGate{}
  32. if len(ids) == 0 {
  33. return ret, nil
  34. }
  35. p := dbmodel.TGate{}
  36. where := [][2]interface{}{}
  37. where = dbmodel.WhereAdd(where, "id in", ids)
  38. list, err := p.List(database.DB(), where, nil, -1, -1)
  39. if err != nil {
  40. return nil, errors.DataBaseError
  41. }
  42. for _, v := range list {
  43. ret[v.ID] = v
  44. }
  45. return ret, nil
  46. }
  47. func getGardenName(gardenIds []int64) (map[int64]string, error) {
  48. gardenM := map[int64]string{}
  49. if len(gardenIds) > 0 {
  50. mreq := pb_v1.GardenInfosRequest{Ids: gardenIds}
  51. mreply, err := pb.System.GardenInfos(context.Background(), &mreq)
  52. if err != nil {
  53. return nil, err
  54. }
  55. for _, v := range mreply.List {
  56. gardenM[v.Id] = v.GardenName
  57. }
  58. }
  59. return gardenM, nil
  60. }
  61. func GateVisitorList(ctx context.Context, req *pb_v1.GateVisitorListRequest) (reply *pb_v1.GateVisitorListReply, err error) {
  62. reply = &pb_v1.GateVisitorListReply{}
  63. // 捕获各个task中的异常并返回给调用者
  64. defer func() {
  65. if r := recover(); r != nil {
  66. err = fmt.Errorf("%+v", r)
  67. e := &status.Status{}
  68. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  69. logger.Error("err",
  70. zap.String("system_err", err.Error()),
  71. zap.Stack("stacktrace"))
  72. }
  73. }
  74. }()
  75. err = checkGateVisitorListParam(req)
  76. if err != nil {
  77. return nil, err
  78. }
  79. g := dbmodel.TGateVisitor{}
  80. where := [][2]interface{}{}
  81. if req.Id > 0 {
  82. where = dbmodel.WhereAdd(where, "id", req.Id)
  83. }
  84. if req.DeviceId > 0 {
  85. where = dbmodel.WhereAdd(where, "device_id", req.DeviceId)
  86. }
  87. if req.End > 0 {
  88. where = dbmodel.WhereAdd(where, "end <", req.End)
  89. }
  90. if req.Start > 0 {
  91. where = dbmodel.WhereAdd(where, "start >=", req.Start)
  92. }
  93. if req.Visitor != "" {
  94. where = dbmodel.WhereAdd(where, "visitor", req.Visitor)
  95. }
  96. if req.Name != "" {
  97. where = dbmodel.WhereAdd(where, "name", req.Name)
  98. }
  99. if req.Uid > 0 {
  100. where = dbmodel.WhereAdd(where, "uid", req.Uid)
  101. }
  102. reply.Total, err = g.Count(database.DB(), where, nil)
  103. if err != nil {
  104. return nil, errors.DataBaseError
  105. }
  106. reply.Page = req.Page
  107. if reply.Total == 0 {
  108. return reply, nil
  109. }
  110. list, err := g.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  111. if err != nil {
  112. return nil, errors.DataBaseError
  113. }
  114. ids := []int64{}
  115. gardenIds := []int64{}
  116. if req.DeviceId != 0 {
  117. ids = []int64{req.DeviceId}
  118. } else {
  119. m := map[int64]bool{}
  120. for _, v := range list {
  121. if m[v.DeviceId] {
  122. continue
  123. }
  124. ids = append(ids, v.DeviceId)
  125. m[v.DeviceId] = true
  126. }
  127. }
  128. m := map[int64]bool{}
  129. for _, v := range list {
  130. if m[v.GardenId] {
  131. continue
  132. }
  133. gardenIds = append(gardenIds, v.GardenId)
  134. m[v.GardenId] = true
  135. }
  136. gateInfoM, err := getGateInfo(ids)
  137. if err != nil {
  138. return nil, err
  139. }
  140. gardenInfoM, err := getGardenName(gardenIds)
  141. if err != nil {
  142. return nil, err
  143. }
  144. reply.List = make([]*pb_v1.GateVisitorListItem, len(list))
  145. for i, v := range list {
  146. item := &pb_v1.GateVisitorListItem{
  147. Uid: v.Uid,
  148. DeviceId: v.DeviceId,
  149. GateName: gateInfoM[v.DeviceId].DeviceName,
  150. Name: v.Name,
  151. Visitor: v.Visitor,
  152. VisitorPhone: v.VisitorPhone,
  153. Phone: v.Phone,
  154. OpenTime: v.OpenTime,
  155. Start: v.Start,
  156. End: v.End,
  157. Status: 1,
  158. Id: v.ID,
  159. Comment: v.Comment,
  160. Qcode: v.Qcode,
  161. Location: gateInfoM[v.DeviceId].Location,
  162. GardenName: gardenInfoM[v.GardenId],
  163. }
  164. if v.OpenTime > 0 {
  165. item.Status = 2
  166. } else if v.End < time.Now().Unix() {
  167. item.Status = 3
  168. }
  169. reply.List[i] = item
  170. }
  171. return reply, nil
  172. }