1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- package query
- import (
- "context"
- "sort"
- jsoniter "github.com/json-iterator/go"
- "gd_adm_data/apis"
- "gd_adm_data/errors"
- "gd_adm_data/model"
- "gd_adm_data/utils"
- "git.getensh.com/common/gopkgsv2/database"
- "gorm.io/gorm"
- )
- type SeriesListRequest struct {
- BrandID string `json:"brand_id"`
- }
- type seriesItem struct {
- BrandID string `json:"brand_id"`
- SeriesID string `json:"series_id"`
- SeriesName string `json:"series_name"`
- SeriesImg string `json:"series_img"`
- }
- type seriesList struct {
- Maker string `json:"maker"`
- SeriesList []seriesItem `json:"series_list"`
- }
- type SeriesListResponse struct {
- List []seriesList `json:"list"`
- }
- func P02(ctx context.Context, params string) (reply *apis.QueryResponse, err error) {
- reply = &apis.QueryResponse{}
- var req SeriesListRequest
- err = jsoniter.UnmarshalFromString(params, &req)
- if err != nil && req.BrandID == "" {
- return nil, errors.ParamsError
- }
- list, err := model.NewSySerieModel().SeriesList(database.DB().Where("brand_id = ? AND status = 1", req.BrandID))
- if err != nil && err != gorm.ErrRecordNotFound {
- return reply, errors.SystemError
- }
- if err == gorm.ErrRecordNotFound {
- return nil, errors.DataNotExistError
- }
- seriesMap := make(map[string][]seriesItem)
- for k := range list {
- series := seriesItem{
- BrandID: list[k].BrandId,
- SeriesID: list[k].SeriesId,
- SeriesName: list[k].SeriesName,
- SeriesImg: utils.GetSeriesLogoById(list[k].SeriesId, list[k].HasImg),
- }
- seriesMap[list[k].Maker] = append(seriesMap[list[k].Maker], series)
- }
- var keys []string
- for k := range seriesMap {
- keys = append(keys, k)
- }
- sort.Strings(keys)
- res := SeriesListResponse{}
- for _, v := range keys {
- si := seriesList{
- Maker: v,
- SeriesList: seriesMap[v],
- }
- res.List = append(res.List, si)
- }
- reply.Data, _ = jsoniter.MarshalToString(res)
- return reply, nil
- }
|