// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package task import ( "adm-dws/apis" "adm-dws/consts" "adm-dws/model" "encoding/json" "gorm.io/gorm" "strconv" "strings" ) func supplementDws5(old, new *model.Dws5) bool { isSupple := false if old.CertificateNumber == "" && new.CertificateNumber != "" { isSupple = true old.CertificateNumber = new.CertificateNumber } if old.CertificateDate == "" && new.CertificateDate != "" { isSupple = true old.CertificateDate = new.CertificateDate } if old.VehicleManufacturer == "" && new.VehicleManufacturer != "" { isSupple = true old.VehicleManufacturer = new.VehicleManufacturer } if old.BrandName == "" && new.BrandName != "" { isSupple = true old.BrandName = new.BrandName } if old.ModelNo == "" && new.ModelNo != "" { isSupple = true old.ModelNo = new.ModelNo } if old.VehicleBodyColorDetail == "" && new.VehicleBodyColorDetail != "" { isSupple = true old.VehicleBodyColorDetail = new.VehicleBodyColorDetail } if old.ChassisModel == "" && new.ChassisModel != "" { isSupple = true old.ChassisModel = new.ChassisModel } if old.ChassisId == "" && new.ChassisId != "" { isSupple = true old.ChassisId = new.ChassisId } if old.ChassisCertificateNumber == "" && new.ChassisCertificateNumber != "" { isSupple = true old.ChassisCertificateNumber = new.ChassisCertificateNumber } if old.EngineType == "" && new.EngineType != "" { isSupple = true old.EngineType = new.EngineType } if old.EngineNo == "" && new.EngineNo != "" { isSupple = true old.EngineNo = new.EngineNo } if old.FuelTypeDetail == "" && new.FuelTypeDetail != "" { isSupple = true old.FuelTypeDetail = new.FuelTypeDetail } if (old.Displacement == "" || old.Displacement == "0") && new.Displacement != "" { isSupple = true old.Displacement = new.Displacement } if (old.DisplacementL == "" || old.DisplacementL == "0") && new.DisplacementL != "" { isSupple = true old.DisplacementL = new.DisplacementL } // TODO 洗数据专用,洗完删除 if new.DisplacementL != "" { /*if !strings.HasSuffix(old.Displacement ,"000"){ isSupple = true old.DisplacementL = new.DisplacementL }*/ isSupple = true old.DisplacementL = new.DisplacementL } if (old.RatedPower == "" || old.RatedPower == "0") && new.RatedPower != "" { isSupple = true old.RatedPower = new.RatedPower } if old.EmissionStandard == "" && new.EmissionStandard != "" { isSupple = true old.EmissionStandard = new.EmissionStandard } if (old.OilWear == "" || old.OilWear == "0") && new.OilWear != "" { isSupple = true old.OilWear = new.OilWear } if (old.Long == "" || old.Long == "0") && new.Long != "" { isSupple = true old.Long = new.Long } if (old.Wide == "" || old.Wide == "0") && new.Wide != "" { isSupple = true old.Wide = new.Wide } if (old.High == "" || old.High == "0") && new.High != "" { isSupple = true old.High = new.High } if (old.CargoLong == "" || old.CargoLong == "0") && new.CargoLong != "" { isSupple = true old.CargoLong = new.CargoLong } if (old.CargoWide == "" || old.CargoWide == "0") && new.CargoWide != "" { isSupple = true old.CargoWide = new.CargoWide } if (old.CargoHigh == "" || old.CargoHigh == "0") && new.CargoHigh != "" { isSupple = true old.CargoHigh = new.CargoHigh } if (old.NumberOfLeafSprings == "" || old.NumberOfLeafSprings == "0") && new.NumberOfLeafSprings != "" { isSupple = true old.NumberOfLeafSprings = new.NumberOfLeafSprings } if (old.TyreNumber == "" || old.TyreNumber == "0") && new.TyreNumber != "" { isSupple = true old.TyreNumber = new.TyreNumber } if (old.TyreSize == "" || old.TyreSize == "0") && new.TyreSize != "" { isSupple = true old.TyreSize = new.TyreSize } if (old.FrontWheelDistance == "" || old.FrontWheelDistance == "0") && new.FrontWheelDistance != "" { isSupple = true old.FrontWheelDistance = new.FrontWheelDistance } if (old.BackWheelDistance == "" || old.BackWheelDistance == "0") && new.BackWheelDistance != "" { isSupple = true old.BackWheelDistance = new.BackWheelDistance } if (old.WheelBase == "" || old.WheelBase == "0") && new.WheelBase != "" { isSupple = true old.WheelBase = new.WheelBase } if (old.AxleWeight == "" || old.AxleWeight == "0") && new.AxleWeight != "" { isSupple = true old.AxleWeight = new.AxleWeight } if (old.AxleNumber == "" || old.AxleNumber == "0") && new.AxleNumber != "" { isSupple = true // 非整数不存 _, err := strconv.Atoi(new.AxleNumber) if err == nil { old.AxleNumber = new.AxleNumber } } if old.SteeringMode == "" && new.SteeringMode != "" { isSupple = true old.SteeringMode = new.SteeringMode } if (old.GrossMass == "" || old.GrossMass == "0") && new.GrossMass != "" { isSupple = true old.GrossMass = new.GrossMass } if (old.UnladenMass == "" || old.UnladenMass == "0") && new.UnladenMass != "" { isSupple = true old.UnladenMass = new.UnladenMass } if (old.ApprovedLoad == "" || old.ApprovedLoad == "0") && new.ApprovedLoad != "" { isSupple = true old.ApprovedLoad = new.ApprovedLoad } if old.LoadMassCoefficient == "" && new.LoadMassCoefficient != "" { isSupple = true old.LoadMassCoefficient = new.LoadMassCoefficient } if (old.TractionMass == "" || old.TractionMass == "0") && new.TractionMass != "" { isSupple = true old.TractionMass = new.TractionMass } if (old.SemitrailerSaddleMaxAllowGrossMass == "" || old.SemitrailerSaddleMaxAllowGrossMass == "0") && new.SemitrailerSaddleMaxAllowGrossMass != "" { isSupple = true old.SemitrailerSaddleMaxAllowGrossMass = new.SemitrailerSaddleMaxAllowGrossMass } if (old.NumberOfPassengers == "" || old.NumberOfPassengers == "0") && new.NumberOfPassengers != "" { isSupple = true old.NumberOfPassengers = new.NumberOfPassengers } if (old.ApprovedNumber == "" || old.ApprovedNumber == "0") && new.ApprovedNumber != "" { isSupple = true old.ApprovedNumber = new.ApprovedNumber } if (old.TopSpeed == "" || old.TopSpeed == "0") && new.TopSpeed != "" { isSupple = true old.TopSpeed = new.TopSpeed } if old.ReleaseDate == "" && new.ReleaseDate != "" { isSupple = true old.ReleaseDate = new.ReleaseDate } return isSupple } func supplementDws5ByDws16(old *model.Dws5, new *model.Dws16) bool { isSupple := false if old.BrandName == "" && new.BrandName != "" { isSupple = true old.BrandName = strings.Split(new.BrandName, ",")[0] } if old.ModelNo == "" && new.ModelNo != "" { isSupple = true old.ModelNo = strings.Split(new.ModelNo, ",")[0] } if old.EmissionStandard == "" && new.EmissionStandard != "" { isSupple = true old.EmissionStandard = strings.Split(new.EmissionStandard, ",")[0] } if (old.Long == "" || old.Long == "0") && new.Long != "" { isSupple = true old.Long = strings.Split(new.Long, ",")[0] } if (old.Wide == "" || old.Wide == "0") && new.Wide != "" { isSupple = true old.Wide = strings.Split(new.Wide, ",")[0] } if (old.High == "" || old.High == "0") && new.High != "" { isSupple = true old.High = strings.Split(new.High, ",")[0] } if (old.CargoLong == "" || old.CargoLong == "0") && new.CargoLong != "" { isSupple = true old.CargoLong = strings.Split(new.CargoLong, ",")[0] } if (old.CargoWide == "" || old.CargoWide == "0") && new.CargoWide != "" { isSupple = true old.CargoWide = strings.Split(new.CargoWide, ",")[0] } if (old.CargoHigh == "" || old.CargoHigh == "0") && new.CargoHigh != "" { isSupple = true old.CargoHigh = strings.Split(new.CargoHigh, ",")[0] } if (old.NumberOfLeafSprings == "" || old.NumberOfLeafSprings == "0") && new.NumberOfLeafSprings != "" { isSupple = true old.NumberOfLeafSprings = strings.Split(new.NumberOfLeafSprings, ",")[0] } if (old.TyreNumber == "" || old.TyreNumber == "0") && new.TyreNumber != "" { isSupple = true old.TyreNumber = strings.Split(new.TyreNumber, ",")[0] } if (old.TyreSize == "" || old.TyreSize == "0") && new.TyreSize != "" { isSupple = true old.TyreSize = strings.Split(new.TyreSize, ",")[0] } if (old.FrontWheelDistance == "" || old.FrontWheelDistance == "0") && new.FrontWheelDistance != "" { isSupple = true old.FrontWheelDistance = strings.Split(new.FrontWheelDistance, ",")[0] } if (old.BackWheelDistance == "" || old.BackWheelDistance == "0") && new.BackWheelDistance != "" { isSupple = true old.BackWheelDistance = strings.Split(new.BackWheelDistance, ",")[0] } if (old.AxleWeight == "" || old.AxleWeight == "0") && new.AxleWeight != "" { isSupple = true old.AxleWeight = strings.Split(new.AxleWeight, ",")[0] } if (old.AxleNumber == "" || old.AxleNumber == "0") && new.AxleNumber != "" { isSupple = true // 非整数不存 _, err := strconv.Atoi(strings.Split(new.AxleNumber, ",")[0]) if err == nil { old.AxleNumber = strings.Split(new.AxleNumber, ",")[0] } } if old.SteeringMode == "" && new.SteeringMode != "" { isSupple = true old.SteeringMode = strings.Split(new.SteeringMode, ",")[0] } if (old.GrossMass == "" || old.GrossMass == "0") && new.GrossMass != "" { isSupple = true old.GrossMass = strings.Split(new.GrossMass, ",")[0] } if (old.UnladenMass == "" || old.UnladenMass == "0") && new.UnladenMass != "" { isSupple = true old.UnladenMass = strings.Split(new.UnladenMass, ",")[0] } if (old.ApprovedLoad == "" || old.ApprovedLoad == "0") && new.ApprovedLoad != "" { isSupple = true old.ApprovedLoad = strings.Split(new.ApprovedLoad, ",")[0] } if (old.LoadMassCoefficient == "" || old.LoadMassCoefficient == "0") && new.LoadMassCoefficient != "" { isSupple = true old.LoadMassCoefficient = strings.Split(new.LoadMassCoefficient, ",")[0] } if (old.TractionMass == "" || old.TractionMass == "0") && new.TractionMass != "" { isSupple = true old.TractionMass = strings.Split(new.TractionMass, ",")[0] } if (old.SemitrailerSaddleMaxAllowGrossMass == "" || old.SemitrailerSaddleMaxAllowGrossMass == "0") && new.SemitrailerSaddleMaxAllowGrossMass != "" { isSupple = true old.SemitrailerSaddleMaxAllowGrossMass = strings.Split(new.SemitrailerSaddleMaxAllowGrossMass, ",")[0] } if (old.NumberOfPassengers == "" || old.NumberOfPassengers == "0") && new.NumberOfPassengers != "" { isSupple = true old.NumberOfPassengers = strings.Split(new.NumberOfPassengers, ",")[0] } if (old.ApprovedNumber == "" || old.ApprovedNumber == "0") && new.ApprovedNumber != "" { isSupple = true old.ApprovedNumber = strings.Split(new.ApprovedNumber, ",")[0] } if (old.TopSpeed == "" || old.TopSpeed == "0") && new.TopSpeed != "" { isSupple = true old.TopSpeed = strings.Split(new.TopSpeed, ",")[0] } if old.FuelTypeDetail == "" && new.FuelTypeDetail != "" { isSupple = true old.FuelTypeDetail = new.FuelTypeDetail } if (old.WheelBase == "" || old.WheelBase == "0") && new.Wheelbase != "" { isSupple = true old.WheelBase = new.Wheelbase } return isSupple } func checkIsNeedSupple(old *model.Dws5) bool { isSupple := false if old.BrandName == "" { isSupple = true return isSupple } if old.ModelNo == "" { isSupple = true return isSupple } if old.EmissionStandard == "" { isSupple = true return isSupple } if old.Long == "" || old.Long == "0" { isSupple = true return isSupple } if old.Wide == "" || old.Wide == "0" { isSupple = true return isSupple } if old.High == "" || old.High == "0" { isSupple = true return isSupple } if old.CargoLong == "" || old.CargoLong == "0" { isSupple = true return isSupple } if old.CargoWide == "" || old.CargoWide == "0" { isSupple = true return isSupple } if old.CargoHigh == "" || old.CargoHigh == "0" { isSupple = true return isSupple } if old.NumberOfLeafSprings == "" || old.NumberOfLeafSprings == "0" { isSupple = true return isSupple } if old.TyreNumber == "" || old.TyreNumber == "0" { isSupple = true return isSupple } if old.TyreSize == "" || old.TyreSize == "0" { isSupple = true return isSupple } if old.FrontWheelDistance == "" || old.FrontWheelDistance == "0" { isSupple = true return isSupple } if old.BackWheelDistance == "" || old.BackWheelDistance == "0" { isSupple = true return isSupple } if old.AxleWeight == "" || old.AxleWeight == "0" { isSupple = true return isSupple } if old.AxleNumber == "" || old.AxleNumber == "0" { isSupple = true return isSupple } if old.SteeringMode == "" || old.SteeringMode == "0" { isSupple = true return isSupple } if old.GrossMass == "" || old.GrossMass == "0" { isSupple = true return isSupple } if old.UnladenMass == "" || old.UnladenMass == "0" { isSupple = true return isSupple } if old.ApprovedLoad == "" || old.ApprovedLoad == "0" { isSupple = true return isSupple } if old.LoadMassCoefficient == "" || old.LoadMassCoefficient == "0" { isSupple = true return isSupple } if old.TractionMass == "" || old.TractionMass == "0" { isSupple = true return isSupple } if old.SemitrailerSaddleMaxAllowGrossMass == "" || old.SemitrailerSaddleMaxAllowGrossMass == "0" { isSupple = true return isSupple } if old.NumberOfPassengers == "" || old.NumberOfPassengers == "0" { isSupple = true return isSupple } if old.ApprovedNumber == "" || old.ApprovedNumber == "0" { isSupple = true return isSupple } if old.TopSpeed == "" || old.TopSpeed == "0" { isSupple = true return isSupple } if old.WheelBase == "" || old.WheelBase == "0" { isSupple = true return isSupple } if old.FuelTypeDetail == "" { isSupple = true return isSupple } return isSupple } func suppleByDws16(db *gorm.DB, dws5 *model.Dws5) bool { isSupple := false if dws5.ModelNo == "" { vinRule := "" if len(dws5.Vin) > 8 { vinRule = dws5.Vin[0:8] } if vinRule == "" { return isSupple } dws11 := &model.Dws11{} err := dws11.Query(db, map[string]interface{}{"vin_rule": vinRule}) if err != nil { return isSupple } isSupple = true dws5.ModelNo = dws11.ModelNo } dws16 := &model.Dws16{} err := dws16.QueryDesc(db, map[string]interface{}{"model_no": dws5.ModelNo}) if err != nil { return isSupple } isSupple1 := supplementDws5ByDws16(dws5, dws16) if isSupple1 { isSupple = isSupple1 } return isSupple } // 合格证信息 func Dws5Task(db *gorm.DB, dwsMessage *apis.DwsMessage, outputSourceCode string) (adsMsgList []*apis.AdsMessage, dwsMsgList []*apis.DwsMessage, err error) { dws5 := &model.Dws5{} err = json.Unmarshal([]byte(dwsMessage.Content), dws5) if err != nil { return nil, nil, nil } if dws5.Vin == "" || (dws5.EngineNo == "" && dws5.ModelNo == "") { return nil, nil, nil } // 排量转换排量参数如果能被10整除则数据/1000入排量(L)字段, 其他入排量(ml)字段 if dws5.Displacement != "" && dws5.DisplacementL == "" { displacement, _ := strconv.Atoi(dws5.Displacement) if displacement%1000 == 0 { displacement = displacement / 1000 dws5.DisplacementL = strconv.Itoa(displacement) } } // 获取style id /*if dws5.ThridStyleId != ""{ dws13 := &model.Dws13{} err = dws13.Query(db,map[string]interface{}{"thrid_style_id":dws5.ThridStyleId}) if err == nil { if dws13.StyleId != "" { dws5.StyleId = dws13.StyleId } } }*/ oldDws5 := &model.Dws5{} err = oldDws5.Query(db, map[string]interface{}{"vin": dws5.Vin}) if err == nil { //oldDws5.StyleId = dws5.StyleId // 有数据 isSupple := supplementDws5(oldDws5, dws5) isNeed := checkIsNeedSupple(oldDws5) if isNeed { isSupple1 := suppleByDws16(db, oldDws5) if isSupple1 { isSupple = isSupple1 } } if isSupple { err = oldDws5.Update(db) if err != nil { return nil, nil, err } } else { return nil, nil, nil } } else { if err == gorm.ErrRecordNotFound { // 没有数据 isNeed := checkIsNeedSupple(dws5) if isNeed { suppleByDws16(db, dws5) } err = dws5.Insert(db) if err != nil { if !strings.Contains(err.Error(), "Duplicate") { return nil, nil, err } } oldDws5 = dws5 } else { // 数据库错误 return nil, nil, err } } // 补充dws1号牌种类 dws4, _ := SupplementDws1PlateType(db, oldDws5.Vin, oldDws5.Long, oldDws5.ApprovedNumber, oldDws5.GrossMass) if dws4 != nil { dwsMsg := NewDwsMessage(dwsMessage, outputSourceCode) dwsMsgByte, _ := json.Marshal(*dws4) dwsMsg.Content = string(dwsMsgByte) adsMsgs, _, err := Dws1Task(db, dwsMsg, consts.SOURCEDWS1) if err == nil { adsMsgList = append(adsMsgList, adsMsgs...) } } adsMsg := NewAdsMessage(dwsMessage, outputSourceCode, consts.ACTIONINSERT) msgByte, _ := json.Marshal(*oldDws5) adsMsg.Content = string(msgByte) adsMsgList = append(adsMsgList, adsMsg) return adsMsgList, dwsMsgList, nil }