card_sync.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. pb_v1 "property-device/pb/v1"
  14. "property-device/utils/gate_utils"
  15. "time"
  16. )
  17. func checkGateCardSyncParam(req *pb_v1.GateCardSyncRequest) error {
  18. switch {
  19. case req.GardenId == 0:
  20. return status.Error(10003, "小区不能为空")
  21. case req.Id == 0:
  22. return status.Error(10003, "记录id不能为空")
  23. }
  24. return nil
  25. }
  26. func getNeedUpdateDevice(recordId int64) ([]dbmodel.TGateCard, error) {
  27. gcard := dbmodel.TGateCard{}
  28. where := [][2]interface{}{}
  29. where = dbmodel.WhereAdd(where, "record_id", recordId)
  30. where = dbmodel.WhereAdd(where, "status !=", gate_utils.WhiteAddStatusAllSuc)
  31. glist, err := gcard.List(database.DB(), where, nil, -1, -1)
  32. if err != nil {
  33. return nil, errors.DataBaseError
  34. }
  35. return glist, nil
  36. }
  37. func GateCardSync(ctx context.Context, req *pb_v1.GateCardSyncRequest) (reply *pb_v1.GateCardSyncReply, err error) {
  38. reply = &pb_v1.GateCardSyncReply{}
  39. // 捕获各个task中的异常并返回给调用者
  40. defer func() {
  41. if r := recover(); r != nil {
  42. err = fmt.Errorf("%+v", r)
  43. e := &status.Status{}
  44. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  45. logger.Error("err",
  46. zap.String("system_err", err.Error()),
  47. zap.Stack("stacktrace"))
  48. }
  49. }
  50. }()
  51. err = checkGateCardSyncParam(req)
  52. if err != nil {
  53. return nil, err
  54. }
  55. up := dbmodel.TUserCard{}
  56. where := [][2]interface{}{}
  57. where = dbmodel.WhereAdd(where, "id", req.Id)
  58. err = up.Find(database.DB(), where)
  59. if err != nil && err != gorm.ErrRecordNotFound {
  60. return nil, errors.DataBaseError
  61. }
  62. if up.ID == 0 {
  63. return nil, errors.ErrRecordNotFound
  64. }
  65. if time.Now().Unix()-up.UpdatedAt.Unix() < 60 {
  66. return nil, status.Error(10003, "下发操作间隔时间不能小于1分钟")
  67. }
  68. gcards, err := getNeedUpdateDevice(up.ID)
  69. if err != nil {
  70. return nil, err
  71. }
  72. if len(gcards) == 0 {
  73. return reply, nil
  74. }
  75. deviceIds := make([]int64, len(gcards))
  76. for i, v := range gcards {
  77. deviceIds[i] = v.DeviceId
  78. }
  79. devices, err := GetDevicesByIds(deviceIds)
  80. if err != nil {
  81. return nil, err
  82. }
  83. deviceM := map[int64]dbmodel.TGate{}
  84. for _, v := range devices {
  85. deviceM[v.ID] = v
  86. }
  87. for _, v := range gcards {
  88. gcard := v
  89. device := deviceM[v.DeviceId]
  90. if device.ID == 0 {
  91. continue
  92. }
  93. whiter := NewCardWhiter(&up, &device, &gcard)
  94. if whiter == nil {
  95. continue
  96. }
  97. whiter.PersonAdd()
  98. if whiter.Command() {
  99. gate_utils.CommandCacheIncrease(device.Sn, device.Protocol)
  100. }
  101. }
  102. values := map[string]interface{}{
  103. "updated_at": time.Now(),
  104. }
  105. _ = up.Update(database.DB(), where, values)
  106. return reply, nil
  107. }