package query import ( "adm-data/errors" "adm-data/model" v1 "adm-data/pb/v1" "adm-data/utils" "context" "encoding/json" "fmt" "strconv" "strings" jsoniter "github.com/json-iterator/go" "git.getensh.com/common/gopkgsv2/database" "git.getensh.com/common/gopkgsv2/logger" "go.uber.org/zap" "google.golang.org/grpc/status" ) 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) == "" { 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 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) // 补充号牌种类 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.Ads21{} if err := ads21.Query(db, map[string]interface{}{"vin_rule": vinRule}); err != nil { return } old["model_no"] = ads21.ModelNo modelNo = ads21.ModelNo } ads19 := &model.Ads19{} 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.NewAds2Model().Get(database.DB().Where("brand_id = ?", ads22.Standard)); err == nil { reqMap["brand_name"] = ads2.BrandName } } else { if ads2, err := model.NewAds2Model().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.NewAds2Model().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.NewAds18Model().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") // 格式化数据编码 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) // 计算强制报废日期 utils.CalcCompulsoryScrapTo(reqMap) // 计算年检到期时间 utils.CalcAnnualExamExpireTime(reqMap) } func U01(ctx context.Context, params string) (reply *v1.QueryResponse, err error) { reply = &v1.QueryResponse{} // 捕获各个task中的异常并返回给调用者 defer func() { if r := recover(); r != nil { err = fmt.Errorf("%+v", r) e := &status.Status{} if er := json.Unmarshal([]byte(err.Error()), e); er != nil { logger.Error("err", zap.String("system_err", err.Error()), zap.Stack("stacktrace")) } } }() reply.Data = params reqMap := make(map[string]interface{}) err = json.Unmarshal([]byte(params), &reqMap) if err != nil { return nil, errors.ParamsError } // 补充车辆数据 supplementData(reqMap) // 格式化数据 formatData(reqMap) reply.Data, _ = jsoniter.MarshalToString(reqMap) return reply, nil }