list.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package application
  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-system/errors"
  13. dbmodel "property-system/model"
  14. pb_v1 "property-system/pb/v1"
  15. )
  16. func checkGardenApplicationListParam(req *pb_v1.GardenApplicationListRequest) error {
  17. if req.PageSize == 0 {
  18. req.PageSize = 10
  19. }
  20. if req.Page == 0 {
  21. req.Page = 1
  22. }
  23. return nil
  24. }
  25. //
  26. func GardenApplicationList(ctx context.Context, req *pb_v1.GardenApplicationListRequest) (reply *pb_v1.GardenApplicationListReply, err error) {
  27. reply = &pb_v1.GardenApplicationListReply{}
  28. // 捕获各个task中的异常并返回给调用者
  29. defer func() {
  30. if r := recover(); r != nil {
  31. err = fmt.Errorf("%+v", r)
  32. e := &status.Status{}
  33. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  34. logger.Error("err",
  35. zap.String("system_err", err.Error()),
  36. zap.Stack("stacktrace"))
  37. }
  38. }
  39. }()
  40. err = checkGardenApplicationListParam(req)
  41. if err != nil {
  42. return nil, err
  43. }
  44. p := dbmodel.TApplicationOrder{}
  45. where := map[string]interface{}{}
  46. if req.GardenId > 0 {
  47. where["garden_id"] = req.GardenId
  48. }
  49. if req.Status > 0 {
  50. where["status"] = req.Status
  51. }
  52. reply.Total, err = p.Count(database.DB(), where, nil)
  53. if err != nil {
  54. return nil, errors.DataBaseError
  55. }
  56. reply.Page = req.Page
  57. if reply.Total == 0 {
  58. return reply, nil
  59. }
  60. list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  61. if err != nil {
  62. return nil, errors.DataBaseError
  63. }
  64. gardenIds := []int64{}
  65. garden := dbmodel.TGardenApproved{}
  66. for _, v := range list {
  67. gardenIds = append(gardenIds, v.GardenId)
  68. }
  69. where = map[string]interface{}{
  70. "id in": gardenIds,
  71. }
  72. glist, err := garden.List(database.DB(), where, nil, -1, -1)
  73. if err != nil {
  74. return nil, errors.DataBaseError
  75. }
  76. gardenM := map[int64]string{}
  77. for _, v := range glist {
  78. gardenM[v.ID] = v.GardenName
  79. }
  80. reply.List = make([]*pb_v1.GardenApplicationItem, len(list))
  81. for i, v := range list {
  82. application := pb_v1.ApplicationInfo{}
  83. json.Unmarshal([]byte(v.ApplicationContent), &application)
  84. item := &pb_v1.GardenApplicationItem{
  85. Icon: application.Icon,
  86. Desc: application.Desc,
  87. Name: application.Name,
  88. Content: application.Content,
  89. Amount: v.Amount,
  90. Enable: application.Enable,
  91. ApplicationId: v.ApplicationId,
  92. OrderId: v.ID,
  93. Status: v.Status,
  94. PayType: v.PayType,
  95. Feedback: v.Feedback,
  96. GardenName: gardenM[v.GardenId],
  97. CreatedAt: v.CreatedAt.Unix(),
  98. }
  99. reply.List[i] = item
  100. }
  101. return reply, nil
  102. }