package query import ( "context" "encoding/json" "fmt" "adm-vehicle-style/errors" "adm-vehicle-style/model" v1 "adm-vehicle-style/pb/v1" "adm-vehicle-style/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 BrandItem struct { BrandID string `json:"brand_id"` BrandName string `json:"brand_name"` Logo string `json:"logo"` BrandNamePrex string `json:"brand_name_prex"` } type BrandListResponse struct { List []BrandItem `json:"list"` } func BrandList(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")) } } }() // 构造分页类 pagination := model.NewPagination(0, 1000, 0).GetLimitOffset() list, err := model.NewSyBrand().List(database.DB().Where("status = 1"), pagination) if err != nil && err != gorm.ErrRecordNotFound { return reply, errors.SystemError } if err == gorm.ErrRecordNotFound { return reply, nil } res := BrandListResponse { List: make([]BrandItem, 0, len(list)), } for _, v := range list { res.List = append(res.List, BrandItem{ BrandID: v.BrandId, BrandName: v.BrandName, Logo: utils.GetBrandLogoById(v.BrandId, int32(v.HasImg)), BrandNamePrex: v.Initial, }) } reply.Data, _ = jsoniter.MarshalToString(res) return reply, nil }