record_add.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package gate_record
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "git.getensh.com/common/gopkgs/logger"
  7. "go.uber.org/zap"
  8. "google.golang.org/grpc/status"
  9. "property-device/model"
  10. pb_v1 "property-device/pb/v1"
  11. "time"
  12. )
  13. func checkGateRecordAddParam(req *pb_v1.GateRecordAddRequest) error {
  14. switch {
  15. case req.DeviceId == 0:
  16. return status.Error(10003, "设备id不能为空")
  17. case req.HouseholdUid == 0 && req.CardNumber == "":
  18. return status.Error(10003, "用户id和ic卡号不能同时为空")
  19. case req.GardenId == 0:
  20. return status.Error(10003, "小区不能为空")
  21. case req.OpenTime == 0:
  22. return status.Error(10003, "开门时间不能为空")
  23. case req.OpenType == 0:
  24. return status.Error(10003, "识别方式不能为空")
  25. }
  26. return nil
  27. }
  28. func GateRecordAdd(ctx context.Context, req *pb_v1.GateRecordAddRequest) (reply *pb_v1.GateRecordAddReply, err error) {
  29. reply = &pb_v1.GateRecordAddReply{}
  30. // 捕获各个task中的异常并返回给调用者
  31. defer func() {
  32. if r := recover(); r != nil {
  33. err = fmt.Errorf("%+v", r)
  34. e := &status.Status{}
  35. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  36. logger.Error("err",
  37. zap.String("system_err", err.Error()),
  38. zap.Stack("stacktrace"))
  39. }
  40. }
  41. }()
  42. err = checkGateRecordAddParam(req)
  43. if err != nil {
  44. return nil, err
  45. }
  46. tags := map[string]string{"device_id": fmt.Sprintf("%d", req.DeviceId)}
  47. fields := map[string]interface{}{
  48. "location": req.Location,
  49. "direction": req.Direction,
  50. "household_user": req.HouseholdUser,
  51. "household_id_number": req.HouseholdIdNumber,
  52. "household_housename": req.HouseholdHousename,
  53. "household_uid": req.HouseholdUid,
  54. "card_number": req.CardNumber,
  55. "card_owner": req.CardOwner,
  56. "online": req.Online,
  57. "garden_id": req.GardenId,
  58. "is_visitor": req.IsVisitor,
  59. "open_time": req.OpenTime,
  60. "visitor_name": req.VisitorName,
  61. "visitor_phone": req.VisitorPhone,
  62. "sn": req.Sn,
  63. "protocol": req.Protocol,
  64. "open_type": req.OpenType,
  65. }
  66. err = model.WriteGateData(fmt.Sprintf("%d", req.DeviceId), tags, fields, time.Now())
  67. if err != nil {
  68. return nil, err
  69. }
  70. return reply, nil
  71. }