12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- package query
- import (
- "adm-data/model"
- "context"
- "encoding/json"
- "fmt"
- "time"
- jsoniter "github.com/json-iterator/go"
- "adm-data/errors"
- v1 "adm-data/pb/v1"
- "git.getensh.com/common/gopkgsv2/database"
- "git.getensh.com/common/gopkgsv2/logger"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- )
- type is01Request struct {
- Vin string `json:"vin"`
- }
- type is01Response struct {
- InsuranceFirstDate string `json:"insurance_first_date"`
- LastCompulsoryInsuranceDate string `json:"last_compulsory_insurance_date"`
- UsePropertyDetail string `json:"use_property_detail"`
- UpdatedAt int64 `json:"updated_at"`
- }
- func InsuranceDate(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 is01Request
- err = jsoniter.UnmarshalFromString(params, &req)
- if err != nil || req.Vin == "" {
- return nil, errors.ParamsError
- }
- var res is01Response
- db := database.DB()
- err = db.Raw("select insurance_first_date, updated_at, use_property_detail from t_adm_ads15 where vin = ? ", req.Vin).Find(&res).Error
- list, err := model.NewAds17Model().List(database.DB().Where("vin = ?", req.Vin))
- b := 0
- out := ""
- for _, v := range list {
- if len(v.InsuranceDate) >= 8 {
- c := v.InsuranceDate + " 15:04:05"
- t, _ := time.Parse("2006-01-02 15:04:05", c)
- a := int(t.Unix())
- if a > b {
- b = a
- out = v.InsuranceDate[0:7]
- }
- } else {
- c := v.InsuranceDate + "-01 15:04:05"
- t, _ := time.Parse("2006-01-02 15:04:05", c)
- a := int(t.Unix())
- if a > b {
- b = a
- out = v.InsuranceDate
- }
- }
- }
- if b != 0 {
- res.LastCompulsoryInsuranceDate = out
- } else {
- res.LastCompulsoryInsuranceDate = ""
- }
- if res.InsuranceFirstDate == "" && len(list) == 0 {
- return nil, errors.DataNotExistError
- }
- reply.Data, _ = jsoniter.MarshalToString(res)
- return reply, nil
- }
|