task14.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. "fmt"
  11. "go.uber.org/zap"
  12. "gorm.io/gorm"
  13. "strings"
  14. "time"
  15. )
  16. // 处理任务5的删除
  17. func handleTask14Delete(ads14 *model.Ads14) error {
  18. if ads14.C2Id == 0 {
  19. return nil
  20. }
  21. err := ads14.Delete(clinit.DB(),map[string]interface{}{"style_id":ads14.StyleId,"c2_id":ads14.C2Id})
  22. if err == gorm.ErrRecordNotFound{
  23. return nil
  24. }
  25. return err
  26. }
  27. // 处理任务14的插入
  28. func handleTask14InsertOrUpdate(ads14 *model.Ads14,db *gorm.DB) (err error){
  29. defer func() {
  30. if r := recover(); r != nil {
  31. err = fmt.Errorf("%+v", r)
  32. l.Error("err",
  33. zap.String("run_task", err.Error()),
  34. zap.Stack("stacktrace"))
  35. }
  36. }()
  37. if ads14.FrontWheelSpecification != ""{
  38. oldAds14 := &model.Ads14{}
  39. ads14.C2Id = 1001
  40. ads14.AttributeRule = ads14.FrontWheelSpecification
  41. err = oldAds14.Query(db,map[string]interface{}{"style_id":ads14.StyleId,"c2_id":ads14.C2Id})
  42. if err != nil{
  43. if err == gorm.ErrRecordNotFound{
  44. err = ads14.Insert(db)
  45. if err != nil && !strings.Contains(err.Error(), "Duplicate") {
  46. return err
  47. }
  48. }else{
  49. return err
  50. }
  51. }
  52. }
  53. if ads14.BackWheelSpecification != ""{
  54. oldAds14 := &model.Ads14{}
  55. ads14.C2Id = 1002
  56. ads14.ID = 0
  57. ads14.AttributeRule = ads14.BackWheelSpecification
  58. err = oldAds14.Query(db,map[string]interface{}{"style_id":ads14.StyleId,"c2_id":ads14.C2Id})
  59. if err != nil{
  60. if err == gorm.ErrRecordNotFound{
  61. err = ads14.Insert(db)
  62. if err != nil && !strings.Contains(err.Error(), "Duplicate") {
  63. return err
  64. }
  65. }else{
  66. return err
  67. }
  68. }
  69. }
  70. /*if ads14.TyreSize != ""{
  71. ads14.C2Id = 1001
  72. ads14.AttributeRule = ads14.TyreSize
  73. err = oldAds14.Query(db,map[string]interface{}{"style_id":ads14.StyleId,"c2_id":ads14.C2Id})
  74. if err != nil{
  75. if err == gorm.ErrRecordNotFound{
  76. err = ads14.Insert(db)
  77. if err != nil && !strings.Contains(err.Error(), "Duplicate") {
  78. return err
  79. }
  80. }else{
  81. return err
  82. }
  83. }
  84. ads14.ID = 0
  85. ads14.C2Id = 1002
  86. oldAds14.ID = 0
  87. err = oldAds14.Query(db,map[string]interface{}{"style_id":ads14.StyleId,"c2_id":ads14.C2Id })
  88. if err != nil{
  89. if err == gorm.ErrRecordNotFound{
  90. err = ads14.Insert(db)
  91. if err != nil && !strings.Contains(err.Error(), "Duplicate") {
  92. return err
  93. }
  94. }else{
  95. return err
  96. }
  97. }
  98. }*/
  99. return nil
  100. }
  101. // 获取维保配件项目
  102. func Task14(adsMsg *apis.AdsMessage) (err error) {
  103. ads14 := &model.Ads14{}
  104. json.Unmarshal([]byte(adsMsg.Content),&ads14)
  105. if ads14.StyleId == ""{
  106. return nil
  107. }
  108. timeNow := time.Now().Unix()
  109. ads14.CreatedAt = timeNow
  110. ads14.UpdatedAt = timeNow
  111. if adsMsg.Action == consts.ACTIONDELETE{
  112. // 删除
  113. err = handleTask14Delete(ads14)
  114. }else{
  115. // 新增或插入
  116. db := clinit.DB().Begin()
  117. err = handleTask14InsertOrUpdate(ads14,db)
  118. if err == nil {
  119. db.Commit()
  120. }else{
  121. db.Rollback()
  122. }
  123. }
  124. return err
  125. }