vehicle_item.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package utils
  4. import (
  5. "fmt"
  6. "regexp"
  7. "strings"
  8. "sync"
  9. "unicode"
  10. )
  11. var PropertyMap = map[string]string{}
  12. var VehicleTypeMap = map[string]string{}
  13. var PropertyCodeMap = map[string]string{}
  14. var VehicleTypeCodeMap = map[string]string{}
  15. var codeMux sync.RWMutex
  16. func IsHan(data string) bool {
  17. for _, r := range data {
  18. if unicode.Is(unicode.Scripts["Han"], r) {
  19. return true
  20. }
  21. }
  22. return false
  23. }
  24. func TrimNumberic(data string) string {
  25. pat := "[0-9]"
  26. re, _ := regexp.Compile(pat)
  27. str := re.ReplaceAllString(data, "")
  28. return str
  29. }
  30. func ClearData(data string) string {
  31. data = strings.Replace(data, " ", "", -1)
  32. data = strings.Replace(data, "/", "", -1)
  33. data = strings.Replace(data, "+", "", -1)
  34. data = strings.Replace(data, ",", "", -1)
  35. data = strings.Replace(data, ".", "", -1)
  36. data = strings.Replace(data, ",", "", -1)
  37. data = strings.Replace(data, "、", "", -1)
  38. return data
  39. }
  40. func GetArray(data string) []string {
  41. data = strings.Replace(data, " ", ";", -1)
  42. data = strings.Replace(data, "/", ";", -1)
  43. data = strings.Replace(data, "+", ";", -1)
  44. data = strings.Replace(data, ",", ";", -1)
  45. data = strings.Replace(data, ".", ";", -1)
  46. data = strings.Replace(data, ",", ";", -1)
  47. data = strings.Replace(data, ",", ";", -1)
  48. data = strings.Replace(data, "、", ";", -1)
  49. data = strings.Replace(data, "-", ";", -1)
  50. data = strings.Replace(data, "(", ";", -1)
  51. data = strings.Replace(data, "(", ";", -1)
  52. data = strings.Replace(data, ")", ";", -1)
  53. data = strings.Replace(data, ")", ";", -1)
  54. return strings.Split(data, ";")
  55. }
  56. func GetPropertyCodeDetail(data string) (string, string) {
  57. if value, ok := PropertyMap[data]; ok {
  58. return value, data
  59. }
  60. if strings.Contains(data, "非营业") {
  61. return PropertyMap["非营运"], "非营运"
  62. }
  63. if strings.Contains(data, "营业公路客运") {
  64. return PropertyMap["公路客运"], "公路客运"
  65. }
  66. if strings.Contains(data, "营业城市公交") {
  67. return PropertyMap["营业城市公交"], "营业城市公交"
  68. }
  69. if strings.Contains(data, "营业出租租赁") {
  70. return PropertyMap["出租客运"], "出租客运"
  71. }
  72. if strings.Contains(data, "营业挂车") {
  73. return PropertyMap["货运"], "货运"
  74. }
  75. if strings.Contains(data, "营业货车") {
  76. return PropertyMap["货运"], "货运"
  77. }
  78. if strings.Contains(data, "运输型") {
  79. return PropertyMap["货运"], "货运"
  80. }
  81. if strings.Contains(data, "化工") {
  82. return PropertyMap["危化品运输"], "危化品运输"
  83. }
  84. return "", data
  85. }
  86. func ParseCodeDetail(data string, codeMap map[string]string, detailMap map[string]string, f func(string) (string, string)) (string, string) {
  87. data = TrimNumberic(data)
  88. if IsHan(data) {
  89. array := GetArray(data)
  90. ret := ""
  91. for _, v := range array {
  92. code := ""
  93. if f != nil {
  94. code, _ = f(v)
  95. } else {
  96. code, _ = detailMap[v]
  97. }
  98. if strings.Contains(ret, code) {
  99. continue
  100. }
  101. ret = ret + code
  102. }
  103. return ret, data
  104. }
  105. if value, ok := codeMap[data]; ok {
  106. return data, value
  107. }
  108. ret := ""
  109. retCode := ""
  110. data = ClearData(data)
  111. for _, v := range data {
  112. code := fmt.Sprintf("%c", v)
  113. if ret == "" {
  114. if detail, ok := codeMap[code]; ok {
  115. ret = detail
  116. retCode = code
  117. }
  118. } else if detail, ok := codeMap[code]; ok {
  119. ret = fmt.Sprintf("%s+%s", ret, detail)
  120. retCode = retCode + code
  121. }
  122. }
  123. return retCode, ret
  124. }
  125. func ParseProperty(property string) (string, string) {
  126. codeMux.RLock()
  127. defer codeMux.RUnlock()
  128. property = strings.ToUpper(property)
  129. return ParseCodeDetail(property, PropertyCodeMap, PropertyMap, GetPropertyCodeDetail)
  130. }
  131. func ParseVehicleType(vehicleType string) (string, string) {
  132. codeMux.RLock()
  133. defer codeMux.RUnlock()
  134. if vehicleType == "轿车" {
  135. return VehicleTypeMap["小型轿车"], vehicleType
  136. }
  137. vehicleType = strings.ToUpper(vehicleType)
  138. if IsHan(vehicleType) {
  139. if value, ok := VehicleTypeMap[vehicleType]; ok {
  140. return value, vehicleType
  141. }
  142. return "", vehicleType
  143. }
  144. if value, ok := VehicleTypeCodeMap[vehicleType]; ok {
  145. return vehicleType, value
  146. }
  147. return "", vehicleType
  148. }