package query import ( "adm-data/errors" "adm-data/parser" v1 "adm-data/pb/v1" "context" "encoding/json" "fmt" "strings" "gorm.io/gorm" 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" ) type p07ReqItem struct { Source string `json:"source"` ThirdStyleId string `json:"third_style_id"` } type p07Request struct { List []p07ReqItem `json:"list"` } type p07Item struct { Price string `json:"price"` BrandId string `json:"brand_id"` BrandName string `json:"brand_name"` ModelYear string `json:"model_year"` SeriesId string `json:"series_id"` MakerId string `json:"maker_id"` Maker string `json:"maker"` SeriesImg string `json:"series_img"` SeriesName string `json:"series_name"` StyleId string `json:"style_id"` StyleName string `json:"style_name"` } type p07Response struct { List []p07Item `json:"list"` } func SearchStyleMapping(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 p07Request err = jsoniter.UnmarshalFromString(params, &req) if err != nil || len(req.List) == 0 { return nil, errors.ParamsError } sql := "SELECT t2.price,t2.brand_id,t2.brand_name,t2.model_year,t2.series_id,t2.maker_id,t2.maker,t2.series_name,t1.style_id,t2.style_name " + "FROM t_adm_ads11 as t1 LEFT JOIN `t_adm_ads5` AS t2 ON t1.style_id = t2.style_id WHERE" for _, v := range req.List { sql += " (t1.source = '" + v.Source + "' and t1.third_style_id = '" + v.ThirdStyleId + "') OR" } sql = strings.TrimRight(sql, "OR") var list []p07Item err = database.DB().Raw(sql).Find(&list).Error if err != nil { if err == gorm.ErrRecordNotFound { return reply, errors.DataNotExistError } return reply, errors.SystemError } res := p07Response{ List: make([]p07Item, 0, len(list)), } for _, v := range list { if v.SeriesId != "" { v.SeriesImg = parser.Conf.Oss.SeriesImage + v.SeriesId + ".png" } if v.StyleId != "" { res.List = append(res.List, v) } } if len(res.List) == 0 { return reply, errors.DataNotExistError } reply.Data, _ = jsoniter.MarshalToString(res) /*response := p07Response{} err = jsoniter.UnmarshalFromString(reply.Data, &response) err = insertTable("P07", "查询车型映射", "32,38", req, response) if err != nil { return nil, err }*/ return reply, nil }