del.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package application
  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. "property-common/errors"
  13. dbmodel "property-common/model"
  14. pb_v1 "property-common/pb/v1"
  15. )
  16. func checkApplicationDelParam(req *pb_v1.ApplicationDelRequest) error {
  17. switch {
  18. case req.Id == 0:
  19. return status.Error(10003, "id不能为空")
  20. }
  21. return nil
  22. }
  23. //
  24. func ApplicationDel(ctx context.Context, req *pb_v1.ApplicationDelRequest) (reply *pb_v1.ApplicationDelReply, err error) {
  25. reply = &pb_v1.ApplicationDelReply{}
  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. // 参数检查
  39. err = checkApplicationDelParam(req)
  40. if err != nil {
  41. return nil, err
  42. }
  43. db := database.DB().Begin()
  44. p := dbmodel.TApplication{}
  45. where := map[string]interface{}{
  46. "id": req.Id,
  47. }
  48. err = p.Delete(db, where)
  49. if err != nil {
  50. db.Rollback()
  51. return nil, errors.DataBaseError
  52. }
  53. ap := dbmodel.TApplicationPermission{}
  54. where = map[string]interface{}{
  55. "application_id": req.Id,
  56. }
  57. err = ap.Delete(db, where)
  58. if err != nil {
  59. db.Rollback()
  60. return nil, errors.DataBaseError
  61. }
  62. pa := dbmodel.TPackageApplication{}
  63. where = map[string]interface{}{
  64. "application_id": req.Id,
  65. }
  66. err = pa.Delete(db, where)
  67. if err != nil {
  68. db.Rollback()
  69. return nil, errors.DataBaseError
  70. }
  71. db.Commit()
  72. return reply, nil
  73. }