123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- // 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"
- "strings"
- "gorm.io/gorm"
- )
- func strimZero(in string) string {
- switch in {
- case "0":
- return ""
- case "0.0":
- return ""
- }
- return in
- }
- func handleDws19(db *gorm.DB, engineType, ratedPower, displacement string, dwsMessage *apis.DwsMessage, outputSourceCode string) (*apis.AdsMessage, error) {
- dws19 := &model.Dws19{}
- dws19.RatedPower = strimZero(ratedPower)
- dws19.Displacement = strimZero(displacement)
- dws19.EngineType = engineType
- if dws19.EngineType == "" {
- return nil, nil
- }
- if dws19.RatedPower == "" && dws19.Displacement == "" {
- return nil, nil
- }
- oldDws19 := &model.Dws19{}
- where := map[string]interface{}{"engine_type": dws19.EngineType}
- err := oldDws19.Query(db, where)
- if err == nil {
- needUpdate := false
- oldDws19.Displacement = strimZero(oldDws19.Displacement)
- oldDws19.RatedPower = strimZero(oldDws19.RatedPower)
- // 有数据
- if dws19.RatedPower != "" && oldDws19.RatedPower != dws19.RatedPower{
- oldDws19.RatedPower = dws19.RatedPower
- needUpdate = true
- }
- if dws19.Displacement != "" && oldDws19.Displacement != dws19.Displacement {
- oldDws19.Displacement = dws19.Displacement
- needUpdate = true
- }
- if !needUpdate {
- return nil, nil
- }
- err = oldDws19.Update(db)
- if err != nil {
- if err != gorm.ErrRecordNotFound {
- return nil, err
- }
- }
- } else {
- // 无数据
- if err == gorm.ErrRecordNotFound {
- err = dws19.Insert(db)
- if err != nil {
- if !strings.Contains(err.Error(), "Duplicate") {
- return nil, err
- }
- }
- oldDws19 = dws19
- } else {
- return nil, err
- }
- }
- adsMsg := NewAdsMessage(dwsMessage, outputSourceCode, consts.ACTIONINSERT)
- msgByte, _ := json.Marshal(*oldDws19)
- adsMsg.Content = string(msgByte)
- return adsMsg, nil
- }
- func Dws19Task(db *gorm.DB, dwsMessage *apis.DwsMessage, outputSourceCode string) (adsMsgList []*apis.AdsMessage, dwsMsgList []*apis.DwsMessage, err error) {
- dws19 := &model.Dws19{}
- err = json.Unmarshal([]byte(dwsMessage.Content), dws19)
- if err != nil {
- return nil, nil, nil
- }
- //dws19.RatedPower = strimZero(dws19.RatedPower)
- //dws19.Displacement = strimZero(dws19.Displacement)
- adsMsg, err := handleDws19(db, dws19.EngineType, dws19.RatedPower, dws19.Displacement, dwsMessage, outputSourceCode)
- if err != nil {
- return nil, nil, err
- }
- if adsMsg != nil {
- adsMsgList = append(adsMsgList, adsMsg)
- }
- if dws19.EngineInfo != "" {
- engineInfoList := gjson.Parse(dws19.EngineInfo).Array()
- for _, v := range engineInfoList {
- engineType := v.Get("engine_model").String()
- displacement := v.Get("engine_cc").String()
- ratePower := v.Get("engine_power").String()
- adsMsg, err := handleDws19(db, engineType, ratePower, displacement, dwsMessage, outputSourceCode)
- if err != nil {
- return nil, nil, err
- }
- if adsMsg != nil {
- adsMsgList = append(adsMsgList, adsMsg)
- }
- }
- }
- return adsMsgList, nil, nil
- }
|