task11.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/common.in/utils"
  7. "gadm-dws/consts"
  8. "gadm-dws/model"
  9. "encoding/json"
  10. "fmt"
  11. "gorm.io/gorm"
  12. "strings"
  13. )
  14. func dws11TaskImpl(db *gorm.DB,dwsMessage *apis.DwsMessage, outputSourceCode string,dws11 *model.Dws11) (adsMsgList []*apis.AdsMessage, dwsMsgList []*apis.DwsMessage, err error) {
  15. dws11.ID = 0
  16. dws11.Uniq = utils.MD5(dws11.VinRule+dws11.ModelNo)
  17. err = dws11.Query(db,map[string]interface{}{"uniq":dws11.Uniq})
  18. if err != nil{
  19. if err != gorm.ErrRecordNotFound{
  20. return nil,nil,err
  21. }
  22. }else{
  23. return nil,nil,nil
  24. }
  25. err = dws11.Insert(db)
  26. if err != nil{
  27. if !strings.Contains(err.Error(), "Duplicate") {
  28. return nil, nil,err
  29. }else{
  30. return nil,nil ,nil
  31. }
  32. }
  33. adsMsg := NewAdsMessage(dwsMessage, outputSourceCode, consts.ACTIONINSERT)
  34. msgByte, _ := json.Marshal(*dws11)
  35. adsMsg.Content = string(msgByte)
  36. adsMsgList = append(adsMsgList,adsMsg)
  37. return adsMsgList, nil, nil
  38. }
  39. func checkIsNormalVin(vinRule string) bool {
  40. for _,v := range vinRule{
  41. if (v>=48 && v <=57) || (v>=65 && v<=90){
  42. continue
  43. }else{
  44. return false
  45. }
  46. }
  47. return true
  48. }
  49. // vin码model_no关系
  50. func Dws11Task(db *gorm.DB,dwsMessage *apis.DwsMessage, outputSourceCode string) (adsMsgList []*apis.AdsMessage, dwsMsgList []*apis.DwsMessage, err error) {
  51. dws11 := &model.Dws11{}
  52. err = json.Unmarshal([]byte(dwsMessage.Content), dws11)
  53. if err != nil {
  54. return nil, nil, nil
  55. }
  56. //fmt.Println("dws11:",*dws11)
  57. if dws11.ModelNo == "" || (dws11.Vin == "" && dws11.VinRules == ""){
  58. return nil, nil, nil
  59. }
  60. dws11.ModelNo = strings.Replace(dws11.ModelNo,",","",-1)
  61. if dws11.Vin != ""{
  62. if len(dws11.Vin) > 8 {
  63. dws11.VinRule = dws11.Vin[0:8]
  64. }else{
  65. return nil,nil,nil
  66. }
  67. return dws11TaskImpl(db,dwsMessage,outputSourceCode,dws11)
  68. }else {
  69. if dws11.VinRules != ""{
  70. /*var result string
  71. for _,v := range dws11.VinRules{
  72. if v == 13 || v == 10{
  73. result = result + ","
  74. }else{
  75. result = result + string(v)
  76. }
  77. }
  78. dws11.VinRules = result*/
  79. dws11.VinRules = strings.Replace(dws11.VinRules,"\r",",",-1)
  80. dws11.VinRules = strings.Replace(dws11.VinRules,"\n",",",-1)
  81. }
  82. // 整车:LFBJEBA7××××××××× 截断前缀
  83. /*vinRuleList := strings.Split( dws11.VinRules ,":")
  84. if len(vinRuleList) > 1{
  85. dws11.VinRules = vinRuleList[1]
  86. }*/
  87. vinRules := strings.Split( dws11.VinRules ,",")
  88. for _,v := range vinRules{
  89. // 整车:LFBJEBA7××××××××× 截断前缀
  90. if strings.Contains(v,":"){
  91. vinRuleList := strings.Split( v ,":")
  92. if len(vinRuleList) > 1{
  93. v = vinRuleList[1]
  94. }
  95. }
  96. vinLen := len(v)
  97. if vinLen > 8 {
  98. if vinLen > 18{
  99. fmt.Println("vin过长:",dws11.ModelNo,dws11.VinRules)
  100. }
  101. dws11.VinRule = v[0:8]
  102. dws11.VinRule = strings.ToUpper(dws11.VinRule)
  103. if !checkIsNormalVin(dws11.VinRule){
  104. fmt.Println("错误的vin rule:",dws11.ModelNo,dws11.VinRules)
  105. continue
  106. }
  107. /*if strings.Contains(dws11.VinRule,"×"){
  108. continue
  109. }*/
  110. adsMsgs,_,err := dws11TaskImpl(db,dwsMessage,outputSourceCode,dws11)
  111. if err != nil{
  112. return nil,nil,err
  113. }
  114. adsMsgList = append(adsMsgList,adsMsgs...)
  115. }
  116. }
  117. }
  118. /*dws11.Uniq = utils.MD5(dws11.VinRule+dws11.ModelNo)
  119. err = dws11.Query(db,map[string]interface{}{"uniq":dws11.Uniq})
  120. if err != nil{
  121. if err != gorm.ErrRecordNotFound{
  122. return nil,nil,err
  123. }
  124. }else{
  125. return nil,nil,nil
  126. }
  127. err = dws11.Insert(db)
  128. if err != nil{
  129. if !strings.Contains(err.Error(), "Duplicate") {
  130. return nil, nil,err
  131. }else{
  132. return nil,nil ,nil
  133. }
  134. }
  135. adsMsg := NewAdsMessage(dwsMessage, outputSourceCode, consts.ACTIONINSERT)
  136. msgByte, _ := json.Marshal(*dws11)
  137. adsMsg.Content = string(msgByte)
  138. adsMsgList = append(adsMsgList,adsMsg)*/
  139. return adsMsgList, nil, nil
  140. }