task11.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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-ads/apis"
  6. "adm-ads/common.in/clinit"
  7. "adm-ads/consts"
  8. "adm-ads/model"
  9. "encoding/json"
  10. "gorm.io/gorm"
  11. "strings"
  12. )
  13. // 处理任务5的删除
  14. func handleTask11Delete(ads11 *model.Ads11) error {
  15. err := ads11.Delete(clinit.DB(),map[string]interface{}{"third_style_id":ads11.ThirdStyleId,"source":ads11.Source})
  16. if err == gorm.ErrRecordNotFound{
  17. return nil
  18. }
  19. return err
  20. }
  21. func supplementAds11(old, new *model.Ads11) bool {
  22. isSupple := false
  23. if old.ThirdBrandId == "" && new.ThirdBrandId != "" {
  24. isSupple = true
  25. old.ThirdBrandId = new.ThirdBrandId
  26. }
  27. if old.ThirdSeriesId == "" && new.ThirdSeriesId != "" {
  28. isSupple = true
  29. old.ThirdSeriesId = new.ThirdSeriesId
  30. }
  31. if old.ThirdBrandName == "" && new.ThirdBrandName != "" {
  32. isSupple = true
  33. old.ThirdBrandName = new.ThirdBrandName
  34. }
  35. if old.ThirdSeriesName == "" && new.ThirdSeriesName != "" {
  36. isSupple = true
  37. old.ThirdSeriesName = new.ThirdSeriesName
  38. }
  39. if old.ThirdStyleName == "" && new.ThirdStyleName != "" {
  40. isSupple = true
  41. old.ThirdStyleName = new.ThirdStyleName
  42. }
  43. if old.ThirdMaker == "" && new.ThirdMaker != "" {
  44. isSupple = true
  45. old.ThirdMaker = new.ThirdMaker
  46. }
  47. if old.ModelYear == "" && new.ModelYear != "" {
  48. isSupple = true
  49. old.ModelYear = new.ModelYear
  50. }
  51. if old.StyleId == "" && new.StyleId != "" {
  52. isSupple = true
  53. old.StyleId = new.StyleId
  54. }
  55. return isSupple
  56. }
  57. // 处理任务5的插入
  58. func handleTask11InsertOrUpdate(ads11 *model.Ads11)error{
  59. oldAds11 := &model.Ads11{}
  60. db := clinit.DB()
  61. err := oldAds11.Query(db,map[string]interface{}{"third_style_id":ads11.ThirdStyleId,"source":ads11.Source})
  62. if err != nil{
  63. if err == gorm.ErrRecordNotFound{
  64. err = ads11.Insert(db)
  65. if err != nil && !strings.Contains(err.Error(), "Duplicate") {
  66. return err
  67. }
  68. return nil
  69. }
  70. return err
  71. }else{
  72. isSupple := supplementAds11(oldAds11,ads11)
  73. if isSupple {
  74. oldAds11.UpdatedAt = ads11.UpdatedAt
  75. err = oldAds11.Update(db)
  76. if err != nil{
  77. return err
  78. }
  79. }else{
  80. return nil
  81. }
  82. }
  83. return nil
  84. }
  85. // 车型映射
  86. func Task11(adsMsg *apis.AdsMessage) (err error) {
  87. ads11 := &model.Ads11{}
  88. json.Unmarshal([]byte(adsMsg.Content),&ads11)
  89. if ads11.ThirdStyleId == ""{
  90. return nil
  91. }
  92. ads11.CreatedAt = adsMsg.Timestamp
  93. ads11.UpdatedAt = adsMsg.Timestamp
  94. if adsMsg.Action == consts.ACTIONDELETE{
  95. // 删除
  96. err = handleTask11Delete(ads11)
  97. }else{
  98. // 新增或插入
  99. err = handleTask11InsertOrUpdate(ads11)
  100. }
  101. return err
  102. }