add.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. "strings"
  13. "time"
  14. "xingjia-management-gateway/apis"
  15. "xingjia-management-gateway/consts"
  16. "xingjia-management-gateway/errors"
  17. "xingjia-management-gateway/impl/v1/local_objs"
  18. "xingjia-management-gateway/impl/v1/operation_log"
  19. dbmodel "xingjia-management-gateway/model"
  20. )
  21. func checkJtContentAddParam(req *apis.JtContentAddRequest) error {
  22. switch {
  23. case consts.JtContentTable[req.ContentType] == "":
  24. return status.Error(10003, "未知内容类型")
  25. case req.SelfId == 0:
  26. return status.Error(10003, "未知操作者")
  27. case req.PublishStatus != 1 && req.PublishStatus != 2:
  28. return status.Error(10003, "请选择是否发布")
  29. }
  30. return nil
  31. }
  32. func getJtContent(p dbmodel.TJtContent) string {
  33. publishStatus := "发布"
  34. if p.PublishStatus != 1 {
  35. publishStatus = "暂不发布"
  36. }
  37. m := map[string]interface{}{
  38. "标题": p.Title,
  39. "内容": p.Content,
  40. "发布状态": publishStatus,
  41. }
  42. bytes, _ := json.Marshal(m)
  43. return string(bytes)
  44. }
  45. //
  46. func JtContentAdd(ctx context.Context, req *apis.JtContentAddRequest) (reply *apis.JtContentAddReply, err error) {
  47. reply = &apis.JtContentAddReply{}
  48. // 捕获各个task中的异常并返回给调用者
  49. defer func() {
  50. if r := recover(); r != nil {
  51. err = fmt.Errorf("%+v", r)
  52. e := &status.Status{}
  53. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  54. logger.Error("err",
  55. zap.String("system_err", err.Error()),
  56. zap.Stack("stacktrace"))
  57. }
  58. }
  59. }()
  60. if err = checkJtContentAddParam(req); err != nil {
  61. return nil, err
  62. }
  63. now := time.Now()
  64. p := dbmodel.NewJtContent(consts.JtContentTable[req.ContentType])
  65. p.Content = req.Content
  66. p.PublishStatus = req.PublishStatus
  67. p.FirstPics = strings.Join(req.FirstPics, ";")
  68. p.CreatedAt = now
  69. p.UpdatedAt = now
  70. p.Title = req.Title
  71. db := database.DB().Begin()
  72. err = p.Insert(db)
  73. defer func() {
  74. if err != nil {
  75. db.Rollback()
  76. }
  77. }()
  78. if err != nil {
  79. return nil, errors.DataBaseError
  80. }
  81. // 操作日志
  82. content := getJtContent(*p)
  83. logReq := &apis.OperationLogAddRequest{
  84. OriginContent: "",
  85. TargetContent: content,
  86. Uid: req.SelfId,
  87. User: req.SelfName,
  88. Module: int32(req.ContentType),
  89. OperationTime: now.Unix(),
  90. Operation: consts.OperationActionAdd,
  91. }
  92. err = operation_log.OperationLogAddHandle(logReq, db)
  93. if err != nil {
  94. return nil, err
  95. }
  96. inList := req.FirstPics
  97. array := local_objs.GetObjFromText(req.Content)
  98. inList = append(inList, array...)
  99. err = local_objs.InAndOut(db, inList, []string{})
  100. if err != nil {
  101. return nil, err
  102. }
  103. db.Commit()
  104. return reply, nil
  105. }