task10.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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/common.in/utils"
  7. "gadm-dws/consts"
  8. "gadm-dws/model"
  9. "encoding/json"
  10. "gorm.io/gorm"
  11. "strings"
  12. )
  13. func insertOrUpdateDws10(db *gorm.DB, dws10 *model.Dws10, dwsMessage *apis.DwsMessage, outputSourceCode string) (adsMsgList []*apis.AdsMessage, dwsMsgList []*apis.DwsMessage, err error) {
  14. if dws10.ThirdStyleId != "" {
  15. dws13 := &model.Dws13{}
  16. err = dws13.Query(db, map[string]interface{}{"third_style_id": dws10.ThirdStyleId})
  17. if err == nil {
  18. if len(dws13.StyleId) > 6 {
  19. dws10.SeriesId = dws13.StyleId[0:6]
  20. }
  21. if dws10.ModelYear == "" && dws13.ModelYear != "" {
  22. dws10.ModelYear = dws13.ModelYear
  23. }
  24. }
  25. }
  26. if dws10.SeriesId == "" {
  27. return nil, nil, nil
  28. }
  29. dws10.Uniq = utils.MD5(dws10.ModelNo + dws10.ModelYear + dws10.Vin10 + dws10.VinRule + dws10.SeriesId + dws10.DisplacementL + dws10.AirIntakForm + dws10.ThirdSeriesId)
  30. oldDws10 := &model.Dws10{}
  31. err = oldDws10.Query(db, map[string]interface{}{"uniq": dws10.Uniq})
  32. if err != nil {
  33. if err != gorm.ErrRecordNotFound {
  34. return nil, nil, err
  35. }
  36. } else {
  37. return nil, nil, nil
  38. }
  39. // 查询车系id为空的,为空的补充
  40. /*if dws10.SeriesId != "" {
  41. uniq := utils.MD5(dws10.ModelNo + dws10.ModelYear + dws10.Vin10 + dws10.VinRule + dws10.DisplacementL + dws10.AirIntakForm + dws10.ThirdSeriesId)
  42. err = oldDws10.Query(db, map[string]interface{}{"uniq": uniq})
  43. if err != nil {
  44. if err != gorm.ErrRecordNotFound {
  45. return nil, nil, err
  46. }
  47. } else {
  48. dws10.ID = oldDws10.ID
  49. adsMsg := NewAdsMessage(dwsMessage, outputSourceCode, consts.ACTIONDELETE)
  50. msgByte, _ := json.Marshal(*oldDws10)
  51. adsMsg.Content = string(msgByte)
  52. adsMsgList = append(adsMsgList, adsMsg)
  53. }
  54. }
  55. if dws10.ID == 0 {
  56. err = dws10.Insert(db)
  57. if err != nil {
  58. if !strings.Contains(err.Error(), "Duplicate") {
  59. return nil, nil, err
  60. } else {
  61. return nil, nil, nil
  62. }
  63. }
  64. } else {
  65. err = dws10.Update(db)
  66. if err != nil {
  67. if err != gorm.ErrRecordNotFound {
  68. return nil, nil, err
  69. }
  70. }
  71. }*/
  72. err = dws10.Insert(db)
  73. if err != nil {
  74. if !strings.Contains(err.Error(), "Duplicate") {
  75. return nil, nil, err
  76. } else {
  77. return nil, nil, nil
  78. }
  79. }
  80. adsMsg := NewAdsMessage(dwsMessage, outputSourceCode, consts.ACTIONINSERT)
  81. msgByte, _ := json.Marshal(*dws10)
  82. adsMsg.Content = string(msgByte)
  83. adsMsgList = append(adsMsgList, adsMsg)
  84. return adsMsgList, nil, nil
  85. }
  86. // vin定型规则
  87. func Dws10Task(db *gorm.DB, dwsMessage *apis.DwsMessage, outputSourceCode string) (adsMsgList []*apis.AdsMessage, dwsMsgList []*apis.DwsMessage, err error) {
  88. dws10 := &model.Dws10{}
  89. err = json.Unmarshal([]byte(dwsMessage.Content), dws10)
  90. if err != nil {
  91. return nil, nil, nil
  92. }
  93. if dws10.SeriesSource != "" {
  94. dws10.Source = dws10.SeriesSource
  95. }
  96. if dws10.ModelNo == "" {
  97. return nil, nil, nil
  98. }
  99. if dws10.Vin != "" {
  100. lenVin := len(dws10.Vin)
  101. if lenVin > 8 {
  102. dws10.VinRule = dws10.Vin[0:8]
  103. }
  104. if lenVin > 10 {
  105. dws10.Vin10 = dws10.Vin[9:10]
  106. }
  107. }
  108. if dws10.VinRule == "" {
  109. return nil, nil, nil
  110. }
  111. if dws10.ThirdStyleId != "" || dws10.SeriesId != "" {
  112. adsMsgs, _, err := insertOrUpdateDws10(db, dws10, dwsMessage, outputSourceCode)
  113. if err != nil {
  114. return nil, nil, err
  115. }
  116. if len(adsMsgs) > 0 {
  117. adsMsgList = append(adsMsgList, adsMsgs...)
  118. }
  119. }
  120. if dws10.ThirdStyleIds != "" {
  121. thirdStyleIds := strings.Split(dws10.ThirdStyleIds, ",")
  122. for _, v := range thirdStyleIds {
  123. if v == "" {
  124. continue
  125. }
  126. dws10.ThirdStyleId = v
  127. dws10.ID = 0
  128. dws10.SeriesId = ""
  129. adsMsgs, _, err := insertOrUpdateDws10(db, dws10, dwsMessage, outputSourceCode)
  130. if err != nil {
  131. return nil, nil, err
  132. }
  133. if len(adsMsgs) > 0 {
  134. adsMsgList = append(adsMsgList, adsMsgs...)
  135. }
  136. }
  137. }
  138. /* if dws10.ThirdStyleId != "" {
  139. dws13 := &model.Dws13{}
  140. err = dws13.Query(db, map[string]interface{}{"third_style_id": dws10.ThirdStyleId})
  141. if err == nil {
  142. if len(dws13.StyleId) > 6 {
  143. dws10.SeriesId = dws13.StyleId[0:6]
  144. }
  145. }
  146. }
  147. dws10.Uniq = utils.MD5(dws10.ModelNo + dws10.ModelYear + dws10.Vin10 + dws10.VinRule + dws10.SeriesId + dws10.DisplacementL + dws10.AirIntakForm + dws10.ThirdSeriesId)
  148. oldDws10 := &model.Dws10{}
  149. err = oldDws10.Query(db, map[string]interface{}{"uniq": dws10.Uniq})
  150. if err != nil {
  151. if err != gorm.ErrRecordNotFound {
  152. return nil, nil, err
  153. }
  154. } else {
  155. return nil, nil, nil
  156. }
  157. // 查询车系id为空的,为空的补充
  158. if dws10.SeriesId != "" {
  159. uniq := utils.MD5(dws10.ModelNo + dws10.ModelYear + dws10.Vin10 + dws10.VinRule + dws10.DisplacementL + dws10.AirIntakForm + dws10.ThirdSeriesId)
  160. err = oldDws10.Query(db, map[string]interface{}{"uniq": uniq})
  161. if err != nil {
  162. if err != gorm.ErrRecordNotFound {
  163. return nil, nil, err
  164. }
  165. } else {
  166. dws10.ID = oldDws10.ID
  167. adsMsg := NewAdsMessage(dwsMessage, outputSourceCode, consts.ACTIONDELETE)
  168. msgByte, _ := json.Marshal(*oldDws10)
  169. adsMsg.Content = string(msgByte)
  170. adsMsgList = append(adsMsgList, adsMsg)
  171. }
  172. }
  173. if dws10.ID == 0 {
  174. err = dws10.Insert(db)
  175. if err != nil {
  176. if !strings.Contains(err.Error(), "Duplicate") {
  177. return nil, nil, err
  178. } else {
  179. return nil, nil, nil
  180. }
  181. }
  182. } else {
  183. err = dws10.Update(db)
  184. if err != nil {
  185. if err != gorm.ErrRecordNotFound {
  186. return nil, nil, err
  187. }
  188. }
  189. }
  190. adsMsg := NewAdsMessage(dwsMessage, outputSourceCode, consts.ACTIONINSERT)
  191. msgByte, _ := json.Marshal(*dws10)
  192. adsMsg.Content = string(msgByte)
  193. adsMsgList = append(adsMsgList, adsMsg)
  194. */
  195. return adsMsgList, nil, nil
  196. }