// 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" "github.com/tidwall/gjson" "gorm.io/gorm" "strings" ) // 车牌和vin码对应关系(错误关系) func Dws2Task(db *gorm.DB,dwsMessage *apis.DwsMessage, outputSourceCode string) (adsMsgList []*apis.AdsMessage, dwsMsgList []*apis.DwsMessage, err error) { // 解析数据 dws2 := &model.Dws2{} err = json.Unmarshal([]byte(dwsMessage.Content), dws2) if err != nil { return nil, nil, nil } // 参数判断 if dws2.PlateNo == "" || dws2.Vin == ""{ return nil ,nil ,nil } // 计算号牌种类 if dws2.PlateType == ""{ dws2.PlateType = CalcPlateType(db,dws2.Vin) } isExpire := gjson.Get(dwsMessage.Content, "is_expire").String() if isExpire == consts.EXPIREDATA { // 删除dws1 where := map[string]interface{}{"plate_no": dws2.PlateNo, "vin": dws2.Vin} dws1 := &model.Dws1{} err = dws1.Query(db, where) if err != nil { if err != gorm.ErrRecordNotFound { return nil,nil, err } }else{ if dws2.PlateType == ""{ dws2.PlateType = dws1.PlateType } err = dws1.Delete(db,nil) if err != nil{ if err != gorm.ErrRecordNotFound { return nil,nil, err } } } // 插入更新dws2 oldDws2 := &model.Dws2{} err = oldDws2.Query(db,where) if err != nil{ if err == gorm.ErrRecordNotFound{ err = dws2.Insert(db) if err != nil { if !strings.Contains(err.Error(), "Duplicate") { return nil,nil, err } } }else{ return nil,nil,err } }else{ if oldDws2.PlateType == "" && dws2.PlateType != ""{ oldDws2.PlateType = dws2.PlateType err = oldDws2.Update(db) if err != nil{ if err != gorm.ErrRecordNotFound{ return nil,nil,err } } } } // 删除dws4 dws4 := &model.Dws4{} err = dws4.Delete(db, where) if err != nil { if err != gorm.ErrRecordNotFound { return nil,nil, err } } }else{ return nil ,nil ,nil } adsMsg := NewAdsMessage(dwsMessage, outputSourceCode, consts.ACTIONDELETE) msgByte, _ := json.Marshal(*dws2) adsMsg.Content = string(msgByte) adsMsgList = append(adsMsgList,adsMsg) return adsMsgList, nil, nil }