task7.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2019 autocareai.com. All rights reserved.
  2. // Use of this source code is governed by autocareai.com.
  3. package task
  4. import (
  5. "gadm-dws/apis"
  6. "gadm-dws/consts"
  7. "gadm-dws/model"
  8. "encoding/json"
  9. "gorm.io/gorm"
  10. "strings"
  11. )
  12. func supplementDws7(old, new *model.Dws7) (bool) {
  13. isSupple := false
  14. if old.InitialRegistrationDate == "" && new.InitialRegistrationDate != "" {
  15. isSupple = true
  16. old.InitialRegistrationDate = new.InitialRegistrationDate
  17. }
  18. if old.VehicleType == "" && new.VehicleType != "" {
  19. isSupple = true
  20. old.VehicleType = new.VehicleType
  21. }
  22. if old.UseProperty == "" && new.UseProperty != "" {
  23. isSupple = true
  24. old.UseProperty = new.UseProperty
  25. }
  26. if old.FuelType == "" && new.FuelType != "" {
  27. isSupple = true
  28. old.FuelType = new.FuelType
  29. }
  30. if old.ModelNo == "" && new.ModelNo != "" {
  31. isSupple = true
  32. old.ModelNo = new.ModelNo
  33. }
  34. if old.VehicleBodyColor == "" && new.VehicleBodyColor != "" {
  35. isSupple = true
  36. old.VehicleBodyColor = new.VehicleBodyColor
  37. }
  38. return isSupple
  39. }
  40. // 获取交管信息
  41. func Dws7Task(db *gorm.DB,dwsMessage *apis.DwsMessage, outputSourceCode string) (adsMsgList []*apis.AdsMessage, dwsMsgList []*apis.DwsMessage, err error) {
  42. dws7 := &model.Dws7{}
  43. err = json.Unmarshal([]byte(dwsMessage.Content), dws7)
  44. if err != nil {
  45. return nil, nil, nil
  46. }
  47. if (dws7.Vin == "") || (dws7.InitialRegistrationDate == "" &&
  48. dws7.UseProperty == "" &&
  49. dws7.VehicleType == "" &&
  50. dws7.FuelType == "" &&
  51. dws7.ModelNo == "" &&
  52. dws7.VehicleBodyColor == "" ){
  53. return nil, nil, nil
  54. }
  55. oldDws7 := &model.Dws7{}
  56. err = oldDws7.Query(db, map[string]interface{}{"vin": dws7.Vin})
  57. if err == nil {
  58. // 有数据
  59. isSupple := supplementDws7(oldDws7,dws7)
  60. if isSupple {
  61. err = oldDws7.Update(db)
  62. if err != nil{
  63. return nil,nil,err
  64. }
  65. }else{
  66. return nil,nil,nil
  67. }
  68. } else {
  69. if err == gorm.ErrRecordNotFound {
  70. // 没有数据
  71. err = dws7.Insert(db)
  72. if err != nil {
  73. if !strings.Contains(err.Error(), "Duplicate") {
  74. return nil,nil, err
  75. }
  76. }
  77. oldDws7 = dws7
  78. } else {
  79. // 数据库错误
  80. return nil, nil, err
  81. }
  82. }
  83. adsMsg := NewAdsMessage(dwsMessage, outputSourceCode, consts.ACTIONINSERT)
  84. msgByte, _ := json.Marshal(*oldDws7)
  85. adsMsg.Content = string(msgByte)
  86. adsMsgList = append(adsMsgList,adsMsg)
  87. return adsMsgList, nil, nil
  88. }