del.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package contact
  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. "time"
  14. "xingjia-management-gateway/apis"
  15. "xingjia-management-gateway/consts"
  16. "xingjia-management-gateway/errors"
  17. "xingjia-management-gateway/impl/v1/operation_log"
  18. dbmodel "xingjia-management-gateway/model"
  19. )
  20. func checkContactDelParam(req *apis.ContactDelRequest) error {
  21. switch {
  22. case req.Id == 0:
  23. return status.Error(10003, "删除目标不能为空")
  24. case req.SelfId == 0:
  25. return status.Error(10003, "未知操作者")
  26. }
  27. return nil
  28. }
  29. //
  30. func ContactDel(ctx context.Context, req *apis.ContactDelRequest) (reply *apis.ContactDelReply, err error) {
  31. reply = &apis.ContactDelReply{}
  32. // 捕获各个task中的异常并返回给调用者
  33. defer func() {
  34. if r := recover(); r != nil {
  35. err = fmt.Errorf("%+v", r)
  36. e := &status.Status{}
  37. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  38. logger.Error("err",
  39. zap.String("system_err", err.Error()),
  40. zap.Stack("stacktrace"))
  41. }
  42. }
  43. }()
  44. if err = checkContactDelParam(req); err != nil {
  45. return nil, err
  46. }
  47. now := time.Now()
  48. old := dbmodel.TContact{}
  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. db := database.DB().Begin()
  60. defer func() {
  61. if err != nil {
  62. db.Rollback()
  63. }
  64. }()
  65. p := dbmodel.TContact{}
  66. err = p.Delete(db, where)
  67. if err != nil {
  68. return nil, errors.DataBaseError
  69. }
  70. // 操作日志
  71. logReq := &apis.OperationLogAddRequest{
  72. OriginContent: getContactContent(old),
  73. TargetContent: "",
  74. Uid: req.SelfId,
  75. User: req.SelfName,
  76. Module: consts.OperationModuleContact,
  77. OperationTime: now.Unix(),
  78. Operation: consts.OperationActionDel,
  79. }
  80. err = operation_log.OperationLogAddHandle(logReq, db)
  81. if err != nil {
  82. return nil, err
  83. }
  84. db.Commit()
  85. return reply, nil
  86. }