list.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. "xingjia-management-gateway/apis"
  14. "xingjia-management-gateway/consts"
  15. "xingjia-management-gateway/errors"
  16. dbmodel "xingjia-management-gateway/model"
  17. )
  18. func checkJtContentListParam(req *apis.JtContentListRequest) error {
  19. switch {
  20. case consts.JtContentTable[req.ContentType] == "":
  21. return status.Error(10003, "未知内容类型")
  22. }
  23. if req.Page == 0 {
  24. req.Page = 1
  25. }
  26. if req.PageSize == 0 {
  27. req.PageSize = 10
  28. }
  29. return nil
  30. }
  31. //
  32. func JtContentList(ctx context.Context, req *apis.JtContentListRequest) (reply *apis.JtContentListReply, err error) {
  33. reply = &apis.JtContentListReply{}
  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 = checkJtContentListParam(req); err != nil {
  47. return nil, err
  48. }
  49. p := dbmodel.NewJtContent(consts.JtContentTable[req.ContentType])
  50. reply.Page = req.Page
  51. reply.Total, err = p.Count(database.DB(), nil, nil)
  52. if err != nil {
  53. return nil, errors.DataBaseError
  54. }
  55. if reply.Total == 0 {
  56. return reply, nil
  57. }
  58. list, err := p.List(database.DB(), nil, nil, int(req.Page), int(req.PageSize))
  59. if err != nil {
  60. return nil, errors.DataBaseError
  61. }
  62. reply.List = make([]*apis.JtContentItem, len(list))
  63. for i, v := range list {
  64. item := &apis.JtContentItem{
  65. Id: v.ID,
  66. CreatedAt: v.UpdatedAt.Unix(),
  67. Title: v.Title,
  68. Content: v.Content,
  69. FirstPics: []string{},
  70. PublishStatus: v.PublishStatus,
  71. }
  72. array := strings.Split(v.FirstPics, ";")
  73. if len(array) > 0 && array[0] != "" {
  74. item.FirstPics = array
  75. } else {
  76. item.FirstPics = make([]string, 0)
  77. }
  78. reply.List[i] = item
  79. }
  80. return reply, nil
  81. }