list.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package series
  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. "adm-vehicle-style/utils"
  11. "git.getensh.com/common/gopkgsv2/database"
  12. "git.getensh.com/common/gopkgsv2/logger"
  13. "go.uber.org/zap"
  14. "google.golang.org/grpc/status"
  15. "gorm.io/gorm"
  16. )
  17. func List(ctx context.Context, req *v1.SeriesListRequest) (reply *v1.SeriesListReply, err error) {
  18. reply = &v1.SeriesListReply{}
  19. // 捕获各个task中的异常并返回给调用者
  20. defer func() {
  21. if r := recover(); r != nil {
  22. err = fmt.Errorf("%+v", r)
  23. e := &status.Status{}
  24. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  25. logger.Error("err",
  26. zap.String("system_err", err.Error()),
  27. zap.Stack("stacktrace"))
  28. }
  29. }
  30. }()
  31. var join bool
  32. db := database.DB()
  33. if req.BrandName != "" {
  34. db = db.Where("brand_name = ?", req.BrandName)
  35. join = true
  36. }
  37. if req.BrandId != "" {
  38. db = db.Where("t1.brand_id = ?", req.BrandId)
  39. join = true
  40. }
  41. if req.OldSeriesName != ""{
  42. db = db.Where("old_series_name like ?", "%"+req.OldSeriesName+"%")
  43. //db = db.Where("old_series_name = ?", req.OldSeriesName)
  44. }
  45. if req.SeriesName != "" {
  46. db = db.Where("series_name = ?", req.SeriesName)
  47. }
  48. if req.SeriesId != "" {
  49. db = db.Where("series_id = ?", req.SeriesId)
  50. }
  51. if req.HasImg == -1 {
  52. db = db.Where("t1.has_img = 0")
  53. } else if req.HasImg == 1 {
  54. db = db.Where("t1.has_img = 1")
  55. }
  56. if req.Status == -1 {
  57. db = db.Where("t1.status = 0")
  58. } else if req.Status == 1 {
  59. db = db.Where("t1.status = 1")
  60. }
  61. pageSize := consts.PageSize
  62. if req.PageSize != 0 {
  63. pageSize = int(req.PageSize)
  64. }
  65. // 构造分页类
  66. pagination := model.NewPagination(int(req.Page), pageSize, 0).GetLimitOffset()
  67. count, err := model.NewSySerieModel().Count(db, join)
  68. if err != nil && err != gorm.ErrRecordNotFound {
  69. return reply, errors.SystemError
  70. }
  71. if err == gorm.ErrRecordNotFound || count == 0 {
  72. return reply, nil
  73. }
  74. list, err := model.NewSySerieModel().List(db, pagination, join)
  75. if err != nil && err != gorm.ErrRecordNotFound {
  76. return reply, errors.SystemError
  77. }
  78. if err == gorm.ErrRecordNotFound {
  79. return reply, nil
  80. }
  81. reply.List = make([]*v1.SeriesList, 0, len(list))
  82. for _, v := range list {
  83. reply.List = append(reply.List, &v1.SeriesList{
  84. Id: v.ID,
  85. BrandName: v.BrandName,
  86. SeriesId: v.SeriesId,
  87. Maker: v.Maker,
  88. SeriesName: v.SeriesName,
  89. Status: v.Status,
  90. Image: utils.GetSeriesLogoById(v.SeriesId, v.HasImg),
  91. BrandId: v.BrandId,
  92. OldSeriesName:v.OldSeriesName,
  93. })
  94. }
  95. // 计算分页结果
  96. pagination.CalPage(int(count))
  97. reply.CurrentPage = int64(pagination.CurrentPage)
  98. reply.PerPage = int64(pagination.PerPage)
  99. reply.Total = int64(pagination.Total)
  100. reply.FirstPage = int64(pagination.FirstPage)
  101. reply.LastPage = int64(pagination.LastPage)
  102. reply.PrevPage = int64(pagination.PrevPage)
  103. reply.NextPage = int64(pagination.NextPage)
  104. return reply, err
  105. }