add.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package visitor
  2. import (
  3. "context"
  4. "crypto/rc4"
  5. "encoding/json"
  6. "fmt"
  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. "property-device/impl/v1/gate_match"
  14. dbmodel "property-device/model"
  15. "property-device/parser"
  16. pb_v1 "property-device/pb/v1"
  17. "property-device/utils/gate_utils"
  18. "time"
  19. )
  20. const (
  21. GateTypeCode = 1
  22. GateTypeFace = 2
  23. )
  24. func checkGateVisitorAddParam(req *pb_v1.GateVisitorAddRequest) error {
  25. switch {
  26. case req.DeviceId == 0:
  27. return status.Error(10003, "设备id不能为空")
  28. case req.Uid == 0 || req.Name == "" || req.Phone == "" || req.GardenId == 0:
  29. return status.Error(10003, "不支持的门禁类型")
  30. case req.VisitorPhone == "" || req.Visitor == "" || req.Comment == "":
  31. return status.Error(10003, "访客信息不能为空")
  32. }
  33. if req.End < 1 || req.Start < 1 {
  34. return status.Error(10003, "访客邀约请选择时间段")
  35. }
  36. if req.End-req.Start > 3600*24 {
  37. return status.Error(10003, "访客二维码时间不能超过一天")
  38. }
  39. return nil
  40. }
  41. func Rc4Encrypt(plain string, key string) string {
  42. dest := make([]byte, len(plain))
  43. cipher2, _ := rc4.NewCipher([]byte(key))
  44. cipher2.XORKeyStream(dest, []byte(plain))
  45. return fmt.Sprintf("%X", dest)
  46. }
  47. func GateVisitorAdd(ctx context.Context, req *pb_v1.GateVisitorAddRequest) (reply *pb_v1.GateVisitorAddReply, err error) {
  48. reply = &pb_v1.GateVisitorAddReply{}
  49. // 捕获各个task中的异常并返回给调用者
  50. defer func() {
  51. if r := recover(); r != nil {
  52. err = fmt.Errorf("%+v", r)
  53. e := &status.Status{}
  54. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  55. logger.Error("err",
  56. zap.String("system_err", err.Error()),
  57. zap.Stack("stacktrace"))
  58. }
  59. }
  60. }()
  61. err = checkGateVisitorAddParam(req)
  62. if err != nil {
  63. return nil, err
  64. }
  65. now := time.Now()
  66. g := dbmodel.TGate{}
  67. where := [][2]interface{}{}
  68. where = dbmodel.WhereAdd(where, "id", req.DeviceId)
  69. err = g.Find(database.DB(), where)
  70. if err != nil && err != gorm.ErrRecordNotFound {
  71. return nil, errors.DataBaseError
  72. }
  73. if g.ID == 0 {
  74. return nil, errors.ErrRecordNotFound
  75. }
  76. whiteMatchInfo, err := gate_match.GetMatchInfoFromUserDb(g.GardenId, fmt.Sprintf("%d", req.Uid), &gate_utils.GateCommonInfo{GardenId: g.GardenId, DeviceId: g.ID})
  77. if err != nil {
  78. return nil, err
  79. }
  80. if whiteMatchInfo.DeviceId == 0 {
  81. return nil, status.Error(10003, "您不在白名单中,请联系物业添加白名单")
  82. }
  83. p := dbmodel.TGateVisitor{
  84. Uid: req.Uid,
  85. DeviceId: req.DeviceId,
  86. Name: req.Name,
  87. Phone: req.Phone,
  88. Visitor: req.Visitor,
  89. VisitorPhone: req.VisitorPhone,
  90. Start: req.Start,
  91. End: req.End,
  92. OpenTime: 0,
  93. CreatedAt: now,
  94. UpdatedAt: now,
  95. GardenId: req.GardenId,
  96. Comment: req.Comment,
  97. }
  98. db := database.DB().Begin()
  99. err = p.Insert(db)
  100. if err != nil {
  101. db.Rollback()
  102. return nil, errors.DataBaseError
  103. }
  104. if p.ID == 0 {
  105. db.Rollback()
  106. return nil, errors.DataBaseError
  107. }
  108. str := fmt.Sprintf("[%d-%s-%s-%d,%d,%s,%s,0,0,A]",
  109. req.Uid, req.VisitorPhone, req.Visitor, p.ID,
  110. req.GardenId,
  111. time.Unix(req.Start, 0).Format("20060102150405"),
  112. time.Unix(req.End, 0).Format("20060102150405"))
  113. qcode := "CB01" + Rc4Encrypt(str, parser.Conf.GateKey)
  114. where = [][2]interface{}{}
  115. where = dbmodel.WhereAdd(where, "id", p.ID)
  116. values := map[string]interface{}{
  117. "qcode": qcode,
  118. }
  119. err = p.Update(db, where, values)
  120. if err != nil {
  121. db.Rollback()
  122. return nil, errors.DataBaseError
  123. }
  124. reply.Id = p.ID
  125. reply.Qcode = qcode
  126. db.Commit()
  127. return reply, nil
  128. }