search.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package brand
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "adm-vehicle-style/errors"
  7. "adm-vehicle-style/model"
  8. v1 "adm-vehicle-style/pb/v1"
  9. "git.getensh.com/common/gopkgsv2/database"
  10. "git.getensh.com/common/gopkgsv2/logger"
  11. "go.uber.org/zap"
  12. "google.golang.org/grpc/status"
  13. "gorm.io/gorm"
  14. )
  15. func Search(ctx context.Context, req *v1.SearchRequest) (reply *v1.SearchReply, err error) {
  16. reply = &v1.SearchReply{}
  17. // 捕获各个task中的异常并返回给调用者
  18. defer func() {
  19. if r := recover(); r != nil {
  20. err = fmt.Errorf("%+v", r)
  21. e := &status.Status{}
  22. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  23. logger.Error("err",
  24. zap.String("system_err", err.Error()),
  25. zap.Stack("stacktrace"))
  26. }
  27. }
  28. }()
  29. db := database.DB()
  30. if req.MakerId != "" {
  31. db = db.Where("maker_id = ?", req.MakerId)
  32. }
  33. if req.SeriesId != "" {
  34. db = db.Where("series_id = ?", req.SeriesId)
  35. }
  36. pagination := model.NewPagination(0, 100000, 0).GetLimitOffset()
  37. switch req.Type {
  38. case 1:
  39. // 品牌搜索
  40. // 构造分页类
  41. if req.Search != "" {
  42. db = db.Where("brand_name like ?", "%"+req.Search+"%")
  43. }
  44. list, err := model.NewSyBrand().List(db, pagination)
  45. if err != nil && err != gorm.ErrRecordNotFound {
  46. return reply, errors.SystemError
  47. }
  48. if err == gorm.ErrRecordNotFound {
  49. return reply, nil
  50. }
  51. reply.List = make([]*v1.SearchList, 0, len(list))
  52. for _, v := range list {
  53. reply.List = append(reply.List, &v1.SearchList{
  54. Id: v.BrandId,
  55. Name: v.BrandName,
  56. })
  57. }
  58. case 2:
  59. // 厂商搜索
  60. if req.Search != "" {
  61. db = db.Where("maker like ?", "%"+req.Search+"%")
  62. }
  63. if req.BrandId != "" {
  64. db = db.Where("brand_id = ?", req.BrandId)
  65. }
  66. list, err := model.NewSySerieModel().MakerList(db)
  67. if err != nil && err != gorm.ErrRecordNotFound {
  68. return reply, errors.SystemError
  69. }
  70. if err == gorm.ErrRecordNotFound {
  71. return reply, nil
  72. }
  73. reply.List = make([]*v1.SearchList, 0, len(list))
  74. for _, v := range list {
  75. reply.List = append(reply.List, &v1.SearchList{
  76. Id: v.MakerId,
  77. Name: v.Maker,
  78. })
  79. }
  80. case 3:
  81. // 车系搜索
  82. if req.BrandId != "" {
  83. db = db.Where("t1.brand_id = ?", req.BrandId)
  84. }
  85. if req.Search != "" {
  86. db = db.Where("series_name like ?", "%"+req.Search+"%")
  87. }
  88. list, err := model.NewSySerieModel().List(db, pagination, false)
  89. if err != nil && err != gorm.ErrRecordNotFound {
  90. return reply, errors.SystemError
  91. }
  92. if err == gorm.ErrRecordNotFound {
  93. return reply, nil
  94. }
  95. reply.List = make([]*v1.SearchList, 0, len(list))
  96. for _, v := range list {
  97. reply.List = append(reply.List, &v1.SearchList{
  98. Id: v.SeriesId,
  99. Name: v.SeriesName,
  100. })
  101. }
  102. case 4:
  103. // 车型搜索
  104. if req.Search != "" {
  105. db = db.Where("style_name like ?", "%"+req.Search+"%")
  106. }
  107. list, err := model.NewSyStyleModel().List(db, pagination, false)
  108. if err != nil && err != gorm.ErrRecordNotFound {
  109. return reply, errors.SystemError
  110. }
  111. if err == gorm.ErrRecordNotFound {
  112. return reply, nil
  113. }
  114. reply.List = make([]*v1.SearchList, 0, len(list))
  115. for _, v := range list {
  116. reply.List = append(reply.List, &v1.SearchList{
  117. Id: v.StyleId,
  118. Name: v.StyleName,
  119. })
  120. }
  121. }
  122. return reply, nil
  123. }