123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package utils
- import (
- "fmt"
- "regexp"
- "strings"
- "sync"
- "unicode"
- )
- var PropertyMap = map[string]string{}
- var VehicleTypeMap = map[string]string{}
- var PropertyCodeMap = map[string]string{}
- var VehicleTypeCodeMap = map[string]string{}
- var codeMux sync.RWMutex
- func IsHan(data string) bool {
- for _, r := range data {
- if unicode.Is(unicode.Scripts["Han"], r) {
- return true
- }
- }
- return false
- }
- func TrimNumberic(data string) string {
- pat := "[0-9]"
- re, _ := regexp.Compile(pat)
- str := re.ReplaceAllString(data, "")
- return str
- }
- func ClearData(data string) string {
- data = strings.Replace(data, " ", "", -1)
- data = strings.Replace(data, "/", "", -1)
- data = strings.Replace(data, "+", "", -1)
- data = strings.Replace(data, ",", "", -1)
- data = strings.Replace(data, ".", "", -1)
- data = strings.Replace(data, ",", "", -1)
- data = strings.Replace(data, "、", "", -1)
- return data
- }
- func GetArray(data string) []string {
- data = strings.Replace(data, " ", ";", -1)
- data = strings.Replace(data, "/", ";", -1)
- data = strings.Replace(data, "+", ";", -1)
- data = strings.Replace(data, ",", ";", -1)
- data = strings.Replace(data, ".", ";", -1)
- data = strings.Replace(data, ",", ";", -1)
- data = strings.Replace(data, ",", ";", -1)
- data = strings.Replace(data, "、", ";", -1)
- data = strings.Replace(data, "-", ";", -1)
- data = strings.Replace(data, "(", ";", -1)
- data = strings.Replace(data, "(", ";", -1)
- data = strings.Replace(data, ")", ";", -1)
- data = strings.Replace(data, ")", ";", -1)
- return strings.Split(data, ";")
- }
- func GetPropertyCodeDetail(data string) (string, string) {
- if value, ok := PropertyMap[data]; ok {
- return value, data
- }
- if strings.Contains(data, "非营业") {
- return PropertyMap["非营运"], "非营运"
- }
- if strings.Contains(data, "营业公路客运") {
- return PropertyMap["公路客运"], "公路客运"
- }
- if strings.Contains(data, "营业城市公交") {
- return PropertyMap["营业城市公交"], "营业城市公交"
- }
- if strings.Contains(data, "营业出租租赁") {
- return PropertyMap["出租客运"], "出租客运"
- }
- if strings.Contains(data, "营业挂车") {
- return PropertyMap["货运"], "货运"
- }
- if strings.Contains(data, "营业货车") {
- return PropertyMap["货运"], "货运"
- }
- if strings.Contains(data, "运输型") {
- return PropertyMap["货运"], "货运"
- }
- if strings.Contains(data, "化工") {
- return PropertyMap["危化品运输"], "危化品运输"
- }
- return "", data
- }
- func ParseCodeDetail(data string, codeMap map[string]string, detailMap map[string]string, f func(string) (string, string)) (string, string) {
- data = TrimNumberic(data)
- if IsHan(data) {
- array := GetArray(data)
- ret := ""
- for _, v := range array {
- code := ""
- if f != nil {
- code, _ = f(v)
- } else {
- code, _ = detailMap[v]
- }
- if strings.Contains(ret, code) {
- continue
- }
- ret = ret + code
- }
- return ret, data
- }
- if value, ok := codeMap[data]; ok {
- return data, value
- }
- ret := ""
- retCode := ""
- data = ClearData(data)
- for _, v := range data {
- code := fmt.Sprintf("%c", v)
- if ret == "" {
- if detail, ok := codeMap[code]; ok {
- ret = detail
- retCode = code
- }
- } else if detail, ok := codeMap[code]; ok {
- ret = fmt.Sprintf("%s+%s", ret, detail)
- retCode = retCode + code
- }
- }
- return retCode, ret
- }
- func ParseProperty(property string) (string, string) {
- codeMux.RLock()
- defer codeMux.RUnlock()
- property = strings.ToUpper(property)
- return ParseCodeDetail(property, PropertyCodeMap, PropertyMap, GetPropertyCodeDetail)
- }
- func ParseVehicleType(vehicleType string) (string, string) {
- codeMux.RLock()
- defer codeMux.RUnlock()
- if vehicleType == "轿车" {
- return VehicleTypeMap["小型轿车"], vehicleType
- }
- vehicleType = strings.ToUpper(vehicleType)
- if IsHan(vehicleType) {
- if value, ok := VehicleTypeMap[vehicleType]; ok {
- return value, vehicleType
- }
- return "", vehicleType
- }
- if value, ok := VehicleTypeCodeMap[vehicleType]; ok {
- return vehicleType, value
- }
- return "", vehicleType
- }
|