// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package task import ( "adm-ads/apis" "adm-ads/consts" "adm-ads/model" "encoding/json" "strings" "adm-ads/common.in/clinit" "gorm.io/gorm" ) // 处理任务5的删除 func handleTask10Delete(ads10 *model.Ads10) error { err := ads10.Delete(clinit.DB(), map[string]interface{}{"vin": ads10.Vin}) if err == gorm.ErrRecordNotFound { return nil } return err } func supplementAds10(old, new *model.Ads10) 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 } 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 old.AxleNumber = new.AxleNumber } if (old.SteeringMode == "" || old.SteeringMode == "0") && 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 } // 处理任务5的插入 func handleTask10InsertOrUpdate(ads10 *model.Ads10) error { oldAds10 := &model.Ads10{} db := clinit.DB() err := oldAds10.Query(db, map[string]interface{}{"vin": ads10.Vin}) if err != nil { if err == gorm.ErrRecordNotFound { err = ads10.Insert(db) if err != nil && !strings.Contains(err.Error(), "Duplicate") { return err } return nil } return err } else { // 更新时间大于消息时间表示是后面的消息,不处理 /*if oldAds10.UpdatedAt > ads10.UpdatedAt{ return nil }*/ isSupple := supplementAds10(oldAds10, ads10) if isSupple { oldAds10.UpdatedAt = ads10.UpdatedAt err = oldAds10.Update(db) if err != nil { return err } } else { return nil } } return nil } // 合格证信息 func Task10(adsMsg *apis.AdsMessage) (err error) { ads10 := &model.Ads10{} json.Unmarshal([]byte(adsMsg.Content), &ads10) if ads10.Vin == "" { return nil } ads10.CreatedAt = adsMsg.Timestamp ads10.UpdatedAt = adsMsg.Timestamp if adsMsg.Action == consts.ACTIONDELETE { // 删除 err = handleTask10Delete(ads10) } else { // 新增或插入 err = handleTask10InsertOrUpdate(ads10) } return err }