style.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. jsoniter "github.com/json-iterator/go"
  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. type StyleListRequest struct {
  18. SeriesID string `json:"series_id"`
  19. }
  20. type styleItem struct {
  21. BrandID string `json:"brand_id"`
  22. Maker string `json:"maker"`
  23. ModelYear string `json:"model_year"`
  24. Price string `json:"price"`
  25. SeriesID string `json:"series_id"`
  26. StyleID string `json:"style_id"`
  27. StyleName string `json:"style_name"`
  28. }
  29. type styleList struct {
  30. ModelYear string `json:"model_year"`
  31. StyleList []styleItem `json:"style_list"`
  32. }
  33. type StyleListResponse struct {
  34. List []styleList `json:"list"`
  35. }
  36. func StyleList(ctx context.Context, params string) (reply *v1.QueryResponse, err error) {
  37. reply = &v1.QueryResponse{}
  38. // 捕获各个task中的异常并返回给调用者
  39. defer func() {
  40. if r := recover(); r != nil {
  41. err = fmt.Errorf("%+v", r)
  42. e := &status.Status{}
  43. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  44. logger.Error("err",
  45. zap.String("system_err", err.Error()),
  46. zap.Stack("stacktrace"))
  47. }
  48. }
  49. }()
  50. var req StyleListRequest
  51. err = jsoniter.UnmarshalFromString(params, &req)
  52. if err != nil || req.SeriesID == "" {
  53. return nil, errors.ParamsError
  54. }
  55. list, err := model.NewSyStyleModel().
  56. StyleList(database.DB().Where("series_id = ? AND is_on = 1", req.SeriesID), "brand_id, maker, model_year, price, series_id, style_id, style_name")
  57. if err != nil && err != gorm.ErrRecordNotFound {
  58. return reply, errors.SystemError
  59. }
  60. if err == gorm.ErrRecordNotFound {
  61. return reply, nil
  62. }
  63. yearMap := make(map[string][]styleItem)
  64. for _, v := range list {
  65. yearMap[v.ModelYear] = append(yearMap[v.ModelYear], styleItem{
  66. BrandID: v.BrandId,
  67. Maker: v.Maker,
  68. ModelYear: v.ModelYear,
  69. Price: v.Price,
  70. SeriesID: v.SeriesId,
  71. StyleID: v.StyleId,
  72. StyleName: v.StyleName,
  73. })
  74. }
  75. var keys []string
  76. keyLen := 0
  77. for k := range yearMap {
  78. keys = append(keys, k)
  79. keyLen++
  80. }
  81. sort.Strings(keys)
  82. res := StyleListResponse{}
  83. for i := keyLen - 1; i >= 0; i-- {
  84. st := styleList{
  85. ModelYear: keys[i],
  86. StyleList: yearMap[keys[i]],
  87. }
  88. res.List = append(res.List, st)
  89. }
  90. reply.Data, _ = jsoniter.MarshalToString(res)
  91. return reply, nil
  92. }