device_list.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package gate_unit
  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. "gorm.io/gorm"
  11. "property-device/errors"
  12. dbmodel "property-device/model"
  13. pb_v1 "property-device/pb/v1"
  14. "property-device/utils/gate_utils"
  15. )
  16. func checkGateUnitDeviceParam(req *pb_v1.GateUnitDeviceRequest) error {
  17. switch {
  18. case req.GardenId == 0:
  19. return status.Error(10003, "小区不能为空")
  20. case len(req.UnitId) == 0:
  21. return status.Error(10003, "单元不能为空")
  22. }
  23. return nil
  24. }
  25. func GateUnitDevice(ctx context.Context, req *pb_v1.GateUnitDeviceRequest) (reply *pb_v1.GateUnitDeviceReply, err error) {
  26. reply = &pb_v1.GateUnitDeviceReply{}
  27. // 捕获各个task中的异常并返回给调用者
  28. defer func() {
  29. if r := recover(); r != nil {
  30. err = fmt.Errorf("%+v", r)
  31. e := &status.Status{}
  32. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  33. logger.Error("err",
  34. zap.String("system_err", err.Error()),
  35. zap.Stack("stacktrace"))
  36. }
  37. }
  38. }()
  39. err = checkGateUnitDeviceParam(req)
  40. if err != nil {
  41. return nil, err
  42. }
  43. if err != nil {
  44. return nil, err
  45. }
  46. p := &dbmodel.TGateUnit{}
  47. where := [][2]interface{}{}
  48. where = dbmodel.WhereAdd(where, "garden_id", req.GardenId)
  49. where = dbmodel.WhereAdd(where, "unit_id in", req.UnitId)
  50. if req.OnlyHas {
  51. err = p.Find(database.DB(), where)
  52. if err != nil && err != gorm.ErrRecordNotFound {
  53. return nil, errors.DataBaseError
  54. }
  55. if p.ID > 0 {
  56. reply.HasDevice = true
  57. }
  58. return reply, nil
  59. }
  60. list, err := p.List(database.DB(), where, nil, -1, -1)
  61. if err != nil {
  62. return nil, errors.DataBaseError
  63. }
  64. deviceIds := []int64{}
  65. dm := map[int64]bool{}
  66. for _, v := range list {
  67. if dm[v.DeviceId] {
  68. continue
  69. }
  70. dm[v.DeviceId] = true
  71. deviceIds = append(deviceIds, v.DeviceId)
  72. }
  73. if len(deviceIds) == 0 {
  74. return reply, nil
  75. }
  76. gt := &dbmodel.TGate{}
  77. where = [][2]interface{}{}
  78. where = dbmodel.WhereAdd(where, "id in", deviceIds)
  79. glist, err := gt.List(database.DB(), where, nil, -1, -1)
  80. if err != nil {
  81. return nil, errors.DataBaseError
  82. }
  83. if len(glist) == 0 {
  84. return reply, nil
  85. }
  86. reply.List = make([]*pb_v1.GateItem, len(glist))
  87. for i, v := range glist {
  88. reply.List[i] = &pb_v1.GateItem{
  89. // 设备id
  90. DeviceId: v.ID,
  91. // 设备名
  92. DeviceName: v.DeviceName,
  93. // 序列号
  94. Sn: v.Sn,
  95. // 厂商
  96. Manufactor: v.Manufactor,
  97. // 授权key
  98. AuthKey: v.AuthKey,
  99. // 协议
  100. Protocol: v.Protocol,
  101. // 小区id
  102. GardenId: v.GardenId,
  103. // 小区名
  104. GardenName: "",
  105. // 出库人
  106. OutUser: v.OutUser,
  107. // 出库时间
  108. OutTime: v.OutTime,
  109. // 1 在线 2 离线
  110. Status: v.Status,
  111. Enable: false,
  112. // 1 进场 2 出场 3 进出场
  113. Direction: v.Direction,
  114. Location: v.Location,
  115. QcodeSupport: v.QcodeSupport,
  116. PicSupport: v.PicSupport,
  117. CardSupport: v.CardSupport,
  118. Ip: v.Ip,
  119. Mac: v.Mac,
  120. UserName: v.UserName,
  121. Password: v.Password,
  122. ProtocolDesc: gate_utils.GateProtocolNameMap[v.Protocol],
  123. }
  124. if v.Enable == 1 {
  125. reply.List[i].Enable = true
  126. }
  127. }
  128. return reply, nil
  129. }