list.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package material
  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-common/errors"
  13. dbmodel "property-common/model"
  14. pb_v1 "property-common/pb/v1"
  15. )
  16. //
  17. func MaterialList(ctx context.Context, req *pb_v1.MaterialListRequest) (reply *pb_v1.MaterialListReply, err error) {
  18. // 捕获各个task中的异常并返回给调用者
  19. defer func() {
  20. if r := recover(); r != nil {
  21. err = fmt.Errorf("%+v", r)
  22. e := &status.Status{}
  23. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  24. logger.Error("err",
  25. zap.String("system_err", err.Error()),
  26. zap.Stack("stacktrace"))
  27. }
  28. }
  29. }()
  30. reply = &pb_v1.MaterialListReply{}
  31. p := &dbmodel.TMaterial{}
  32. if req.Page == 0 {
  33. req.Page = 1
  34. }
  35. if req.PageSize == 0 {
  36. req.PageSize = 10
  37. }
  38. reply.Page = req.Page
  39. where := map[string]interface{}{}
  40. if len(req.Mtypes) > 0 {
  41. where["mtype in"] = req.Mtypes
  42. }
  43. reply.Total, err = p.Count(database.DB(), where, nil)
  44. if err != nil {
  45. return nil, errors.DataBaseError
  46. }
  47. if reply.Total == 0 {
  48. return reply, nil
  49. }
  50. list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  51. if err != nil {
  52. return nil, errors.DataBaseError
  53. }
  54. reply.List = make([]*pb_v1.MaterialItem, len(list))
  55. for i, v := range list {
  56. item := &pb_v1.MaterialItem{
  57. Id: v.ID,
  58. Enable: v.Enable,
  59. Mtype: v.Mtype,
  60. Pic: v.Pic,
  61. CreatedAt: v.CreatedAt.Unix(),
  62. }
  63. reply.List[i] = item
  64. }
  65. return reply, nil
  66. }