list.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package gate
  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. "property-device/utils/gate_utils"
  15. )
  16. func checkGateListParam(req *pb_v1.GateListRequest) error {
  17. if req.Page == 0 {
  18. req.Page = 1
  19. }
  20. if req.PageSize == 0 {
  21. req.PageSize = 10
  22. }
  23. return nil
  24. }
  25. func getGardenName(list []dbmodel.TGate) (map[int64]string, error) {
  26. gardenM := map[int64]string{}
  27. gardenIds := []int64{}
  28. for _, v := range list {
  29. if v.GardenId == 0 {
  30. continue
  31. }
  32. if _, ok := gardenM[v.GardenId]; ok {
  33. continue
  34. }
  35. gardenM[v.GardenId] = ""
  36. gardenIds = append(gardenIds, v.GardenId)
  37. }
  38. if len(gardenIds) > 0 {
  39. mreq := pb_v1.GardenInfosRequest{Ids: gardenIds}
  40. mreply, err := pb.System.GardenInfos(context.Background(), &mreq)
  41. if err != nil {
  42. return nil, err
  43. }
  44. for _, v := range mreply.List {
  45. gardenM[v.Id] = v.GardenName
  46. }
  47. }
  48. return gardenM, nil
  49. }
  50. func GateList(ctx context.Context, req *pb_v1.GateListRequest) (reply *pb_v1.GateListReply, err error) {
  51. reply = &pb_v1.GateListReply{}
  52. // 捕获各个task中的异常并返回给调用者
  53. defer func() {
  54. if r := recover(); r != nil {
  55. err = fmt.Errorf("%+v", r)
  56. e := &status.Status{}
  57. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  58. logger.Error("err",
  59. zap.String("system_err", err.Error()),
  60. zap.Stack("stacktrace"))
  61. }
  62. }
  63. }()
  64. err = checkGateListParam(req)
  65. if err != nil {
  66. return nil, err
  67. }
  68. p := &dbmodel.TGate{}
  69. where := [][2]interface{}{}
  70. if len(req.UnitIds) > 0 {
  71. if req.GardenId == 0 {
  72. return nil, status.Error(10003, "小区不能为空")
  73. }
  74. w := dbmodel.TGateUnit{}
  75. filter := [][2]interface{}{}
  76. filter = dbmodel.WhereAdd(filter, "garden_id", req.GardenId)
  77. filter = dbmodel.WhereAdd(filter, "unit_id in", req.UnitIds)
  78. list, err := w.List(database.DB(), filter, nil, -1, -1)
  79. if err != nil {
  80. return nil, errors.DataBaseError
  81. }
  82. ids := []int64{}
  83. for _, v := range list {
  84. ids = append(ids, v.DeviceId)
  85. }
  86. if len(ids) == 0 {
  87. return reply, nil
  88. }
  89. where = dbmodel.WhereAdd(where, "id in", ids)
  90. }
  91. if req.GardenId > 0 {
  92. where = dbmodel.WhereAdd(where, "garden_id", req.GardenId)
  93. }
  94. if req.Sn != "" {
  95. where = dbmodel.WhereAdd(where, "sn like", "%"+req.Sn+"%")
  96. }
  97. if req.DeviceId != 0 {
  98. where = dbmodel.WhereAdd(where, "id", req.DeviceId)
  99. }
  100. if req.Out == 1 {
  101. where = dbmodel.WhereAdd(where, "garden_id >", 0)
  102. }
  103. if req.Out == 2 {
  104. where = dbmodel.WhereAdd(where, "garden_id", 0)
  105. }
  106. if req.Manufactor != "" {
  107. where = dbmodel.WhereAdd(where, "manufactor like", "%"+req.Manufactor+"%")
  108. }
  109. if req.DeviceName != "" {
  110. where = dbmodel.WhereAdd(where, "device_name like", "%"+req.DeviceName+"%")
  111. }
  112. if req.QcodeSupport > 0 {
  113. where = dbmodel.WhereAdd(where, "qcode_support", req.QcodeSupport)
  114. }
  115. if req.PicSupport > 0 {
  116. where = dbmodel.WhereAdd(where, "pic_support", req.PicSupport)
  117. }
  118. if req.CardSupport > 0 {
  119. where = dbmodel.WhereAdd(where, "card_support", req.CardSupport)
  120. }
  121. count, err := p.Count(database.DB(), where, nil)
  122. if err != nil {
  123. return nil, errors.DataBaseError
  124. }
  125. reply.Total = count
  126. reply.Page = req.Page
  127. if count == 0 {
  128. return reply, nil
  129. }
  130. list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  131. if err != nil {
  132. return nil, errors.DataBaseError
  133. }
  134. gardenM, err := getGardenName(list)
  135. if err != nil {
  136. return nil, err
  137. }
  138. reply.List = make([]*pb_v1.GateItem, len(list))
  139. for i, v := range list {
  140. reply.List[i] = &pb_v1.GateItem{
  141. // 设备id
  142. DeviceId: v.ID,
  143. // 设备名
  144. DeviceName: v.DeviceName,
  145. // 序列号
  146. Sn: v.Sn,
  147. // 厂商
  148. Manufactor: v.Manufactor,
  149. // 授权key
  150. AuthKey: v.AuthKey,
  151. // 协议
  152. Protocol: v.Protocol,
  153. // 小区id
  154. GardenId: v.GardenId,
  155. // 小区名
  156. GardenName: gardenM[v.GardenId],
  157. // 出库人
  158. OutUser: v.OutUser,
  159. // 出库时间
  160. OutTime: v.OutTime,
  161. // 1 在线 2 离线
  162. Status: v.Status,
  163. Enable: false,
  164. // 1 进场 2 出场 3 进出场
  165. Direction: v.Direction,
  166. Location: v.Location,
  167. QcodeSupport: v.QcodeSupport,
  168. PicSupport: v.PicSupport,
  169. CardSupport: v.CardSupport,
  170. Ip: v.Ip,
  171. Mac: v.Mac,
  172. UserName: v.UserName,
  173. Password: v.Password,
  174. ProtocolDesc: gate_utils.GateProtocolNameMap[v.Protocol],
  175. Port: v.Port,
  176. }
  177. if v.Enable == 1 {
  178. reply.List[i].Enable = true
  179. }
  180. }
  181. return reply, nil
  182. }