list.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package brand
  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.BrandListRequest) (reply *v1.BrandListReply, err error) {
  18. reply = &v1.BrandListReply{}
  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. db := database.DB()
  32. if req.BrandName != "" {
  33. db = db.Where("brand_name = ?", req.BrandName)
  34. }
  35. if req.BrandId != "" {
  36. db = db.Where("brand_id = ?", req.BrandId)
  37. }
  38. if req.Initial != "" {
  39. db = db.Where("initial = ?", req.Initial)
  40. }
  41. if req.OldBrandName != ""{
  42. db = db.Where("old_brand_name like ?", "%"+req.OldBrandName+"%")
  43. //db = db.Where("old_brand_name = ?", req.OldBrandName)
  44. }
  45. if req.HasImg == -1 {
  46. db = db.Where("has_img = 0")
  47. } else if req.HasImg == 1 {
  48. db = db.Where("has_img = 1")
  49. }
  50. if req.Status == -1 {
  51. db = db.Where("status = 0")
  52. } else if req.Status == 1 {
  53. db = db.Where("status = 1")
  54. }
  55. pageSize := consts.PageSize
  56. if req.PageSize != 0 {
  57. pageSize = int(req.PageSize)
  58. }
  59. // 构造分页类
  60. pagination := model.NewPagination(int(req.Page), pageSize, 0).GetLimitOffset()
  61. count, err := model.NewSyBrand().Count(db)
  62. if err != nil && err != gorm.ErrRecordNotFound {
  63. return reply, errors.SystemError
  64. }
  65. if err == gorm.ErrRecordNotFound || count == 0 {
  66. return reply, nil
  67. }
  68. list, err := model.NewSyBrand().List(db, pagination)
  69. if err != nil && err != gorm.ErrRecordNotFound {
  70. return reply, errors.SystemError
  71. }
  72. if err == gorm.ErrRecordNotFound {
  73. return reply, nil
  74. }
  75. reply.List = make([]*v1.BrandList, 0, len(list))
  76. for _, v := range list {
  77. reply.List = append(reply.List, &v1.BrandList{
  78. Id: v.ID,
  79. Initial: v.Initial,
  80. BrandName: v.BrandName,
  81. BrandId: v.BrandId,
  82. Weight: v.Weight,
  83. HasImg: v.HasImg,
  84. Status: v.Status,
  85. Image: utils.GetBrandLogoById(v.BrandId, int32(v.HasImg)),
  86. OldBrandName:v.OldBrandName,
  87. })
  88. }
  89. // 计算分页结果
  90. pagination.CalPage(int(count))
  91. reply.CurrentPage = int64(pagination.CurrentPage)
  92. reply.PerPage = int64(pagination.PerPage)
  93. reply.Total = int64(pagination.Total)
  94. reply.FirstPage = int64(pagination.FirstPage)
  95. reply.LastPage = int64(pagination.LastPage)
  96. reply.PrevPage = int64(pagination.PrevPage)
  97. reply.NextPage = int64(pagination.NextPage)
  98. return reply, err
  99. }