task13.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package task
  4. import (
  5. "adm-dws/apis"
  6. "adm-dws/consts"
  7. "adm-dws/model"
  8. "encoding/json"
  9. "gorm.io/gorm"
  10. "strings"
  11. )
  12. func supplementDws13(old, new *model.Dws13) (bool) {
  13. isSupple := false
  14. if old.ThirdBrandId == "" && new.ThirdBrandId != ""{
  15. isSupple = true
  16. old.ThirdBrandId = new.ThirdBrandId
  17. }
  18. if old.ThirdSeriesId == "" && new.ThirdSeriesId != ""{
  19. isSupple = true
  20. old.ThirdSeriesId = new.ThirdSeriesId
  21. }
  22. if old.ThirdBrandName == "" && new.ThirdBrandName != ""{
  23. isSupple = true
  24. old.ThirdBrandName = new.ThirdBrandName
  25. }
  26. if old.ThirdSeriesName == "" && new.ThirdSeriesName != ""{
  27. isSupple = true
  28. old.ThirdSeriesName = new.ThirdSeriesName
  29. }
  30. if old.ThirdStyleName == "" && new.ThirdStyleName != ""{
  31. isSupple = true
  32. old.ThirdStyleName = new.ThirdStyleName
  33. }
  34. if old.ThirdMaker == "" && new.ThirdMaker != ""{
  35. isSupple = true
  36. old.ThirdMaker = new.ThirdMaker
  37. }
  38. if old.ModelYear == "" && new.ModelYear != ""{
  39. isSupple = true
  40. old.ModelYear = new.ModelYear
  41. }
  42. if old.StyleId == "" && new.StyleId != ""{
  43. isSupple = true
  44. old.StyleId = new.StyleId
  45. }
  46. return isSupple
  47. }
  48. // 车型映射
  49. func Dws13Task(db *gorm.DB,dwsMessage *apis.DwsMessage, outputSourceCode string) (adsMsgList []*apis.AdsMessage, dwsMsgList []*apis.DwsMessage, err error) {
  50. dws13 := &model.Dws13{}
  51. err = json.Unmarshal([]byte(dwsMessage.Content), dws13)
  52. if err != nil {
  53. return nil, nil, nil
  54. }
  55. if dws13.ThirdStyleId == ""{
  56. return nil, nil, nil
  57. }
  58. oldDws13 := &model.Dws13{}
  59. err = oldDws13.Query(db,map[string]interface{}{"third_style_id":dws13.ThirdStyleId,"source":dws13.Source})
  60. if err != nil{
  61. if err != gorm.ErrRecordNotFound{
  62. return nil,nil,err
  63. }
  64. // 查无直接插入
  65. err = dws13.Insert(db)
  66. if err != nil{
  67. if !strings.Contains(err.Error(), "Duplicate") {
  68. return nil, nil,err
  69. }else{
  70. return nil,nil ,nil
  71. }
  72. }
  73. }else{
  74. // 有数据更新
  75. isSupple := supplementDws13(oldDws13,dws13)
  76. if !isSupple{// 无更新
  77. return nil ,nil ,nil
  78. }
  79. err = oldDws13.Update(db)
  80. if err != nil{
  81. return nil,nil, err
  82. }
  83. dws13 = oldDws13
  84. }
  85. adsMsg := NewAdsMessage(dwsMessage, outputSourceCode, consts.ACTIONINSERT)
  86. msgByte, _ := json.Marshal(*dws13)
  87. adsMsg.Content = string(msgByte)
  88. adsMsgList = append(adsMsgList,adsMsg)
  89. return adsMsgList, nil, nil
  90. }