can_bind_devices.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package gate_card
  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. "property-device/pb"
  14. pb_v1 "property-device/pb/v1"
  15. "property-device/utils/gate_utils"
  16. )
  17. func checkGateCardCanBindDevicesParam(req *pb_v1.GateCardCanBindDevicesRequest) error {
  18. if req.GardenId < 0 {
  19. return status.Error(10003, "小区不能为空")
  20. }
  21. if req.CardNumber == "" {
  22. return status.Error(10003, "卡号不能为空")
  23. }
  24. return nil
  25. }
  26. func getGardenName(list []dbmodel.TGate) (map[int64]string, error) {
  27. gardenM := map[int64]string{}
  28. gardenIds := []int64{}
  29. for _, v := range list {
  30. if v.GardenId == 0 {
  31. continue
  32. }
  33. if _, ok := gardenM[v.GardenId]; ok {
  34. continue
  35. }
  36. gardenM[v.GardenId] = ""
  37. gardenIds = append(gardenIds, v.GardenId)
  38. }
  39. if len(gardenIds) > 0 {
  40. mreq := pb_v1.GardenInfosRequest{Ids: gardenIds}
  41. mreply, err := pb.System.GardenInfos(context.Background(), &mreq)
  42. if err != nil {
  43. return nil, err
  44. }
  45. for _, v := range mreply.List {
  46. gardenM[v.Id] = v.GardenName
  47. }
  48. }
  49. return gardenM, nil
  50. }
  51. func getCardBindDevices(gardenId int64, cardNumber string) (map[int64]bool, error) {
  52. ucard := dbmodel.TUserCard{}
  53. where := [][2]interface{}{}
  54. where = dbmodel.WhereAdd(where, "garden_id", gardenId)
  55. where = dbmodel.WhereAdd(where, "card_number", cardNumber)
  56. err := ucard.Find(database.DB(), where)
  57. if err != nil && err != gorm.ErrRecordNotFound {
  58. return nil, errors.DataBaseError
  59. }
  60. ret := map[int64]bool{}
  61. if ucard.ID == 0 {
  62. return ret, nil
  63. }
  64. gcard := dbmodel.TGateCard{}
  65. where = [][2]interface{}{}
  66. where = dbmodel.WhereAdd(where, "record_id", ucard.ID)
  67. list, err := gcard.List(database.DB(), where, nil, -1, -1)
  68. if err != nil {
  69. return nil, errors.DataBaseError
  70. }
  71. for _, v := range list {
  72. ret[v.DeviceId] = true
  73. }
  74. return ret, nil
  75. }
  76. func GateCardCanBindDevices(ctx context.Context, req *pb_v1.GateCardCanBindDevicesRequest) (reply *pb_v1.GateCardCanBindDevicesReply, err error) {
  77. reply = &pb_v1.GateCardCanBindDevicesReply{}
  78. // 捕获各个task中的异常并返回给调用者
  79. defer func() {
  80. if r := recover(); r != nil {
  81. err = fmt.Errorf("%+v", r)
  82. e := &status.Status{}
  83. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  84. logger.Error("err",
  85. zap.String("system_err", err.Error()),
  86. zap.Stack("stacktrace"))
  87. }
  88. }
  89. }()
  90. err = checkGateCardCanBindDevicesParam(req)
  91. if err != nil {
  92. return nil, err
  93. }
  94. bindedIdM, err := getCardBindDevices(req.GardenId, req.CardNumber)
  95. if err != nil {
  96. return nil, err
  97. }
  98. p := &dbmodel.TGate{}
  99. where := [][2]interface{}{}
  100. where = dbmodel.WhereAdd(where, "garden_id", req.GardenId)
  101. protocols := []int32{}
  102. for protocol, array := range gate_utils.GateProtocolFuntionMap {
  103. if array[2] == 1 {
  104. protocols = append(protocols, protocol)
  105. }
  106. }
  107. where = dbmodel.WhereAdd(where, "protocol in", protocols)
  108. list, err := p.List(database.DB(), where, nil, -1, -1)
  109. if err != nil {
  110. return nil, errors.DataBaseError
  111. }
  112. reply.List = []*pb_v1.GateItem{}
  113. for _, v := range list {
  114. if bindedIdM[v.ID] {
  115. continue
  116. }
  117. item := &pb_v1.GateItem{
  118. // 设备id
  119. DeviceId: v.ID,
  120. // 设备名
  121. DeviceName: v.DeviceName,
  122. // 序列号
  123. Sn: v.Sn,
  124. // 厂商
  125. Manufactor: v.Manufactor,
  126. // 授权key
  127. AuthKey: v.AuthKey,
  128. // 协议
  129. Protocol: v.Protocol,
  130. // 小区id
  131. GardenId: v.GardenId,
  132. // 小区名
  133. GardenName: "",
  134. // 出库人
  135. OutUser: v.OutUser,
  136. // 出库时间
  137. OutTime: v.OutTime,
  138. // 1 在线 2 离线
  139. Status: v.Status,
  140. Enable: false,
  141. // 1 进场 2 出场 3 进出场
  142. Direction: v.Direction,
  143. Location: v.Location,
  144. QcodeSupport: v.QcodeSupport,
  145. PicSupport: v.PicSupport,
  146. CardSupport: v.CardSupport,
  147. Ip: v.Ip,
  148. Mac: v.Mac,
  149. UserName: v.UserName,
  150. Password: v.Password,
  151. ProtocolDesc: gate_utils.GateProtocolNameMap[v.Protocol],
  152. Port: v.Port,
  153. }
  154. if v.Enable == 1 {
  155. item.Enable = true
  156. }
  157. reply.List = append(reply.List, item)
  158. }
  159. return reply, nil
  160. }