task15.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. "strings"
  10. "gorm.io/gorm"
  11. )
  12. // 姓名身份证对应关系(正确关系)
  13. func Dws15Task(db *gorm.DB,dwsMessage *apis.DwsMessage, outputSourceCode string) (adsMsgList []*apis.AdsMessage, dwsMsgList []*apis.DwsMessage, err error) {
  14. dws15 := &model.Dws15{}
  15. err = json.Unmarshal([]byte(dwsMessage.Content), dws15)
  16. if err != nil {
  17. return nil, nil, nil
  18. }
  19. dws15.UpdatedAt = dwsMessage.Timestamp
  20. dws15.CreatedAt = dwsMessage.Timestamp
  21. if dws15.Name == "" || dws15.IdCard == ""{
  22. return nil, nil, nil
  23. }
  24. oldDws15 := &model.Dws15{}
  25. where := map[string]interface{}{"id_card": dws15.IdCard}
  26. err = oldDws15.Query(db, where)
  27. if err == nil {
  28. // 有数据
  29. if oldDws15.Name == dws15.Name {
  30. // 不处理
  31. return nil, nil,nil
  32. } else {
  33. // 更新时间大于消息时间表示是后面的消息,不处理
  34. if oldDws15.UpdatedAt > dws15.UpdatedAt{
  35. return nil,nil,nil
  36. }
  37. oldDws15.Name = dws15.Name
  38. oldDws15.UpdatedAt = dws15.UpdatedAt
  39. err = oldDws15.Update(db)
  40. if err != nil{
  41. if err != gorm.ErrRecordNotFound{
  42. return nil,nil,err
  43. }
  44. }
  45. }
  46. } else {
  47. // 无数据
  48. if err == gorm.ErrRecordNotFound {
  49. err = dws15.Insert(db)
  50. if err != nil {
  51. if !strings.Contains(err.Error(), "Duplicate") {
  52. return nil,nil, err
  53. }
  54. }
  55. oldDws15 = dws15
  56. } else{
  57. return nil,nil,err
  58. }
  59. }
  60. adsMsg := NewAdsMessage(dwsMessage, outputSourceCode, consts.ACTIONINSERT)
  61. msgByte, _ := json.Marshal(*oldDws15)
  62. adsMsg.Content = string(msgByte)
  63. adsMsgList = append(adsMsgList,adsMsg)
  64. return adsMsgList, nil, nil
  65. }