recovery.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 checkGateRecoveryParam(req *pb_v1.GateRecoveryRequest) error {
  17. switch {
  18. case req.DeviceId == 0:
  19. return status.Error(10003, "设备id不能为空")
  20. }
  21. return nil
  22. }
  23. func GateRecovery(ctx context.Context, req *pb_v1.GateRecoveryRequest) (reply *pb_v1.GateRecoveryReply, err error) {
  24. reply = &pb_v1.GateRecoveryReply{}
  25. // 捕获各个task中的异常并返回给调用者
  26. defer func() {
  27. if r := recover(); r != nil {
  28. err = fmt.Errorf("%+v", r)
  29. e := &status.Status{}
  30. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  31. logger.Error("err",
  32. zap.String("system_err", err.Error()),
  33. zap.Stack("stacktrace"))
  34. }
  35. }
  36. }()
  37. err = checkGateRecoveryParam(req)
  38. if err != nil {
  39. return nil, err
  40. }
  41. p := &dbmodel.TGate{}
  42. where := [][2]interface{}{}
  43. where = dbmodel.WhereAdd(where, "id", req.DeviceId)
  44. err = p.Find(database.DB(), where)
  45. if err != nil && err != gorm.ErrRecordNotFound {
  46. return nil, errors.DataBaseError
  47. }
  48. if p.ID == 0 {
  49. return nil, errors.ErrRecordNotFound
  50. }
  51. if p.GardenId == 0 {
  52. return reply, nil
  53. }
  54. if p.Enable == 1 {
  55. return nil, status.Error(10003, "设备未禁用无法回收")
  56. }
  57. now := time.Now()
  58. values := map[string]interface{}{
  59. "garden_id": 0,
  60. "enable": 1,
  61. "updated_at": now,
  62. "out_time": 0,
  63. "out_user": "",
  64. }
  65. db := database.DB().Begin()
  66. err = p.Update(db, where, values)
  67. if err != nil {
  68. db.Rollback()
  69. return nil, errors.DataBaseError
  70. }
  71. where = [][2]interface{}{}
  72. where = dbmodel.WhereAdd(where, "device_id", req.DeviceId)
  73. gu := dbmodel.TGateUnit{}
  74. gc := dbmodel.TGateCommand{}
  75. gv := dbmodel.TGateVisitor{}
  76. gp := dbmodel.TGatePic{}
  77. err = gu.Delete(db, where)
  78. if err != nil {
  79. db.Rollback()
  80. return nil, errors.DataBaseError
  81. }
  82. err = gc.Delete(db, where)
  83. if err != nil {
  84. db.Rollback()
  85. return nil, errors.DataBaseError
  86. }
  87. err = gv.Delete(db, where)
  88. if err != nil {
  89. db.Rollback()
  90. return nil, errors.DataBaseError
  91. }
  92. err = gp.Delete(db, where)
  93. if err != nil {
  94. db.Rollback()
  95. return nil, errors.DataBaseError
  96. }
  97. //todo commandCacheDecrease(req.DeviceId)
  98. db.Commit()
  99. return reply, nil
  100. }