P02.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package query
  2. import (
  3. "context"
  4. "sort"
  5. jsoniter "github.com/json-iterator/go"
  6. "gd_adm_data/apis"
  7. "gd_adm_data/errors"
  8. "gd_adm_data/model"
  9. "gd_adm_data/utils"
  10. "git.getensh.com/common/gopkgsv2/database"
  11. "gorm.io/gorm"
  12. )
  13. type SeriesListRequest struct {
  14. BrandID string `json:"brand_id"`
  15. }
  16. type seriesItem struct {
  17. BrandID string `json:"brand_id"`
  18. SeriesID string `json:"series_id"`
  19. SeriesName string `json:"series_name"`
  20. SeriesImg string `json:"series_img"`
  21. }
  22. type seriesList struct {
  23. Maker string `json:"maker"`
  24. SeriesList []seriesItem `json:"series_list"`
  25. }
  26. type SeriesListResponse struct {
  27. List []seriesList `json:"list"`
  28. }
  29. func P02(ctx context.Context, params string) (reply *apis.QueryResponse, err error) {
  30. reply = &apis.QueryResponse{}
  31. var req SeriesListRequest
  32. err = jsoniter.UnmarshalFromString(params, &req)
  33. if err != nil && req.BrandID == "" {
  34. return nil, errors.ParamsError
  35. }
  36. list, err := model.NewSySerieModel().SeriesList(database.DB().Where("brand_id = ? AND status = 1", req.BrandID))
  37. if err != nil && err != gorm.ErrRecordNotFound {
  38. return reply, errors.SystemError
  39. }
  40. if err == gorm.ErrRecordNotFound {
  41. return nil, errors.DataNotExistError
  42. }
  43. seriesMap := make(map[string][]seriesItem)
  44. for k := range list {
  45. series := seriesItem{
  46. BrandID: list[k].BrandId,
  47. SeriesID: list[k].SeriesId,
  48. SeriesName: list[k].SeriesName,
  49. SeriesImg: utils.GetSeriesLogoById(list[k].SeriesId, list[k].HasImg),
  50. }
  51. seriesMap[list[k].Maker] = append(seriesMap[list[k].Maker], series)
  52. }
  53. var keys []string
  54. for k := range seriesMap {
  55. keys = append(keys, k)
  56. }
  57. sort.Strings(keys)
  58. res := SeriesListResponse{}
  59. for _, v := range keys {
  60. si := seriesList{
  61. Maker: v,
  62. SeriesList: seriesMap[v],
  63. }
  64. res.List = append(res.List, si)
  65. }
  66. reply.Data, _ = jsoniter.MarshalToString(res)
  67. return reply, nil
  68. }