ods18.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. "strings"
  11. )
  12. func ParasOds18(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. engineNo := gjson.Get(result, "engine_number").String()
  27. vin := requestParam.Get("vin").String()
  28. if vin == "" || engineNo == "" {
  29. return nil, fmt.Errorf("数据异常")
  30. }
  31. dataMap := make(map[string]string)
  32. dataMap["vin"] = vin
  33. dataMap["engine_no"] = engineNo
  34. ret = append(ret, dataMap)
  35. return ret, nil
  36. }
  37. // ZCRK vin 查发动机号(141-003)
  38. func HandleOnlineOds18(msg *apis.OdsMessage) (dataMapList []map[string]string, err error) {
  39. dataMapList, err = ParasOds18(msg.Content)
  40. if err != nil {
  41. // 解析不出来数据直接返回
  42. return nil, nil
  43. }
  44. if len(dataMapList) == 0 {
  45. return nil, nil
  46. }
  47. // 入本地库
  48. ods18 := &model.Ods18{}
  49. ods18.Vin = dataMapList[0]["vin"]
  50. ods18.Content = msg.Content
  51. err = ods18.Insert(clinit.DB())
  52. if err != nil {
  53. if !strings.Contains(err.Error(), "Duplicate") {
  54. return nil, err
  55. } else {
  56. where := map[string]interface{}{"vin": ods18.Vin}
  57. oldOds18 := &model.Ods18{}
  58. err = oldOds18.Query(clinit.DB(), where)
  59. if err == nil {
  60. oldDataMapList, _ := ParasOds18(oldOds18.Content)
  61. if checkDataMapListEqual(oldDataMapList, dataMapList) {
  62. return nil, nil
  63. }
  64. }
  65. ods18.UpdateWhere(clinit.DB(), where)
  66. }
  67. }
  68. return dataMapList, nil
  69. }