task6.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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-dws/apis"
  6. "adm-dws/consts"
  7. "adm-dws/model"
  8. "encoding/json"
  9. "gorm.io/gorm"
  10. "strings"
  11. )
  12. func supplementDws6(old, new *model.Dws6) (bool,string) {
  13. isSupple := false
  14. dwsMsgStr := ""
  15. if old.InsuranceFirstDate == "" && new.InsuranceFirstDate != "" {
  16. isSupple = true
  17. old.InsuranceFirstDate = new.InsuranceFirstDate
  18. }else if old.InsuranceFirstDate != "" && new.InsuranceFirstDate != "" {
  19. if new.InsuranceFirstDate < old.InsuranceFirstDate{
  20. msg := map[string]string{"initial_registration_date":old.InsuranceFirstDate,"vin":old.Vin}
  21. msgByte, _ := json.Marshal(msg)
  22. dwsMsgStr = string(msgByte)
  23. isSupple = true
  24. old.InsuranceFirstDate = new.InsuranceFirstDate
  25. }else if new.InsuranceFirstDate > old.InsuranceFirstDate{
  26. msg := map[string]string{"initial_registration_date":new.InsuranceFirstDate,"vin":old.Vin}
  27. msgByte, _ := json.Marshal(msg)
  28. dwsMsgStr = string(msgByte)
  29. }
  30. }
  31. if old.Province == "" && new.Province != "" {
  32. isSupple = true
  33. old.Province = new.Province
  34. }
  35. if old.City == "" && new.City != "" {
  36. isSupple = true
  37. old.City = new.City
  38. }
  39. if old.District == "" && new.District != "" {
  40. isSupple = true
  41. old.District = new.District
  42. }
  43. if old.UsePropertyDetail == "" && new.UsePropertyDetail != "" {
  44. isSupple = true
  45. old.UsePropertyDetail = new.UsePropertyDetail
  46. }
  47. return isSupple,dwsMsgStr
  48. }
  49. // 交强险首保日期
  50. func Dws6Task(db *gorm.DB,dwsMessage *apis.DwsMessage, outputSourceCode string) (adsMsgList []*apis.AdsMessage, dwsMsgList []*apis.DwsMessage, err error) {
  51. dws6 := &model.Dws6{}
  52. err = json.Unmarshal([]byte(dwsMessage.Content), dws6)
  53. if err != nil {
  54. return nil, nil, nil
  55. }
  56. if (dws6.Vin == "") || (dws6.InsuranceFirstDate == "" && dws6.UsePropertyDetail == ""){
  57. return nil, nil, nil
  58. }
  59. if dws6.InsuranceFirstDate != ""{
  60. dws6.InsuranceFirstDate = strings.Split(dws6.InsuranceFirstDate," ")[0]
  61. }
  62. oldDws6 := &model.Dws6{}
  63. err = oldDws6.Query(db, map[string]interface{}{"vin": dws6.Vin})
  64. if err == nil {
  65. // 有数据
  66. isSupple,dwsMsgStr := supplementDws6(oldDws6,dws6)
  67. if dwsMsgStr != ""{
  68. dwsMsg := NewDwsMessage(dwsMessage, outputSourceCode)
  69. dwsMsg.Content = dwsMsgStr
  70. dwsMsgList = append(dwsMsgList,dwsMsg)
  71. }
  72. if isSupple {
  73. err = oldDws6.Update(db)
  74. if err != nil{
  75. return nil,nil,err
  76. }
  77. }else{
  78. return nil,dwsMsgList,nil
  79. }
  80. } else {
  81. if err == gorm.ErrRecordNotFound {
  82. // 没有数据
  83. err = dws6.Insert(db)
  84. if err != nil {
  85. if !strings.Contains(err.Error(), "Duplicate") {
  86. return nil,nil, err
  87. }
  88. }
  89. oldDws6 = dws6
  90. } else {
  91. // 数据库错误
  92. return nil, nil, err
  93. }
  94. }
  95. adsMsg := NewAdsMessage(dwsMessage, outputSourceCode, consts.ACTIONINSERT)
  96. msgByte, _ := json.Marshal(*oldDws6)
  97. adsMsg.Content = string(msgByte)
  98. adsMsgList = append(adsMsgList,adsMsg)
  99. return adsMsgList, dwsMsgList, nil
  100. }