task18.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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/model"
  8. "encoding/json"
  9. "gorm.io/gorm"
  10. "strings"
  11. )
  12. func supplementAds18(old, new *model.Ads18) (bool) {
  13. isSupple := false
  14. if old.PlateType == "" && new.PlateType != "" {
  15. isSupple = true
  16. old.PlateType = new.PlateType
  17. }
  18. if old.VehicleType == "" && new.VehicleType != "" {
  19. isSupple = true
  20. old.VehicleType = new.VehicleType
  21. }
  22. if old.VehicleTypeDetail == "" && new.VehicleTypeDetail != "" {
  23. isSupple = true
  24. old.VehicleTypeDetail = new.VehicleTypeDetail
  25. }
  26. return isSupple
  27. }
  28. // 计算号牌种类
  29. func Task18(adsMsg *apis.AdsMessage) (err error) {
  30. ads18 := &model.Ads18{}
  31. json.Unmarshal([]byte(adsMsg.Content), ads18)
  32. if ads18.ModelNo == "" || ads18.PlateType == ""{
  33. return nil
  34. }
  35. db := clinit.DB()
  36. oldAds18 := &model.Ads18{}
  37. err = oldAds18.Query(db,map[string]interface{}{"model_no":ads18.ModelNo})
  38. if err == nil {
  39. if supplementAds18(oldAds18,ads18){
  40. err = oldAds18.Update(db)
  41. if err != nil{
  42. return err
  43. }
  44. }
  45. return nil
  46. }else{
  47. if err != gorm.ErrRecordNotFound{
  48. return err
  49. }
  50. }
  51. err = ads18.Insert(db)
  52. if err != nil{
  53. if !strings.Contains(err.Error(), "Duplicate") {
  54. return err
  55. }
  56. }
  57. return nil
  58. }