task1.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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-dws/apis"
  6. "adm-dws/consts"
  7. "adm-dws/model"
  8. "encoding/json"
  9. "strings"
  10. "github.com/tidwall/gjson"
  11. "gorm.io/gorm"
  12. )
  13. func isInDws2(db *gorm.DB,plateNo,plateType,vin string) (bool,error) {
  14. dws2 := &model.Dws2{}
  15. where := map[string]interface{}{"plate_no": plateNo,"vin": vin}
  16. err := dws2.Query(db, where)
  17. if err == nil {
  18. if dws2.PlateType == "" && plateType != ""{
  19. dws2.PlateType = plateType
  20. dws2.Update(db)
  21. }
  22. return true,nil
  23. }
  24. if err != gorm.ErrRecordNotFound {
  25. return false, err
  26. }
  27. return false,nil
  28. }
  29. func isInDws1(db *gorm.DB,dws1 *model.Dws1)(bool,error){
  30. // 判断是否存在于dws1
  31. where := map[string]interface{}{"plate_no": dws1.PlateNo,"vin":dws1.Vin}
  32. err := dws1.Query(db, where)
  33. if err == nil {
  34. // 有数据,直接返回
  35. return true,nil
  36. }else {
  37. // 没有数据
  38. if err == gorm.ErrRecordNotFound{
  39. return false,nil
  40. }else{
  41. // 数据库错误
  42. return false,err
  43. }
  44. }
  45. }
  46. func handlePlateTypeExist(dws1 *model.Dws1,db *gorm.DB) (bool,error) {
  47. // 删除dws4中数据
  48. dws4 := &model.Dws4{}
  49. where := map[string]interface{}{"plate_no": dws1.PlateNo,"vin":dws1.Vin}
  50. err := dws4.Delete(db,where)
  51. if err != nil {
  52. if err != gorm.ErrRecordNotFound {
  53. return false,err
  54. }
  55. }
  56. // 插入或更新dws1
  57. // 通过车牌查询
  58. where = map[string]interface{}{"plate_no": dws1.PlateNo,"plate_type":dws1.PlateType}
  59. oldDws1 := &model.Dws1{}
  60. err = oldDws1.Query(db, where)
  61. if err != nil {
  62. // 没有数据
  63. if err == gorm.ErrRecordNotFound { // 没有数据则插入
  64. err = dws1.Insert(db)
  65. if err != nil {
  66. if !strings.Contains(err.Error(), "Duplicate") {
  67. return false,err
  68. }
  69. }
  70. }
  71. } else { // 有数据,判断是否一样
  72. if oldDws1.Vin == dws1.Vin {
  73. return true,nil
  74. } else {
  75. // 更新时间大于消息时间表示是后面的消息,不处理
  76. if oldDws1.UpdatedAt > dws1.UpdatedAt{
  77. return true,nil
  78. }
  79. // vin 码不一样
  80. // 插入过期数据
  81. dws2 := &model.Dws2{}
  82. dws2.PlateNo = oldDws1.PlateNo
  83. dws2.PlateType = oldDws1.PlateType
  84. dws2.Vin = oldDws1.Vin
  85. err = dws2.Insert(db)
  86. if err != nil {
  87. if !strings.Contains(err.Error(), "Duplicate") {
  88. return false,err
  89. }
  90. }
  91. // 更新为新数据
  92. oldDws1.Vin = dws1.Vin
  93. oldDws1.UpdatedAt = dws1.UpdatedAt
  94. err = oldDws1.Update(db)
  95. if err != nil{
  96. if !strings.Contains(err.Error(), "Duplicate") {
  97. return false,err
  98. }
  99. }
  100. }
  101. }
  102. return false,nil
  103. }
  104. // 车牌和vin码对应关系(正确关系)
  105. func Dws1Task(db *gorm.DB,dwsMessage *apis.DwsMessage, outputSourceCode string) (adsMsgList []*apis.AdsMessage, dwsMsgList []*apis.DwsMessage, err error) {
  106. dws1 := &model.Dws1{}
  107. err = json.Unmarshal([]byte(dwsMessage.Content), dws1)
  108. if err != nil {
  109. return nil, nil, nil
  110. }
  111. dws1.UpdatedAt = dwsMessage.Timestamp
  112. dws1.CreatedAt = dwsMessage.Timestamp
  113. // 参数判断
  114. if dws1.PlateNo == "" || dws1.Vin == ""{
  115. return nil ,nil ,nil
  116. }
  117. isExpire := gjson.Get(dwsMessage.Content, "is_expire").String()
  118. if isExpire != consts.EXPIREDATA {
  119. // 计算号牌种类
  120. if dws1.PlateType == ""{
  121. dws1.PlateType = calcPlateType(dws1.Long,dws1.ApprovedNumber,dws1.GrossMass)
  122. if dws1.PlateType == ""{
  123. dws1.PlateType = CalcPlateType(db,dws1.Vin)
  124. }
  125. }
  126. if dws1.PlateType == ""{
  127. // 号牌种类为空
  128. // 判断是否在dws1
  129. isInDws1 ,err := isInDws1(db,dws1)
  130. if err != nil{
  131. return nil,nil,err
  132. }
  133. if isInDws1{
  134. return nil,nil,nil
  135. }
  136. // 发送到dws4
  137. dwsMsg := NewDwsMessage(dwsMessage,outputSourceCode)
  138. msgByte, _ := json.Marshal(*dws1)
  139. dwsMsg.Content = string(msgByte)
  140. adsMsgs,_,err := Dws4Task(db,dwsMsg, consts.SOURCEDWS4)
  141. if err != nil{
  142. return nil,nil,err
  143. }
  144. adsMsgList = append(adsMsgList,adsMsgs...)
  145. }else{
  146. // 号牌种类不为空
  147. // 判断错误关系表中是否存在,存在直接返回
  148. isInDws2 ,err := isInDws2(db,dws1.PlateNo,dws1.PlateType,dws1.Vin)
  149. if err != nil{
  150. return nil,nil,err
  151. }
  152. if isInDws2 {
  153. return nil,nil,nil
  154. }
  155. isExist ,err := handlePlateTypeExist(dws1,db)
  156. if err != nil{
  157. return nil,nil,err
  158. }
  159. // 已经存在不同步到ads
  160. if isExist{
  161. return nil,nil,nil
  162. }
  163. // 发送到ads
  164. adsMsg := NewAdsMessage(dwsMessage, outputSourceCode, consts.ACTIONINSERT)
  165. msgByte, _ := json.Marshal(*dws1)
  166. adsMsg.Content = string(msgByte)
  167. adsMsgList = append(adsMsgList,adsMsg)
  168. }
  169. }
  170. return adsMsgList, nil, nil
  171. }