vehicle.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. package utils
  2. import (
  3. "regexp"
  4. "strconv"
  5. "strings"
  6. "unicode"
  7. "unicode/utf8"
  8. "adm-ods/common.in/jsonrpc2"
  9. )
  10. const (
  11. VINLEN = 17
  12. PLATELEN = 6
  13. PLATENEWLEN = 7
  14. SF = "京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川贵云藏陕甘青宁新"
  15. )
  16. var RUNMODE = "prod"
  17. func SetRunmode(runMode string) {
  18. RUNMODE = runMode
  19. }
  20. func ParsePlate(plate string) (string, string) {
  21. var index int
  22. for i, r := range plate {
  23. if !unicode.Is(unicode.Scripts["Han"], r) {
  24. index = i
  25. break
  26. }
  27. }
  28. sf := plate[0:index]
  29. hphm := plate[index:]
  30. return sf, hphm
  31. }
  32. func CheckVinFormat(vin string) (string, error) {
  33. vin = strings.TrimSpace(vin)
  34. vin = strings.ToUpper(vin)
  35. strconv.Itoa(len(vin))
  36. r, _ := regexp.Compile(`[0-9A-Z]{` + strconv.Itoa(len(vin)) + `}`)
  37. if r.MatchString(vin) {
  38. return vin, nil
  39. }
  40. return vin, jsonrpc2.NewJsonError(20003, "请求参数格式不对,车架号包含特殊字符")
  41. }
  42. func CheckVinFormat17Bit(vin string) (string, error) {
  43. vin = strings.TrimSpace(vin)
  44. vin = strings.ToUpper(vin)
  45. if len(vin) != VINLEN {
  46. return vin, jsonrpc2.NewJsonError(20003, "请求参数格式不对,车架号不为17位")
  47. }
  48. r, _ := regexp.Compile(`[0-9A-Z]{17}`)
  49. if r.MatchString(vin) {
  50. return vin, nil
  51. }
  52. return vin, jsonrpc2.NewJsonError(20003, "请求参数格式不对,车架号包含特殊字符")
  53. }
  54. func IsCommonPlateType(plateType string) bool {
  55. switch plateType {
  56. case "51":
  57. return true
  58. case "52":
  59. return true
  60. case "01":
  61. return true
  62. case "02":
  63. return true
  64. default:
  65. return false
  66. }
  67. }
  68. func isUnknowPlateType(plateType string) bool {
  69. switch plateType {
  70. case "02":
  71. return false
  72. case "03":
  73. return false
  74. case "04":
  75. return false
  76. case "15":
  77. return false
  78. case "16":
  79. return false
  80. case "23":
  81. return false
  82. case "24":
  83. return false
  84. case "26":
  85. return false
  86. case "27":
  87. return false
  88. default:
  89. return true
  90. }
  91. }
  92. func getPlateType(plateNo string) string {
  93. switch {
  94. case strings.HasPrefix(plateNo, "使"):
  95. return "03"
  96. case strings.HasSuffix(plateNo, "使"):
  97. return "03"
  98. // 领馆汽车
  99. case strings.HasSuffix(plateNo, "领"):
  100. return "04"
  101. case strings.HasSuffix(plateNo, "挂"):
  102. return "15"
  103. case strings.HasSuffix(plateNo, "学"):
  104. return "16"
  105. case strings.HasSuffix(plateNo, "警"):
  106. return "23"
  107. case strings.HasSuffix(plateNo, "港"):
  108. return "26"
  109. case strings.HasSuffix(plateNo, "澳"):
  110. return "27"
  111. default:
  112. return "02"
  113. }
  114. }
  115. func isSupportPlateType(plateType, plateNumber string) bool {
  116. /*switch plateType {
  117. case "01":
  118. return true
  119. case "02":
  120. return true
  121. case "03":
  122. return true
  123. case "04":
  124. return true
  125. case "05":
  126. return true
  127. case "06":
  128. return true
  129. case "07":
  130. return true
  131. case "08":
  132. return true
  133. case "09":
  134. return true
  135. case "10":
  136. return true
  137. case "11":
  138. return true
  139. case "12":
  140. return true
  141. case "13":
  142. return true
  143. case "14":
  144. return true
  145. case "15":
  146. return true
  147. case "16":
  148. return true
  149. case "17":
  150. return true
  151. case "20":
  152. return true
  153. case "21":
  154. return true
  155. case "22":
  156. return true
  157. case "23":
  158. return true
  159. case "24":
  160. return true
  161. case "25":
  162. return true
  163. case "26":
  164. return true
  165. case "27":
  166. return true
  167. case "51":
  168. return true
  169. case "52":
  170. return true
  171. case "99":
  172. return true
  173. default:
  174. return false
  175. }*/
  176. if plateType != "" {
  177. if plateType != "02" && plateType != "52" {
  178. return false
  179. }
  180. } else {
  181. for _, r := range plateNumber {
  182. if unicode.Is(unicode.Scripts["Han"], r) {
  183. return false
  184. }
  185. }
  186. }
  187. return true
  188. }
  189. func CheckPlateNoFormat(plateNo *string, plateType string, merchantId int64) (string, error) {
  190. *plateNo = strings.TrimSpace(*plateNo)
  191. *plateNo = strings.ToUpper(*plateNo)
  192. return plateType, nil
  193. if RUNMODE != "prod" {
  194. if strings.HasPrefix(*plateNo, "测") {
  195. return plateType, nil
  196. }
  197. }
  198. sf, plateNumber := ParsePlate(*plateNo)
  199. plateLen := utf8.RuneCountInString(plateNumber)
  200. if strings.HasSuffix(*plateNo, "使") {
  201. if plateType == "" {
  202. plateType = "03"
  203. }
  204. }
  205. if plateType == "" && plateLen == PLATENEWLEN {
  206. if plateNumber[1] > 64 && plateNumber[1] < 91 {
  207. plateType = "52"
  208. } else if plateNumber[PLATENEWLEN-1] > 64 && plateNumber[PLATENEWLEN-1] < 91 {
  209. plateType = "51"
  210. } else {
  211. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,新能源汽车车牌格式不正确")
  212. }
  213. }
  214. if !isSupportPlateType(plateType, plateNumber) && merchantId != 15 { //15号GD商户
  215. return "", jsonrpc2.NewJsonError(20004, "参数错误,暂不支持非小型车号牌调用")
  216. }
  217. // 判断车牌号码合法性
  218. if len(sf) == 0 && !strings.HasSuffix(*plateNo, "使") {
  219. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,车牌号码格式不正确")
  220. } else if sf != "使" {
  221. if !strings.Contains(SF, sf) {
  222. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,不支持的省份")
  223. }
  224. }
  225. // 检查车牌是否包含i o字符
  226. if strings.Contains(plateNumber, "I") || strings.Contains(plateNumber, "O") {
  227. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,车牌号码包含I或O字符")
  228. }
  229. count := 0
  230. for _, v := range plateNumber {
  231. if 64 < v && v < 91 {
  232. count++
  233. }
  234. }
  235. if strings.HasPrefix(*plateNo, "使") || strings.HasSuffix(*plateNo, "使") {
  236. if plateType == "" {
  237. return "03", nil
  238. } else {
  239. if plateType != "03" {
  240. return "", jsonrpc2.NewJsonError(20003, "请求参数格式不对,车牌和车牌类型不匹配")
  241. }
  242. return plateType, nil
  243. }
  244. } else if strings.HasSuffix(*plateNo, "领") {
  245. if plateType == "" {
  246. return "04", nil
  247. } else {
  248. if plateType != "04" {
  249. return "", jsonrpc2.NewJsonError(20003, "请求参数格式不对,车牌和车牌类型不匹配")
  250. }
  251. return plateType, nil
  252. }
  253. } else if plateType == "51" || plateType == "52" {
  254. if count > 3 {
  255. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,新能源车牌号码超过3个字母")
  256. } else if plateLen != PLATENEWLEN {
  257. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,新能源车牌号码长度不正确")
  258. } else {
  259. if plateNumber[1] > 64 && plateNumber[1] < 91 {
  260. if plateType != "52" {
  261. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,新能源车牌号码和车牌类型不匹配")
  262. }
  263. } else if plateNumber[PLATENEWLEN-1] > 64 && plateNumber[PLATENEWLEN-1] < 91 {
  264. if plateType != "51" {
  265. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,新能源车牌号码和车牌类型不匹配")
  266. }
  267. } else {
  268. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,新能源汽车车牌格式不正确")
  269. }
  270. }
  271. } else {
  272. if count > 3 {
  273. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,车牌号码超过3个字母")
  274. } else if plateLen != PLATELEN {
  275. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,车牌号码长度不正确")
  276. }
  277. }
  278. if strings.Contains("0123456789", plateNumber[0:1]) {
  279. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,号牌号码第一位不能为数字")
  280. }
  281. if plateType == "51" || plateType == "52" {
  282. return plateType, nil
  283. }
  284. if plateType != "" && isUnknowPlateType(plateType) {
  285. return plateType, nil
  286. }
  287. plateTypeNew := getPlateType(*plateNo)
  288. if plateType != "" && plateTypeNew != plateType {
  289. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,车牌号码和车牌类型不匹配")
  290. }
  291. return plateTypeNew, nil
  292. }
  293. func VehicleDefaultPlateType(plateNo *string) (plateType string, err error) {
  294. *plateNo = strings.TrimSpace(*plateNo)
  295. *plateNo = strings.ToUpper(*plateNo)
  296. sf, plateNumber := ParsePlate(*plateNo)
  297. if len(sf) == 0 {
  298. return "", jsonrpc2.NewJsonError(20003, "参数格式不对,车牌号码无省份")
  299. }
  300. plateLen := utf8.RuneCountInString(plateNumber)
  301. if plateLen == PLATENEWLEN {
  302. if plateNumber[1] > 64 && plateNumber[1] < 91 {
  303. plateType = "52"
  304. return plateType, nil
  305. } else if plateNumber[PLATENEWLEN-1] > 64 && plateNumber[PLATENEWLEN-1] < 91 {
  306. plateType = "51"
  307. return plateType, nil
  308. } else {
  309. return plateType, jsonrpc2.NewJsonError(20003, "参数格式不对,新能源汽车车牌格式不正确")
  310. }
  311. } else if plateLen == PLATELEN {
  312. plateType = getPlateType(*plateNo)
  313. return plateType, nil
  314. } else {
  315. return plateType, jsonrpc2.NewJsonError(20003, "参数格式不对,车牌长度不正确")
  316. }
  317. return plateType, nil
  318. }
  319. func CheckEngineFormat(engineNo string) error {
  320. if len(engineNo) < 6 {
  321. return jsonrpc2.NewJsonError(20003, "参数格式不对,发动机号小于6位")
  322. }
  323. for _, r := range engineNo {
  324. if unicode.Is(unicode.Scripts["Han"], r) {
  325. return jsonrpc2.NewJsonError(20003, "参数格式不对,发动机号包括汉字")
  326. }
  327. }
  328. return nil
  329. }
  330. // 获取车辆是车型,0 烧油,1 电动
  331. func GetDefaultPlateType(plateno string) int {
  332. _, plateNumber := ParsePlate(plateno)
  333. plateLen := len(strings.TrimSpace(plateNumber))
  334. if plateLen == PLATELEN {
  335. return 0
  336. } else if plateLen == PLATENEWLEN {
  337. return 1
  338. }
  339. return 2
  340. }
  341. func AddSySource(sySoucre, sourceName string) string {
  342. if sySoucre == "" {
  343. sySoucre = sourceName
  344. } else if !strings.Contains(sySoucre, sourceName) {
  345. sySoucre = sySoucre + "," + sourceName
  346. }
  347. return sySoucre
  348. }
  349. func ParseEngineNo(engineNo, engineType string) string {
  350. if strings.HasPrefix(engineNo, engineType) {
  351. engineNo = strings.TrimSpace(engineNo[len(engineType):])
  352. }
  353. var re string
  354. for _, v := range engineNo {
  355. if (v >= '0' && v <= '9') || (v >= 'a' && v <= 'z') || (v >= 'A' && v <= 'Z') || v == '/' {
  356. re = re + string(v)
  357. }
  358. }
  359. return re
  360. }
  361. func GetSeat(seatStr string) int {
  362. seatList := strings.Split(seatStr, "-")
  363. maxSeat := 0
  364. for _, v := range seatList {
  365. seat, _ := strconv.Atoi(v)
  366. if seat > maxSeat {
  367. maxSeat = seat
  368. }
  369. }
  370. return maxSeat
  371. }