list.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package style
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "adm-vehicle-style/consts"
  7. "adm-vehicle-style/errors"
  8. "adm-vehicle-style/model"
  9. v1 "adm-vehicle-style/pb/v1"
  10. "git.getensh.com/common/gopkgsv2/database"
  11. "git.getensh.com/common/gopkgsv2/logger"
  12. "go.uber.org/zap"
  13. "google.golang.org/grpc/status"
  14. "gorm.io/gorm"
  15. )
  16. func List(ctx context.Context, req *v1.StyleListRequest) (reply *v1.StyleListReply, err error) {
  17. reply = &v1.StyleListReply{}
  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. db := database.DB()
  31. if req.BrandName != "" {
  32. db = db.Where("brand_name = ?", req.BrandName)
  33. }
  34. if req.SeriesName != "" {
  35. db = db.Where("series_name = ?", req.SeriesName)
  36. }
  37. if req.Maker != "" {
  38. db = db.Where("maker = ?", req.Maker)
  39. }
  40. if req.ModelYear != 0 {
  41. db = db.Where("model_year = ?", req.ModelYear)
  42. }
  43. if req.StyleName != "" {
  44. db = db.Where("style_name like ?", "%"+req.StyleName+"%")
  45. }
  46. if req.BrandId != "" {
  47. db = db.Where("brand_id = ?", req.BrandId)
  48. }
  49. if req.SeriesId != "" {
  50. db = db.Where("series_id = ?", req.SeriesId)
  51. }
  52. if req.StyleId != "" {
  53. db = db.Where("t1.style_id = ?", req.StyleId)
  54. }
  55. switch req.Status {
  56. case -1:
  57. db = db.Where("is_on = 0")
  58. case 1:
  59. db = db.Where("is_on = 1")
  60. }
  61. var join bool
  62. switch req.HasManual {
  63. case -1:
  64. db = db.Where("t2.id is null")
  65. join = true
  66. case 1:
  67. db = db.Where("t2.id <> 0")
  68. join = true
  69. }
  70. pageSize := consts.PageSize
  71. if req.PageSize != 0 {
  72. pageSize = int(req.PageSize)
  73. }
  74. // 构造分页类
  75. pagination := model.NewPagination(int(req.Page), pageSize, 0).GetLimitOffset()
  76. count, err := model.NewSyStyleModel().Count(db, join)
  77. if err != nil && err != gorm.ErrRecordNotFound {
  78. return reply, errors.SystemError
  79. }
  80. if err == gorm.ErrRecordNotFound || count == 0 {
  81. return reply, nil
  82. }
  83. list, err := model.NewSyStyleModel().List(db, pagination, join)
  84. if err != nil && err != gorm.ErrRecordNotFound {
  85. return reply, errors.SystemError
  86. }
  87. if err == gorm.ErrRecordNotFound {
  88. return reply, nil
  89. }
  90. reply.List = make([]*v1.StyleList, 0, len(list))
  91. for _, v := range list {
  92. reply.List = append(reply.List, &v1.StyleList{
  93. Id: v.ID,
  94. StyleId: v.StyleId,
  95. BrandName: v.BrandName,
  96. Maker: v.Maker,
  97. SeriesName: v.SeriesName,
  98. StyleName: v.StyleName,
  99. Status: v.IsOn,
  100. MaintainId: v.MaintainId,
  101. SeriesId: v.SeriesId,
  102. BrandId: v.BrandId,
  103. })
  104. }
  105. // 计算分页结果
  106. pagination.CalPage(int(count))
  107. reply.CurrentPage = int64(pagination.CurrentPage)
  108. reply.PerPage = int64(pagination.PerPage)
  109. reply.Total = int64(pagination.Total)
  110. reply.FirstPage = int64(pagination.FirstPage)
  111. reply.LastPage = int64(pagination.LastPage)
  112. reply.PrevPage = int64(pagination.PrevPage)
  113. reply.NextPage = int64(pagination.NextPage)
  114. return reply, nil
  115. }