white_match.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package gate_match
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "git.getensh.com/common/gopkgs/cache"
  7. "git.getensh.com/common/gopkgs/database"
  8. "git.getensh.com/common/gopkgs/logger"
  9. "go.uber.org/zap"
  10. "google.golang.org/grpc/status"
  11. "gorm.io/gorm"
  12. "property-device/errors"
  13. dbmodel "property-device/model"
  14. "property-device/pb"
  15. pb_v1 "property-device/pb/v1"
  16. "property-device/utils/gate_utils"
  17. "strconv"
  18. )
  19. func checkGateWhiteMatchParam(req *pb_v1.GateWhiteMatchRequest) string {
  20. switch {
  21. case req.Sn == "" || req.Protocol == 0:
  22. return "设备序列号不能为空"
  23. case req.CodeVal == "":
  24. return "卡号或用户id不能为空"
  25. case req.CodeType == 0:
  26. return "认证类型不能为空"
  27. }
  28. return ""
  29. }
  30. func getWhiteMatchKey(gardenId int64, codeType int32, codeVal string) string {
  31. key := fmt.Sprintf("gate_white_%v_%v_%v", gardenId, codeType, codeVal)
  32. return key
  33. }
  34. func setWhiteMatchToRedis(gardenId int64, codeType int32, codeVal string, reply *pb_v1.GateWhiteMatchReply) {
  35. key := getWhiteMatchKey(gardenId, codeType, codeVal)
  36. bytes, _ := json.Marshal(reply)
  37. cache.Redis().SetEx(key, 300, string(bytes))
  38. }
  39. func getWhiteMatchFromRedis(gardenId int64, codeType int32, codeVal string) (info *pb_v1.GateWhiteMatchReply) {
  40. key := getWhiteMatchKey(gardenId, codeType, codeVal)
  41. str, _ := cache.Redis().Get(key)
  42. if str == "" {
  43. return nil
  44. }
  45. ret := pb_v1.GateWhiteMatchReply{}
  46. json.Unmarshal([]byte(str), &ret)
  47. return &ret
  48. }
  49. func getMatchInfoFromCardDb(gardenId int64, codeVal string, gateInfo *gate_utils.GateCommonInfo) (*pb_v1.GateWhiteMatchReply, error) {
  50. ucards := dbmodel.TUserCard{}
  51. where := [][2]interface{}{}
  52. where = dbmodel.WhereAdd(where, "garden_id", gardenId)
  53. where = dbmodel.WhereAdd(where, "card_number", codeVal)
  54. err := ucards.Find(database.DB(), where)
  55. if err != nil && err != gorm.ErrRecordNotFound {
  56. return nil, errors.DataBaseError
  57. }
  58. if ucards.ID == 0 {
  59. return &pb_v1.GateWhiteMatchReply{}, nil
  60. }
  61. gcards := dbmodel.TGateCard{}
  62. where = [][2]interface{}{}
  63. where = dbmodel.WhereAdd(where, "device_id", gateInfo.DeviceId)
  64. where = dbmodel.WhereAdd(where, "record_id", ucards.ID)
  65. err = gcards.Find(database.DB(), where)
  66. if err != nil && err != gorm.ErrRecordNotFound {
  67. return nil, errors.DataBaseError
  68. }
  69. if gcards.ID == 0 {
  70. return &pb_v1.GateWhiteMatchReply{}, nil
  71. }
  72. ret := pb_v1.GateWhiteMatchReply{
  73. Status: 1,
  74. Name: ucards.Name,
  75. HouseName: "",
  76. Phone: "",
  77. IdNumber: "",
  78. Uid: ucards.Uid,
  79. CardNumber: "",
  80. DeviceName: gateInfo.DeviceName,
  81. DeviceId: gateInfo.DeviceId,
  82. GardenId: gardenId,
  83. }
  84. return &ret, nil
  85. }
  86. func GetMatchInfoFromUserDb(gardenId int64, codeVal string, gateInfo *gate_utils.GateCommonInfo) (*pb_v1.GateWhiteMatchReply, error) {
  87. uid, _ := strconv.Atoi(codeVal)
  88. if uid == 0 {
  89. return nil, status.Error(10003, "uid错误")
  90. }
  91. mreq := pb_v1.GardenHouseholdListRequest{GardenId: gardenId, Uid: int64(uid), PageSize: -1, Page: -1}
  92. mreply, err := pb.Garden.GardenHouseholdList(context.Background(), &mreq)
  93. if err != nil {
  94. return nil, err
  95. }
  96. if len(mreply.List) == 0 {
  97. return nil, status.Error(10003, "用户不存在")
  98. }
  99. unitIds := []int64{}
  100. m := map[int64]pb_v1.GardenHouseholdItem{}
  101. for _, v := range mreply.List {
  102. if _, ok := m[v.UnitId]; ok {
  103. continue
  104. }
  105. unitIds = append(unitIds, v.UnitId)
  106. m[v.UnitId] = *v
  107. }
  108. p := dbmodel.TGateUnit{}
  109. where := [][2]interface{}{}
  110. where = dbmodel.WhereAdd(where, "garden_id", gardenId)
  111. where = dbmodel.WhereAdd(where, "device_id", gateInfo.DeviceId)
  112. where = dbmodel.WhereAdd(where, "unit_id in", unitIds)
  113. err = p.Find(database.DB(), where)
  114. if err != nil && err != gorm.ErrRecordNotFound {
  115. return nil, errors.DataBaseError
  116. }
  117. if p.ID == 0 {
  118. return &pb_v1.GateWhiteMatchReply{}, nil
  119. }
  120. ret := pb_v1.GateWhiteMatchReply{
  121. Status: 1,
  122. Name: m[p.UnitId].Name,
  123. HouseName: m[p.UnitId].HouseName,
  124. Phone: m[p.UnitId].Phone,
  125. IdNumber: m[p.UnitId].IdNumber,
  126. Uid: codeVal,
  127. CardNumber: "",
  128. DeviceName: gateInfo.DeviceName,
  129. DeviceId: gateInfo.DeviceId,
  130. GardenId: gardenId,
  131. }
  132. return &ret, nil
  133. }
  134. func getMatchInfoFromDb(gardenId int64, codeType int32, codeVal string, gateInfo *gate_utils.GateCommonInfo) (*pb_v1.GateWhiteMatchReply, error) {
  135. if codeType == 1 {
  136. return GetMatchInfoFromUserDb(gardenId, codeVal, gateInfo)
  137. }
  138. return getMatchInfoFromCardDb(gardenId, codeVal, gateInfo)
  139. }
  140. func GateWhiteMatch(ctx context.Context, req *pb_v1.GateWhiteMatchRequest) (reply *pb_v1.GateWhiteMatchReply, err error) {
  141. reply = &pb_v1.GateWhiteMatchReply{}
  142. // 捕获各个task中的异常并返回给调用者
  143. defer func() {
  144. if r := recover(); r != nil {
  145. err = fmt.Errorf("%+v", r)
  146. e := &status.Status{}
  147. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  148. logger.Error("err",
  149. zap.String("system_err", err.Error()),
  150. zap.Stack("stacktrace"))
  151. }
  152. }
  153. }()
  154. desc := checkGateWhiteMatchParam(req)
  155. if desc != "" {
  156. reply.Status = 0
  157. return reply, nil
  158. }
  159. gateInfo, err := gate_utils.GetGateGarden(req.Sn, req.Protocol)
  160. if err != nil {
  161. return nil, err
  162. }
  163. matchInfo := getWhiteMatchFromRedis(gateInfo.GardenId, req.CodeType, req.CodeVal)
  164. if matchInfo != nil {
  165. return matchInfo, nil
  166. }
  167. matchInfo, err = getMatchInfoFromDb(gateInfo.GardenId, req.CodeType, req.CodeVal, &gateInfo)
  168. if err != nil {
  169. return nil, err
  170. }
  171. if matchInfo.DeviceId == 0 {
  172. matchInfo.Status = 0
  173. return matchInfo, nil
  174. }
  175. matchInfo.Status = 1
  176. setWhiteMatchToRedis(gateInfo.GardenId, req.CodeType, req.CodeVal, matchInfo)
  177. return matchInfo, nil
  178. }