ods2.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/common.in/clinit"
  7. "adm-ods/model"
  8. "fmt"
  9. "github.com/tidwall/gjson"
  10. "go.uber.org/zap"
  11. "strings"
  12. )
  13. // ZQY 车牌查车档119-001
  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. requestArray := gjson.Parse(requestParams).Array()
  20. if len(requestArray) < 1 {
  21. return nil, fmt.Errorf("数据异常,没有数据")
  22. }
  23. plateNo := requestArray[0].Get("cph").String()
  24. if plateNo == "" {
  25. return nil, fmt.Errorf("数据异常,车牌号为空")
  26. }
  27. state := gjson.Get(responseParams, "ok").String()
  28. if state != "1" {
  29. return nil, fmt.Errorf("数据异常,没有数据")
  30. }
  31. responseArray := gjson.Get(responseParams, "ds").Array()
  32. fmt.Println("")
  33. for _, v := range responseArray {
  34. dataMap := make(map[string]string)
  35. dataMap["plate_no"] = plateNo
  36. dataMap["vin"] = v.Get("车辆识别代码").String()
  37. dataMap["brand_name"] = v.Get("品牌").String()
  38. //dataMap["vehicle_type_detail"] = v.Get("车辆名称").String()
  39. dataMap["model_no"] = v.Get("车辆型号").String()
  40. dataMap["use_property_detail"] = v.Get("使用性质").String()
  41. dataMap["insurance_first_date"] = v.Get("初次登记时间").String()
  42. dataMap["fuel_type_detail"] = v.Get("燃料种类").String()
  43. dataMap["displacement"] = v.Get("排量").String()
  44. dataMap["engine_type"] = v.Get("发动机型号").String()
  45. dataMap["approved_number"] = v.Get("额定载客").String()
  46. dataMap["vehicle_body_color_detail"] = v.Get("车身颜色").String()
  47. dataMap["release_date"] = v.Get("出厂日期").String()
  48. dataMap["axle_number"] = v.Get("轴数").String()
  49. dataMap["wheel_base"] = v.Get("轴距").String()
  50. dataMap["front_wheel_distance"] = v.Get("前轮距").String()
  51. dataMap["back_wheel_distance"] = v.Get("后轮距").String()
  52. dataMap["gross_mass"] = v.Get("总质量").String()
  53. dataMap["unladen_mass"] = v.Get("整备质量").String()
  54. dataMap["approved_load"] = v.Get("额定载质量").String()
  55. dataMap["rated_power"] = v.Get("功率").String()
  56. //dataMap["plate_no"] = v.Get("CPH").String()
  57. dataMap["last_compulsory_insurance_date"] = v.Get("承保日期").String()
  58. dataMap["engine_no"] = v.Get("发动机号").String()
  59. dataMap["oil_wear"] = v.Get("油耗").String()
  60. dataMap["long"] = v.Get("长").String()
  61. dataMap["wide"] = v.Get("宽").String()
  62. dataMap["high"] = v.Get("高").String()
  63. dataMap["emission_standard"] = v.Get("排放标准").String()
  64. dataMap["tyre_number"] = v.Get("轮胎数").String()
  65. // TODO 拆分为前后轮胎规格
  66. dataMap["tyre_size"] = v.Get("轮胎规格").String()
  67. //dataMap["front_wheel_specification"] = dataMap["tyre_size"]
  68. //dataMap["back_wheel_specification"] = dataMap["tyre_size"]
  69. dataMap["axle_weight"] = v.Get("轴荷").String()
  70. dataMap["style_name"] = v.Get("车型").String()
  71. ret = append(ret, dataMap)
  72. }
  73. dataMapLen := len(ret)
  74. if dataMapLen == 0 {
  75. l.Error("func",
  76. zap.String("call", "ParasOds2"),
  77. zap.String("args", content),
  78. zap.String("error", "数据异常,无法解析"))
  79. return nil, fmt.Errorf("数据异常")
  80. } else {
  81. if dataMapLen > 1 {
  82. return sortByDate(ret), nil
  83. }
  84. return ret, nil
  85. }
  86. }
  87. func HandleOnlineOds2(msg *apis.OdsMessage) (dataMapList []map[string]string, err error) {
  88. dataMapList, err = ParasOds2(msg.Content)
  89. if err != nil {
  90. // 解析不出来数据直接返回
  91. return nil, nil
  92. }
  93. if len(dataMapList) == 0 {
  94. return nil, nil
  95. }
  96. // 入本地库
  97. ods2 := &model.Ods2{}
  98. ods2.PlateNo = dataMapList[0]["plate_no"]
  99. ods2.Content = msg.Content
  100. err = ods2.Save(clinit.DB())
  101. if err != nil {
  102. if !strings.Contains(err.Error(), "Duplicate") {
  103. return nil, err
  104. } else {
  105. where := map[string]interface{}{"plate_no": ods2.PlateNo}
  106. oldOds2 := &model.Ods2{}
  107. err = oldOds2.Query(clinit.DB(), where)
  108. if err == nil {
  109. oldDataMapList, _ := ParasOds2(oldOds2.Content)
  110. if checkDataMapListEqual(oldDataMapList, dataMapList) {
  111. return nil, nil
  112. }
  113. }
  114. ods2.UpdateWhere(clinit.DB(), where)
  115. }
  116. }
  117. /*for _, dataMap := range dataMapList {
  118. dwsMsg := dutils.NewDwsMessage(msg)
  119. dwsMsg.Content = utils.MarshalJsonString(dataMap)
  120. ret = append(ret, dwsMsg)
  121. }*/
  122. return dataMapList, nil
  123. }