ods4.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. dutils "adm-ods/utils"
  9. "fmt"
  10. "github.com/tidwall/gjson"
  11. "go.uber.org/zap"
  12. "strings"
  13. )
  14. // ZQY 两日期查询119-005
  15. func ParasOds4(content string) (ret []map[string]string, err error) {
  16. data := gjson.Parse(content)
  17. //rawCode := data.Get("raw_code").String()
  18. requestParams := data.Get("request_params").String()
  19. responseParams := data.Get("response_params").String()
  20. requestArray := gjson.Parse(requestParams).Array()
  21. if len(requestArray) < 1 {
  22. return nil, fmt.Errorf("数据异常,没有数据")
  23. }
  24. code := requestArray[0].Get("code").String()
  25. if code == "" {
  26. return nil, fmt.Errorf("数据异常,车牌姓名不匹配")
  27. }
  28. state := gjson.Get(responseParams, "ok").String()
  29. if state != "1" {
  30. return nil, fmt.Errorf("数据异常,没有数据")
  31. }
  32. responseArray := gjson.Get(responseParams, "ds").Array()
  33. for _, v := range responseArray {
  34. dataMap := make(map[string]string)
  35. dataMap["code"] = code
  36. if dutils.IsPlate(code) {
  37. dataMap["plate_no"] = code
  38. } else {
  39. dataMap["vin"] = code
  40. }
  41. if dataMap["vin"] == "" {
  42. dataMap["vin"] = v.Get("vin").String()
  43. }
  44. //dataMap["vin"] = v.Get("vin").String()
  45. dataMap["insurance_first_date"] = v.Get("register_date").String()
  46. dataMap["last_compulsory_insurance_date"] = v.Get("year_date").String()
  47. ret = append(ret, dataMap)
  48. }
  49. dataMapLen := len(ret)
  50. // TODO 排序
  51. if dataMapLen == 0 {
  52. l.Error("func",
  53. zap.String("call", "ParasOds3"),
  54. zap.String("args", content),
  55. zap.String("error", "数据异常,无法解析"))
  56. return nil, fmt.Errorf("数据异常")
  57. } else {
  58. if dataMapLen > 1 {
  59. return sortByDate(ret), nil
  60. }
  61. return ret, nil
  62. }
  63. }
  64. func HandleOnlineOds4(msg *apis.OdsMessage) (dataMapList []map[string]string, err error) {
  65. dataMapList, err = ParasOds4(msg.Content)
  66. if err != nil {
  67. // 解析不出来数据直接返回
  68. return nil, nil
  69. }
  70. if len(dataMapList) == 0 {
  71. return nil, nil
  72. }
  73. // 入本地库
  74. ods4 := &model.Ods4{}
  75. ods4.Code = dataMapList[0]["code"]
  76. ods4.Content = msg.Content
  77. err = ods4.Insert(clinit.DB())
  78. if err != nil {
  79. if !strings.Contains(err.Error(), "Duplicate") {
  80. return nil, err
  81. } else {
  82. // 如果存在判断是否字段相等
  83. where := map[string]interface{}{"code": ods4.Code}
  84. oldOds4 := &model.Ods4{}
  85. err = oldOds4.Query(clinit.DB(), where)
  86. if err == nil {
  87. oldDataMapList, _ := ParasOds4(oldOds4.Content)
  88. if checkDataMapListEqual(oldDataMapList, dataMapList) {
  89. return nil, nil
  90. }
  91. }
  92. ods4.UpdateWhere(clinit.DB(), where)
  93. }
  94. }
  95. for _, dataMap := range dataMapList {
  96. delete(dataMap, "code")
  97. //dwsMsg := dutils.NewDwsMessage(msg)
  98. //dwsMsg.Content = utils.MarshalJsonString(dataMap)
  99. //ret = append(ret, dwsMsg)
  100. }
  101. return dataMapList, nil
  102. }