task10.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. // 处理任务5的删除
  14. func handleTask10Delete(ads10 *model.Ads10) error {
  15. err := ads10.Delete(clinit.DB(), map[string]interface{}{"vin": ads10.Vin})
  16. if err == gorm.ErrRecordNotFound {
  17. return nil
  18. }
  19. return err
  20. }
  21. func supplementAds10(old, new *model.Ads10) bool {
  22. isSupple := false
  23. if old.CertificateNumber == "" && new.CertificateNumber != "" {
  24. isSupple = true
  25. old.CertificateNumber = new.CertificateNumber
  26. }
  27. if old.CertificateDate == "" && new.CertificateDate != "" {
  28. isSupple = true
  29. old.CertificateDate = new.CertificateDate
  30. }
  31. if old.VehicleManufacturer == "" && new.VehicleManufacturer != "" {
  32. isSupple = true
  33. old.VehicleManufacturer = new.VehicleManufacturer
  34. }
  35. if old.BrandName == "" && new.BrandName != "" {
  36. isSupple = true
  37. old.BrandName = new.BrandName
  38. }
  39. if old.ModelNo == "" && new.ModelNo != "" {
  40. isSupple = true
  41. old.ModelNo = new.ModelNo
  42. }
  43. if old.VehicleBodyColorDetail == "" && new.VehicleBodyColorDetail != "" {
  44. isSupple = true
  45. old.VehicleBodyColorDetail = new.VehicleBodyColorDetail
  46. }
  47. if old.ChassisModel == "" && new.ChassisModel != "" {
  48. isSupple = true
  49. old.ChassisModel = new.ChassisModel
  50. }
  51. if old.ChassisId == "" && new.ChassisId != "" {
  52. isSupple = true
  53. old.ChassisId = new.ChassisId
  54. }
  55. if old.ChassisCertificateNumber == "" && new.ChassisCertificateNumber != "" {
  56. isSupple = true
  57. old.ChassisCertificateNumber = new.ChassisCertificateNumber
  58. }
  59. if old.EngineType == "" && new.EngineType != "" {
  60. isSupple = true
  61. old.EngineType = new.EngineType
  62. }
  63. if old.EngineNo == "" && new.EngineNo != "" {
  64. isSupple = true
  65. old.EngineNo = new.EngineNo
  66. }
  67. if old.FuelTypeDetail == "" && new.FuelTypeDetail != "" {
  68. isSupple = true
  69. old.FuelTypeDetail = new.FuelTypeDetail
  70. }
  71. if (old.Displacement == "" || old.Displacement == "0") && new.Displacement != "" {
  72. isSupple = true
  73. old.Displacement = new.Displacement
  74. }
  75. if (old.DisplacementL == "" || old.DisplacementL == "0") && new.DisplacementL != "" {
  76. isSupple = true
  77. old.DisplacementL = new.DisplacementL
  78. }
  79. if (old.RatedPower == "" || old.RatedPower == "0") && new.RatedPower != "" {
  80. isSupple = true
  81. old.RatedPower = new.RatedPower
  82. }
  83. if old.EmissionStandard == "" && new.EmissionStandard != "" {
  84. isSupple = true
  85. old.EmissionStandard = new.EmissionStandard
  86. }
  87. if (old.OilWear == "" || old.OilWear == "0") && new.OilWear != "" {
  88. isSupple = true
  89. old.OilWear = new.OilWear
  90. }
  91. if (old.Long == "" || old.Long == "0") && new.Long != "" {
  92. isSupple = true
  93. old.Long = new.Long
  94. }
  95. if (old.Wide == "" || old.Wide == "0") && new.Wide != "" {
  96. isSupple = true
  97. old.Wide = new.Wide
  98. }
  99. if (old.High == "" || old.High == "0") && new.High != "" {
  100. isSupple = true
  101. old.High = new.High
  102. }
  103. if (old.CargoLong == "" || old.CargoLong == "0") && new.CargoLong != "" {
  104. isSupple = true
  105. old.CargoLong = new.CargoLong
  106. }
  107. if (old.CargoWide == "" || old.CargoWide == "0") && new.CargoWide != "" {
  108. isSupple = true
  109. old.CargoWide = new.CargoWide
  110. }
  111. if (old.CargoHigh == "" || old.CargoHigh == "0") && new.CargoHigh != "" {
  112. isSupple = true
  113. old.CargoHigh = new.CargoHigh
  114. }
  115. if (old.NumberOfLeafSprings == "" || old.NumberOfLeafSprings == "0") && new.NumberOfLeafSprings != "" {
  116. isSupple = true
  117. old.NumberOfLeafSprings = new.NumberOfLeafSprings
  118. }
  119. if (old.TyreNumber == "" || old.TyreNumber == "0") && new.TyreNumber != "" {
  120. isSupple = true
  121. old.TyreNumber = new.TyreNumber
  122. }
  123. if (old.TyreSize == "" || old.TyreSize == "0") && new.TyreSize != "" {
  124. isSupple = true
  125. old.TyreSize = new.TyreSize
  126. }
  127. if (old.FrontWheelDistance == "" || old.FrontWheelDistance == "0") && new.FrontWheelDistance != "" {
  128. isSupple = true
  129. old.FrontWheelDistance = new.FrontWheelDistance
  130. }
  131. if (old.BackWheelDistance == "" || old.BackWheelDistance == "0") && new.BackWheelDistance != "" {
  132. isSupple = true
  133. old.BackWheelDistance = new.BackWheelDistance
  134. }
  135. if (old.WheelBase == "" || old.WheelBase == "0") && new.WheelBase != "" {
  136. isSupple = true
  137. old.WheelBase = new.WheelBase
  138. }
  139. if (old.AxleWeight == "" || old.AxleWeight == "0") && new.AxleWeight != "" {
  140. isSupple = true
  141. old.AxleWeight = new.AxleWeight
  142. }
  143. if (old.AxleNumber == "" || old.AxleNumber == "0") && new.AxleNumber != "" {
  144. isSupple = true
  145. old.AxleNumber = new.AxleNumber
  146. }
  147. if (old.SteeringMode == "" || old.SteeringMode == "0") && new.SteeringMode != "" {
  148. isSupple = true
  149. old.SteeringMode = new.SteeringMode
  150. }
  151. if (old.GrossMass == "" || old.GrossMass == "0") && new.GrossMass != "" {
  152. isSupple = true
  153. old.GrossMass = new.GrossMass
  154. }
  155. if (old.UnladenMass == "" || old.UnladenMass == "0") && new.UnladenMass != "" {
  156. isSupple = true
  157. old.UnladenMass = new.UnladenMass
  158. }
  159. if (old.ApprovedLoad == "" || old.ApprovedLoad == "0") && new.ApprovedLoad != "" {
  160. isSupple = true
  161. old.ApprovedLoad = new.ApprovedLoad
  162. }
  163. if old.LoadMassCoefficient == "" && new.LoadMassCoefficient != "" {
  164. isSupple = true
  165. old.LoadMassCoefficient = new.LoadMassCoefficient
  166. }
  167. if (old.TractionMass == "" || old.TractionMass == "0") && new.TractionMass != "" {
  168. isSupple = true
  169. old.TractionMass = new.TractionMass
  170. }
  171. if (old.SemitrailerSaddleMaxAllowGrossMass == "" || old.SemitrailerSaddleMaxAllowGrossMass == "0") && new.SemitrailerSaddleMaxAllowGrossMass != "" {
  172. isSupple = true
  173. old.SemitrailerSaddleMaxAllowGrossMass = new.SemitrailerSaddleMaxAllowGrossMass
  174. }
  175. if (old.NumberOfPassengers == "" || old.NumberOfPassengers == "0") && new.NumberOfPassengers != "" {
  176. isSupple = true
  177. old.NumberOfPassengers = new.NumberOfPassengers
  178. }
  179. if (old.ApprovedNumber == "" || old.ApprovedNumber == "0") && new.ApprovedNumber != "" {
  180. isSupple = true
  181. old.ApprovedNumber = new.ApprovedNumber
  182. }
  183. if (old.TopSpeed == "" || old.TopSpeed == "0") && new.TopSpeed != "" {
  184. isSupple = true
  185. old.TopSpeed = new.TopSpeed
  186. }
  187. if old.ReleaseDate == "" && new.ReleaseDate != "" {
  188. isSupple = true
  189. old.ReleaseDate = new.ReleaseDate
  190. }
  191. return isSupple
  192. }
  193. // 处理任务5的插入
  194. func handleTask10InsertOrUpdate(ads10 *model.Ads10) error {
  195. oldAds10 := &model.Ads10{}
  196. db := clinit.DB()
  197. err := oldAds10.Query(db, map[string]interface{}{"vin": ads10.Vin})
  198. if err != nil {
  199. if err == gorm.ErrRecordNotFound {
  200. err = ads10.Insert(db)
  201. if err != nil && !strings.Contains(err.Error(), "Duplicate") {
  202. return err
  203. }
  204. return nil
  205. }
  206. return err
  207. } else {
  208. // 更新时间大于消息时间表示是后面的消息,不处理
  209. /*if oldAds10.UpdatedAt > ads10.UpdatedAt{
  210. return nil
  211. }*/
  212. isSupple := supplementAds10(oldAds10, ads10)
  213. if isSupple {
  214. oldAds10.UpdatedAt = ads10.UpdatedAt
  215. err = oldAds10.Update(db)
  216. if err != nil {
  217. return err
  218. }
  219. } else {
  220. return nil
  221. }
  222. }
  223. return nil
  224. }
  225. // 合格证信息
  226. func Task10(adsMsg *apis.AdsMessage) (err error) {
  227. ads10 := &model.Ads10{}
  228. json.Unmarshal([]byte(adsMsg.Content), &ads10)
  229. if ads10.Vin == "" {
  230. return nil
  231. }
  232. ads10.CreatedAt = adsMsg.Timestamp
  233. ads10.UpdatedAt = adsMsg.Timestamp
  234. if adsMsg.Action == consts.ACTIONDELETE {
  235. // 删除
  236. err = handleTask10Delete(ads10)
  237. } else {
  238. // 新增或插入
  239. err = handleTask10InsertOrUpdate(ads10)
  240. }
  241. return err
  242. }