ods19.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. "gadm-ods/apis"
  6. "gadm-ods/common.in/clinit"
  7. "gadm-ods/model"
  8. "fmt"
  9. "github.com/tidwall/gjson"
  10. "strings"
  11. )
  12. func ParasOds19(content string) (ret []map[string]string, err error) {
  13. data := gjson.Parse(content)
  14. requestParams := data.Get("request_params").String()
  15. requestParamsArray := gjson.Parse(requestParams).Array()
  16. if len(requestParamsArray) < 1 {
  17. return nil, fmt.Errorf("数据异常,没有数据")
  18. }
  19. requestParam := requestParamsArray[0]
  20. responseParams := data.Get("response_params").String()
  21. code := gjson.Get(responseParams, "code").String()
  22. if code != "0" {
  23. return nil, fmt.Errorf("数据异常,没有数据")
  24. }
  25. result := gjson.Get(responseParams, "result").String()
  26. vin := requestParam.Get("vin").String()
  27. if vin == "" {
  28. return nil, fmt.Errorf("数据异常")
  29. }
  30. dataMap := make(map[string]string)
  31. dataMap["release_date"] = gjson.Get(result,"manufacturerDate").String()
  32. dataMap["vehicle_manufacturer"] = gjson.Get(result,"manufacturerName").String()
  33. dataMap["model_no"] = gjson.Get(result,"vehicleCode").String()
  34. dataMap["brand_name"] = gjson.Get(result,"brandName").String()
  35. dataMap["engine_no"] = gjson.Get(result,"engineCode").String()
  36. dataMap["wheel_base"]= gjson.Get(result,"wheelbase").String()
  37. dataMap["long"] = gjson.Get(result,"vehicleLength").String()
  38. dataMap["wide"] = gjson.Get(result,"vehicleWidth").String()
  39. dataMap["high"] = gjson.Get(result,"vehicleHeight").String()
  40. dataMap["fuel_type_detail"] = gjson.Get(result,"dynamicType").String()
  41. dataMap["displacement"] =gjson.Get(result,"displacement").String()
  42. dataMap["rated_power"] = gjson.Get(result,"power").String()
  43. dataMap["gross_mass"] = gjson.Get(result,"totalWeight").String()
  44. dataMap["vehicle_type_detail"] = gjson.Get(result,"autoCategory").String()
  45. dataMap["vehicle_body_color_detail"] = gjson.Get(result,"color").String()
  46. dataMap["unladen_mass"] = gjson.Get(result,"curbWeight").String()
  47. dataMap["approved_number"] = gjson.Get(result,"ratedPassengers").String()
  48. dataMap["engine_type"] = gjson.Get(result,"engineModel").String()
  49. dataMap["vin"] = vin
  50. ret = append(ret, dataMap)
  51. return ret, nil
  52. }
  53. // ZCRK vin 查发动机号(141-003)
  54. func HandleOnlineOds19(msg *apis.OdsMessage) (dataMapList []map[string]string, err error) {
  55. dataMapList, err = ParasOds19(msg.Content)
  56. if err != nil {
  57. // 解析不出来数据直接返回
  58. return nil, nil
  59. }
  60. if len(dataMapList) == 0 {
  61. return nil, nil
  62. }
  63. // 入本地库
  64. ods19 := &model.Ods19{}
  65. ods19.Vin = dataMapList[0]["vin"]
  66. ods19.Content = msg.Content
  67. err = ods19.Insert(clinit.DB())
  68. if err != nil {
  69. if !strings.Contains(err.Error(), "Duplicate") {
  70. return nil, err
  71. } else {
  72. where := map[string]interface{}{"vin": ods19.Vin}
  73. oldOds19 := &model.Ods19{}
  74. err = oldOds19.Query(clinit.DB(), where)
  75. if err == nil {
  76. oldDataMapList, _ := ParasOds19(oldOds19.Content)
  77. if checkDataMapListEqual(oldDataMapList, dataMapList) {
  78. return nil, nil
  79. }
  80. }
  81. ods19.UpdateWhere(clinit.DB(), where)
  82. }
  83. }
  84. return dataMapList, nil
  85. }