a01_plate_search_vin.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package query
  2. import (
  3. "adm-data/errors"
  4. "adm-data/model"
  5. v1 "adm-data/pb/v1"
  6. "context"
  7. "encoding/json"
  8. "fmt"
  9. jsoniter "github.com/json-iterator/go"
  10. "git.getensh.com/common/gopkgsv2/logger"
  11. "go.uber.org/zap"
  12. "google.golang.org/grpc/status"
  13. "git.getensh.com/common/gopkgsv2/database"
  14. )
  15. type a01Request struct {
  16. PlateNo string `json:"plate_no"`
  17. PlateType string `json:"plate_type"`
  18. }
  19. type a01Data struct {
  20. PlateType string `json:"plate_type"`
  21. Vin string `json:"vin"`
  22. UpdatedAt int64 `json:"updated_at"`
  23. }
  24. type a01Response struct {
  25. List []a01Data `json:"list"`
  26. }
  27. func PlateVIN(ctx context.Context, params string) (reply *v1.QueryResponse, err error) {
  28. reply = &v1.QueryResponse{}
  29. // 捕获各个task中的异常并返回给调用者
  30. defer func() {
  31. if r := recover(); r != nil {
  32. err = fmt.Errorf("%+v", r)
  33. e := &status.Status{}
  34. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  35. logger.Error("err",
  36. zap.String("system_err", err.Error()),
  37. zap.Stack("stacktrace"))
  38. }
  39. }
  40. }()
  41. var req a01Request
  42. err = jsoniter.UnmarshalFromString(params, &req)
  43. if err != nil && (req.PlateNo == "" || req.PlateType == "") {
  44. return nil, errors.ParamsError
  45. }
  46. if req.PlateType == "" {
  47. list, err := model.NewAds1Model().List(database.DB().Where("plate_no = ?", req.PlateNo))
  48. if err != nil {
  49. return reply, errors.DataNotExistError
  50. }
  51. res := a01Response{
  52. List: make([]a01Data, 0, len(list)),
  53. }
  54. for _, v := range list {
  55. res.List = append(res.List, a01Data{
  56. PlateType: v.PlateType,
  57. Vin: v.Vin,
  58. UpdatedAt:v.UpdatedAt,
  59. })
  60. }
  61. reply.Data, _ = jsoniter.MarshalToString(res)
  62. return reply, nil
  63. }
  64. list, err := model.NewAds1Model().List(database.DB().Where("plate_no = ? and plate_type = ?", req.PlateNo, req.PlateType))
  65. if err != nil {
  66. return reply, errors.DataNotExistError
  67. }
  68. res := a01Response{
  69. List: make([]a01Data, 0, len(list)),
  70. }
  71. for _, v := range list {
  72. res.List = append(res.List, a01Data{
  73. PlateType: v.PlateType,
  74. Vin: v.Vin,
  75. UpdatedAt:v.UpdatedAt,
  76. })
  77. }
  78. reply.Data, _ = jsoniter.MarshalToString(res)
  79. return reply, nil
  80. }