del.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package visitor
  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 checkGateVisitorDelParam(req *pb_v1.GateVisitorDelRequest) error {
  16. switch {
  17. case req.Id == 0:
  18. return status.Error(10003, "id不能为空")
  19. case !req.Manager && req.Uid == 0:
  20. return status.Error(10003, "用户不能为空")
  21. }
  22. return nil
  23. }
  24. func GateVisitorDel(ctx context.Context, req *pb_v1.GateVisitorDelRequest) (reply *pb_v1.GateVisitorDelReply, err error) {
  25. reply = &pb_v1.GateVisitorDelReply{}
  26. // 捕获各个task中的异常并返回给调用者
  27. defer func() {
  28. if r := recover(); r != nil {
  29. err = fmt.Errorf("%+v", r)
  30. e := &status.Status{}
  31. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  32. logger.Error("err",
  33. zap.String("system_err", err.Error()),
  34. zap.Stack("stacktrace"))
  35. }
  36. }
  37. }()
  38. err = checkGateVisitorDelParam(req)
  39. if err != nil {
  40. return nil, err
  41. }
  42. g := dbmodel.TGateVisitor{}
  43. where := [][2]interface{}{}
  44. where = dbmodel.WhereAdd(where, "id", req.Id)
  45. if req.Uid > 0 {
  46. where = dbmodel.WhereAdd(where, "uid", req.Uid)
  47. }
  48. err = g.Find(database.DB(), where)
  49. if err != nil && err != gorm.ErrRecordNotFound {
  50. return nil, errors.DataBaseError
  51. }
  52. if g.ID == 0 {
  53. return nil, errors.ErrRecordNotFound
  54. }
  55. if g.OpenTime > 0 {
  56. return nil, status.Error(10003, "已同行数据不能删除")
  57. }
  58. err = g.Delete(database.DB(), where)
  59. if err != nil {
  60. return nil, errors.DataBaseError
  61. }
  62. return reply, nil
  63. }