task15.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/consts"
  7. "adm-ads/model"
  8. "encoding/json"
  9. "strings"
  10. "adm-ads/common.in/clinit"
  11. "gorm.io/gorm"
  12. )
  13. // 处理任务6的删除
  14. func handleTask15Delete(ads15 *model.Ads15) error {
  15. err := ads15.Delete(clinit.DB(), map[string]interface{}{"vin": ads15.Vin})
  16. if err == gorm.ErrRecordNotFound {
  17. return nil
  18. }
  19. return err
  20. }
  21. func supplementAds15(old, new *model.Ads15) bool {
  22. isSupple := false
  23. if old.InsuranceFirstDate == "" && new.InsuranceFirstDate != "" {
  24. isSupple = true
  25. old.InsuranceFirstDate = new.InsuranceFirstDate
  26. } else if old.InsuranceFirstDate != "" && new.InsuranceFirstDate != "" {
  27. isSupple = true
  28. old.InsuranceFirstDate = new.InsuranceFirstDate
  29. }
  30. if old.Province == "" && new.Province != "" {
  31. isSupple = true
  32. old.Province = new.Province
  33. }
  34. if old.City == "" && new.City != "" {
  35. isSupple = true
  36. old.City = new.City
  37. }
  38. if old.District == "" && new.District != "" {
  39. isSupple = true
  40. old.District = new.District
  41. }
  42. if old.UsePropertyDetail == "" && new.UsePropertyDetail != "" {
  43. isSupple = true
  44. old.UsePropertyDetail = new.UsePropertyDetail
  45. }
  46. return isSupple
  47. }
  48. // 处理任务5的插入
  49. func handleTask15InsertOrUpdate(ads15 *model.Ads15) error {
  50. oldAds15 := &model.Ads15{}
  51. err := oldAds15.Query(clinit.DB(), map[string]interface{}{"vin": ads15.Vin})
  52. if err != nil {
  53. if err == gorm.ErrRecordNotFound {
  54. err = ads15.Insert(clinit.DB())
  55. if err != nil && !strings.Contains(err.Error(), "Duplicate") {
  56. return err
  57. }
  58. return nil
  59. }
  60. return err
  61. } else {
  62. // 更新时间大于消息时间表示是后面的消息,不处理
  63. if oldAds15.UpdatedAt > ads15.UpdatedAt {
  64. return nil
  65. }
  66. isSupple := supplementAds15(oldAds15, ads15)
  67. if isSupple {
  68. oldAds15.UpdatedAt = ads15.UpdatedAt
  69. err = oldAds15.Update(clinit.DB())
  70. if err != nil {
  71. return err
  72. }
  73. } else {
  74. return nil
  75. }
  76. }
  77. return nil
  78. }
  79. // 交强险首保日期
  80. func Task15(adsMsg *apis.AdsMessage) (err error) {
  81. ads15 := &model.Ads15{}
  82. json.Unmarshal([]byte(adsMsg.Content), &ads15)
  83. if (ads15.Vin == "") || (ads15.InsuranceFirstDate == "" && ads15.UsePropertyDetail == "") {
  84. return nil
  85. }
  86. if ads15.InsuranceFirstDate != "" {
  87. ads15.InsuranceFirstDate = strings.Split(ads15.InsuranceFirstDate, " ")[0]
  88. }
  89. ads15.CreatedAt = adsMsg.Timestamp
  90. ads15.UpdatedAt = adsMsg.Timestamp
  91. if adsMsg.Action == consts.ACTIONDELETE {
  92. // 删除
  93. err = handleTask15Delete(ads15)
  94. } else {
  95. // 新增或插入
  96. err = handleTask15InsertOrUpdate(ads15)
  97. }
  98. return err
  99. }