ods12.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 ConvertPlateType(s string) (plateColor string, exist bool) {
  13. plateTypeMap := map[string]string{
  14. "1": "01",
  15. "0": "02",
  16. "5": "51",
  17. "4": "52",
  18. }
  19. if v, ok := plateTypeMap[s]; ok {
  20. return v, true
  21. }
  22. return
  23. }
  24. // ZCRK 姓名车牌号核查(河南)(141-001)
  25. func ParasOds12(content string) (ret []map[string]string, err error) {
  26. data := gjson.Parse(content)
  27. requestParams := data.Get("request_params").String()
  28. requestParamsArray := gjson.Parse(requestParams).Array()
  29. if len(requestParamsArray) < 1 {
  30. return nil, fmt.Errorf("数据异常,没有数据")
  31. }
  32. requestParam := requestParamsArray[0]
  33. responseParams := data.Get("response_params").String()
  34. code := gjson.Get(responseParams, "code").String()
  35. if code != "0" {
  36. return nil, fmt.Errorf("数据异常,没有数据")
  37. }
  38. result := gjson.Get(responseParams, "result").String()
  39. consistent := gjson.Get(result, "consistent").Bool()
  40. if !consistent {
  41. return nil, fmt.Errorf("数据异常,车牌姓名不匹配")
  42. }
  43. plateNo := requestParam.Get("plateNo").String()
  44. plateType := requestParam.Get("plateType").String()
  45. if plateType == "" {
  46. plateColor := requestParam.Get("plateColor").String()
  47. if plateColor != "" {
  48. plateType, _ = ConvertPlateType(plateColor)
  49. }
  50. }
  51. owner := requestParam.Get("name").String()
  52. if plateNo == "" || plateType == "" || owner == "" {
  53. return nil, fmt.Errorf("数据异常,数据为空")
  54. }
  55. dataMap := make(map[string]string)
  56. dataMap["plate_no"] = plateNo
  57. dataMap["plate_type"] = plateType
  58. dataMap["owner"] = owner
  59. ret = append(ret, dataMap)
  60. return ret, nil
  61. }
  62. func HandleOnlineOds12(msg *apis.OdsMessage) (dataMapList []map[string]string, err error) {
  63. dataMapList, err = ParasOds12(msg.Content)
  64. if err != nil {
  65. // 解析不出来数据直接返回
  66. return nil, nil
  67. }
  68. if len(dataMapList) == 0 {
  69. return nil, nil
  70. }
  71. // 入本地库
  72. ods12 := &model.Ods12{}
  73. ods12.PlateNo = dataMapList[0]["plate_no"]
  74. ods12.PlateType = dataMapList[0]["plate_type"]
  75. ods12.Owner = dataMapList[0]["owner"]
  76. ods12.Content = msg.Content
  77. err = ods12.Insert(clinit.DB())
  78. if err != nil {
  79. if !strings.Contains(err.Error(), "Duplicate") {
  80. return nil, err
  81. } else {
  82. where := map[string]interface{}{"plate_no": ods12.PlateNo, "plate_type": ods12.PlateType, "owner": ods12.Owner}
  83. oldOds12 := &model.Ods12{}
  84. err = oldOds12.Query(clinit.DB(), where)
  85. if err == nil {
  86. oldDataMapList, _ := ParasOds12(oldOds12.Content)
  87. if checkDataMapListEqual(oldDataMapList, dataMapList) {
  88. return nil, nil
  89. }
  90. }
  91. ods12.UpdateWhere(clinit.DB(), where)
  92. }
  93. }
  94. /*for _, dataMap := range dataMapList {
  95. dwsMsg := dutils.NewDwsMessage(msg)
  96. dwsMsg.Content = utils.MarshalJsonString(dataMap)
  97. ret = append(ret, dwsMsg)
  98. }*/
  99. return dataMapList, nil
  100. }