task12.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. "gorm.io/gorm"
  10. "strings"
  11. )
  12. func supplementDws12(old, new *model.Dws12) (bool) {
  13. isSupple := false
  14. if old.PlateType == "" && new.PlateType != "" {
  15. isSupple = true
  16. old.PlateType = new.PlateType
  17. }
  18. if new.VehicleType != "" {
  19. if old.VehicleType != new.VehicleType{
  20. isSupple = true
  21. old.VehicleType = new.VehicleType
  22. }
  23. }
  24. if old.VehicleTypeDetail == "" && new.VehicleTypeDetail != "" {
  25. isSupple = true
  26. old.VehicleTypeDetail = new.VehicleTypeDetail
  27. }
  28. return isSupple
  29. }
  30. func calcMoto(dws12 *model.Dws12){
  31. // 摩托车不处理
  32. if dws12.VehicleTypeDetail != "" && strings.Contains(dws12.VehicleTypeDetail,"摩托车"){
  33. if dws12.VehicleTypeDetail == "两轮轻便摩托车"{
  34. dws12.VehicleType = "M22"
  35. dws12.PlateType = "08"
  36. }else if dws12.VehicleTypeDetail == "两轮摩托车"{
  37. dws12.VehicleType = "M21"
  38. dws12.PlateType = "07"
  39. }else{
  40. if dws12.TyreNumber == "2"{
  41. if dws12.RatedPower <= "4" && dws12.Displacement<= "50"{
  42. dws12.VehicleType = "M22"
  43. dws12.PlateType = "08"
  44. }else{
  45. dws12.VehicleType = "M21"
  46. dws12.PlateType = "07"
  47. }
  48. }else{
  49. if dws12.RatedPower <= "4" && dws12.Displacement<= "50"{
  50. dws12.PlateType = "08"
  51. }else{
  52. dws12.PlateType = "07"
  53. }
  54. }
  55. }
  56. }else{
  57. dws12.VehicleType = ""
  58. dws12.PlateType = ""
  59. }
  60. }
  61. // 计算号牌种类
  62. func Dws12Task(db *gorm.DB,dwsMessage *apis.DwsMessage, outputSourceCode string) (adsMsgList []*apis.AdsMessage, dwsMsgList []*apis.DwsMessage, err error) {
  63. dws12 := &model.Dws12{}
  64. err = json.Unmarshal([]byte(dwsMessage.Content), dws12)
  65. if err != nil {
  66. return nil, nil, nil
  67. }
  68. if dws12.ModelNo == ""{
  69. return nil, nil, nil
  70. }
  71. // 摩托车不处理
  72. /*if dws12.VehicleTypeDetail != "" && strings.Contains(dws12.VehicleTypeDetail,"摩托车"){
  73. return nil,nil,nil
  74. }*/
  75. dws12.ModelNo = strings.Replace(dws12.ModelNo,",","",-1)
  76. if dws12.Long != ""{
  77. //dws12.Long,_ = strconv.Unquote(dws12.Long)
  78. dws12.Long = strings.Replace(dws12.Long,`"`,"",-1)
  79. dws12.Long = strings.Split(dws12.Long,",")[0]
  80. }
  81. if dws12.ApprovedNumber != ""{
  82. dws12.ApprovedNumber = strings.Replace(dws12.ApprovedNumber,`"`,"",-1)
  83. dws12.ApprovedNumber = strings.Split(dws12.ApprovedNumber,",")[0]
  84. }
  85. if dws12.GrossMass != ""{
  86. dws12.GrossMass = strings.Replace(dws12.GrossMass,`"`,"",-1)
  87. //dws12.GrossMass,_ = strconv.Unquote(dws12.GrossMass)
  88. dws12.GrossMass = strings.Split(dws12.GrossMass,",")[0]
  89. }
  90. if dws12.IsMoto == "1"{
  91. calcMoto(dws12)
  92. }else{
  93. if dws12.PlateType == ""{
  94. dws12.PlateType = calcPlateType(dws12.Long,dws12.ApprovedNumber,dws12.GrossMass)
  95. }
  96. }
  97. if dws12.PlateType == ""{
  98. return nil ,nil,nil
  99. }
  100. oldDws12 := &model.Dws12{}
  101. err = oldDws12.Query(db,map[string]interface{}{"model_no":dws12.ModelNo})
  102. if err == nil {
  103. if supplementDws12(oldDws12,dws12){
  104. err = oldDws12.Update(db)
  105. if err != nil{
  106. return nil,nil,err
  107. }
  108. }else{
  109. return nil, nil, nil
  110. }
  111. }else{
  112. if err != gorm.ErrRecordNotFound{
  113. return nil, nil, err
  114. }
  115. err = dws12.Insert(db)
  116. if err != nil{
  117. if !strings.Contains(err.Error(), "Duplicate") {
  118. return nil, nil,err
  119. }
  120. }
  121. oldDws12 = dws12
  122. }
  123. adsMsg := NewAdsMessage(dwsMessage, outputSourceCode, consts.ACTIONINSERT)
  124. msgByte, _ := json.Marshal(*oldDws12)
  125. adsMsg.Content = string(msgByte)
  126. adsMsgList = append(adsMsgList,adsMsg)
  127. return adsMsgList, nil, nil
  128. }