del.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. )
  15. func checkGateDelParam(req *pb_v1.GateDelRequest) error {
  16. switch {
  17. case req.DeviceId == 0:
  18. return status.Error(10003, "设备id不能为空")
  19. }
  20. return nil
  21. }
  22. func GateDel(ctx context.Context, req *pb_v1.GateDelRequest) (reply *pb_v1.GateDelReply, err error) {
  23. reply = &pb_v1.GateDelReply{}
  24. // 捕获各个task中的异常并返回给调用者
  25. defer func() {
  26. if r := recover(); r != nil {
  27. err = fmt.Errorf("%+v", r)
  28. e := &status.Status{}
  29. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  30. logger.Error("err",
  31. zap.String("system_err", err.Error()),
  32. zap.Stack("stacktrace"))
  33. }
  34. }
  35. }()
  36. err = checkGateDelParam(req)
  37. if err != nil {
  38. return nil, err
  39. }
  40. p := &dbmodel.TGate{}
  41. where := [][2]interface{}{}
  42. where = dbmodel.WhereAdd(where, "id", req.DeviceId)
  43. err = p.Find(database.DB(), where)
  44. if err != nil && err != gorm.ErrRecordNotFound {
  45. return nil, errors.DataBaseError
  46. }
  47. if p.ID == 0 {
  48. return nil, errors.ErrRecordNotFound
  49. }
  50. if p.GardenId > 0 {
  51. return nil, status.Error(10003, "请先回收设备再删除")
  52. }
  53. err = p.Delete(database.DB(), where)
  54. if err != nil {
  55. return nil, errors.DataBaseError
  56. }
  57. return reply, nil
  58. }