P10.go 6.3 KB

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