package query import ( "context" "sort" jsoniter "github.com/json-iterator/go" "gd_adm_data/apis" "gd_adm_data/errors" "gd_adm_data/model" "git.getensh.com/common/gopkgsv2/database" "gorm.io/gorm" ) type StyleListRequest struct { SeriesID string `json:"series_id"` } type styleItem struct { BrandID string `json:"brand_id"` Maker string `json:"maker"` ModelYear string `json:"model_year"` Price string `json:"price"` SeriesID string `json:"series_id"` StyleID string `json:"style_id"` StyleName string `json:"style_name"` } type styleList struct { ModelYear string `json:"model_year"` StyleList []styleItem `json:"style_list"` } type StyleListResponse struct { List []styleList `json:"list"` } func P03(ctx context.Context, params string) (reply *apis.QueryResponse, err error) { reply = &apis.QueryResponse{} var req StyleListRequest err = jsoniter.UnmarshalFromString(params, &req) if err != nil && req.SeriesID == "" { return nil, errors.ParamsError } list, err := model.NewSyStyleModel(). 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) if err != nil && err != gorm.ErrRecordNotFound { return reply, errors.SystemError } if err == gorm.ErrRecordNotFound { return nil, errors.DataNotExistError } yearMap := make(map[string][]styleItem) for _, v := range list { yearMap[v.ModelYear] = append(yearMap[v.ModelYear], styleItem{ BrandID: v.BrandId, Maker: v.Maker, ModelYear: v.ModelYear, Price: v.Price, SeriesID: v.SeriesId, StyleID: v.StyleId, StyleName: v.StyleName, }) } var keys []string keyLen := 0 for k := range yearMap { keys = append(keys, k) keyLen++ } sort.Strings(keys) res := StyleListResponse{} for i := keyLen - 1; i >= 0; i-- { st := styleList{ ModelYear: keys[i], StyleList: yearMap[keys[i]], } res.List = append(res.List, st) } reply.Data, _ = jsoniter.MarshalToString(res) return reply, nil }