123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- package query
- import (
- "context"
- "encoding/json"
- "fmt"
- "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 p10Data struct {
- AutoPrice string `json:"auto_price"`
- BrandId string `json:"brand_id"`
- BrandName string `json:"brand_name"`
- ModelYear string `json:"model_year"`
- SeriesId string `json:"series_id"`
- SeriesImg string `json:"series_img"`
- SeriesName string `json:"series_name"`
- StyleId string `json:"style_id"`
- StyleName string `json:"style_name"`
- Maker string `json:"maker"`
- MakerId string `json:"maker_id"`
- }
- type p10Response struct {
- List []p10Data `json:"list"`
- }
- type p10Request struct {
- Vin string `json:"vin"`
- }
- func p10(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 p10Request
- err = jsoniter.UnmarshalFromString(params, &req)
- if err != nil && req.Vin == "" {
- return nil, errors.ParamsError
- }
- var (
- conditon = "ads8"
- vin8 = req.Vin[:8]
- ads8 = []model.Ads8{}
- ads9 = &model.Ads9{}
- ads10 = &model.Ads10{}
- ads11 = []model.Ads11{}
- ads23 = []model.Ads23{}
- db = database.DB()
- list = []model.GdSyStyle{}
- year int
- )
- Loop:
- for {
- switch conditon {
- case "ads8":
- // ads8
- ads8, err = model.NewAds8Model().List(database.DB().Where("vin = ?", req.Vin))
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataNotExistError
- }
- if err == gorm.ErrRecordNotFound {
- conditon = "ads23"
- } else {
- conditon = "ads11"
- }
- case "ads10":
- ads10, err = model.NewAds10Model().Get(database.DB().Where("vin = ?", req.Vin))
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataNotExistError
- }
- conditon = "ads9"
- db = database.DB().Where("vin_rule = ? AND vin10 = ? AND series_id <> ''", vin8, string(req.Vin[9]))
- if err == nil && ads10.ID > 0 {
- if ads10.DisplacementL != "" {
- db = db.Where("displacement_l = ?", ads10.DisplacementL)
- }
- if ads10.ModelNo != "" {
- db = db.Where("model_no = ?", ads10.ModelNo)
- }
- }
- case "ads11":
- db = database.DB()
- or := false
- if len(ads8) != 0 {
- for _, v := range ads8 {
- if !or {
- db = db.Where("source = ? AND third_style_id = ?", v.Source, v.ThirdStyleId)
- or = true
- } else {
- db = db.Or("source = ? AND third_style_id = ?", v.Source, v.ThirdStyleId)
- }
- }
- } else {
- for _, v := range ads23 {
- if !or {
- db = db.Where("source = ? AND third_style_id = ?", v.Source, v.ThirdStyleId)
- or = true
- } else {
- db = db.Or("source = ? AND third_style_id = ?", v.Source, v.ThirdStyleId)
- }
- }
- }
- ads11, err = model.NewAds11Model().List1(db)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataNotExistError
- }
- if err == gorm.ErrRecordNotFound {
- // ads-10
- conditon = "ads10"
- } else {
- s := ""
- for _, v := range ads11 {
- s += v.StyleId
- }
- if len(s) == 0 {
- // ads-10
- conditon = "ads10"
- } else {
- // ads-5
- conditon = "ads5"
- db = database.DB()
- styleIds := []string{}
- for _, v := range ads11 {
- styleIds = append(styleIds, v.StyleId)
- }
- db = db.Where("style_id in (?)", styleIds)
- }
- }
- case "ads9", "ads9-1":
- ads9, err = model.NewAds9Model().Get(db)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataNotExistError
- }
- if err == gorm.ErrRecordNotFound {
- if conditon == "ads9-1" {
- return nil, errors.DataNotExistError
- }
- db = database.DB().Where("vin_rule = ? AND series_id <> ''", vin8)
- conditon = "ads9-1"
- } else {
- if conditon == "ads9" {
- db = database.DB().Where("series_id = ? AND model_year = ? AND displacement_l = ? ", ads9.SeriesId, ads9.ModelYear, ads9.DisplacementL)
- conditon = "ads5"
- } else {
- // 根据Vin10取出年份
- year = utils.GetYearByVin10(string(req.Vin[9]))
- db = database.DB().Where("series_id = ? AND model_year = ?", ads9.SeriesId, year)
- conditon = "ads5-3years"
- }
- }
- case "ads5":
- list, err = model.NewSyStyleModel().
- StyleList(db, "price, brand_id, brand_name, model_year, series_id, series_name, style_id, style_name", 10000)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataNotExistError
- }
- if err == gorm.ErrRecordNotFound {
- db = database.DB().Where("vin_rule = ? AND series_id <> ''", vin8)
- conditon = "ads9-1"
- }
- if err == nil && len(list) > 0 {
- break Loop
- }
- case "ads5-3years":
- for i := 0; i < 3; i++ {
- list, err = model.NewSyStyleModel().
- StyleList(db, "price, brand_id, brand_name, model_year, series_id, series_name, style_id, style_name", 10000)
- if err != nil {
- year--
- db = database.DB().Where("series_id = ? AND model_year = ?", ads9.SeriesId, year)
- continue
- }
- break Loop
- }
- break Loop
- case "ads23":
- // ads23
- ads23, err = model.NewAds23Model().List(database.DB().Where("vin = ?", req.Vin))
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataNotExistError
- }
- if err == gorm.ErrRecordNotFound {
- conditon = "ads10"
- } else {
- conditon = "ads11"
- }
- }
- }
- if len(list) == 0 {
- return nil, errors.DataNotExistError
- }
- res := p10Response{
- List: make([]p10Data, 0, len(list)),
- }
- for _, v := range list {
- series, err := model.NewSySerieModel().Get(database.DB().Where("series_id = ?", v.SeriesId))
- if err != nil {
- return nil, errors.DataNotExistError
- }
- res.List = append(res.List, p10Data{
- AutoPrice: v.Price,
- BrandId: v.BrandId,
- BrandName: v.BrandName,
- ModelYear: v.ModelYear,
- SeriesId: v.SeriesId,
- SeriesImg: utils.GetSeriesLogoById(v.SeriesId, series.HasImg),
- SeriesName: v.SeriesName,
- StyleId: v.StyleId,
- StyleName: v.StyleName,
- Maker: series.Maker,
- MakerId: series.MakerId,
- })
- }
- reply.Data, _ = jsoniter.MarshalToString(res)
- return reply, nil
- }
|