task1.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. "github.com/tidwall/gjson"
  10. "gorm.io/gorm"
  11. "strings"
  12. )
  13. // 处理任务1的删除
  14. func handleTask1Delete(adsMsg *apis.AdsMessage) error {
  15. content := adsMsg.Content
  16. plateNo := gjson.Get(content, "plate_no").String()
  17. //plateType := gjson.Get(content, "plate_type").String()
  18. vin := gjson.Get(content, "vin").String()
  19. if plateNo == "" || vin == ""{
  20. return nil
  21. }
  22. ads1 := &model.Ads1{}
  23. err := ads1.Delete(clinit.DB(),map[string]interface{}{"plate_no":plateNo,"vin":vin})
  24. return err
  25. }
  26. // 处理任务1的插入
  27. func handleTask1InsertOrUpdate(adsMsg *apis.AdsMessage)error{
  28. content := adsMsg.Content
  29. plateNo := gjson.Get(content, "plate_no").String()
  30. plateType := gjson.Get(content, "plate_type").String()
  31. vin := gjson.Get(content, "vin").String()
  32. if plateNo == "" || vin == ""{
  33. return nil
  34. }
  35. ads1 := &model.Ads1{}
  36. where := map[string]interface{}{"plate_no":plateNo}
  37. var ads1List []model.Ads1
  38. err := ads1.QueryAll(clinit.DB(),where,&ads1List)
  39. if len(ads1List) == 0 {
  40. err = gorm.ErrRecordNotFound
  41. }
  42. if err != nil{
  43. // 没有数据插入
  44. if err == gorm.ErrRecordNotFound{
  45. ads1.PlateNo = plateNo
  46. ads1.PlateType = plateType
  47. ads1.Vin = vin
  48. ads1.CreatedAt = adsMsg.Timestamp
  49. ads1.UpdatedAt = adsMsg.Timestamp
  50. err = ads1.Insert(clinit.DB())
  51. if err != nil && !strings.Contains(err.Error(), "Duplicate") {
  52. return err
  53. }
  54. return nil
  55. }
  56. return err
  57. }
  58. for _,v := range ads1List{
  59. // 号牌种类相同的数据,判断vin码是不是相等
  60. if plateType != "" && v.PlateType != "" && plateType == v.PlateType{
  61. // 更新时间大于消息时间表示是后面的消息,不处理
  62. if v.UpdatedAt > adsMsg.Timestamp{
  63. return nil
  64. }
  65. if vin != v.Vin{
  66. v.Vin= vin
  67. v.UpdatedAt = adsMsg.Timestamp
  68. err = v.Update(clinit.DB())
  69. return err
  70. }
  71. return nil
  72. }else if v.Vin == vin{
  73. // 车架号相同的数据
  74. if v.PlateType == "" && plateType != ""{
  75. // 补充号牌种类
  76. v.PlateType = plateType
  77. v.UpdatedAt = adsMsg.Timestamp
  78. err = v.Update(clinit.DB())
  79. return err
  80. }
  81. return nil
  82. }
  83. }
  84. // 最后插入,号牌种类不同,vin码不同的数据
  85. ads1.PlateNo = plateNo
  86. ads1.PlateType = plateType
  87. ads1.Vin = vin
  88. ads1.CreatedAt = adsMsg.Timestamp
  89. ads1.UpdatedAt = adsMsg.Timestamp
  90. err = ads1.Insert(clinit.DB())
  91. if err != nil && !strings.Contains(err.Error(), "Duplicate") {
  92. return err
  93. }
  94. return nil
  95. }
  96. // 车牌和vin码对应关系(正确关系)
  97. func Task1(adsMsg *apis.AdsMessage) (err error) {
  98. if adsMsg.Action == consts.ACTIONDELETE{
  99. // 删除
  100. err = handleTask1Delete(adsMsg)
  101. }else{
  102. // 新增或插入
  103. err = handleTask1InsertOrUpdate(adsMsg)
  104. }
  105. return err
  106. }