publish.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. "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 checkJtContentPublishParam(req *apis.JtContentPublishRequest) error {
  21. switch {
  22. case consts.JtContentTable[req.ContentType] == "":
  23. return status.Error(10003, "未知内容类型")
  24. case req.SelfId == 0:
  25. return status.Error(10003, "未知操作者")
  26. case req.Id == 0:
  27. return status.Error(10003, "目标不能为空")
  28. case req.PublishStatus != 1 && req.PublishStatus != 2:
  29. return status.Error(10003, "请选择是否发布")
  30. }
  31. return nil
  32. }
  33. //
  34. func JtContentPublish(ctx context.Context, req *apis.JtContentPublishRequest) (reply *apis.JtContentPublishReply, err error) {
  35. reply = &apis.JtContentPublishReply{}
  36. // 捕获各个task中的异常并返回给调用者
  37. defer func() {
  38. if r := recover(); r != nil {
  39. err = fmt.Errorf("%+v", r)
  40. e := &status.Status{}
  41. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  42. logger.Error("err",
  43. zap.String("system_err", err.Error()),
  44. zap.Stack("stacktrace"))
  45. }
  46. }
  47. }()
  48. if err = checkJtContentPublishParam(req); err != nil {
  49. return nil, err
  50. }
  51. now := time.Now()
  52. old := dbmodel.NewJtContent(consts.JtContentTable[req.ContentType])
  53. where := map[string]interface{}{
  54. "id": req.Id,
  55. }
  56. err = old.Find(database.DB(), where)
  57. if err != nil && err != gorm.ErrRecordNotFound {
  58. return nil, errors.DataBaseError
  59. }
  60. if old.ID == 0 {
  61. return nil, errors.ErrRecordNotFound
  62. }
  63. values := map[string]interface{}{
  64. "publish_status":req.PublishStatus,
  65. "updated_at": time.Now(),
  66. }
  67. db := database.DB().Begin()
  68. defer func() {
  69. if err != nil {
  70. db.Rollback()
  71. }
  72. }()
  73. p := dbmodel.NewJtContent(consts.JtContentTable[req.ContentType])
  74. err = p.Update(db, where, values)
  75. if err != nil {
  76. return nil, errors.DataBaseError
  77. }
  78. // 操作日志
  79. content := getJtContent(*old)
  80. tmp := *old
  81. tmp.PublishStatus = req.PublishStatus
  82. tcontent := getJtContent(tmp)
  83. logReq := &apis.OperationLogAddRequest{
  84. OriginContent: content,
  85. TargetContent: tcontent,
  86. Uid: req.SelfId,
  87. User: req.SelfName,
  88. Module: int32(req.ContentType),
  89. OperationTime: now.Unix(),
  90. Operation: consts.OperationActionUpdate,
  91. }
  92. err = operation_log.OperationLogAddHandle(logReq, db)
  93. if err != nil {
  94. return nil, err
  95. }
  96. db.Commit()
  97. return reply, nil
  98. }