123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- // 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
- }
|