123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- // 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/common.in/clinit"
- "adm-ads/consts"
- "adm-ads/model"
- "encoding/json"
- "gorm.io/gorm"
- "strings"
- )
- // 处理任务16的删除
- func handleTask16Delete(ads16 *model.Ads16) error {
- err := ads16.Delete(clinit.DB(),map[string]interface{}{"vin":ads16.Vin})
- if err == gorm.ErrRecordNotFound{
- return nil
- }
- return err
- }
- func supplementAds16(old, new *model.Ads16) (bool) {
- isSupple := false
- if old.InitialRegistrationDate == "" && new.InitialRegistrationDate != "" {
- isSupple = true
- old.InitialRegistrationDate = new.InitialRegistrationDate
- }
- if old.VehicleType == "" && new.VehicleType != "" {
- isSupple = true
- old.VehicleType = new.VehicleType
- }
- if old.UseProperty == "" && new.UseProperty != "" {
- isSupple = true
- old.UseProperty = new.UseProperty
- }
- if old.FuelType == "" && new.FuelType != "" {
- isSupple = true
- old.FuelType = new.FuelType
- }
- if old.ModelNo == "" && new.ModelNo != "" {
- isSupple = true
- old.ModelNo = new.ModelNo
- }
- if old.VehicleBodyColor == "" && new.VehicleBodyColor != "" {
- isSupple = true
- old.VehicleBodyColor = new.VehicleBodyColor
- }
- return isSupple
- }
- // 处理任务16的插入
- func handleTask16InsertOrUpdate(ads16 *model.Ads16)error{
- oldAds16 := &model.Ads16{}
- err := oldAds16.Query(clinit.DB(),map[string]interface{}{"vin":ads16.Vin})
- if err != nil{
- if err == gorm.ErrRecordNotFound{
- err = ads16.Insert(clinit.DB())
- if err != nil && !strings.Contains(err.Error(), "Duplicate") {
- return err
- }
- return nil
- }
- return err
- }else{
- // 更新时间大于消息时间表示是后面的消息,不处理
- /*if oldAds16.UpdatedAt > ads16.UpdatedAt{
- return nil
- }*/
- isSupple := supplementAds16(oldAds16,ads16)
- if isSupple {
- oldAds16.UpdatedAt = ads16.UpdatedAt
- err = oldAds16.Update(clinit.DB())
- if err != nil{
- return err
- }
- }else{
- return nil
- }
- }
- return nil
- }
- // 获取交管信息
- func Task16(adsMsg *apis.AdsMessage) (err error) {
- ads16 := &model.Ads16{}
- json.Unmarshal([]byte(adsMsg.Content),&ads16)
- if (ads16.Vin == "") || (ads16.InitialRegistrationDate == "" &&
- ads16.UseProperty == "" &&
- ads16.VehicleType == "" &&
- ads16.FuelType == "" &&
- ads16.ModelNo == "" &&
- ads16.VehicleBodyColor == "" ){
- return nil
- }
- ads16.CreatedAt = adsMsg.Timestamp
- ads16.UpdatedAt = adsMsg.Timestamp
- if adsMsg.Action == consts.ACTIONDELETE{
- // 删除
- err = handleTask16Delete(ads16)
- }else{
- // 新增或插入
- err = handleTask16InsertOrUpdate(ads16)
- }
- return err
- }
|