p07_search_style_mapping.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package query
  2. import (
  3. "adm-data/errors"
  4. "adm-data/parser"
  5. v1 "adm-data/pb/v1"
  6. "context"
  7. "encoding/json"
  8. "fmt"
  9. "strings"
  10. "gorm.io/gorm"
  11. jsoniter "github.com/json-iterator/go"
  12. "git.getensh.com/common/gopkgsv2/database"
  13. "git.getensh.com/common/gopkgsv2/logger"
  14. "go.uber.org/zap"
  15. "google.golang.org/grpc/status"
  16. )
  17. type p07ReqItem struct {
  18. Source string `json:"source"`
  19. ThirdStyleId string `json:"third_style_id"`
  20. }
  21. type p07Request struct {
  22. List []p07ReqItem `json:"list"`
  23. }
  24. type p07Item struct {
  25. Price string `json:"price"`
  26. BrandId string `json:"brand_id"`
  27. BrandName string `json:"brand_name"`
  28. ModelYear string `json:"model_year"`
  29. SeriesId string `json:"series_id"`
  30. MakerId string `json:"maker_id"`
  31. Maker string `json:"maker"`
  32. SeriesImg string `json:"series_img"`
  33. SeriesName string `json:"series_name"`
  34. StyleId string `json:"style_id"`
  35. StyleName string `json:"style_name"`
  36. }
  37. type p07Response struct {
  38. List []p07Item `json:"list"`
  39. }
  40. func SearchStyleMapping(ctx context.Context, params string) (reply *v1.QueryResponse, err error) {
  41. reply = &v1.QueryResponse{}
  42. // 捕获各个task中的异常并返回给调用者
  43. defer func() {
  44. if r := recover(); r != nil {
  45. err = fmt.Errorf("%+v", r)
  46. e := &status.Status{}
  47. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  48. logger.Error("err",
  49. zap.String("system_err", err.Error()),
  50. zap.Stack("stacktrace"))
  51. }
  52. }
  53. }()
  54. var req p07Request
  55. err = jsoniter.UnmarshalFromString(params, &req)
  56. if err != nil || len(req.List) == 0 {
  57. return nil, errors.ParamsError
  58. }
  59. 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 " +
  60. "FROM t_adm_ads11 as t1 LEFT JOIN `t_adm_ads5` AS t2 ON t1.style_id = t2.style_id WHERE"
  61. for _, v := range req.List {
  62. sql += " (t1.source = '" + v.Source + "' and t1.third_style_id = '" + v.ThirdStyleId + "') OR"
  63. }
  64. sql = strings.TrimRight(sql, "OR")
  65. var list []p07Item
  66. err = database.DB().Raw(sql).Find(&list).Error
  67. if err != nil {
  68. if err == gorm.ErrRecordNotFound {
  69. return reply, errors.DataNotExistError
  70. }
  71. return reply, errors.SystemError
  72. }
  73. res := p07Response{
  74. List: make([]p07Item, 0, len(list)),
  75. }
  76. for _, v := range list {
  77. if v.SeriesId != "" {
  78. v.SeriesImg = parser.Conf.Oss.SeriesImage + v.SeriesId + ".png"
  79. }
  80. if v.StyleId != "" {
  81. res.List = append(res.List, v)
  82. }
  83. }
  84. if len(res.List) == 0 {
  85. return reply, errors.DataNotExistError
  86. }
  87. reply.Data, _ = jsoniter.MarshalToString(res)
  88. /*response := p07Response{}
  89. err = jsoniter.UnmarshalFromString(reply.Data, &response)
  90. err = insertTable("P07", "查询车型映射", "32,38", req, response)
  91. if err != nil {
  92. return nil, err
  93. }*/
  94. return reply, nil
  95. }