neighbor_article_add.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package neighbor
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "gorm.io/gorm"
  9. "property-garden/errors"
  10. "property-garden/impl/v1/oss_utils"
  11. dbmodel "property-garden/model"
  12. pb_v1 "property-garden/pb/v1"
  13. "property-garden/utils"
  14. "strings"
  15. "time"
  16. "git.getensh.com/common/gopkgs/database"
  17. "git.getensh.com/common/gopkgs/logger"
  18. "go.uber.org/zap"
  19. "google.golang.org/grpc/status"
  20. )
  21. func checkNeighborArticleAddParam(req *pb_v1.NeighborArticleAddRequest) error {
  22. switch {
  23. case req.GardenId == 0:
  24. return status.Error(10003, "小区不能为空")
  25. case req.ClassId == 0:
  26. return status.Error(10003, "分类id不能为空")
  27. case len(req.Title) > 128:
  28. return status.Error(10003, "标题过长")
  29. case req.Content == "" && len(req.Pics) == 0:
  30. return status.Error(10003, "内容不能为空")
  31. case req.Uid == 0:
  32. return status.Error(10003, "uid 不能为空")
  33. }
  34. return nil
  35. }
  36. func NeighborArticleAdd(ctx context.Context, req *pb_v1.NeighborArticleAddRequest) (reply *pb_v1.NeighborArticleAddReply, err error) {
  37. reply = &pb_v1.NeighborArticleAddReply{}
  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. // 参数检查
  51. err = checkNeighborArticleAddParam(req)
  52. if err != nil {
  53. return nil, err
  54. }
  55. now := time.Now()
  56. dbname := utils.GetGardenDbName(req.GardenId)
  57. class := dbmodel.NewNeighborClass(dbname)
  58. where := map[string]interface{}{
  59. "id": req.ClassId,
  60. }
  61. err = class.Find(database.DB(), where)
  62. if err != nil && err != gorm.ErrRecordNotFound {
  63. return nil, errors.DataBaseError
  64. }
  65. if class.ID == 0 {
  66. return nil, status.Error(10003, "分类不存在")
  67. }
  68. db := database.DB().Begin()
  69. p := dbmodel.TNeighborArticle{
  70. Title: req.Title,
  71. ClassId: req.ClassId,
  72. Content: req.Content,
  73. Pics: utils.StringJoin(req.Pics, ";"),
  74. UpdatedAt: now,
  75. CreatedAt: now,
  76. UserIcon: req.UserIcon,
  77. Uid: req.Uid,
  78. NickName: req.NickName,
  79. }
  80. p.SetTable(dbname)
  81. err = p.Insert(db)
  82. if err != nil {
  83. db.Rollback()
  84. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  85. return nil, status.Error(10003, "分类名已存在")
  86. }
  87. return nil, errors.DataBaseError
  88. }
  89. values := map[string]interface{}{
  90. "article_count": gorm.Expr("article_count + ?", 1),
  91. }
  92. if err = class.Update(db, where, values); err != nil {
  93. db.Rollback()
  94. return nil, errors.DataBaseError
  95. }
  96. if err := oss_utils.OssObjAdd(req.Pics, nil); err != nil {
  97. db.Rollback()
  98. return nil, err
  99. }
  100. db.Commit()
  101. return reply, nil
  102. }