task2.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. "github.com/tidwall/gjson"
  10. "gorm.io/gorm"
  11. "strings"
  12. )
  13. // 车牌和vin码对应关系(错误关系)
  14. func Dws2Task(db *gorm.DB,dwsMessage *apis.DwsMessage, outputSourceCode string) (adsMsgList []*apis.AdsMessage, dwsMsgList []*apis.DwsMessage, err error) {
  15. // 解析数据
  16. dws2 := &model.Dws2{}
  17. err = json.Unmarshal([]byte(dwsMessage.Content), dws2)
  18. if err != nil {
  19. return nil, nil, nil
  20. }
  21. // 参数判断
  22. if dws2.PlateNo == "" || dws2.Vin == ""{
  23. return nil ,nil ,nil
  24. }
  25. // 计算号牌种类
  26. if dws2.PlateType == ""{
  27. dws2.PlateType = CalcPlateType(db,dws2.Vin)
  28. }
  29. isExpire := gjson.Get(dwsMessage.Content, "is_expire").String()
  30. if isExpire == consts.EXPIREDATA {
  31. // 删除dws1
  32. where := map[string]interface{}{"plate_no": dws2.PlateNo, "vin": dws2.Vin}
  33. dws1 := &model.Dws1{}
  34. err = dws1.Query(db, where)
  35. if err != nil {
  36. if err != gorm.ErrRecordNotFound {
  37. return nil,nil, err
  38. }
  39. }else{
  40. if dws2.PlateType == ""{
  41. dws2.PlateType = dws1.PlateType
  42. }
  43. err = dws1.Delete(db,nil)
  44. if err != nil{
  45. if err != gorm.ErrRecordNotFound {
  46. return nil,nil, err
  47. }
  48. }
  49. }
  50. // 插入更新dws2
  51. oldDws2 := &model.Dws2{}
  52. err = oldDws2.Query(db,where)
  53. if err != nil{
  54. if err == gorm.ErrRecordNotFound{
  55. err = dws2.Insert(db)
  56. if err != nil {
  57. if !strings.Contains(err.Error(), "Duplicate") {
  58. return nil,nil, err
  59. }
  60. }
  61. }else{
  62. return nil,nil,err
  63. }
  64. }else{
  65. if oldDws2.PlateType == "" && dws2.PlateType != ""{
  66. oldDws2.PlateType = dws2.PlateType
  67. err = oldDws2.Update(db)
  68. if err != nil{
  69. if err != gorm.ErrRecordNotFound{
  70. return nil,nil,err
  71. }
  72. }
  73. }
  74. }
  75. // 删除dws4
  76. dws4 := &model.Dws4{}
  77. err = dws4.Delete(db, where)
  78. if err != nil {
  79. if err != gorm.ErrRecordNotFound {
  80. return nil,nil, err
  81. }
  82. }
  83. }else{
  84. return nil ,nil ,nil
  85. }
  86. adsMsg := NewAdsMessage(dwsMessage, outputSourceCode, consts.ACTIONDELETE)
  87. msgByte, _ := json.Marshal(*dws2)
  88. adsMsg.Content = string(msgByte)
  89. adsMsgList = append(adsMsgList,adsMsg)
  90. return adsMsgList, nil, nil
  91. }