list.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package system_msg
  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. "property-garden/errors"
  13. dbmodel "property-garden/model"
  14. pb_v1 "property-garden/pb/v1"
  15. "property-garden/utils"
  16. )
  17. //
  18. func SystemMsgList(ctx context.Context, req *pb_v1.SystemMsgListRequest) (reply *pb_v1.SystemMsgListReply, err error) {
  19. reply = &pb_v1.SystemMsgListReply{}
  20. // 捕获各个task中的异常并返回给调用者
  21. defer func() {
  22. if r := recover(); r != nil {
  23. err = fmt.Errorf("%+v", r)
  24. e := &status.Status{}
  25. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  26. logger.Error("err",
  27. zap.String("system_err", err.Error()),
  28. zap.Stack("stacktrace"))
  29. }
  30. }
  31. }()
  32. if req.GardenId < 1 {
  33. return nil, errors.ParamsError
  34. }
  35. if req.Uid < 1 {
  36. return nil, status.Error(10003, "uid不能为空")
  37. }
  38. if req.Page == 0 {
  39. req.Page = 1
  40. }
  41. if req.PageSize == 0 {
  42. req.PageSize = 10
  43. }
  44. dbname := utils.GetGardenDbName(req.GardenId)
  45. p := dbmodel.NewMsg(dbname)
  46. where := map[string]interface{}{
  47. fmt.Sprintf("system_uid=%d or system_uid=0", req.Uid): "",
  48. }
  49. reply.Page = req.Page
  50. reply.Total, err = p.Count(database.DB(), where, nil)
  51. if err != nil {
  52. return nil, errors.DataBaseError
  53. }
  54. if reply.Total == 0 {
  55. return reply, nil
  56. }
  57. list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  58. if err != nil {
  59. return nil, errors.DataBaseError
  60. }
  61. reply.List = make([]*pb_v1.SystemMsgItem, len(list))
  62. for i, v := range list {
  63. reply.List[i] = &pb_v1.SystemMsgItem{
  64. Id: v.ID,
  65. Content: v.Content,
  66. Code: v.Code,
  67. CreatedAt: v.CreatedAt.Format("2006-01-02 15:04:05"),
  68. }
  69. }
  70. return reply, nil
  71. }