phone_del.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package service_phone
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "git.getensh.com/common/gopkgs/database"
  9. "git.getensh.com/common/gopkgs/logger"
  10. "go.uber.org/zap"
  11. "google.golang.org/grpc/status"
  12. "gorm.io/gorm"
  13. "property-garden/errors"
  14. dbmodel "property-garden/model"
  15. pb_v1 "property-garden/pb/v1"
  16. "property-garden/utils"
  17. )
  18. func checkServicePhoneDelParam(req *pb_v1.ServicePhoneDelRequest) error {
  19. switch {
  20. case req.GardenId == 0:
  21. return status.Error(10003, "小区不能为空")
  22. case req.Id < 1:
  23. return status.Error(10003, "id 不能为空")
  24. }
  25. return nil
  26. }
  27. func ServicePhoneDel(ctx context.Context, req *pb_v1.ServicePhoneDelRequest) (reply *pb_v1.ServicePhoneDelReply, err error) {
  28. reply = &pb_v1.ServicePhoneDelReply{}
  29. // 捕获各个task中的异常并返回给调用者
  30. defer func() {
  31. if r := recover(); r != nil {
  32. err = fmt.Errorf("%+v", r)
  33. e := &status.Status{}
  34. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  35. logger.Error("err",
  36. zap.String("system_err", err.Error()),
  37. zap.Stack("stacktrace"))
  38. }
  39. }
  40. }()
  41. // 参数检查
  42. err = checkServicePhoneDelParam(req)
  43. if err != nil {
  44. return nil, err
  45. }
  46. dbname := utils.GetGardenDbName(req.GardenId)
  47. // 获取旧数据
  48. old := dbmodel.NewServicePhone(dbname)
  49. where := map[string]interface{}{
  50. "id":req.Id,
  51. }
  52. err = old.Find(database.DB(), where)
  53. if err != nil && err != gorm.ErrRecordNotFound {
  54. return nil, errors.DataBaseError
  55. }
  56. if old.ID == 0 {
  57. return nil, errors.ErrRecordNotFound
  58. }
  59. // 删除数据
  60. p := dbmodel.NewServicePhone(dbname)
  61. err = p.Delete(database.DB(), where)
  62. if err != nil {
  63. return nil, errors.DataBaseError
  64. }
  65. return reply, nil
  66. }