set.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package gate
  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. "time"
  15. )
  16. func checkGateSetParam(req *pb_v1.GateSetRequest) error {
  17. switch {
  18. case req.DeviceId == 0:
  19. return status.Error(10003, "设备id不能为空")
  20. case req.GardenId == 0:
  21. return status.Error(10003, "小区id不能为空")
  22. case req.Location == "":
  23. return status.Error(10003, "位置不能为空")
  24. case req.Direction == 0:
  25. return status.Error(10003, "进场出场类型不能为空")
  26. }
  27. return nil
  28. }
  29. func GateSet(ctx context.Context, req *pb_v1.GateSetRequest) (reply *pb_v1.GateSetReply, err error) {
  30. reply = &pb_v1.GateSetReply{}
  31. // 捕获各个task中的异常并返回给调用者
  32. defer func() {
  33. if r := recover(); r != nil {
  34. err = fmt.Errorf("%+v", r)
  35. e := &status.Status{}
  36. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  37. logger.Error("err",
  38. zap.String("system_err", err.Error()),
  39. zap.Stack("stacktrace"))
  40. }
  41. }
  42. }()
  43. err = checkGateSetParam(req)
  44. if err != nil {
  45. return nil, err
  46. }
  47. p := &dbmodel.TGate{}
  48. where := [][2]interface{}{}
  49. where = dbmodel.WhereAdd(where, "id", req.DeviceId)
  50. where = dbmodel.WhereAdd(where, "garden_id", req.GardenId)
  51. err = p.Find(database.DB(), where)
  52. if err != nil && err != gorm.ErrRecordNotFound {
  53. return nil, errors.DataBaseError
  54. }
  55. if p.ID == 0 {
  56. return nil, errors.ErrRecordNotFound
  57. }
  58. now := time.Now()
  59. values := map[string]interface{}{
  60. "updated_at": now,
  61. "location": req.Location,
  62. "direction": req.Direction,
  63. "ip": req.Ip,
  64. "user_name": req.UserName,
  65. "password": req.Password,
  66. "mac": req.Mac,
  67. "port": req.Port,
  68. }
  69. if req.GateName != "" {
  70. values["device_name"] = req.GateName
  71. }
  72. err = p.Update(database.DB(), where, values)
  73. if err != nil {
  74. return nil, errors.DataBaseError
  75. }
  76. delGateInfoFromRedis(req.DeviceId, "", 0)
  77. delGateInfoFromRedis(0, p.Sn, p.Protocol)
  78. return reply, nil
  79. }