p10.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package query
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "adm-data/errors"
  7. "adm-data/model"
  8. v1 "adm-data/pb/v1"
  9. "adm-data/utils"
  10. jsoniter "github.com/json-iterator/go"
  11. "git.getensh.com/common/gopkgsv2/database"
  12. "git.getensh.com/common/gopkgsv2/logger"
  13. "go.uber.org/zap"
  14. "google.golang.org/grpc/status"
  15. "gorm.io/gorm"
  16. )
  17. type p10Data struct {
  18. AutoPrice string `json:"auto_price"`
  19. BrandId string `json:"brand_id"`
  20. BrandName string `json:"brand_name"`
  21. ModelYear string `json:"model_year"`
  22. SeriesId string `json:"series_id"`
  23. SeriesImg string `json:"series_img"`
  24. SeriesName string `json:"series_name"`
  25. StyleId string `json:"style_id"`
  26. StyleName string `json:"style_name"`
  27. Maker string `json:"maker"`
  28. MakerId string `json:"maker_id"`
  29. }
  30. type p10Response struct {
  31. List []p10Data `json:"list"`
  32. }
  33. type p10Request struct {
  34. Vin string `json:"vin"`
  35. }
  36. func p10(ctx context.Context, params string) (reply *v1.QueryResponse, err error) {
  37. reply = &v1.QueryResponse{}
  38. // 捕获各个task中的异常并返回给调用者
  39. defer func() {
  40. if r := recover(); r != nil {
  41. err = fmt.Errorf("%+v", r)
  42. e := &status.Status{}
  43. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  44. logger.Error("err",
  45. zap.String("system_err", err.Error()),
  46. zap.Stack("stacktrace"))
  47. }
  48. }
  49. }()
  50. var req p10Request
  51. err = jsoniter.UnmarshalFromString(params, &req)
  52. if err != nil && req.Vin == "" {
  53. return nil, errors.ParamsError
  54. }
  55. var (
  56. conditon = "ads8"
  57. vin8 = req.Vin[:8]
  58. ads8 = []model.Ads8{}
  59. ads9 = &model.Ads9{}
  60. ads10 = &model.Ads10{}
  61. ads11 = []model.Ads11{}
  62. ads23 = []model.Ads23{}
  63. db = database.DB()
  64. list = []model.GdSyStyle{}
  65. year int
  66. )
  67. Loop:
  68. for {
  69. switch conditon {
  70. case "ads8":
  71. // ads8
  72. ads8, err = model.NewAds8Model().List(database.DB().Where("vin = ?", req.Vin))
  73. if err != nil && err != gorm.ErrRecordNotFound {
  74. return nil, errors.DataNotExistError
  75. }
  76. if err == gorm.ErrRecordNotFound {
  77. conditon = "ads23"
  78. } else {
  79. conditon = "ads11"
  80. }
  81. case "ads10":
  82. ads10, err = model.NewAds10Model().Get(database.DB().Where("vin = ?", req.Vin))
  83. if err != nil && err != gorm.ErrRecordNotFound {
  84. return nil, errors.DataNotExistError
  85. }
  86. conditon = "ads9"
  87. db = database.DB().Where("vin_rule = ? AND vin10 = ? AND series_id <> ''", vin8, string(req.Vin[9]))
  88. if err == nil && ads10.ID > 0 {
  89. if ads10.DisplacementL != "" {
  90. db = db.Where("displacement_l = ?", ads10.DisplacementL)
  91. }
  92. if ads10.ModelNo != "" {
  93. db = db.Where("model_no = ?", ads10.ModelNo)
  94. }
  95. }
  96. case "ads11":
  97. db = database.DB()
  98. or := false
  99. if len(ads8) != 0 {
  100. for _, v := range ads8 {
  101. if !or {
  102. db = db.Where("source = ? AND third_style_id = ?", v.Source, v.ThirdStyleId)
  103. or = true
  104. } else {
  105. db = db.Or("source = ? AND third_style_id = ?", v.Source, v.ThirdStyleId)
  106. }
  107. }
  108. } else {
  109. for _, v := range ads23 {
  110. if !or {
  111. db = db.Where("source = ? AND third_style_id = ?", v.Source, v.ThirdStyleId)
  112. or = true
  113. } else {
  114. db = db.Or("source = ? AND third_style_id = ?", v.Source, v.ThirdStyleId)
  115. }
  116. }
  117. }
  118. ads11, err = model.NewAds11Model().List1(db)
  119. if err != nil && err != gorm.ErrRecordNotFound {
  120. return nil, errors.DataNotExistError
  121. }
  122. if err == gorm.ErrRecordNotFound {
  123. // ads-10
  124. conditon = "ads10"
  125. } else {
  126. s := ""
  127. for _, v := range ads11 {
  128. s += v.StyleId
  129. }
  130. if len(s) == 0 {
  131. // ads-10
  132. conditon = "ads10"
  133. } else {
  134. // ads-5
  135. conditon = "ads5"
  136. db = database.DB()
  137. styleIds := []string{}
  138. for _, v := range ads11 {
  139. styleIds = append(styleIds, v.StyleId)
  140. }
  141. db = db.Where("style_id in (?)", styleIds)
  142. }
  143. }
  144. case "ads9", "ads9-1":
  145. ads9, err = model.NewAds9Model().Get(db)
  146. if err != nil && err != gorm.ErrRecordNotFound {
  147. return nil, errors.DataNotExistError
  148. }
  149. if err == gorm.ErrRecordNotFound {
  150. if conditon == "ads9-1" {
  151. return nil, errors.DataNotExistError
  152. }
  153. db = database.DB().Where("vin_rule = ? AND series_id <> ''", vin8)
  154. conditon = "ads9-1"
  155. } else {
  156. if conditon == "ads9" {
  157. db = database.DB().Where("series_id = ? AND model_year = ? AND displacement_l = ? ", ads9.SeriesId, ads9.ModelYear, ads9.DisplacementL)
  158. conditon = "ads5"
  159. } else {
  160. // 根据Vin10取出年份
  161. year = utils.GetYearByVin10(string(req.Vin[9]))
  162. db = database.DB().Where("series_id = ? AND model_year = ?", ads9.SeriesId, year)
  163. conditon = "ads5-3years"
  164. }
  165. }
  166. case "ads5":
  167. list, err = model.NewSyStyleModel().
  168. StyleList(db, "price, brand_id, brand_name, model_year, series_id, series_name, style_id, style_name", 10000)
  169. if err != nil && err != gorm.ErrRecordNotFound {
  170. return nil, errors.DataNotExistError
  171. }
  172. if err == gorm.ErrRecordNotFound {
  173. db = database.DB().Where("vin_rule = ? AND series_id <> ''", vin8)
  174. conditon = "ads9-1"
  175. }
  176. if err == nil && len(list) > 0 {
  177. break Loop
  178. }
  179. case "ads5-3years":
  180. for i := 0; i < 3; i++ {
  181. list, err = model.NewSyStyleModel().
  182. StyleList(db, "price, brand_id, brand_name, model_year, series_id, series_name, style_id, style_name", 10000)
  183. if err != nil {
  184. year--
  185. db = database.DB().Where("series_id = ? AND model_year = ?", ads9.SeriesId, year)
  186. continue
  187. }
  188. break Loop
  189. }
  190. break Loop
  191. case "ads23":
  192. // ads23
  193. ads23, err = model.NewAds23Model().List(database.DB().Where("vin = ?", req.Vin))
  194. if err != nil && err != gorm.ErrRecordNotFound {
  195. return nil, errors.DataNotExistError
  196. }
  197. if err == gorm.ErrRecordNotFound {
  198. conditon = "ads10"
  199. } else {
  200. conditon = "ads11"
  201. }
  202. }
  203. }
  204. if len(list) == 0 {
  205. return nil, errors.DataNotExistError
  206. }
  207. res := p10Response{
  208. List: make([]p10Data, 0, len(list)),
  209. }
  210. for _, v := range list {
  211. series, err := model.NewSySerieModel().Get(database.DB().Where("series_id = ?", v.SeriesId))
  212. if err != nil {
  213. return nil, errors.DataNotExistError
  214. }
  215. res.List = append(res.List, p10Data{
  216. AutoPrice: v.Price,
  217. BrandId: v.BrandId,
  218. BrandName: v.BrandName,
  219. ModelYear: v.ModelYear,
  220. SeriesId: v.SeriesId,
  221. SeriesImg: utils.GetSeriesLogoById(v.SeriesId, series.HasImg),
  222. SeriesName: v.SeriesName,
  223. StyleId: v.StyleId,
  224. StyleName: v.StyleName,
  225. Maker: series.Maker,
  226. MakerId: series.MakerId,
  227. })
  228. }
  229. reply.Data, _ = jsoniter.MarshalToString(res)
  230. return reply, nil
  231. }