is01_insurance_date.go 2.2 KB

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