123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736 |
- package query
- import (
- "context"
- "encoding/json"
- "fmt"
- "gd_adm_data/apis"
- "gd_adm_data/errors"
- "gd_adm_data/model"
- "gd_adm_data/utils"
- "strconv"
- "strings"
- jsoniter "github.com/json-iterator/go"
- "git.getensh.com/common/gopkgsv2/database"
- )
- func calcMoto(vehicleTypeDetail, tyreNumber, displacement, ratedPower string) (plateType string, vehicleType string) {
- // 摩托车不处理
- if vehicleTypeDetail != "" && strings.Contains(vehicleTypeDetail, "摩托") {
- if vehicleTypeDetail == "两轮轻便摩托车" {
- vehicleType = "M22"
- plateType = "08"
- } else if vehicleTypeDetail == "两轮摩托车" {
- vehicleType = "M21"
- plateType = "07"
- } else {
- ratedPowerFloat, _ := strconv.ParseFloat(ratedPower, 64)
- displacementFloat, _ := strconv.ParseFloat(displacement, 64)
- if tyreNumber == "2" {
- if ratedPowerFloat <= 4 && displacementFloat <= 50 {
- vehicleType = "M22"
- plateType = "08"
- } else {
- vehicleType = "M21"
- plateType = "07"
- }
- } else {
- if ratedPowerFloat <= 4 && displacementFloat <= 50 {
- plateType = "08"
- } else {
- plateType = "07"
- }
- }
- }
- }
- return plateType, vehicleType
- }
- func getString(key string, old map[string]interface{}) string {
- if v, ok := old[key]; ok {
- switch v.(type) {
- case string:
- return strings.Split(v.(string), ",")[0]
- case int:
- return strconv.Itoa(v.(int))
- case int16:
- return strconv.Itoa(int(v.(int16)))
- case int32:
- return strconv.Itoa(int(v.(int32)))
- case int64:
- return strconv.Itoa(int(v.(int64)))
- case float64:
- return fmt.Sprintf("%1.0f", v.(float64))
- case float32:
- return fmt.Sprintf("%1.0f", v.(float32))
- }
- } else {
- return ""
- }
- return ""
- }
- func calcPlateType(longStr, approvedNumberStr, grossMassStr string) string {
- long, _ := strconv.Atoi(longStr)
- approvedNumber, _ := strconv.Atoi(approvedNumberStr)
- grossMass, _ := strconv.Atoi(grossMassStr)
- if long >= 6000 || approvedNumber >= 20 || grossMass >= 4500 {
- return "01"
- }
- if (long != 0) && (approvedNumber != 0 || grossMass != 0) {
- if long < 6000 && approvedNumber > 9 {
- return "01"
- } else {
- return "02"
- }
- }
- return ""
- }
- func supplementPlateTypeByCalc(old map[string]interface{}) {
- if v, ok := old["plate_type"]; ok {
- switch v.(type) {
- case string:
- if v.(string) == "" {
- vehicleTypeDetail := getString("vehicle_type_detail", old)
- tyreNumber := getString("tyre_number", old)
- displacement := getString("displacement", old)
- ratedPower := getString("rated_power", old)
- plateType, vehicleType := calcMoto(vehicleTypeDetail, tyreNumber, displacement, ratedPower)
- if plateType != "" {
- old["plate_type"] = plateType
- if _, ok1 := old["vehicle_type"]; ok1 {
- old["vehicle_type"] = vehicleType
- }
- } else {
- longStr := getString("long", old)
- approvedNumberStr := getString("approved_number", old)
- grossMassStr := getString("gross_mass", old)
- old["plate_type"] = calcPlateType(longStr, approvedNumberStr, grossMassStr)
- }
- }
- }
- }
- }
- func ToInt(newMap map[string]string, k string) (ret int) {
- if v, ok := newMap[k]; ok {
- if v != "" {
- v = strings.Split(v, ",")[0]
- }
- ret, _ = strconv.Atoi(v)
- }
- return ret
- }
- func ToFloat64(newMap map[string]string, k string) (ret float64) {
- if v, ok := newMap[k]; ok {
- if v != "" {
- v = strings.Split(v, ",")[0]
- }
- ret, _ = strconv.ParseFloat(v, 64)
- }
- return ret
- }
- func ToFloat32(newMap map[string]string, k string) (ret float32) {
- if v, ok := newMap[k]; ok {
- ret1, _ := strconv.ParseFloat(v, 32)
- ret = float32(ret1)
- }
- return ret
- }
- func toString(in interface{}) string {
- switch in.(type) {
- case string:
- return in.(string)
- case int:
- if in.(int) == 0 {
- return ""
- }
- return strconv.Itoa(in.(int))
- case int16:
- if in.(int16) == 0 {
- return ""
- }
- return strconv.Itoa(int(in.(int16)))
- case int32:
- if in.(int32) == 0 {
- return ""
- }
- return strconv.Itoa(int(in.(int32)))
- case int64:
- if in.(int64) == 0 {
- return ""
- }
- return strconv.Itoa(int(in.(int64)))
- case float64:
- if in.(float64) == 0 {
- return ""
- }
- return strconv.FormatFloat(in.(float64), 'f', -1, 64)
- case float32:
- if in.(float32) == 0 {
- return ""
- }
- return strconv.FormatFloat(float64(in.(float32)), 'f', -1, 32)
- }
- return ""
- }
- /*
- func formatType(old interface{},newMap map[string]string,k string) interface{}{
- switch old.(type) {
- case string:
- if old.(string) == "" || old.(string) == "0"{
- return old
- }
- case int:
- if old.(int) == 0 {
- return ToInt(newMap, k)
- }
- case int16:
- if old.(int16) == 0 {
- return int16(ToInt(newMap, k))
- }
- case int32:
- if old.(int32) == 0 {
- return int32(ToInt(newMap, k))
- }
- case int64:
- if old.(int64) == 0 {
- return int64(ToInt(newMap, k))
- }
- case float64:
- if old.(float64) == 0 {
- return int64(ToFloat64(newMap, k))
- }
- case float32:
- if old.(float32) == 0 {
- return int64(ToFloat32(newMap, k))
- }
- }
- return
- }*/
- func supplementEngineType(modelNo string, reqMap map[string]interface{}) {
- if modelNo == "" {
- return
- }
- where := make(map[string]interface{})
- where["model_no"] = modelNo
- needSupplment := false
- isExist := false
- if v, ok := reqMap["engine_type"]; ok {
- engineType := v.(string)
- if engineType != "" {
- isExist = true
- where["engine_type"] = engineType
- } else {
- needSupplment = true
- }
- }
- if v, ok := reqMap["displacement"]; ok {
- displacement := toString(v)
- if displacement != "" {
- if !isExist {
- where["displacement"] = displacement
- }
- } else {
- needSupplment = true
- }
- }
- if v, ok := reqMap["rated_power"]; ok {
- rated_power := toString(v)
- if rated_power != "" {
- if !isExist {
- where["rated_power"] = rated_power
- }
- } else {
- needSupplment = true
- }
- }
- if !needSupplment {
- return
- }
- db := database.DB()
- engineInfo := &model.EngineInfo{}
- if err := engineInfo.Query(db, where); err != nil {
- fmt.Println("err", err)
- return
- }
- newData, _ := json.Marshal(engineInfo)
- newMap := make(map[string]string)
- json.Unmarshal(newData, &newMap)
- for k, v1 := range reqMap {
- if k != "displacement" && k != "rated_power" && k != "engine_type" {
- continue
- }
- switch v1.(type) {
- case string:
- if v1.(string) == "" || v1.(string) == "0" {
- reqMap[k] = newMap[k]
- }
- case int:
- if v1.(int) == 0 {
- reqMap[k] = ToInt(newMap, k)
- }
- case int16:
- if v1.(int16) == 0 {
- reqMap[k] = int16(ToInt(newMap, k))
- }
- case int32:
- if v1.(int32) == 0 {
- reqMap[k] = int32(ToInt(newMap, k))
- }
- case int64:
- if v1.(int64) == 0 {
- reqMap[k] = int64(ToInt(newMap, k))
- }
- case float64:
- if v1.(float64) == 0 {
- reqMap[k] = float64(ToFloat64(newMap, k))
- }
- case float32:
- if v1.(float32) == 0 {
- reqMap[k] = float32(ToFloat32(newMap, k))
- }
- }
- }
- /*if v, ok := reqMap["engine_type"]; ok {
- engineType := v.(string)
- if engineType == ""{
- db := database.DB()
- ads24 := &model.Ads24{}
- if err := ads24.Query(db, map[string]interface{}{"model_no": modelNo}); err != nil {
- return
- }
- reqMap["engine_type"] = ads24.EngineType
- if ads24.EngineType != ""{
- ads25 := &model.Ads25{}
- if err := ads25.Query(db, map[string]interface{}{"engine_type": ads24.EngineType}); err != nil {
- return
- }
- newData, _ := json.Marshal(ads25)
- newMap := make(map[string]string)
- json.Unmarshal(newData, &newMap)
- for k, v1 := range reqMap {
- if k != "displacement" && k != "rated_power"{
- continue
- }
- switch v1.(type) {
- case string:
- if v1.(string) == "" || v1.(string) == "0"{
- reqMap[k] = newMap[k]
- }
- case int:
- if v1.(int) == 0 {
- reqMap[k] = ToInt(newMap, k)
- }
- case int16:
- if v1.(int16) == 0 {
- reqMap[k] = int16(ToInt(newMap, k))
- }
- case int32:
- if v1.(int32) == 0 {
- reqMap[k] = int32(ToInt(newMap, k))
- }
- case int64:
- if v1.(int64) == 0 {
- reqMap[k] = int64(ToInt(newMap, k))
- }
- case float64:
- if v1.(float64) == 0 {
- reqMap[k] = int64(ToFloat64(newMap, k))
- }
- case float32:
- if v1.(float32) == 0 {
- reqMap[k] = int64(ToFloat32(newMap, k))
- }
- }
- }
- }
- }
- }*/
- }
- func supplementData(reqMap map[string]interface{}) {
- modelNo := ""
- vin := ""
- if v, ok := reqMap["model_no"]; ok {
- modelNo = v.(string)
- }
- if v, ok := reqMap["vin"]; ok {
- vin = v.(string)
- }
- // 补充数据
- modelNo = supplementByAds19(vin, modelNo, reqMap)
- supplementEngineType(modelNo, reqMap)
- // 补充号牌种类
- supplementPlateType(modelNo, reqMap)
- supplementPlateTypeByCalc(reqMap)
- }
- func supplementByAds19(vin string, modelNo string, old map[string]interface{}) (newModelNo string) {
- if modelNo == "" && vin == "" {
- return
- }
- db := database.DB()
- if modelNo == "" {
- vinRule := ""
- if len(vin) > 8 {
- vinRule = vin[0:8]
- }
- if vinRule == "" {
- return
- }
- ads21 := &model.Dws11{}
- if err := ads21.Query(db, map[string]interface{}{"vin_rule": vinRule}); err != nil {
- return
- }
- old["model_no"] = ads21.ModelNo
- modelNo = ads21.ModelNo
- }
- ads19 := &model.Dws16{}
- if err := ads19.Query(db, map[string]interface{}{"model_no": modelNo}); err != nil {
- return modelNo
- }
- newData, _ := json.Marshal(ads19)
- newMap := make(map[string]string)
- json.Unmarshal(newData, &newMap)
- for k, v1 := range old {
- switch v1.(type) {
- case string:
- if v1.(string) == "" {
- if newMap[k] != "" {
- newMap[k] = strings.Split(newMap[k], ",")[0]
- }
- old[k] = newMap[k]
- }
- case int:
- if v1.(int) == 0 {
- old[k] = ToInt(newMap, k)
- }
- case int16:
- if v1.(int16) == 0 {
- old[k] = int16(ToInt(newMap, k))
- }
- case int32:
- if v1.(int32) == 0 {
- old[k] = int32(ToInt(newMap, k))
- }
- case int64:
- if v1.(int64) == 0 {
- old[k] = int64(ToInt(newMap, k))
- }
- case float64:
- if v1.(float64) == 0 {
- old[k] = int64(ToFloat64(newMap, k))
- }
- case float32:
- if v1.(float32) == 0 {
- old[k] = int64(ToFloat32(newMap, k))
- }
- }
- }
- return modelNo
- }
- func formatDataCode(m map[string]interface{}, s, t string, f func(string) (string, string)) {
- if v, ok := m[s]; ok {
- if _, ok = v.(string); ok && v.(string) == "" {
- if v, ok = m[t]; ok {
- if _, ok = v.(string); ok && v.(string) != "" {
- m[s], _ = f(v.(string))
- return
- }
- }
- }
- }
- if v, ok := m[t]; ok {
- if _, ok = v.(string); ok && v.(string) == "" {
- if v, ok = m[s]; ok {
- if _, ok = v.(string); ok && v.(string) != "" {
- m[t], _ = f(v.(string))
- return
- }
- }
- }
- }
- }
- func formatDate(m map[string]interface{}, s string) {
- if v, ok := m[s]; ok {
- if _, ok = v.(string); ok && v.(string) != "" {
- m[s] = utils.FormatDate(v.(string))
- }
- }
- }
- func formatDisplacement(reqMap map[string]interface{}) {
- if v, ok := reqMap["displacement"]; ok {
- var (
- temp int
- )
- switch v.(type) {
- case string:
- temp, _ = strconv.Atoi(v.(string))
- case float64:
- temp = int(v.(float64))
- case int64:
- temp = int(v.(int64))
- }
- if temp > 100000 {
- temp /= 1000
- switch v.(type) {
- case string:
- reqMap["displacement"] = strconv.Itoa(temp)
- case float64:
- reqMap["displacement"] = float64(temp)
- case int64:
- reqMap["displacement"] = int64(temp)
- }
- }
- if temp > 0 {
- if l, ok := reqMap["displacement_l"]; ok {
- if _, ok = l.(string); ok && l.(string) == "" {
- reqMap["displacement_l"] = strconv.FormatFloat(float64(temp)/float64(1000), 'f', 1, 64)
- }
- }
- }
- }
- if v, ok := reqMap["displacement_l"]; ok {
- if _, ok = v.(string); ok && v.(string) != "" {
- tmp := strings.Replace(v.(string), "L", "", -1)
- tmp = strings.Replace(tmp, "T", "", -1)
- displacementL, _ := strconv.ParseFloat(tmp, 64)
- if displacementL > 100 {
- reqMap["displacement_l"] = strconv.FormatFloat(float64(displacementL)/float64(1000), 'f', 1, 64)
- }
- if l, ok := reqMap["displacement"]; ok {
- var (
- temp int
- )
- switch l.(type) {
- case string:
- temp, _ = strconv.Atoi(l.(string))
- case float64:
- temp = int(l.(float64))
- case int64:
- temp = int(l.(int64))
- }
- if temp == 0 {
- if displacementL > 100 {
- temp = int(displacementL)
- } else {
- temp = int(displacementL * float64(1000))
- }
- switch l.(type) {
- case string:
- reqMap["displacement"] = strconv.Itoa(temp)
- case float64:
- reqMap["displacement"] = float64(temp)
- case int64:
- reqMap["displacement"] = int64(temp)
- }
- }
- }
- }
- }
- }
- func formatBrand(reqMap map[string]interface{}) {
- if v, ok := reqMap["brand_name"]; ok {
- if _, ok = v.(string); ok && v.(string) != "" {
- if ads22, err := model.NewAds22Model().Get(database.DB().Where("source = ? and type = 6", v)); err == nil {
- if ads22.Standard != "" {
- reqMap["brand_id"] = ads22.Standard
- if ads2, err := model.NewDws20Model().Get(database.DB().Where("brand_id = ?", ads22.Standard)); err == nil {
- reqMap["brand_name"] = ads2.BrandName
- }
- } else {
- if ads2, err := model.NewDws20Model().Get(database.DB().Where("brand_name = ?", v)); err == nil {
- reqMap["brand_name"] = ads2.BrandName
- if ads2.BrandId != "" {
- reqMap["brand_id"] = ads2.BrandId
- }
- }
- }
- } else {
- if ads2, err := model.NewDws20Model().Get(database.DB().Where("brand_name = ?", v)); err == nil {
- reqMap["brand_name"] = ads2.BrandName
- if ads2.BrandId != "" {
- reqMap["brand_id"] = ads2.BrandId
- }
- }
- }
- }
- }
- }
- func formatEmissionStandard(reqMap map[string]interface{}) {
- if v, ok := reqMap["emission_standard"]; ok {
- if _, ok = v.(string); ok && v.(string) != "" {
- reqMap["emission_standard"] = utils.EmissionStandard(v.(string))
- }
- }
- }
- func supplementPlateType(modelNo string, reqMap map[string]interface{}) {
- if v, ok := reqMap["plate_type"]; ok {
- if _, ok = v.(string); ok && v.(string) == "" {
- if v1, ok := reqMap["plate_no"]; ok {
- if _, ok = v1.(string); ok {
- plateNo := v1.(string)
- if plateNo != "" {
- plateType, _ := utils.VehicleDefaultPlateType(&plateNo)
- if plateType != "" {
- reqMap["plate_type"] = plateType
- return
- }
- }
- }
- }
- if modelNo == "" {
- return
- }
- if ads18, err := model.NewDws12Model().Get(database.DB().Where("model_no = ?", modelNo)); err == nil {
- reqMap["plate_type"] = ads18.PlateType
- }
- }
- }
- }
- // 判断首保日期和初登日期
- func supplementRegisterDate(reqMap map[string]interface{}) {
- if insuranceDate, ok := reqMap["insurance_first_date"]; ok {
- // 存在首保日期
- if initialRegistrationDate, ok := reqMap["initial_registration_date"]; ok && initialRegistrationDate == "" {
- reqMap["initial_registration_date"] = insuranceDate
- }
- }
- }
- func formatYearMonth(m map[string]interface{}, s string) {
- if v, ok := m[s]; ok {
- if _, ok = v.(string); ok && v.(string) != "" {
- if len(v.(string)) > utils.MonthLen {
- m[s] = v.(string)[0:utils.MonthLen]
- }
- }
- }
- }
- func formatData(reqMap map[string]interface{}) {
- // 补充初等
- supplementRegisterDate(reqMap)
- // 格式化日期
- formatDate(reqMap, "initial_registration_date")
- formatDate(reqMap, "inspection_result_effective_to")
- formatDate(reqMap, "release_date")
- formatDate(reqMap, "insurance_first_date")
- formatDate(reqMap, "compulsory_scrap_to")
- formatYearMonth(reqMap, "last_compulsory_insurance_date")
- calcLatestInsuredate(reqMap)
- // 格式化数据编码
- formatDataCode(reqMap, "use_property", "use_property_detail", utils.ParseProperty)
- formatDataCode(reqMap, "fuel_type", "fuel_type_detail", utils.ParseFuel)
- formatDataCode(reqMap, "vehicle_type", "vehicle_type_detail", utils.VehicleType)
- formatDataCode(reqMap, "vehicle_body_color", "vehicle_body_color_detail", utils.ParseColor)
- formatDataCode(reqMap, "body_color", "body_color_detail", utils.ParseColor)
- // 格式化排放标准
- formatEmissionStandard(reqMap)
- // 格式化排量
- formatDisplacement(reqMap)
- // 格式化品牌
- formatBrand(reqMap)
- FormatField(reqMap, "engine_type")
- FormatField(reqMap, "axle_weight")
- FormatField(reqMap, "tyre_number")
- FormatField(reqMap, "axle_number")
- FormatField(reqMap, "tyre_size")
- FormatField(reqMap, "oil_wear")
- FormatField(reqMap, "rated_power")
- FormatField(reqMap, "traction_mass")
- FormatField(reqMap, "front_wheel_distance")
- FormatField(reqMap, "back_wheel_distance")
- FormatField(reqMap, "gross_mass")
- FormatField(reqMap, "unladen_mass")
- FormatField(reqMap, "approved_load")
- FormatField(reqMap, "wheel_base")
- FormatField(reqMap, "long")
- FormatField(reqMap, "wide")
- FormatField(reqMap, "high")
- FormatField(reqMap, "approved_number")
- FormatField(reqMap, "displacement")
- FormatField(reqMap, "displacement_l")
- FormatField(reqMap, "engine_no")
- // 计算强制报废日期
- utils.CalcCompulsoryScrapTo(reqMap)
- // 计算年检到期时间
- utils.CalcAnnualExamExpireTime(reqMap)
- }
- func U01(ctx context.Context, params string) (reply *apis.QueryResponse, err error) {
- reply = &apis.QueryResponse{}
- reply.Data = params
- reqMap := make(map[string]interface{})
- err = json.Unmarshal([]byte(params), &reqMap)
- if err != nil {
- return reply, errors.ParamsError
- }
- // 补充车辆数据
- supplementData(reqMap)
- // 格式化数据
- formatData(reqMap)
- reply.Data, _ = jsoniter.MarshalToString(reqMap)
- return reply, nil
- }
|