package brand import ( "context" "encoding/json" "fmt" "adm-vehicle-style/errors" "adm-vehicle-style/model" v1 "adm-vehicle-style/pb/v1" "git.getensh.com/common/gopkgsv2/database" "git.getensh.com/common/gopkgsv2/logger" "go.uber.org/zap" "google.golang.org/grpc/status" "gorm.io/gorm" ) func Search(ctx context.Context, req *v1.SearchRequest) (reply *v1.SearchReply, err error) { reply = &v1.SearchReply{} // 捕获各个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")) } } }() db := database.DB() if req.MakerId != "" { db = db.Where("maker_id = ?", req.MakerId) } if req.SeriesId != "" { db = db.Where("series_id = ?", req.SeriesId) } pagination := model.NewPagination(0, 100000, 0).GetLimitOffset() switch req.Type { case 1: // 品牌搜索 // 构造分页类 if req.Search != "" { db = db.Where("brand_name like ?", "%"+req.Search+"%") } list, err := model.NewSyBrand().List(db, pagination) if err != nil && err != gorm.ErrRecordNotFound { return reply, errors.SystemError } if err == gorm.ErrRecordNotFound { return reply, nil } reply.List = make([]*v1.SearchList, 0, len(list)) for _, v := range list { reply.List = append(reply.List, &v1.SearchList{ Id: v.BrandId, Name: v.BrandName, }) } case 2: // 厂商搜索 if req.Search != "" { db = db.Where("maker like ?", "%"+req.Search+"%") } if req.BrandId != "" { db = db.Where("brand_id = ?", req.BrandId) } list, err := model.NewSySerieModel().MakerList(db) if err != nil && err != gorm.ErrRecordNotFound { return reply, errors.SystemError } if err == gorm.ErrRecordNotFound { return reply, nil } reply.List = make([]*v1.SearchList, 0, len(list)) for _, v := range list { reply.List = append(reply.List, &v1.SearchList{ Id: v.MakerId, Name: v.Maker, }) } case 3: // 车系搜索 if req.BrandId != "" { db = db.Where("t1.brand_id = ?", req.BrandId) } if req.Search != "" { db = db.Where("series_name like ?", "%"+req.Search+"%") } list, err := model.NewSySerieModel().List(db, pagination, false) if err != nil && err != gorm.ErrRecordNotFound { return reply, errors.SystemError } if err == gorm.ErrRecordNotFound { return reply, nil } reply.List = make([]*v1.SearchList, 0, len(list)) for _, v := range list { reply.List = append(reply.List, &v1.SearchList{ Id: v.SeriesId, Name: v.SeriesName, }) } case 4: // 车型搜索 if req.Search != "" { db = db.Where("style_name like ?", "%"+req.Search+"%") } list, err := model.NewSyStyleModel().List(db, pagination, false) if err != nil && err != gorm.ErrRecordNotFound { return reply, errors.SystemError } if err == gorm.ErrRecordNotFound { return reply, nil } reply.List = make([]*v1.SearchList, 0, len(list)) for _, v := range list { reply.List = append(reply.List, &v1.SearchList{ Id: v.StyleId, Name: v.StyleName, }) } } return reply, nil }