// 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/common.in/utils" "gadm-dws/consts" "gadm-dws/model" "encoding/json" "gorm.io/gorm" "strings" ) func insertOrUpdateDws10(db *gorm.DB, dws10 *model.Dws10, dwsMessage *apis.DwsMessage, outputSourceCode string) (adsMsgList []*apis.AdsMessage, dwsMsgList []*apis.DwsMessage, err error) { if dws10.ThirdStyleId != "" { dws13 := &model.Dws13{} err = dws13.Query(db, map[string]interface{}{"third_style_id": dws10.ThirdStyleId}) if err == nil { if len(dws13.StyleId) > 6 { dws10.SeriesId = dws13.StyleId[0:6] } if dws10.ModelYear == "" && dws13.ModelYear != "" { dws10.ModelYear = dws13.ModelYear } } } if dws10.SeriesId == "" { return nil, nil, nil } dws10.Uniq = utils.MD5(dws10.ModelNo + dws10.ModelYear + dws10.Vin10 + dws10.VinRule + dws10.SeriesId + dws10.DisplacementL + dws10.AirIntakForm + dws10.ThirdSeriesId) oldDws10 := &model.Dws10{} err = oldDws10.Query(db, map[string]interface{}{"uniq": dws10.Uniq}) if err != nil { if err != gorm.ErrRecordNotFound { return nil, nil, err } } else { return nil, nil, nil } // 查询车系id为空的,为空的补充 /*if dws10.SeriesId != "" { uniq := utils.MD5(dws10.ModelNo + dws10.ModelYear + dws10.Vin10 + dws10.VinRule + dws10.DisplacementL + dws10.AirIntakForm + dws10.ThirdSeriesId) err = oldDws10.Query(db, map[string]interface{}{"uniq": uniq}) if err != nil { if err != gorm.ErrRecordNotFound { return nil, nil, err } } else { dws10.ID = oldDws10.ID adsMsg := NewAdsMessage(dwsMessage, outputSourceCode, consts.ACTIONDELETE) msgByte, _ := json.Marshal(*oldDws10) adsMsg.Content = string(msgByte) adsMsgList = append(adsMsgList, adsMsg) } } if dws10.ID == 0 { err = dws10.Insert(db) if err != nil { if !strings.Contains(err.Error(), "Duplicate") { return nil, nil, err } else { return nil, nil, nil } } } else { err = dws10.Update(db) if err != nil { if err != gorm.ErrRecordNotFound { return nil, nil, err } } }*/ err = dws10.Insert(db) if err != nil { if !strings.Contains(err.Error(), "Duplicate") { return nil, nil, err } else { return nil, nil, nil } } adsMsg := NewAdsMessage(dwsMessage, outputSourceCode, consts.ACTIONINSERT) msgByte, _ := json.Marshal(*dws10) adsMsg.Content = string(msgByte) adsMsgList = append(adsMsgList, adsMsg) return adsMsgList, nil, nil } // vin定型规则 func Dws10Task(db *gorm.DB, dwsMessage *apis.DwsMessage, outputSourceCode string) (adsMsgList []*apis.AdsMessage, dwsMsgList []*apis.DwsMessage, err error) { dws10 := &model.Dws10{} err = json.Unmarshal([]byte(dwsMessage.Content), dws10) if err != nil { return nil, nil, nil } if dws10.SeriesSource != "" { dws10.Source = dws10.SeriesSource } if dws10.ModelNo == "" { return nil, nil, nil } if dws10.Vin != "" { lenVin := len(dws10.Vin) if lenVin > 8 { dws10.VinRule = dws10.Vin[0:8] } if lenVin > 10 { dws10.Vin10 = dws10.Vin[9:10] } } if dws10.VinRule == "" { return nil, nil, nil } if dws10.ThirdStyleId != "" || dws10.SeriesId != "" { adsMsgs, _, err := insertOrUpdateDws10(db, dws10, dwsMessage, outputSourceCode) if err != nil { return nil, nil, err } if len(adsMsgs) > 0 { adsMsgList = append(adsMsgList, adsMsgs...) } } if dws10.ThirdStyleIds != "" { thirdStyleIds := strings.Split(dws10.ThirdStyleIds, ",") for _, v := range thirdStyleIds { if v == "" { continue } dws10.ThirdStyleId = v dws10.ID = 0 dws10.SeriesId = "" adsMsgs, _, err := insertOrUpdateDws10(db, dws10, dwsMessage, outputSourceCode) if err != nil { return nil, nil, err } if len(adsMsgs) > 0 { adsMsgList = append(adsMsgList, adsMsgs...) } } } /* if dws10.ThirdStyleId != "" { dws13 := &model.Dws13{} err = dws13.Query(db, map[string]interface{}{"third_style_id": dws10.ThirdStyleId}) if err == nil { if len(dws13.StyleId) > 6 { dws10.SeriesId = dws13.StyleId[0:6] } } } dws10.Uniq = utils.MD5(dws10.ModelNo + dws10.ModelYear + dws10.Vin10 + dws10.VinRule + dws10.SeriesId + dws10.DisplacementL + dws10.AirIntakForm + dws10.ThirdSeriesId) oldDws10 := &model.Dws10{} err = oldDws10.Query(db, map[string]interface{}{"uniq": dws10.Uniq}) if err != nil { if err != gorm.ErrRecordNotFound { return nil, nil, err } } else { return nil, nil, nil } // 查询车系id为空的,为空的补充 if dws10.SeriesId != "" { uniq := utils.MD5(dws10.ModelNo + dws10.ModelYear + dws10.Vin10 + dws10.VinRule + dws10.DisplacementL + dws10.AirIntakForm + dws10.ThirdSeriesId) err = oldDws10.Query(db, map[string]interface{}{"uniq": uniq}) if err != nil { if err != gorm.ErrRecordNotFound { return nil, nil, err } } else { dws10.ID = oldDws10.ID adsMsg := NewAdsMessage(dwsMessage, outputSourceCode, consts.ACTIONDELETE) msgByte, _ := json.Marshal(*oldDws10) adsMsg.Content = string(msgByte) adsMsgList = append(adsMsgList, adsMsg) } } if dws10.ID == 0 { err = dws10.Insert(db) if err != nil { if !strings.Contains(err.Error(), "Duplicate") { return nil, nil, err } else { return nil, nil, nil } } } else { err = dws10.Update(db) if err != nil { if err != gorm.ErrRecordNotFound { return nil, nil, err } } } adsMsg := NewAdsMessage(dwsMessage, outputSourceCode, consts.ACTIONINSERT) msgByte, _ := json.Marshal(*dws10) adsMsg.Content = string(msgByte) adsMsgList = append(adsMsgList, adsMsg) */ return adsMsgList, nil, nil }