P03.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. "git.getensh.com/common/gopkgsv2/database"
  10. "gorm.io/gorm"
  11. )
  12. type StyleListRequest struct {
  13. SeriesID string `json:"series_id"`
  14. }
  15. type styleItem struct {
  16. BrandID string `json:"brand_id"`
  17. Maker string `json:"maker"`
  18. ModelYear string `json:"model_year"`
  19. Price string `json:"price"`
  20. SeriesID string `json:"series_id"`
  21. StyleID string `json:"style_id"`
  22. StyleName string `json:"style_name"`
  23. }
  24. type styleList struct {
  25. ModelYear string `json:"model_year"`
  26. StyleList []styleItem `json:"style_list"`
  27. }
  28. type StyleListResponse struct {
  29. List []styleList `json:"list"`
  30. }
  31. func P03(ctx context.Context, params string) (reply *apis.QueryResponse, err error) {
  32. reply = &apis.QueryResponse{}
  33. var req StyleListRequest
  34. err = jsoniter.UnmarshalFromString(params, &req)
  35. if err != nil && req.SeriesID == "" {
  36. return nil, errors.ParamsError
  37. }
  38. list, err := model.NewSyStyleModel().
  39. StyleList(database.DB().Where("series_id = ? AND is_on = 1", req.SeriesID), "brand_id, maker, model_year, price, series_id, style_id, style_name", 1000)
  40. if err != nil && err != gorm.ErrRecordNotFound {
  41. return reply, errors.SystemError
  42. }
  43. if err == gorm.ErrRecordNotFound {
  44. return nil, errors.DataNotExistError
  45. }
  46. yearMap := make(map[string][]styleItem)
  47. for _, v := range list {
  48. yearMap[v.ModelYear] = append(yearMap[v.ModelYear], styleItem{
  49. BrandID: v.BrandId,
  50. Maker: v.Maker,
  51. ModelYear: v.ModelYear,
  52. Price: v.Price,
  53. SeriesID: v.SeriesId,
  54. StyleID: v.StyleId,
  55. StyleName: v.StyleName,
  56. })
  57. }
  58. var keys []string
  59. keyLen := 0
  60. for k := range yearMap {
  61. keys = append(keys, k)
  62. keyLen++
  63. }
  64. sort.Strings(keys)
  65. res := StyleListResponse{}
  66. for i := keyLen - 1; i >= 0; i-- {
  67. st := styleList{
  68. ModelYear: keys[i],
  69. StyleList: yearMap[keys[i]],
  70. }
  71. res.List = append(res.List, st)
  72. }
  73. reply.Data, _ = jsoniter.MarshalToString(res)
  74. return reply, nil
  75. }