vehicle.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. package utils
  2. import (
  3. "regexp"
  4. "strconv"
  5. "strings"
  6. "unicode"
  7. "unicode/utf8"
  8. "adm-dws/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 GetPlateType(plateNo string) string {
  116. switch {
  117. case strings.HasPrefix(plateNo, "使"):
  118. return "03"
  119. case strings.HasSuffix(plateNo, "使"):
  120. return "03"
  121. // 领馆汽车
  122. case strings.HasSuffix(plateNo, "领"):
  123. return "04"
  124. case strings.HasSuffix(plateNo, "挂"):
  125. return "15"
  126. case strings.HasSuffix(plateNo, "学"):
  127. return "16"
  128. case strings.HasSuffix(plateNo, "警"):
  129. return "23"
  130. case strings.HasSuffix(plateNo, "港"):
  131. return "26"
  132. case strings.HasSuffix(plateNo, "澳"):
  133. return "27"
  134. default:
  135. return "02"
  136. }
  137. }
  138. func isSupportPlateType(plateType, plateNumber string) bool {
  139. /*switch plateType {
  140. case "01":
  141. return true
  142. case "02":
  143. return true
  144. case "03":
  145. return true
  146. case "04":
  147. return true
  148. case "05":
  149. return true
  150. case "06":
  151. return true
  152. case "07":
  153. return true
  154. case "08":
  155. return true
  156. case "09":
  157. return true
  158. case "10":
  159. return true
  160. case "11":
  161. return true
  162. case "12":
  163. return true
  164. case "13":
  165. return true
  166. case "14":
  167. return true
  168. case "15":
  169. return true
  170. case "16":
  171. return true
  172. case "17":
  173. return true
  174. case "20":
  175. return true
  176. case "21":
  177. return true
  178. case "22":
  179. return true
  180. case "23":
  181. return true
  182. case "24":
  183. return true
  184. case "25":
  185. return true
  186. case "26":
  187. return true
  188. case "27":
  189. return true
  190. case "51":
  191. return true
  192. case "52":
  193. return true
  194. case "99":
  195. return true
  196. default:
  197. return false
  198. }*/
  199. if plateType != "" {
  200. if plateType != "02" && plateType != "52" {
  201. return false
  202. }
  203. } else {
  204. for _, r := range plateNumber {
  205. if unicode.Is(unicode.Scripts["Han"], r) {
  206. return false
  207. }
  208. }
  209. }
  210. return true
  211. }
  212. func CheckPlateNoFormat(plateNo *string, plateType string, merchantId int64) (string, error) {
  213. *plateNo = strings.TrimSpace(*plateNo)
  214. *plateNo = strings.ToUpper(*plateNo)
  215. return plateType, nil
  216. if RUNMODE != "prod" {
  217. if strings.HasPrefix(*plateNo, "测") {
  218. return plateType, nil
  219. }
  220. }
  221. sf, plateNumber := ParsePlate(*plateNo)
  222. plateLen := utf8.RuneCountInString(plateNumber)
  223. if strings.HasSuffix(*plateNo, "使") {
  224. if plateType == "" {
  225. plateType = "03"
  226. }
  227. }
  228. if plateType == "" && plateLen == PLATENEWLEN {
  229. if plateNumber[1] > 64 && plateNumber[1] < 91 {
  230. plateType = "52"
  231. } else if plateNumber[PLATENEWLEN-1] > 64 && plateNumber[PLATENEWLEN-1] < 91 {
  232. plateType = "51"
  233. } else {
  234. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,新能源汽车车牌格式不正确")
  235. }
  236. }
  237. if !isSupportPlateType(plateType, plateNumber) && merchantId != 15 { //15号GD商户
  238. return "", jsonrpc2.NewJsonError(20004, "参数错误,暂不支持非小型车号牌调用")
  239. }
  240. // 判断车牌号码合法性
  241. if len(sf) == 0 && !strings.HasSuffix(*plateNo, "使") {
  242. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,车牌号码格式不正确")
  243. } else if sf != "使" {
  244. if !strings.Contains(SF, sf) {
  245. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,不支持的省份")
  246. }
  247. }
  248. // 检查车牌是否包含i o字符
  249. if strings.Contains(plateNumber, "I") || strings.Contains(plateNumber, "O") {
  250. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,车牌号码包含I或O字符")
  251. }
  252. count := 0
  253. for _, v := range plateNumber {
  254. if 64 < v && v < 91 {
  255. count++
  256. }
  257. }
  258. if strings.HasPrefix(*plateNo, "使") || strings.HasSuffix(*plateNo, "使") {
  259. if plateType == "" {
  260. return "03", nil
  261. } else {
  262. if plateType != "03" {
  263. return "", jsonrpc2.NewJsonError(20003, "请求参数格式不对,车牌和车牌类型不匹配")
  264. }
  265. return plateType, nil
  266. }
  267. } else if strings.HasSuffix(*plateNo, "领") {
  268. if plateType == "" {
  269. return "04", nil
  270. } else {
  271. if plateType != "04" {
  272. return "", jsonrpc2.NewJsonError(20003, "请求参数格式不对,车牌和车牌类型不匹配")
  273. }
  274. return plateType, nil
  275. }
  276. } else if plateType == "51" || plateType == "52" {
  277. if count > 3 {
  278. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,新能源车牌号码超过3个字母")
  279. } else if plateLen != PLATENEWLEN {
  280. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,新能源车牌号码长度不正确")
  281. } else {
  282. if plateNumber[1] > 64 && plateNumber[1] < 91 {
  283. if plateType != "52" {
  284. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,新能源车牌号码和车牌类型不匹配")
  285. }
  286. } else if plateNumber[PLATENEWLEN-1] > 64 && plateNumber[PLATENEWLEN-1] < 91 {
  287. if plateType != "51" {
  288. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,新能源车牌号码和车牌类型不匹配")
  289. }
  290. } else {
  291. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,新能源汽车车牌格式不正确")
  292. }
  293. }
  294. } else {
  295. if count > 3 {
  296. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,车牌号码超过3个字母")
  297. } else if plateLen != PLATELEN {
  298. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,车牌号码长度不正确")
  299. }
  300. }
  301. if strings.Contains("0123456789", plateNumber[0:1]) {
  302. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,号牌号码第一位不能为数字")
  303. }
  304. if plateType == "51" || plateType == "52" {
  305. return plateType, nil
  306. }
  307. if plateType != "" && isUnknowPlateType(plateType) {
  308. return plateType, nil
  309. }
  310. plateTypeNew := getPlateType(*plateNo)
  311. if plateType != "" && plateTypeNew != plateType {
  312. return plateType, jsonrpc2.NewJsonError(20003, "请求参数格式不对,车牌号码和车牌类型不匹配")
  313. }
  314. return plateTypeNew, nil
  315. }
  316. func VehicleDefaultPlateType(plateNo *string) (plateType string, err error) {
  317. *plateNo = strings.TrimSpace(*plateNo)
  318. *plateNo = strings.ToUpper(*plateNo)
  319. sf, plateNumber := ParsePlate(*plateNo)
  320. if len(sf) == 0 {
  321. return "", jsonrpc2.NewJsonError(20003, "参数格式不对,车牌号码无省份")
  322. }
  323. plateLen := utf8.RuneCountInString(plateNumber)
  324. if plateLen == PLATENEWLEN {
  325. if plateNumber[1] > 64 && plateNumber[1] < 91 {
  326. plateType = "52"
  327. return plateType, nil
  328. } else if plateNumber[PLATENEWLEN-1] > 64 && plateNumber[PLATENEWLEN-1] < 91 {
  329. plateType = "51"
  330. return plateType, nil
  331. } else {
  332. return plateType, jsonrpc2.NewJsonError(20003, "参数格式不对,新能源汽车车牌格式不正确")
  333. }
  334. } else if plateLen == PLATELEN {
  335. plateType = getPlateType(*plateNo)
  336. return plateType, nil
  337. } else {
  338. return plateType, jsonrpc2.NewJsonError(20003, "参数格式不对,车牌长度不正确")
  339. }
  340. return plateType, nil
  341. }
  342. func CheckEngineFormat(engineNo string) error {
  343. if len(engineNo) < 6 {
  344. return jsonrpc2.NewJsonError(20003, "参数格式不对,发动机号小于6位")
  345. }
  346. for _, r := range engineNo {
  347. if unicode.Is(unicode.Scripts["Han"], r) {
  348. return jsonrpc2.NewJsonError(20003, "参数格式不对,发动机号包括汉字")
  349. }
  350. }
  351. return nil
  352. }
  353. // 获取车辆是车型,0 烧油,1 电动
  354. func GetDefaultPlateType(plateno string) int {
  355. _, plateNumber := ParsePlate(plateno)
  356. plateLen := len(strings.TrimSpace(plateNumber))
  357. if plateLen == PLATELEN {
  358. return 0
  359. } else if plateLen == PLATENEWLEN {
  360. return 1
  361. }
  362. return 2
  363. }
  364. func AddSySource(sySoucre, sourceName string) string {
  365. if sySoucre == "" {
  366. sySoucre = sourceName
  367. } else if !strings.Contains(sySoucre, sourceName) {
  368. sySoucre = sySoucre + "," + sourceName
  369. }
  370. return sySoucre
  371. }
  372. func ParseEngineNo(engineNo, engineType string) string {
  373. if strings.HasPrefix(engineNo, engineType) {
  374. engineNo = strings.TrimSpace(engineNo[len(engineType):])
  375. }
  376. var re string
  377. for _, v := range engineNo {
  378. if (v >= '0' && v <= '9') || (v >= 'a' && v <= 'z') || (v >= 'A' && v <= 'Z') || v == '/' {
  379. re = re + string(v)
  380. }
  381. }
  382. return re
  383. }
  384. func GetSeat(seatStr string) int {
  385. seatList := strings.Split(seatStr, "-")
  386. maxSeat := 0
  387. for _, v := range seatList {
  388. seat, _ := strconv.Atoi(v)
  389. if seat > maxSeat {
  390. maxSeat = seat
  391. }
  392. }
  393. return maxSeat
  394. }