123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- // 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/model"
- "encoding/json"
- "gorm.io/gorm"
- "strings"
- )
- // 计算号牌种类
- func Task20(adsMsg *apis.AdsMessage) (err error) {
- ads20 := &model.Ads20{}
- json.Unmarshal([]byte(adsMsg.Content), ads20)
- ads20.UpdatedAt = adsMsg.Timestamp
- ads20.CreatedAt = adsMsg.Timestamp
- if ads20.IdCard == "" || ads20.Name == ""{
- return nil
- }
- db := clinit.DB()
- oldAds20 := &model.Ads20{}
- where := map[string]interface{}{"id_card": ads20.IdCard}
- err = oldAds20.Query(db, where)
- if err == nil {
- // 有数据
- if oldAds20.Name == ads20.Name {
- // 不处理
- return nil
- } else {
- // 更新时间大于消息时间表示是后面的消息,不处理
- if oldAds20.UpdatedAt > ads20.UpdatedAt{
- return nil
- }
- oldAds20.Name = ads20.Name
- oldAds20.UpdatedAt = ads20.UpdatedAt
- err = oldAds20.Update(db)
- if err != nil{
- if err != gorm.ErrRecordNotFound{
- return err
- }
- }
- }
- } else {
- // 无数据
- if err == gorm.ErrRecordNotFound {
- err = ads20.Insert(db)
- if err != nil {
- if !strings.Contains(err.Error(), "Duplicate") {
- return err
- }
- }
- } else{
- return err
- }
- }
- return nil
- }
|