123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- // 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"
- "github.com/tidwall/gjson"
- "gorm.io/gorm"
- "strings"
- )
- // 处理任务1的删除
- func handleTask1Delete(adsMsg *apis.AdsMessage) error {
- content := adsMsg.Content
- plateNo := gjson.Get(content, "plate_no").String()
- //plateType := gjson.Get(content, "plate_type").String()
- vin := gjson.Get(content, "vin").String()
- if plateNo == "" || vin == ""{
- return nil
- }
- ads1 := &model.Ads1{}
- err := ads1.Delete(clinit.DB(),map[string]interface{}{"plate_no":plateNo,"vin":vin})
- return err
- }
- // 处理任务1的插入
- func handleTask1InsertOrUpdate(adsMsg *apis.AdsMessage)error{
- content := adsMsg.Content
- plateNo := gjson.Get(content, "plate_no").String()
- plateType := gjson.Get(content, "plate_type").String()
- vin := gjson.Get(content, "vin").String()
- if plateNo == "" || vin == ""{
- return nil
- }
- ads1 := &model.Ads1{}
- where := map[string]interface{}{"plate_no":plateNo}
- var ads1List []model.Ads1
- err := ads1.QueryAll(clinit.DB(),where,&ads1List)
- if len(ads1List) == 0 {
- err = gorm.ErrRecordNotFound
- }
- if err != nil{
- // 没有数据插入
- if err == gorm.ErrRecordNotFound{
- ads1.PlateNo = plateNo
- ads1.PlateType = plateType
- ads1.Vin = vin
- ads1.CreatedAt = adsMsg.Timestamp
- ads1.UpdatedAt = adsMsg.Timestamp
- err = ads1.Insert(clinit.DB())
- if err != nil && !strings.Contains(err.Error(), "Duplicate") {
- return err
- }
- return nil
- }
- return err
- }
- for _,v := range ads1List{
- // 号牌种类相同的数据,判断vin码是不是相等
- if plateType != "" && v.PlateType != "" && plateType == v.PlateType{
- // 更新时间大于消息时间表示是后面的消息,不处理
- if v.UpdatedAt > adsMsg.Timestamp{
- return nil
- }
- if vin != v.Vin{
- v.Vin= vin
- v.UpdatedAt = adsMsg.Timestamp
- err = v.Update(clinit.DB())
- return err
- }
- return nil
- }else if v.Vin == vin{
- // 车架号相同的数据
- if v.PlateType == "" && plateType != ""{
- // 补充号牌种类
- v.PlateType = plateType
- v.UpdatedAt = adsMsg.Timestamp
- err = v.Update(clinit.DB())
- return err
- }
- return nil
- }
- }
- // 最后插入,号牌种类不同,vin码不同的数据
- ads1.PlateNo = plateNo
- ads1.PlateType = plateType
- ads1.Vin = vin
- ads1.CreatedAt = adsMsg.Timestamp
- ads1.UpdatedAt = adsMsg.Timestamp
- err = ads1.Insert(clinit.DB())
- if err != nil && !strings.Contains(err.Error(), "Duplicate") {
- return err
- }
- return nil
- }
- // 车牌和vin码对应关系(正确关系)
- func Task1(adsMsg *apis.AdsMessage) (err error) {
- if adsMsg.Action == consts.ACTIONDELETE{
- // 删除
- err = handleTask1Delete(adsMsg)
- }else{
- // 新增或插入
- err = handleTask1InsertOrUpdate(adsMsg)
- }
- return err
- }
|