ods2.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2019 autocareai.com. All rights reserved.
  2. // Use of this source code is governed by autocareai.com.
  3. package analysis
  4. import (
  5. "fmt"
  6. "gadm-ods/apis"
  7. "gadm-ods/common.in/clinit"
  8. "gadm-ods/model"
  9. "gadm-ods/utils"
  10. "github.com/tidwall/gjson"
  11. "strings"
  12. )
  13. // cdbd 二要素
  14. func ParasOds2(content string) (ret []map[string]string, err error) {
  15. data := gjson.Parse(content)
  16. //rawCode := data.Get("raw_code").String()
  17. requestParams := data.Get("request_params").String()
  18. responseParams := data.Get("response_params").String()
  19. if requestParams == "" {
  20. return nil, fmt.Errorf("数据异常,没有数据")
  21. }
  22. idCard := gjson.Get(requestParams,"id_card").String()
  23. plateNum := gjson.Get(requestParams,"plate_num").String()
  24. plateNo := "川"+plateNum
  25. dataMap := make(map[string]string)
  26. dataMap["plate_no"] = plateNo
  27. dataMap["id_card"] = idCard
  28. code := gjson.Get(responseParams, "code").Int()
  29. if code == 200 {
  30. result := gjson.Get(responseParams, "result").String()
  31. rdata, err := utils.SM4ECBBase64Decrypt(result)
  32. if err != nil {
  33. fmt.Println("SM4ECBBase64Decrypt error:", err)
  34. return nil, fmt.Errorf("数据异常,解密失败")
  35. }
  36. isMatch := gjson.Get(rdata,"match_result").String()
  37. if isMatch == "1"{
  38. dataMap["state"] = gjson.Get(rdata,"car_detail.car_state").String()
  39. //dataMap[""] = gjson.Get(rdata,"car_detail.reg_date").String()
  40. dataMap["brand_name"] = gjson.Get(rdata,"car_detail.model_name").String()
  41. dataMap["model_no"] = gjson.Get(rdata,"car_detail.model_type").String()
  42. dataMap["vehicle_type"] = gjson.Get(rdata,"car_detail.vehicle_type").String()
  43. dataMap["vin"] = gjson.Get(rdata,"car_detail.vin").String()
  44. dataMap["initial_registration_date"] = gjson.Get(rdata,"car_detail.initial_reg_date").String()
  45. }else{
  46. return nil, fmt.Errorf("数据异常,不匹配")
  47. }
  48. } else{
  49. return nil, fmt.Errorf("数据异常,无记录")
  50. }
  51. ret = append(ret, dataMap)
  52. return ret, nil
  53. }
  54. func HandleOnlineOds2(msg *apis.OdsMessage) (dataMapList []map[string]string, err error) {
  55. dataMapList, err = ParasOds2(msg.Content)
  56. if err != nil {
  57. // 解析不出来数据直接返回
  58. return nil, nil
  59. }
  60. if len(dataMapList) == 0 {
  61. return nil, nil
  62. }
  63. // 入本地库
  64. ods2 := &model.Ods2{}
  65. ods2.PlateNo = dataMapList[0]["plate_no"]
  66. ods2.IdCard = dataMapList[0]["id_card"]
  67. ods2.Content = msg.Content
  68. err = ods2.Save(clinit.DB())
  69. if err != nil {
  70. if !strings.Contains(err.Error(), "Duplicate") {
  71. return nil, err
  72. } else {
  73. where := map[string]interface{}{"plate_no": ods2.PlateNo,"id_card":ods2.IdCard}
  74. oldOds2 := &model.Ods2{}
  75. err = oldOds2.Query(clinit.DB(), where)
  76. if err == nil {
  77. oldDataMapList, _ := ParasOds2(oldOds2.Content)
  78. if checkDataMapListEqual(oldDataMapList, dataMapList) {
  79. return nil, nil
  80. }
  81. }
  82. ods2.UpdateWhere(clinit.DB(), where)
  83. }
  84. }
  85. /*for _, dataMap := range dataMapList {
  86. dwsMsg := dutils.NewDwsMessage(msg)
  87. dwsMsg.Content = utils.MarshalJsonString(dataMap)
  88. ret = append(ret, dwsMsg)
  89. }*/
  90. return dataMapList, nil
  91. }