update.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 checkJtContentUpdateParam(req *apis.JtContentUpdateRequest) 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. case req.Id == 0:
  29. return status.Error(10003, "目标不能为空")
  30. case req.PublishStatus != 1 && req.PublishStatus != 2:
  31. return status.Error(10003, "请选择是否发布")
  32. }
  33. return nil
  34. }
  35. //
  36. func JtContentUpdate(ctx context.Context, req *apis.JtContentUpdateRequest) (reply *apis.JtContentUpdateReply, err error) {
  37. reply = &apis.JtContentUpdateReply{}
  38. // 捕获各个task中的异常并返回给调用者
  39. defer func() {
  40. if r := recover(); r != nil {
  41. err = fmt.Errorf("%+v", r)
  42. e := &status.Status{}
  43. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  44. logger.Error("err",
  45. zap.String("system_err", err.Error()),
  46. zap.Stack("stacktrace"))
  47. }
  48. }
  49. }()
  50. if err = checkJtContentUpdateParam(req); err != nil {
  51. return nil, err
  52. }
  53. now := time.Now()
  54. old := dbmodel.NewJtContent(consts.JtContentTable[req.ContentType])
  55. where := map[string]interface{}{
  56. "id": req.Id,
  57. }
  58. err = old.Find(database.DB(), where)
  59. if err != nil && err != gorm.ErrRecordNotFound {
  60. return nil, errors.DataBaseError
  61. }
  62. if old.ID == 0 {
  63. return nil, errors.ErrRecordNotFound
  64. }
  65. values := map[string]interface{}{
  66. "first_pics": strings.Join(req.FirstPics, ";"),
  67. "content": req.Content,
  68. "publish_status": req.PublishStatus,
  69. "updated_at": time.Now(),
  70. "title": req.Title,
  71. }
  72. db := database.DB().Begin()
  73. defer func() {
  74. if err != nil {
  75. db.Rollback()
  76. }
  77. }()
  78. p := dbmodel.NewJtContent(consts.JtContentTable[req.ContentType])
  79. err = p.Update(db, where, values)
  80. if err != nil {
  81. return nil, errors.DataBaseError
  82. }
  83. p.Content = values["content"].(string)
  84. p.FirstPics = values["first_pics"].(string)
  85. // 操作日志
  86. content := getJtContent(*old)
  87. tcontent := getJtContent(*p)
  88. logReq := &apis.OperationLogAddRequest{
  89. OriginContent: content,
  90. TargetContent: tcontent,
  91. Uid: req.SelfId,
  92. User: req.SelfName,
  93. Module: int32(req.ContentType),
  94. OperationTime: now.Unix(),
  95. Operation: consts.OperationActionUpdate,
  96. }
  97. err = operation_log.OperationLogAddHandle(logReq, db)
  98. if err != nil {
  99. return nil, err
  100. }
  101. inList := req.FirstPics
  102. array := local_objs.GetObjFromText(req.Content)
  103. inList = append(inList, array...)
  104. outList := strings.Split(old.FirstPics, ";")
  105. array2 := local_objs.GetObjFromText(old.Content)
  106. outList = append(outList, array2...)
  107. err = local_objs.InAndOut(db, inList, outList)
  108. if err != nil {
  109. return nil, err
  110. }
  111. db.Commit()
  112. return reply, nil
  113. }