123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- // 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"
- "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
- }
|