series.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package query
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "sort"
  7. "adm-vehicle-style/errors"
  8. "adm-vehicle-style/model"
  9. v1 "adm-vehicle-style/pb/v1"
  10. "adm-vehicle-style/utils"
  11. jsoniter "github.com/json-iterator/go"
  12. "git.getensh.com/common/gopkgsv2/database"
  13. "git.getensh.com/common/gopkgsv2/logger"
  14. "go.uber.org/zap"
  15. "google.golang.org/grpc/status"
  16. "gorm.io/gorm"
  17. )
  18. type SeriesListRequest struct {
  19. BrandID string `json:"brand_id"`
  20. }
  21. type seriesItem struct {
  22. BrandID string `json:"brand_id"`
  23. SeriesID string `json:"series_id"`
  24. SeriesName string `json:"series_name"`
  25. SeriesImg string `json:"series_img"`
  26. }
  27. type seriesList struct {
  28. Maker string `json:"maker"`
  29. SeriesList []seriesItem `json:"series_list"`
  30. }
  31. type SeriesListResponse struct {
  32. List []seriesList `json:"list"`
  33. }
  34. func SeriesList(ctx context.Context, params string) (reply *v1.QueryResponse, err error) {
  35. reply = &v1.QueryResponse{}
  36. // 捕获各个task中的异常并返回给调用者
  37. defer func() {
  38. if r := recover(); r != nil {
  39. err = fmt.Errorf("%+v", r)
  40. e := &status.Status{}
  41. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  42. logger.Error("err",
  43. zap.String("system_err", err.Error()),
  44. zap.Stack("stacktrace"))
  45. }
  46. }
  47. }()
  48. var req SeriesListRequest
  49. err = jsoniter.UnmarshalFromString(params, &req)
  50. if err != nil || req.BrandID == "" {
  51. return nil, errors.ParamsError
  52. }
  53. list, err := model.NewSySerieModel().SeriesList(database.DB().Where("brand_id = ? AND status = 1", req.BrandID))
  54. if err != nil && err != gorm.ErrRecordNotFound {
  55. return reply, errors.SystemError
  56. }
  57. if err == gorm.ErrRecordNotFound {
  58. return reply, nil
  59. }
  60. seriesMap := make(map[string][]seriesItem)
  61. for k := range list {
  62. series := seriesItem{
  63. BrandID: list[k].BrandId,
  64. SeriesID: list[k].SeriesId,
  65. SeriesName: list[k].SeriesName,
  66. SeriesImg: utils.GetSeriesLogoById(list[k].SeriesId, list[k].HasImg),
  67. }
  68. seriesMap[list[k].Maker] = append(seriesMap[list[k].Maker], series)
  69. }
  70. var keys []string
  71. for k := range seriesMap {
  72. keys = append(keys, k)
  73. }
  74. sort.Strings(keys)
  75. res := SeriesListResponse{}
  76. for _,v := range keys{
  77. si := seriesList{
  78. Maker: v,
  79. SeriesList: seriesMap[v],
  80. }
  81. res.List = append(res.List, si)
  82. }
  83. reply.Data, _ = jsoniter.MarshalToString(res)
  84. return reply, nil
  85. }