123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- // Copyright 2019 autocareai.com. All rights reserved.
- // Use of this source code is governed by autocareai.com.
- package task
- import (
- "gadm-dws/apis"
- "gadm-dws/consts"
- "gadm-dws/model"
- "encoding/json"
- "strings"
- "github.com/tidwall/gjson"
- "gorm.io/gorm"
- )
- func isInDws2(db *gorm.DB,plateNo,plateType,vin string) (bool,error) {
- dws2 := &model.Dws2{}
- where := map[string]interface{}{"plate_no": plateNo,"vin": vin}
- err := dws2.Query(db, where)
- if err == nil {
- if dws2.PlateType == "" && plateType != ""{
- dws2.PlateType = plateType
- dws2.Update(db)
- }
- return true,nil
- }
- if err != gorm.ErrRecordNotFound {
- return false, err
- }
- return false,nil
- }
- func isInDws1(db *gorm.DB,dws1 *model.Dws1)(bool,error){
- // 判断是否存在于dws1
- where := map[string]interface{}{"plate_no": dws1.PlateNo,"vin":dws1.Vin}
- err := dws1.Query(db, where)
- if err == nil {
- // 有数据,直接返回
- return true,nil
- }else {
- // 没有数据
- if err == gorm.ErrRecordNotFound{
- return false,nil
- }else{
- // 数据库错误
- return false,err
- }
- }
- }
- func handlePlateTypeExist(dws1 *model.Dws1,db *gorm.DB) (bool,error) {
- // 删除dws4中数据
- dws4 := &model.Dws4{}
- where := map[string]interface{}{"plate_no": dws1.PlateNo,"vin":dws1.Vin}
- err := dws4.Delete(db,where)
- if err != nil {
- if err != gorm.ErrRecordNotFound {
- return false,err
- }
- }
- // 插入或更新dws1
- // 通过车牌查询
- where = map[string]interface{}{"plate_no": dws1.PlateNo,"plate_type":dws1.PlateType}
- oldDws1 := &model.Dws1{}
- err = oldDws1.Query(db, where)
- if err != nil {
- // 没有数据
- if err == gorm.ErrRecordNotFound { // 没有数据则插入
- err = dws1.Insert(db)
- if err != nil {
- if !strings.Contains(err.Error(), "Duplicate") {
- return false,err
- }
- }
- }
- } else { // 有数据,判断是否一样
- if oldDws1.Vin == dws1.Vin {
- return true,nil
- } else {
- // 更新时间大于消息时间表示是后面的消息,不处理
- if oldDws1.UpdatedAt > dws1.UpdatedAt{
- return true,nil
- }
- // vin 码不一样
- // 插入过期数据
- dws2 := &model.Dws2{}
- dws2.PlateNo = oldDws1.PlateNo
- dws2.PlateType = oldDws1.PlateType
- dws2.Vin = oldDws1.Vin
- err = dws2.Insert(db)
- if err != nil {
- if !strings.Contains(err.Error(), "Duplicate") {
- return false,err
- }
- }
- // 更新为新数据
- oldDws1.Vin = dws1.Vin
- oldDws1.UpdatedAt = dws1.UpdatedAt
- err = oldDws1.Update(db)
- if err != nil{
- if !strings.Contains(err.Error(), "Duplicate") {
- return false,err
- }
- }
- }
- }
- return false,nil
- }
- // 车牌和vin码对应关系(正确关系)
- func Dws1Task(db *gorm.DB,dwsMessage *apis.DwsMessage, outputSourceCode string) (adsMsgList []*apis.AdsMessage, dwsMsgList []*apis.DwsMessage, err error) {
- dws1 := &model.Dws1{}
- err = json.Unmarshal([]byte(dwsMessage.Content), dws1)
- if err != nil {
- return nil, nil, nil
- }
- dws1.UpdatedAt = dwsMessage.Timestamp
- dws1.CreatedAt = dwsMessage.Timestamp
- // 参数判断
- if dws1.PlateNo == "" || dws1.Vin == ""{
- return nil ,nil ,nil
- }
- isExpire := gjson.Get(dwsMessage.Content, "is_expire").String()
- if isExpire != consts.EXPIREDATA {
- // 计算号牌种类
- if dws1.PlateType == ""{
- dws1.PlateType = calcPlateType(dws1.Long,dws1.ApprovedNumber,dws1.GrossMass)
- if dws1.PlateType == ""{
- dws1.PlateType = CalcPlateType(db,dws1.Vin)
- }
- }
- if dws1.PlateType == ""{
- // 号牌种类为空
- // 判断是否在dws1
- isInDws1 ,err := isInDws1(db,dws1)
- if err != nil{
- return nil,nil,err
- }
- if isInDws1{
- return nil,nil,nil
- }
- // 发送到dws4
- dwsMsg := NewDwsMessage(dwsMessage,outputSourceCode)
- msgByte, _ := json.Marshal(*dws1)
- dwsMsg.Content = string(msgByte)
- adsMsgs,_,err := Dws4Task(db,dwsMsg, consts.SOURCEDWS4)
- if err != nil{
- return nil,nil,err
- }
- adsMsgList = append(adsMsgList,adsMsgs...)
- }else{
- // 号牌种类不为空
- // 判断错误关系表中是否存在,存在直接返回
- isInDws2 ,err := isInDws2(db,dws1.PlateNo,dws1.PlateType,dws1.Vin)
- if err != nil{
- return nil,nil,err
- }
- if isInDws2 {
- return nil,nil,nil
- }
- isExist ,err := handlePlateTypeExist(dws1,db)
- if err != nil{
- return nil,nil,err
- }
- // 已经存在不同步到ads
- if isExist{
- return nil,nil,nil
- }
- // 发送到ads
- adsMsg := NewAdsMessage(dwsMessage, outputSourceCode, consts.ACTIONINSERT)
- msgByte, _ := json.Marshal(*dws1)
- adsMsg.Content = string(msgByte)
- adsMsgList = append(adsMsgList,adsMsg)
- }
- }
- return adsMsgList, nil, nil
- }
|