12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package task
- import (
- "adm-dws/apis"
- "adm-dws/consts"
- "adm-dws/model"
- "encoding/json"
- "strings"
- "gorm.io/gorm"
- )
- // 车牌和姓名对应关系(正确关系)
- func Dws3Task(db *gorm.DB,dwsMessage *apis.DwsMessage, outputSourceCode string) (adsMsgList []*apis.AdsMessage, dwsMsgList []*apis.DwsMessage, err error) {
- dws3 := &model.Dws3{}
- err = json.Unmarshal([]byte(dwsMessage.Content), dws3)
- if err != nil {
- return nil, nil, nil
- }
- dws3.UpdatedAt = dwsMessage.Timestamp
- dws3.CreatedAt = dwsMessage.Timestamp
- if dws3.Owner == "" && dws3.Name != ""{
- dws3.Owner = dws3.Name
- }
- if dws3.Owner == "" || dws3.PlateNo == "" || dws3.PlateType == ""{
- return nil, nil, nil
- }
- oldDws3 := &model.Dws3{}
- where := map[string]interface{}{"plate_no": dws3.PlateNo, "plate_type": dws3.PlateType}
- err = oldDws3.Query(db, where)
- if err == nil {
- // 有数据
- if oldDws3.Owner == dws3.Owner {
- // 不处理
- return nil, nil,nil
- } else {
- // 更新时间大于消息时间表示是后面的消息,不处理
- if oldDws3.UpdatedAt > dws3.UpdatedAt{
- return nil,nil,nil
- }
- oldDws3.Owner = dws3.Owner
- oldDws3.UpdatedAt = dws3.UpdatedAt
- err = oldDws3.Update(db)
- if err != nil{
- if err != gorm.ErrRecordNotFound{
- return nil,nil,err
- }
- }
- }
- } else {
- // 无数据
- if err == gorm.ErrRecordNotFound {
- err = dws3.Insert(db)
- if err != nil {
- if !strings.Contains(err.Error(), "Duplicate") {
- return nil,nil, err
- }
- }
- oldDws3 = dws3
- } else{
- return nil,nil,err
- }
- }
- adsMsg := NewAdsMessage(dwsMessage, outputSourceCode, consts.ACTIONINSERT)
- msgByte, _ := json.Marshal(*oldDws3)
- adsMsg.Content = string(msgByte)
- adsMsgList = append(adsMsgList,adsMsg)
- return adsMsgList, nil, nil
- }
|