task3.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. "strings"
  10. "gorm.io/gorm"
  11. )
  12. // 车牌和姓名对应关系(正确关系)
  13. func Dws3Task(db *gorm.DB,dwsMessage *apis.DwsMessage, outputSourceCode string) (adsMsgList []*apis.AdsMessage, dwsMsgList []*apis.DwsMessage, err error) {
  14. dws3 := &model.Dws3{}
  15. err = json.Unmarshal([]byte(dwsMessage.Content), dws3)
  16. if err != nil {
  17. return nil, nil, nil
  18. }
  19. dws3.UpdatedAt = dwsMessage.Timestamp
  20. dws3.CreatedAt = dwsMessage.Timestamp
  21. if dws3.Owner == "" && dws3.Name != ""{
  22. dws3.Owner = dws3.Name
  23. }
  24. if dws3.Owner == "" || dws3.PlateNo == "" || dws3.PlateType == ""{
  25. return nil, nil, nil
  26. }
  27. oldDws3 := &model.Dws3{}
  28. where := map[string]interface{}{"plate_no": dws3.PlateNo, "plate_type": dws3.PlateType}
  29. err = oldDws3.Query(db, where)
  30. if err == nil {
  31. // 有数据
  32. if oldDws3.Owner == dws3.Owner {
  33. // 不处理
  34. return nil, nil,nil
  35. } else {
  36. // 更新时间大于消息时间表示是后面的消息,不处理
  37. if oldDws3.UpdatedAt > dws3.UpdatedAt{
  38. return nil,nil,nil
  39. }
  40. oldDws3.Owner = dws3.Owner
  41. oldDws3.UpdatedAt = dws3.UpdatedAt
  42. err = oldDws3.Update(db)
  43. if err != nil{
  44. if err != gorm.ErrRecordNotFound{
  45. return nil,nil,err
  46. }
  47. }
  48. }
  49. } else {
  50. // 无数据
  51. if err == gorm.ErrRecordNotFound {
  52. err = dws3.Insert(db)
  53. if err != nil {
  54. if !strings.Contains(err.Error(), "Duplicate") {
  55. return nil,nil, err
  56. }
  57. }
  58. oldDws3 = dws3
  59. } else{
  60. return nil,nil,err
  61. }
  62. }
  63. adsMsg := NewAdsMessage(dwsMessage, outputSourceCode, consts.ACTIONINSERT)
  64. msgByte, _ := json.Marshal(*oldDws3)
  65. adsMsg.Content = string(msgByte)
  66. adsMsgList = append(adsMsgList,adsMsg)
  67. return adsMsgList, nil, nil
  68. }