del.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package jt
  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. "strings"
  14. "time"
  15. "xingjia-management-gateway/apis"
  16. "xingjia-management-gateway/consts"
  17. "xingjia-management-gateway/errors"
  18. "xingjia-management-gateway/impl/v1/local_objs"
  19. "xingjia-management-gateway/impl/v1/operation_log"
  20. dbmodel "xingjia-management-gateway/model"
  21. )
  22. func checkJtContentDelParam(req *apis.JtContentDelRequest) error {
  23. switch {
  24. case consts.JtContentTable[req.ContentType] == "":
  25. return status.Error(10003, "未知内容类型")
  26. case req.SelfId == 0:
  27. return status.Error(10003, "未知操作者")
  28. }
  29. return nil
  30. }
  31. //
  32. func JtContentDel(ctx context.Context, req *apis.JtContentDelRequest) (reply *apis.JtContentDelReply, err error) {
  33. reply = &apis.JtContentDelReply{}
  34. // 捕获各个task中的异常并返回给调用者
  35. defer func() {
  36. if r := recover(); r != nil {
  37. err = fmt.Errorf("%+v", r)
  38. e := &status.Status{}
  39. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  40. logger.Error("err",
  41. zap.String("system_err", err.Error()),
  42. zap.Stack("stacktrace"))
  43. }
  44. }
  45. }()
  46. if err = checkJtContentDelParam(req); err != nil {
  47. return nil, err
  48. }
  49. now := time.Now()
  50. old := dbmodel.NewJtContent(consts.JtContentTable[req.ContentType])
  51. where := map[string]interface{}{
  52. "id": req.Id,
  53. }
  54. err = old.Find(database.DB(), where)
  55. if err != nil && err != gorm.ErrRecordNotFound {
  56. return nil, errors.DataBaseError
  57. }
  58. if old.ID == 0 {
  59. return nil, errors.ErrRecordNotFound
  60. }
  61. db := database.DB().Begin()
  62. defer func() {
  63. if err != nil {
  64. db.Rollback()
  65. }
  66. }()
  67. p := dbmodel.NewJtContent(consts.JtContentTable[req.ContentType])
  68. err = p.Delete(db, where)
  69. if err != nil {
  70. return nil, errors.DataBaseError
  71. }
  72. // 操作日志
  73. content := getJtContent(*old)
  74. logReq := &apis.OperationLogAddRequest{
  75. OriginContent: content,
  76. TargetContent: "",
  77. Uid: req.SelfId,
  78. User: req.SelfName,
  79. Module: int32(req.ContentType),
  80. OperationTime: now.Unix(),
  81. Operation: consts.OperationActionDel,
  82. }
  83. err = operation_log.OperationLogAddHandle(logReq, db)
  84. if err != nil {
  85. return nil, err
  86. }
  87. outList := strings.Split(old.FirstPics, ";")
  88. array := local_objs.GetObjFromText(old.Content)
  89. outList = append(outList, array...)
  90. err = local_objs.InAndOut(db, []string{}, outList)
  91. if err != nil {
  92. return nil, err
  93. }
  94. db.Commit()
  95. return reply, nil
  96. }