ods6.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package analysis
  4. import (
  5. "adm-ods/apis"
  6. "adm-ods/model"
  7. "fmt"
  8. "strings"
  9. "adm-ods/common.in/clinit"
  10. "github.com/tidwall/gjson"
  11. )
  12. // spy 两日期查询128-004,128-006
  13. func ParasOds6(content string) (ret []map[string]string, err error) {
  14. data := gjson.Parse(content)
  15. requestParams := data.Get("request_params").String()
  16. responseParams := data.Get("response_params").String()
  17. cp := gjson.Get(requestParams, "cp").String()
  18. if cp == "" {
  19. return nil, fmt.Errorf("数据异常,车牌姓名不匹配")
  20. }
  21. code := gjson.Get(responseParams, "respCode").String()
  22. if code != "0" {
  23. return nil, fmt.Errorf("数据异常,没有数据")
  24. }
  25. stateCode := gjson.Get(responseParams, "data.stateCode").String()
  26. if stateCode == "0" {
  27. return nil, fmt.Errorf("数据异常,没有数据")
  28. }
  29. dataArray := gjson.Get(responseParams, "data").Array()
  30. if len(dataArray) == 0 {
  31. dataMap := make(map[string]string)
  32. dataMap["cp"] = cp
  33. respData := gjson.Get(responseParams, "data").String()
  34. dataMap["plate_no"] = gjson.Get(respData, "cp").String()
  35. dataMap["vin"] = gjson.Get(respData, "vin").String()
  36. dataMap["last_compulsory_insurance_date"] = gjson.Get(respData, "insuranceMonth").String()
  37. dataMap["insurance_first_date"] = gjson.Get(respData, "registerDate").String()
  38. dataMap["use_property_detail"] = gjson.Get(respData, "usage").String()
  39. ret = append(ret, dataMap)
  40. }else{
  41. for _,v := range dataArray{
  42. dataMap := make(map[string]string)
  43. dataMap["cp"] = cp
  44. dataMap["plate_no"] = v.Get("cp").String()
  45. dataMap["vin"] = v.Get("vin").String()
  46. dataMap["last_compulsory_insurance_date"] = v.Get("insuranceMonth").String()
  47. dataMap["insurance_first_date"] = v.Get("registerDate").String()
  48. dataMap["use_property_detail"] = v.Get("usage").String()
  49. dataMap["city"] = v.Get( "city").String()
  50. dataMap["province"] = v.Get( "province").String()
  51. ret = append(ret, dataMap)
  52. }
  53. }
  54. return ret, nil
  55. }
  56. func HandleOnlineOds6(msg *apis.OdsMessage) (dataMapList []map[string]string, err error) {
  57. dataMapList, err = ParasOds6(msg.Content)
  58. if err != nil {
  59. // 解析不出来数据直接返回
  60. return nil, nil
  61. }
  62. if len(dataMapList) == 0 {
  63. return nil, nil
  64. }
  65. // 入本地库
  66. ods6 := &model.Ods6{}
  67. ods6.Code = dataMapList[0]["cp"]
  68. if ods6.Code == "" {
  69. fmt.Println("主键值为空")
  70. return nil, nil
  71. }
  72. ods6.Content = msg.Content
  73. err = ods6.Insert(clinit.DB())
  74. if err != nil {
  75. if !strings.Contains(err.Error(), "Duplicate") {
  76. return nil, err
  77. } else {
  78. where := map[string]interface{}{"code": ods6.Code}
  79. oldOds6 := &model.Ods6{}
  80. err = oldOds6.Query(clinit.DB(), where)
  81. if err == nil {
  82. oldDataMapList, _ := ParasOds6(oldOds6.Content)
  83. if checkDataMapListEqual(oldDataMapList, dataMapList) {
  84. return nil, nil
  85. }
  86. }
  87. ods6.UpdateWhere(clinit.DB(), where)
  88. }
  89. }
  90. for _, dataMap := range dataMapList {
  91. delete(dataMap, "cp")
  92. //dwsMsg := dutils.NewDwsMessage(msg)
  93. //dwsMsg.Content = utils.MarshalJsonString(dataMap)
  94. //ret = append(ret, dwsMsg)
  95. }
  96. return dataMapList, nil
  97. }