// 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" ) // 处理任务3的删除 func handleTask3Delete(ads3 *model.Ads3) error { err := ads3.Delete(clinit.DB(),map[string]interface{}{"plate_no":ads3.PlateNo,"plate_type":ads3.PlateType}) if err == gorm.ErrRecordNotFound{ return nil } return err } // 处理任务3的插入 func handleTask3InsertOrUpdate(ads3 *model.Ads3)error{ oldAds3 := &model.Ads3{} err := oldAds3.Query(clinit.DB(),map[string]interface{}{"plate_no":ads3.PlateNo,"plate_type":ads3.PlateType}) if err != nil{ if err == gorm.ErrRecordNotFound{ err = ads3.Insert(clinit.DB()) if err != nil && !strings.Contains(err.Error(), "Duplicate") { return err } return nil } return err }else{ // 更新时间大于消息时间表示是后面的消息,不处理 if oldAds3.UpdatedAt > ads3.UpdatedAt{ return nil } if oldAds3.Owner == ads3.Owner { return nil }else{ oldAds3.Owner = ads3.Owner oldAds3.UpdatedAt = ads3.UpdatedAt err = oldAds3.Update(clinit.DB()) if err == gorm.ErrRecordNotFound{ return nil }else{ return err } } } return nil } // 车牌和所有人对应关系(正确关系) func Task3(adsMsg *apis.AdsMessage) (err error) { ads3 := &model.Ads3{} json.Unmarshal([]byte(adsMsg.Content),&ads3) if ads3.PlateNo == "" || ads3.PlateType == "" || ads3.Owner == ""{ return nil } ads3.CreatedAt = adsMsg.Timestamp ads3.UpdatedAt = adsMsg.Timestamp if adsMsg.Action == consts.ACTIONDELETE{ // 删除 err = handleTask3Delete(ads3) }else{ // 新增或插入 err = handleTask3InsertOrUpdate(ads3) } return err }