task20.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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/model"
  8. "encoding/json"
  9. "gorm.io/gorm"
  10. "strings"
  11. )
  12. // 计算号牌种类
  13. func Task20(adsMsg *apis.AdsMessage) (err error) {
  14. ads20 := &model.Ads20{}
  15. json.Unmarshal([]byte(adsMsg.Content), ads20)
  16. ads20.UpdatedAt = adsMsg.Timestamp
  17. ads20.CreatedAt = adsMsg.Timestamp
  18. if ads20.IdCard == "" || ads20.Name == ""{
  19. return nil
  20. }
  21. db := clinit.DB()
  22. oldAds20 := &model.Ads20{}
  23. where := map[string]interface{}{"id_card": ads20.IdCard}
  24. err = oldAds20.Query(db, where)
  25. if err == nil {
  26. // 有数据
  27. if oldAds20.Name == ads20.Name {
  28. // 不处理
  29. return nil
  30. } else {
  31. // 更新时间大于消息时间表示是后面的消息,不处理
  32. if oldAds20.UpdatedAt > ads20.UpdatedAt{
  33. return nil
  34. }
  35. oldAds20.Name = ads20.Name
  36. oldAds20.UpdatedAt = ads20.UpdatedAt
  37. err = oldAds20.Update(db)
  38. if err != nil{
  39. if err != gorm.ErrRecordNotFound{
  40. return err
  41. }
  42. }
  43. }
  44. } else {
  45. // 无数据
  46. if err == gorm.ErrRecordNotFound {
  47. err = ads20.Insert(db)
  48. if err != nil {
  49. if !strings.Contains(err.Error(), "Duplicate") {
  50. return err
  51. }
  52. }
  53. } else{
  54. return err
  55. }
  56. }
  57. return nil
  58. }