ods3.go 4.3 KB

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