package query import ( "context" "encoding/json" "fmt" "sort" "adm-data/errors" "adm-data/model" v1 "adm-data/pb/v1" "adm-data/utils" jsoniter "github.com/json-iterator/go" "git.getensh.com/common/gopkgsv2/database" "git.getensh.com/common/gopkgsv2/logger" "go.uber.org/zap" "google.golang.org/grpc/status" "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 SeriesList(ctx context.Context, params string) (reply *v1.QueryResponse, err error) { reply = &v1.QueryResponse{} // 捕获各个task中的异常并返回给调用者 defer func() { if r := recover(); r != nil { err = fmt.Errorf("%+v", r) e := &status.Status{} if er := json.Unmarshal([]byte(err.Error()), e); er != nil { logger.Error("err", zap.String("system_err", err.Error()), zap.Stack("stacktrace")) } } }() 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 }