package query import ( "adm-data/errors" "adm-data/model" v1 "adm-data/pb/v1" "context" "encoding/json" "fmt" jsoniter "github.com/json-iterator/go" "git.getensh.com/common/gopkgsv2/logger" "go.uber.org/zap" "google.golang.org/grpc/status" "git.getensh.com/common/gopkgsv2/database" ) type a01Request struct { PlateNo string `json:"plate_no"` PlateType string `json:"plate_type"` } type a01Data struct { PlateType string `json:"plate_type"` Vin string `json:"vin"` UpdatedAt int64 `json:"updated_at"` } type a01Response struct { List []a01Data `json:"list"` } func PlateVIN(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 a01Request err = jsoniter.UnmarshalFromString(params, &req) if err != nil && (req.PlateNo == "" || req.PlateType == "") { return nil, errors.ParamsError } if req.PlateType == "" { list, err := model.NewAds1Model().List(database.DB().Where("plate_no = ?", req.PlateNo)) if err != nil { return reply, errors.DataNotExistError } res := a01Response{ List: make([]a01Data, 0, len(list)), } for _, v := range list { res.List = append(res.List, a01Data{ PlateType: v.PlateType, Vin: v.Vin, UpdatedAt:v.UpdatedAt, }) } reply.Data, _ = jsoniter.MarshalToString(res) return reply, nil } list, err := model.NewAds1Model().List(database.DB().Where("plate_no = ? and plate_type = ?", req.PlateNo, req.PlateType)) if err != nil { return reply, errors.DataNotExistError } res := a01Response{ List: make([]a01Data, 0, len(list)), } for _, v := range list { res.List = append(res.List, a01Data{ PlateType: v.PlateType, Vin: v.Vin, UpdatedAt:v.UpdatedAt, }) } reply.Data, _ = jsoniter.MarshalToString(res) return reply, nil }