123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- // 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"
- "gorm.io/gorm"
- "strings"
- )
- func supplementDws7(old, new *model.Dws7) (bool) {
- isSupple := false
- if old.InitialRegistrationDate == "" && new.InitialRegistrationDate != "" {
- isSupple = true
- old.InitialRegistrationDate = new.InitialRegistrationDate
- }
- if old.VehicleType == "" && new.VehicleType != "" {
- isSupple = true
- old.VehicleType = new.VehicleType
- }
- if old.UseProperty == "" && new.UseProperty != "" {
- isSupple = true
- old.UseProperty = new.UseProperty
- }
- if old.FuelType == "" && new.FuelType != "" {
- isSupple = true
- old.FuelType = new.FuelType
- }
- if old.ModelNo == "" && new.ModelNo != "" {
- isSupple = true
- old.ModelNo = new.ModelNo
- }
- if old.VehicleBodyColor == "" && new.VehicleBodyColor != "" {
- isSupple = true
- old.VehicleBodyColor = new.VehicleBodyColor
- }
- return isSupple
- }
- // 获取交管信息
- func Dws7Task(db *gorm.DB,dwsMessage *apis.DwsMessage, outputSourceCode string) (adsMsgList []*apis.AdsMessage, dwsMsgList []*apis.DwsMessage, err error) {
- dws7 := &model.Dws7{}
- err = json.Unmarshal([]byte(dwsMessage.Content), dws7)
- if err != nil {
- return nil, nil, nil
- }
- if (dws7.Vin == "") || (dws7.InitialRegistrationDate == "" &&
- dws7.UseProperty == "" &&
- dws7.VehicleType == "" &&
- dws7.FuelType == "" &&
- dws7.ModelNo == "" &&
- dws7.VehicleBodyColor == "" ){
- return nil, nil, nil
- }
- oldDws7 := &model.Dws7{}
- err = oldDws7.Query(db, map[string]interface{}{"vin": dws7.Vin})
- if err == nil {
- // 有数据
- isSupple := supplementDws7(oldDws7,dws7)
- if isSupple {
- err = oldDws7.Update(db)
- if err != nil{
- return nil,nil,err
- }
- }else{
- return nil,nil,nil
- }
- } else {
- if err == gorm.ErrRecordNotFound {
- // 没有数据
- err = dws7.Insert(db)
- if err != nil {
- if !strings.Contains(err.Error(), "Duplicate") {
- return nil,nil, err
- }
- }
- oldDws7 = dws7
- } else {
- // 数据库错误
- return nil, nil, err
- }
- }
- adsMsg := NewAdsMessage(dwsMessage, outputSourceCode, consts.ACTIONINSERT)
- msgByte, _ := json.Marshal(*oldDws7)
- adsMsg.Content = string(msgByte)
- adsMsgList = append(adsMsgList,adsMsg)
- return adsMsgList, nil, nil
- }
|