task19.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2019 autocareai.com. All rights reserved.
  2. // Use of this source code is governed by autocareai.com.
  3. package task
  4. import (
  5. "gadm-dws/apis"
  6. "gadm-dws/consts"
  7. "gadm-dws/model"
  8. "encoding/json"
  9. "github.com/tidwall/gjson"
  10. "strings"
  11. "gorm.io/gorm"
  12. )
  13. func strimZero(in string) string {
  14. switch in {
  15. case "0":
  16. return ""
  17. case "0.0":
  18. return ""
  19. }
  20. return in
  21. }
  22. func handleDws19(db *gorm.DB, engineType, ratedPower, displacement string, dwsMessage *apis.DwsMessage, outputSourceCode string) (*apis.AdsMessage, error) {
  23. dws19 := &model.Dws19{}
  24. dws19.RatedPower = strimZero(ratedPower)
  25. dws19.Displacement = strimZero(displacement)
  26. dws19.EngineType = engineType
  27. if dws19.EngineType == "" {
  28. return nil, nil
  29. }
  30. if dws19.RatedPower == "" && dws19.Displacement == "" {
  31. return nil, nil
  32. }
  33. oldDws19 := &model.Dws19{}
  34. where := map[string]interface{}{"engine_type": dws19.EngineType}
  35. err := oldDws19.Query(db, where)
  36. if err == nil {
  37. needUpdate := false
  38. oldDws19.Displacement = strimZero(oldDws19.Displacement)
  39. oldDws19.RatedPower = strimZero(oldDws19.RatedPower)
  40. // 有数据
  41. if dws19.RatedPower != "" && oldDws19.RatedPower != dws19.RatedPower{
  42. oldDws19.RatedPower = dws19.RatedPower
  43. needUpdate = true
  44. }
  45. if dws19.Displacement != "" && oldDws19.Displacement != dws19.Displacement {
  46. oldDws19.Displacement = dws19.Displacement
  47. needUpdate = true
  48. }
  49. if !needUpdate {
  50. return nil, nil
  51. }
  52. err = oldDws19.Update(db)
  53. if err != nil {
  54. if err != gorm.ErrRecordNotFound {
  55. return nil, err
  56. }
  57. }
  58. } else {
  59. // 无数据
  60. if err == gorm.ErrRecordNotFound {
  61. err = dws19.Insert(db)
  62. if err != nil {
  63. if !strings.Contains(err.Error(), "Duplicate") {
  64. return nil, err
  65. }
  66. }
  67. oldDws19 = dws19
  68. } else {
  69. return nil, err
  70. }
  71. }
  72. adsMsg := NewAdsMessage(dwsMessage, outputSourceCode, consts.ACTIONINSERT)
  73. msgByte, _ := json.Marshal(*oldDws19)
  74. adsMsg.Content = string(msgByte)
  75. return adsMsg, nil
  76. }
  77. func Dws19Task(db *gorm.DB, dwsMessage *apis.DwsMessage, outputSourceCode string) (adsMsgList []*apis.AdsMessage, dwsMsgList []*apis.DwsMessage, err error) {
  78. dws19 := &model.Dws19{}
  79. err = json.Unmarshal([]byte(dwsMessage.Content), dws19)
  80. if err != nil {
  81. return nil, nil, nil
  82. }
  83. //dws19.RatedPower = strimZero(dws19.RatedPower)
  84. //dws19.Displacement = strimZero(dws19.Displacement)
  85. adsMsg, err := handleDws19(db, dws19.EngineType, dws19.RatedPower, dws19.Displacement, dwsMessage, outputSourceCode)
  86. if err != nil {
  87. return nil, nil, err
  88. }
  89. if adsMsg != nil {
  90. adsMsgList = append(adsMsgList, adsMsg)
  91. }
  92. if dws19.EngineInfo != "" {
  93. engineInfoList := gjson.Parse(dws19.EngineInfo).Array()
  94. for _, v := range engineInfoList {
  95. engineType := v.Get("engine_model").String()
  96. displacement := v.Get("engine_cc").String()
  97. ratePower := v.Get("engine_power").String()
  98. adsMsg, err := handleDws19(db, engineType, ratePower, displacement, dwsMessage, outputSourceCode)
  99. if err != nil {
  100. return nil, nil, err
  101. }
  102. if adsMsg != nil {
  103. adsMsgList = append(adsMsgList, adsMsg)
  104. }
  105. }
  106. }
  107. return adsMsgList, nil, nil
  108. }