task16.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package task
  4. import (
  5. "adm-ads/apis"
  6. "adm-ads/common.in/clinit"
  7. "adm-ads/consts"
  8. "adm-ads/model"
  9. "encoding/json"
  10. "gorm.io/gorm"
  11. "strings"
  12. )
  13. // 处理任务16的删除
  14. func handleTask16Delete(ads16 *model.Ads16) error {
  15. err := ads16.Delete(clinit.DB(),map[string]interface{}{"vin":ads16.Vin})
  16. if err == gorm.ErrRecordNotFound{
  17. return nil
  18. }
  19. return err
  20. }
  21. func supplementAds16(old, new *model.Ads16) (bool) {
  22. isSupple := false
  23. if old.InitialRegistrationDate == "" && new.InitialRegistrationDate != "" {
  24. isSupple = true
  25. old.InitialRegistrationDate = new.InitialRegistrationDate
  26. }
  27. if old.VehicleType == "" && new.VehicleType != "" {
  28. isSupple = true
  29. old.VehicleType = new.VehicleType
  30. }
  31. if old.UseProperty == "" && new.UseProperty != "" {
  32. isSupple = true
  33. old.UseProperty = new.UseProperty
  34. }
  35. if old.FuelType == "" && new.FuelType != "" {
  36. isSupple = true
  37. old.FuelType = new.FuelType
  38. }
  39. if old.ModelNo == "" && new.ModelNo != "" {
  40. isSupple = true
  41. old.ModelNo = new.ModelNo
  42. }
  43. if old.VehicleBodyColor == "" && new.VehicleBodyColor != "" {
  44. isSupple = true
  45. old.VehicleBodyColor = new.VehicleBodyColor
  46. }
  47. return isSupple
  48. }
  49. // 处理任务16的插入
  50. func handleTask16InsertOrUpdate(ads16 *model.Ads16)error{
  51. oldAds16 := &model.Ads16{}
  52. err := oldAds16.Query(clinit.DB(),map[string]interface{}{"vin":ads16.Vin})
  53. if err != nil{
  54. if err == gorm.ErrRecordNotFound{
  55. err = ads16.Insert(clinit.DB())
  56. if err != nil && !strings.Contains(err.Error(), "Duplicate") {
  57. return err
  58. }
  59. return nil
  60. }
  61. return err
  62. }else{
  63. // 更新时间大于消息时间表示是后面的消息,不处理
  64. /*if oldAds16.UpdatedAt > ads16.UpdatedAt{
  65. return nil
  66. }*/
  67. isSupple := supplementAds16(oldAds16,ads16)
  68. if isSupple {
  69. oldAds16.UpdatedAt = ads16.UpdatedAt
  70. err = oldAds16.Update(clinit.DB())
  71. if err != nil{
  72. return err
  73. }
  74. }else{
  75. return nil
  76. }
  77. }
  78. return nil
  79. }
  80. // 获取交管信息
  81. func Task16(adsMsg *apis.AdsMessage) (err error) {
  82. ads16 := &model.Ads16{}
  83. json.Unmarshal([]byte(adsMsg.Content),&ads16)
  84. if (ads16.Vin == "") || (ads16.InitialRegistrationDate == "" &&
  85. ads16.UseProperty == "" &&
  86. ads16.VehicleType == "" &&
  87. ads16.FuelType == "" &&
  88. ads16.ModelNo == "" &&
  89. ads16.VehicleBodyColor == "" ){
  90. return nil
  91. }
  92. ads16.CreatedAt = adsMsg.Timestamp
  93. ads16.UpdatedAt = adsMsg.Timestamp
  94. if adsMsg.Action == consts.ACTIONDELETE{
  95. // 删除
  96. err = handleTask16Delete(ads16)
  97. }else{
  98. // 新增或插入
  99. err = handleTask16InsertOrUpdate(ads16)
  100. }
  101. return err
  102. }